mirror of
https://github.com/mehotkhan/BandersnatchInteractive.git
synced 2025-07-28 01:33:23 +00:00
assets/scripts.js: Improve choice handling
- Apply effects of the default choice even if the user didn't click anything - Pave way for implementing disableImmediateSceneTransition
This commit is contained in:
parent
2fce7361e9
commit
e7b80f431a
1 changed files with 34 additions and 23 deletions
|
@ -123,11 +123,13 @@ function addItem(ul, text, url) {
|
||||||
ul.appendChild(li);
|
ul.appendChild(li);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nextChoice = -1;
|
||||||
var nextSegment = null;
|
var nextSegment = null;
|
||||||
|
|
||||||
function setNextSegment(segmentId, comment) {
|
function setNextSegment(segmentId, comment) {
|
||||||
console.log('setNextSegment', segmentId, comment);
|
console.log('setNextSegment', segmentId, comment);
|
||||||
nextSegment = segmentId;
|
nextSegment = segmentId;
|
||||||
|
nextChoice = -1;
|
||||||
var ul = newList("nextSegment");
|
var ul = newList("nextSegment");
|
||||||
var caption = 'nextSegment: ' + segmentId;
|
var caption = 'nextSegment: ' + segmentId;
|
||||||
addItem(ul, comment ? caption + ' (' + comment + ')' : caption,
|
addItem(ul, comment ? caption + ' (' + comment + ')' : caption,
|
||||||
|
@ -152,31 +154,34 @@ function addZones(segmentId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ul = newList("nextSegments");
|
ul = newList("nextSegments");
|
||||||
|
let defaultSegmentId = null;
|
||||||
for (const [k, v] of Object.entries(segmentMap.segments[segmentId].next)) {
|
for (const [k, v] of Object.entries(segmentMap.segments[segmentId].next)) {
|
||||||
let caption = k;
|
let caption = k;
|
||||||
if (segmentMap.segments[segmentId].defaultNext == k) {
|
if (segmentMap.segments[segmentId].defaultNext == k) {
|
||||||
caption = '[' + caption + ']';
|
caption = '[' + caption + ']';
|
||||||
setNextSegment(k);
|
defaultSegmentId = k;
|
||||||
}
|
}
|
||||||
addItem(ul, caption, 'javascript:playSegment("' + k + '")');
|
addItem(ul, caption, 'javascript:playSegment("' + k + '")');
|
||||||
}
|
}
|
||||||
|
setNextSegment(defaultSegmentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
var globalChoices = {};
|
var currentChoices = [];
|
||||||
|
|
||||||
function addChoices(r) {
|
function addChoices(r) {
|
||||||
|
currentChoices = [];
|
||||||
|
nextChoice = -1;
|
||||||
var ul = newList("choices");
|
var ul = newList("choices");
|
||||||
document.getElementById("choiceCaption").innerHTML = '';
|
document.getElementById("choiceCaption").innerHTML = '';
|
||||||
if (!r) return;
|
if (!r) return;
|
||||||
|
|
||||||
|
currentChoices = r.choices;
|
||||||
|
nextChoice = r.defaultChoiceIndex;
|
||||||
|
|
||||||
let index = 0;
|
let index = 0;
|
||||||
|
|
||||||
for (let x of r.choices) {
|
for (let x of r.choices) {
|
||||||
console.log(x.id, 'choice saved');
|
|
||||||
globalChoices[x.id] = x;
|
|
||||||
|
|
||||||
var caption = r.defaultChoiceIndex == index ? '[' + x.text + ']' : x.text;
|
var caption = r.defaultChoiceIndex == index ? '[' + x.text + ']' : x.text;
|
||||||
addItem(ul, caption, 'javascript:choice("' +
|
addItem(ul, caption, 'javascript:choice(' + index + ')');
|
||||||
(x.segmentId ? x.segmentId : (x.sg ? x.sg : x.id)) + '", "' + x.text + '", "' + x.id + '")');
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,10 +244,7 @@ function ontimeupdate(evt) {
|
||||||
console.log('ontimeupdate', lastSegment, '->', currentSegment, ms, msToString(ms), seeked);
|
console.log('ontimeupdate', lastSegment, '->', currentSegment, ms, msToString(ms), seeked);
|
||||||
lastSegment = currentSegment;
|
lastSegment = currentSegment;
|
||||||
if (!seeked) {
|
if (!seeked) {
|
||||||
// TODO: activate and apply user choice (whether or not it
|
if (playNextSegment()) {
|
||||||
// was default) instead of just playing the next segment.
|
|
||||||
segmentTransition = true;
|
|
||||||
if (playSegment(nextSegment, true)) {
|
|
||||||
// playSegment decided to seek, which means that this
|
// playSegment decided to seek, which means that this
|
||||||
// currentSegment is invalid, and a recursive
|
// currentSegment is invalid, and a recursive
|
||||||
// ontimeupdate invocation should have taken care of
|
// ontimeupdate invocation should have taken care of
|
||||||
|
@ -250,7 +252,6 @@ function ontimeupdate(evt) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setNextSegment(null);
|
|
||||||
addZones(currentSegment);
|
addZones(currentSegment);
|
||||||
placeChanged = true;
|
placeChanged = true;
|
||||||
}
|
}
|
||||||
|
@ -319,6 +320,22 @@ function ontimeupdate(evt) {
|
||||||
timerId = setTimeout(ontimeupdate, timeLeft);
|
timerId = setTimeout(ontimeupdate, timeLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function playNextSegment() {
|
||||||
|
if (nextChoice >= 0) {
|
||||||
|
let x = currentChoices[nextChoice];
|
||||||
|
let choiceId = x.segmentId ? x.segmentId : (x.sg ? x.sg : x.id);
|
||||||
|
var segmentId = findSegment(choiceId);
|
||||||
|
console.log('choice', choiceId, 'nextSegment', segmentId);
|
||||||
|
applyImpression(x.impressionData);
|
||||||
|
nextSegment = segmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
segmentTransition = true;
|
||||||
|
let segment = nextSegment;
|
||||||
|
nextSegment = null;
|
||||||
|
return playSegment(segment, true);
|
||||||
|
}
|
||||||
|
|
||||||
function jumpForward() {
|
function jumpForward() {
|
||||||
var ms = getCurrentMs();
|
var ms = getCurrentMs();
|
||||||
var segmentId = getSegmentId(ms);
|
var segmentId = getSegmentId(ms);
|
||||||
|
@ -333,7 +350,7 @@ function jumpForward() {
|
||||||
if (interactionMs) {
|
if (interactionMs) {
|
||||||
seek(interactionMs);
|
seek(interactionMs);
|
||||||
} else {
|
} else {
|
||||||
playSegment(nextSegment);
|
playNextSegment();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -450,20 +467,14 @@ window.onload = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
function seek(ms) {
|
function seek(ms) {
|
||||||
clearTimeout(timerId);
|
|
||||||
console.log('seek', ms);
|
console.log('seek', ms);
|
||||||
momentSelected = null;
|
|
||||||
document.getElementById("video").currentTime = ms / 1000.0;
|
document.getElementById("video").currentTime = ms / 1000.0;
|
||||||
ontimeupdate(null);
|
ontimeupdate(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function choice(choiceId, text, id) {
|
function choice(choiceIndex) {
|
||||||
var segmentId = findSegment(choiceId);
|
nextChoice = choiceIndex;
|
||||||
console.log('choice', choiceId, 'nextSegment', segmentId);
|
newList("choices");
|
||||||
applyImpression(globalChoices[id].impressionData);
|
|
||||||
setNextSegment(segmentId, text);
|
|
||||||
momentSelected = choiceId;
|
|
||||||
addChoices(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyImpression(impressionData) {
|
function applyImpression(impressionData) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue