Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
6e2851f
Initialised the undo queue in structure.js
hkirat Jul 1, 2016
c5910c4
Initialised Change and Changes class
hkirat Jul 1, 2016
f4b5c6b
Added prototype functions
hkirat Jul 1, 2016
c0a8285
Added undo feature for GTE.TREE.ADD
hkirat Jul 1, 2016
7b6db98
Added undo feature for GTE.TREE.DELETE
hkirat Jul 1, 2016
5ec344b
Added more functions for deletion
hkirat Jul 1, 2016
6311976
Added buttns and fixed minor bugs
hkirat Jul 1, 2016
0a85601
Added undo feature for GTE.TREE.PLAYER_ASSIGNMENT
hkirat Jul 1, 2016
d930705
Added undo feature for GTE.TREE.ADD of multiaction lines
hkirat Jul 1, 2016
3429df8
Added function to get all nodes in the subtree of a node
hkirat Jul 1, 2016
adb92a1
Added undo feature for GTE.TREE.DELETE of multiaction lines
hkirat Jul 1, 2016
a51984b
Added undo feature for GTE.TREE.PLAYER_ASSIGNMENT of multiaction lines
hkirat Jul 1, 2016
1c266ad
Started with adding undo functionality to GTE.TREE.MERGE
hkirat Jul 3, 2016
f21f76f
Added undo feature to undo initialising isets
hkirat Jul 4, 2016
b958ed2
Added undo feature for GTE.TREE.MERGE
hkirat Jul 4, 2016
ccb0f45
Added undo feature for merging using multiaction lines
hkirat Jul 6, 2016
9518a57
Added undo feature for deletion of isets
hkirat Jul 6, 2016
adc1f6a
Fixed minor rreference bug
hkirat Jul 8, 2016
8f20aec
Added functions to implement deletion of isets
hkirat Jul 8, 2016
af4de7d
Added undo feature to dissolving isets
hkirat Jul 9, 2016
524095e
Added undo feature to GTE.TREE.ADD after assigning isets
hkirat Jul 10, 2016
7633aeb
Fixed minor bugs in previous commit
hkirat Jul 10, 2016
aaf9c3c
Fixed typo and bug in dissolving isets
hkirat Jul 10, 2016
3f97d5d
Added the redo queue
hkirat Jul 17, 2016
9c09c2b
Initialised Event.js
hkirat Jul 17, 2016
1b4b24b
Added redo function
hkirat Jul 17, 2016
bc29c3b
Added redo button to the front end
hkirat Jul 17, 2016
eb4232f
Added event listener to redo button
hkirat Jul 18, 2016
d76a983
Modified redo function
hkirat Jul 18, 2016
34a2c6f
Modified functions
hkirat Jul 18, 2016
ad0d55c
Completed redo function for adding and deleteing nodes
hkirat Jul 18, 2016
b6824c8
undo -> execute
hkirat Jul 18, 2016
6772b66
Deleted Event.js
hkirat Jul 18, 2016
853c8ee
Added redo feature for player assignment
hkirat Jul 18, 2016
9a9dfc2
Code Refactor
hkirat Jul 19, 2016
0645cf7
Fixed minor bug in redoing player assignment
hkirat Jul 19, 2016
71d9c27
Fixed minor bug in redoing player assignment
hkirat Jul 19, 2016
03c7e3f
Added redo feature for Initialising isets
hkirat Jul 20, 2016
1005044
Added redo feature while selecting isets
hkirat Jul 23, 2016
24b0c4a
Removed current functionality of redoing, Reverting to onclick aproach
hkirat Jul 26, 2016
f161bd4
Started with event aproach for redoing events
hkirat Jul 26, 2016
7aed392
Added type variable to event class
hkirat Jul 27, 2016
d4f1132
Added function to find coordinates of a node
hkirat Jul 27, 2016
339a37e
Added functions to event.js
hkirat Jul 27, 2016
a3a6702
Fixed minor bugs
hkirat Jul 27, 2016
4f50ef1
Reinitialised redoqueue when an event is executed
hkirat Jul 29, 2016
e08340f
Added key events for capturing undo and redo
hkirat Aug 1, 2016
661bb6f
Added documentation
hkirat Aug 3, 2016
1263426
Added Add, delete, merge and dissolve buttons to undoable events
hkirat Aug 4, 2016
3298bbe
Shifted click function
hkirat Aug 5, 2016
75c701f
Added return at the end of the function
hkirat Aug 5, 2016
80680e7
Added undo to button-player-add and button-player-remove
hkirat Aug 5, 2016
de8fd4e
Added undo/redo feature to buttons
hkirat Aug 5, 2016
25781ec
Added return value to fix undo feature while dissolving isets
hkirat Aug 6, 2016
1a10436
Added docs
hkirat Aug 6, 2016
3b69cdd
Added docs
hkirat Aug 7, 2016
b0bff35
Added docs and fixed typos
hkirat Aug 8, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions html/css/font.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@
.icon-cog:before {
content: "\e900";
}
.icon-undo:before {
content: "\f0e2";
}
.icon-redo:before {
content: "\f064";
}
7 changes: 7 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<ul class="vertical-centered">
<li><button id="button-settings" class="button button--sacnite button--inverted" alt="Settings"><i class="icon-cog"></i></button></li>
</ul>
<ul class="vertical-centered">
<li><button id="button-undo" class="button button--sacnite button--inverted" alt="Undo" title="Undo"><i class="icon-undo"></i></button></li>
<li><button id="button-redo" class="button button--sacnite button--inverted" alt="Redo" title="Redo"><i class="icon-redo"></i></button></li>
</ul>
</div>
</div>
<div id="canvas">
Expand Down Expand Up @@ -103,5 +107,8 @@
<script src="js/tree/Tree.js"></script>
<script src="js/tree/Node.js"></script>
<script src="js/main.js"></script>
<script src="js/undoredo/Changes.js"></script>
<script src="js/undoredo/Event.js"></script>
<script src="js/undoredo/Change.js"></script>
</body>
</html>
35 changes: 30 additions & 5 deletions html/js/guiutils/MultiAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,66 @@ GTE.TREE = (function(parentModule) {
// for the nodes in the line
var smallestAndLargest = this.findSmallestAndLargest();
// If S < L, add children to those nodes so that ALL nodes have L children now.
var changes = new GTE.TREE.Changes(GTE.MODES.ADD, GTE.REDO.MULTIACTIONLINE, this.nodesInLine[0]);
if (smallestAndLargest.smallest < smallestAndLargest.largest) {
for (var i = 0; i < this.nodesInLine.length; i++) {
while (this.nodesInLine[i].children.length < smallestAndLargest.largest) {
this.nodesInLine[i].onClick();
var nodesAdded = this.nodesInLine[i].onClick(false);
for(var j = 0; j<nodesAdded.length; j++) {
changes.addChange(GTE.MODES.ADD, nodesAdded[j]);
}
}
}
}
// If L = 0, add two children to each node on the multiaction line, else
// If S = L, add one child to each node on the multiaction line.
else if (smallestAndLargest.largest === 0 || smallestAndLargest.smallest === smallestAndLargest.largest) {
for (var j = 0; j < this.nodesInLine.length; j++) {
this.nodesInLine[j].onClick();
var nodesAdded = this.nodesInLine[j].onClick(false);
for(var k = 0; k<nodesAdded.length; k++) {
changes.addChange(GTE.MODES.ADD, nodesAdded[k]);
}
}
}
changes.endSetOfChanges();
break;
case GTE.MODES.DELETE:
// if ANY of the nodes in the multiaction line have children,
// delete all their children but keep the node itself as part
// of the tree (i.e. even if some nodes are leaves already,
// do not delete them). otherwise (that is, ALL nodes in the
// multiaction line are leaves), delete all these leaves.
var changes = new GTE.TREE.Changes(GTE.MODES.DELETE, GTE.REDO.MULTIACTIONLINE, this.nodesInLine[0]);
var allLeaves = true;
for (var k = 0; k < this.nodesInLine.length; k++) {
if (this.nodesInLine[k].children.length > 0) {
allLeaves = false;
this.nodesInLine[k].onClick();
if(this.nodesInLine[k].isLeaf()) {
changes.addChange(GTE.MODES.DELETE, this.nodesInLine[k]);
} else {
changes.addChange(GTE.MODES.PLAYER_ASSIGNMENT, this.nodesInLine[k]);
changes.pushChildrenDeleted(this.nodesInLine[k]);
}
this.nodesInLine[k].onClick(false);
}
}
if (allLeaves) {
for (k = 0; k < this.nodesInLine.length; k++) {
this.nodesInLine[k].onClick();
changes.addChange(GTE.MODES.DELETE, this.nodesInLine[k]);
this.nodesInLine[k].onClick(false);
}
}
changes.endSetOfChanges();
break;
case GTE.MODES.PLAYER_ASSIGNMENT:
// set all nodes on the multiaction line to belong to the
// current player (which may be chance)
var changes = new GTE.TREE.Changes(GTE.MODES.PLAYER_ASSIGNMENT, GTE.REDO.MULTIACTIONLINE, this.nodesInLine[0]);
for (var l = 0; l < this.nodesInLine.length; l++) {
this.nodesInLine[l].onClick();
changes.addChange(GTE.MODES.PLAYER_ASSIGNMENT, this.nodesInLine[l]);
this.nodesInLine[l].onClick(false);
}
changes.endSetOfChanges();
break;
case GTE.MODES.MERGE:
// note that this mode button only works if every node belongs
Expand All @@ -111,15 +131,20 @@ GTE.TREE = (function(parentModule) {
var playerInLoop = null;
var numberOfChildrenInLoop = -1;
var isetInLoop = null;
var changes = new GTE.TREE.Changes(GTE.MODES.MERGE, GTE.REDO.MULTIACTIONLINE, this.nodesInLine[0]);
for (var m = 0; m < this.nodesInLine.length; m++) {
if (playerInLoop === this.nodesInLine[m].player &&
numberOfChildrenInLoop === this.nodesInLine[m].children.length) {
changes.pushMultiactionMerge(isetInLoop, this.nodesInLine[m].iset);
isetInLoop = GTE.tree.merge(isetInLoop, this.nodesInLine[m].iset);
}
isetInLoop = this.nodesInLine[m].iset;
numberOfChildrenInLoop = this.nodesInLine[m].children.length;
playerInLoop = this.nodesInLine[m].player;
}
if(changes.queue.length >= 0) {
changes.endSetOfChanges();
}
GTE.tree.draw();
break;
}
Expand Down
24 changes: 23 additions & 1 deletion html/js/guiutils/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ GTE.UI = (function (parentModule) {
// If iset tools have never been chosen
if (!this.isetToolsRan) {
// Assign singleton isets to each node with no iset
var changes = new GTE.TREE.Changes(GTE.UNDO.INITIALIZEISETS);
changes.pushSingletonChange(GTE.UNDO.INITIALIZEISETS);
changes.endSetOfChanges();
GTE.tree.initializeISets();
this.isetToolsRan = true;
}
Expand Down Expand Up @@ -110,8 +113,13 @@ GTE.UI = (function (parentModule) {
* Handles player buttons onclicks
* @param {Number|String} playerId Player to be selected
*/
Tools.prototype.buttonPlayerHandler = function(playerId) {
Tools.prototype.buttonPlayerHandler = function(playerIndex) {
return function () {
var player = document.getElementsByClassName("button-player")[playerIndex];
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, player);
var playerId = player.getAttribute("player");
if(GTE.tools.activePlayer != playerId)
changes.pushButtonSwitchChange();
GTE.tools.selectPlayer(parseInt(playerId));
};
};
Expand Down Expand Up @@ -189,6 +197,20 @@ GTE.UI = (function (parentModule) {
}
};

Tools.prototype.undo = function() {
if(GTE.UNDOQUEUE.length > 0) {
var evt = GTE.UNDOQUEUE.pop();
GTE.REDOQUEUE.push(evt.event);
evt.undo();
}
}

Tools.prototype.redo = function() {
if(GTE.REDOQUEUE.length > 0) {
GTE.REDOQUEUE.pop().redo();
}
}

/**
* Returns the colour correspondent to a given index. It is used to get the
* player colour. Player id would be the same as colourIndex
Expand Down
38 changes: 37 additions & 1 deletion html/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,41 +80,65 @@
});

document.getElementById("button-add").addEventListener("click", function(){
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, document.getElementById("button-add"));
changes.pushButtonSwitchChange(GTE.MODES.ADD);
GTE.tools.switchMode(GTE.MODES.ADD);
return false;
});

document.getElementById("button-remove").addEventListener("click", function(){
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, document.getElementById("button-remove"));
changes.pushButtonSwitchChange(GTE.MODES.DELETE);
GTE.tools.switchMode(GTE.MODES.DELETE);
return false;
});

document.getElementById("button-merge").addEventListener("click", function(){
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, document.getElementById("button-merge"));
changes.pushButtonSwitchChange(GTE.MODES.MERGE);
GTE.tools.switchMode(GTE.MODES.MERGE);
return false;
});

document.getElementById("button-dissolve").addEventListener("click", function(){
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, document.getElementById("button-dissolve"));
changes.pushButtonSwitchChange(GTE.MODES.DISSOLVE);
GTE.tools.switchMode(GTE.MODES.DISSOLVE);
return false;
});

var playerButtons = document.getElementsByClassName("button-player");
for (var i = 0; i < playerButtons.length; i++) {
playerButtons[i].addEventListener("click",
GTE.tools.buttonPlayerHandler(playerButtons[i].getAttribute("player")));
GTE.tools.buttonPlayerHandler(i));
}

document.getElementById("button-player-more").addEventListener("click", function(){
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, document.getElementById("button-player-more"));
changes.queue.push(new GTE.TREE.Change(null, GTE.UNDO.ADDPLAYER));
changes.endSetOfChanges();
GTE.tools.addPlayer();
return false;
});

document.getElementById("button-player-less").addEventListener("click", function(){
var changes = new GTE.TREE.Changes(GTE.UNDO.BUTTONSWITCH, null, document.getElementById("button-player-less"));
changes.queue.push(new GTE.TREE.Change(null, GTE.UNDO.REMOVEPLAYER));
changes.endSetOfChanges();
GTE.tools.removeLastPlayer();
return false;
});

document.getElementById("button-undo").addEventListener("click", function(){
GTE.tools.undo();
return false;
});

document.getElementById("button-redo").addEventListener("click", function(){
GTE.tools.redo();
return false;
});

document.getElementById("button-settings").addEventListener("click", function(){
var el = document.getElementById("settings");
el.style.display = (el.style.display == "block") ? "none" : "block";
Expand Down Expand Up @@ -200,4 +224,16 @@
settings.style.left = left + 'px';
}

function KeyPress(e) {
var evtobj = window.event? event : e
if (evtobj.keyCode == 90 && evtobj.ctrlKey) {
GTE.tools.undo();
}
if (evtobj.keyCode == 89 && evtobj.ctrlKey) {
GTE.tools.redo();
}
}

document.onkeydown = KeyPress;

}());
23 changes: 23 additions & 0 deletions html/js/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,28 @@ var GTE = (function () {
DEFAULT_CHANCE_NAME: "chance"
};

GTE.UNDO = {
POPSELECTEDQUEUE : 5,
INITIALIZEISETS : 6,
POPISET : 7,
ASSIGNMOVES : 8,
POPMOVES : 9,
ASSIGNISET : 10,
ADDISET : 11,
DEINITIALIZEISETS : 12,
PUSHSELECTEDQUEUE : 13,
PUSHISET : 14,
BUTTONSWITCH : 15,
ADDPLAYER : 16,
REMOVEPLAYER : 17
};

GTE.REDO = {
NODE : 0,
ISET : 1,
MULTIACTIONLINE : 2
}
GTE.UNDOQUEUE = [];
GTE.REDOQUEUE = [];
return GTE;
}());
38 changes: 31 additions & 7 deletions html/js/tree/ISet.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,60 +277,85 @@ GTE.TREE = (function (parentModule) {
/**
* On click function for the information set
*/
ISet.prototype.onClick = function () {
ISet.prototype.onClick = function (undo) {
switch (GTE.MODE) {
case GTE.MODES.ADD:
var changes = new GTE.TREE.Changes(GTE.MODES.ADD, GTE.REDO.ISET, this.firstNode);
if (this.numberOfMoves() === 0) {
// If no children, add two, since one child only doesn't
// make sense
GTE.tree.addChildISetTo(this);
GTE.tree.addChildISetTo(this);
var iset = GTE.tree.addChildISetTo(this);
changes.pushChangesAfterAddingIsets(iset);
var iset = GTE.tree.addChildISetTo(this);
changes.pushChangesAfterAddingIsets(iset);
} else {
GTE.tree.addChildNodeToISet(this);
}// Tell the tree to redraw itself
changes.pushChangesBeforeDissolving(this);
var isets = GTE.tree.addChildNodeToISet(this);
changes.pushChangesAfterAddingIsetsToArray(isets);
}
if(undo)
changes.endSetOfChanges();
// Tell the tree to redraw itself
GTE.tree.draw();
break;
case GTE.MODES.DELETE:
var children = this.getChildrenNodes();
var changes = new GTE.TREE.Changes(GTE.MODES.DELETE, GTE.REDO.ISET, this.firstNode);
if (children.length === 0) {
// Delete node
changes.assignSingletonIsetDeletion(this);
GTE.tree.deleteNode(this.firstNode);
} else {
// Delete all children
changes.assignChangesOnDeletingIset(this);
for (var i = 0; i < children.length; i++) {
// deleteNode() will delete everything below as well
GTE.tree.deleteNode(children[i]);
}
// Dissolve current iset
this.dissolve();
}
changes.endSetOfChanges();
// Tell the tree to redraw itself
GTE.tree.draw();
break;
case GTE.MODES.PLAYER_ASSIGNMENT:
// Change the player of every node in the iset
var nodes = this.getNodes();
var changes = new GTE.TREE.Changes(GTE.MODES.PLAYER_ASSIGNMENT, GTE.REDO.ISET, this.firstNode);
for (var j = 0; j < nodes.length; j++) {
changes.addChange(GTE.MODES.PLAYER_ASSIGNMENT, nodes[j]);
GTE.tree.assignSelectedPlayerToNode(nodes[j]);
}
changes.pushSingletonChange(GTE.UNDO.ASSIGNMOVES, this);
changes.endSetOfChanges();
// Reassign moves (create new moves and assign them to the
// children nodes as reachedBy)
this.reassignMoves();
GTE.tree.draw();
break;
case GTE.MODES.MERGE:
if (this.getPlayer().id !== 0) {
var changes = new GTE.TREE.Changes(GTE.MODES.MERGE, GTE.REDO.ISET, this.firstNode);
changes.addChange(GTE.MODES.MERGE, null, this);
if(GTE.tree.selected.length != 0) {
changes.select = true;
changes.iset = GTE.tree.selected[0];
}
changes.endSetOfChanges();
this.select();
}
break;
case GTE.MODES.DISSOLVE:
var changes = new GTE.TREE.Changes(GTE.MODES.DISSOLVE, GTE.REDO.ISET, this.firstNode);
if(changes.pushChangesBeforeDissolving(this))
changes.endSetOfChanges();
this.dissolve();
GTE.tree.draw();
break;
default:
break;
}

};

/**
Expand Down Expand Up @@ -362,7 +387,6 @@ GTE.TREE = (function (parentModule) {
this.removeNode(nodes[i]);
}
} else { // If there are no nodes
this.moves = []; //remove references to moves
GTE.tree.deleteISetFromList(this);
}
};
Expand Down
Loading