Technically two questions (and a verbose explanation, for which I apologize). Using Twine 1 with Sugarcube 2.
The setup: Instead of the reader choosing from hypertext links, the reader drags an icon into a droppable box. Their choice of icon (let's say, one of four options) sets a variable upon that drop (ie, $firstIcon = true). The reader pushes a button to confirm their choice, and they move on to the next passage as determined by that variable.
Question one: I have the general drag/drop mechanics worked out, but I don't know how to actually set the variable per icon... Because jQuery's draggable doesn't have an event for "got dropped into somewhere," as far as I can tell. I have to set the variable on the droppable side -- but then how do I get Javascript to recognize which of four icons was dropped into the container? Something with $('this')?? My JS knowledge does not extend that far. edit: solved!
Question two: I have a 'redo' button that negates the reader's initial choice of icon and allows everything to be draggable/droppable again so they can pick a new choice. I want to send all icons back to their original positions prior to being dragged.... Is that possible?
Here's the code I've been working on so far. Please be kind with baby's first JS, but feel free to point out where I'm being redundant or silly. Right now I have two icons that match two different droppable containers for easy testing, and then two buttons -- one to move the reader to the next passage, one to start over. The buttons are initially hidden; once the reader successfully drags an icon into a box, the buttons appear.
HTML:
<div id="icon1"></div>
<div id="icon2"></div>
<div id="firstBox"></div>
<div id="secondBox"></div>
<span class="btn1"><<button "next passage">><<goto "wherever">><</button>></span>
<span class="btn2"><<button "redo">><</button>></span>
CSS:
#icon1 {
float: left;
height: 100px;
width: 100px;
background-color: white;
}
#icon2 {
float: left;
height: 100px;
width: 100px;
background-color: grey;
}
#firstBox {
float: right;
height: 200px;
width: 200px;
background-color: blue;
}
#secondBox {
float: right;
height: 200px;
width: 200px;
background-color: yellow;
}
JS:
//hide the buttons at first
jQuery(document).ready(
function($) {
$('button').hide();
});
//set up the icons and containers
$( function() {
$( "#icon1" ).draggable({
revert: true,
containment: "document",
scope: "first",
snap: true
});
$( "#icon2" ).draggable({
revert: true,
containment: "document",
scope: "second",
snap: true
});
$( "#firstBox" ).droppable({
scope: "first",
drop: handleFirstDrop
});
$( "#secondBox" ).droppable({
scope: "second",
drop: handleSecondDrop
});
});
//when the right icon gets dropped into the right box, set a variable accordingly. but i want to have multiple icons within the scope of each container... and i dont know how to differentiate them.
function handleFirstDrop( event, ui ) {
ui.draggable.draggable( 'disable' );
$(this).droppable( 'disable' );
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
ui.draggable.draggable( 'option', 'revert', false );
state.active.variables["correctCards"]++;
state.active.variables["firstChoice"] = true;
if ( state.active.variables["correctCards"] == 1 ) {
$('button').show();
$('.ui-draggable').draggable('disable');
}
}
function handleSecondDrop( event, ui ) {
ui.draggable.draggable( 'disable' );
$(this).droppable( 'disable' );
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
ui.draggable.draggable( 'option', 'revert', false );
state.active.variables["correctCards"]++;
state.active.variables["secondChoice"] = true;
//once an icon has been dropped, show the buttons and freeze the other icons
if ( state.active.variables["correctCards"] == 1 ) {
$('button').show();
$('.ui-draggable').draggable('disable');
}
}
//the redo button. don't know how to send the icons back to their original positions tho.
$( function() {
$('.btn2').click(function() {
state.active.variables["correctCards"] = 0;
state.active.variables["firstChoice"] = false;
state.active.variables["secondChoice"] = false;
$('button').hide();
$('.ui-draggable').draggable('enable');
$('.ui-droppable').droppable('enable');
});
});
Oh, and if the reader presses the 'I'm good to go ahead with my choice' button, they're sent through a dummy passage that invisibly executes this code to determine where to send them:
<<if $firstChoice is true>><<goto "first choice">>
<<elseif $secondChoice is true>><<goto "second choice">>
<</if>>
I'm so sorry for the length of this question, but I wanted to be as thorough as possible haha.