Skip to content

Commit

Permalink
Revised draw event firering, fixes undo/redo firering, added missing
Browse files Browse the repository at this point in the history
fire for state. Fixes #147.
  • Loading branch information
ivmartel committed May 7, 2015
1 parent 3fdf76e commit 97357ef
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/app/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ dwv.App = function ()
fileIO.onload = function (data) {
// load state
var state = new dwv.State(self);
state.fromJSON(data);
state.fromJSON(data, fireEvent);
};
fileIO.onerror = function (error) { handleError(error); };
// main load (asynchronous)
Expand Down Expand Up @@ -587,7 +587,7 @@ dwv.App = function ()
urlIO.onload = function (data) {
// load state
var state = new dwv.State(self);
state.fromJSON(data);
state.fromJSON(data, fireEvent);
};
urlIO.onerror = function (error) { handleError(error); };
// main load (asynchronous)
Expand Down
6 changes: 5 additions & 1 deletion src/app/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dwv.State = function (app)
* Load state.
* @method load
*/
this.fromJSON = function (json) {
this.fromJSON = function (json, eventCallback) {
var data = JSON.parse(json);
// display
app.getViewController().setWindowLevel(data["window-center"], data["window-width"]);
Expand All @@ -70,6 +70,10 @@ dwv.State = function (app)
var cmd = new dwv.tool.DrawGroupCommand(
group, shape.className,
app.getDrawLayer(k) );
if ( typeof eventCallback !== "undefined" ) {
cmd.onExecute = eventCallback;
cmd.onUndo = eventCallback;
}
cmd.execute();
app.getUndoStack().add(cmd);
}
Expand Down
107 changes: 102 additions & 5 deletions src/tools/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dwv.tool.DrawGroupCommand = function (group, name, layer)
layer.add(group);
// draw
layer.draw();
// callback
this.onExecute({'type': 'draw-create', 'id': group.id});
};
/**
* Undo the command.
Expand All @@ -39,9 +41,30 @@ dwv.tool.DrawGroupCommand = function (group, name, layer)
group.remove();
// draw
layer.draw();
// callback
this.onUndo({'type': 'draw-delete', 'id': group.id});
};
}; // DrawGroupCommand class

/**
* Handle an execute event.
* @method onExecute
* @param {Object} event The execute event with type and id.
*/
dwv.tool.DrawGroupCommand.prototype.onExecute = function (/*event*/)
{
// default does nothing.
};
/**
* Handle an undo event.
* @method onUndo
* @param {Object} event The undo event with type and id.
*/
dwv.tool.DrawGroupCommand.prototype.onUndo = function (/*event*/)
{
// default does nothing.
};

/**
* Move group command.
* @class MoveGroupCommand
Expand Down Expand Up @@ -69,6 +92,8 @@ dwv.tool.MoveGroupCommand = function (group, name, translation, layer)
});
// draw
layer.draw();
// callback
this.onExecute({'type': 'draw-move', 'id': group.id});
};
/**
* Undo the command.
Expand All @@ -82,8 +107,29 @@ dwv.tool.MoveGroupCommand = function (group, name, translation, layer)
});
// draw
layer.draw();
// callback
this.onUndo({'type': 'draw-move', 'id': group.id});
};
}; // MoveShapeCommand class
}; // MoveGroupCommand class

/**
* Handle an execute event.
* @method onExecute
* @param {Object} event The execute event with type and id.
*/
dwv.tool.MoveGroupCommand.prototype.onExecute = function (/*event*/)
{
// default does nothing.
};
/**
* Handle an undo event.
* @method onUndo
* @param {Object} event The undo event with type and id.
*/
dwv.tool.MoveGroupCommand.prototype.onUndo = function (/*event*/)
{
// default does nothing.
};

/**
* Change group command.
Expand All @@ -109,6 +155,8 @@ dwv.tool.ChangeGroupCommand = function (name, func, startAnchor, endAnchor, laye
func( endAnchor, image );
// draw
layer.draw();
// callback
this.onExecute({'type': 'draw-change'});
};
/**
* Undo the command.
Expand All @@ -119,8 +167,29 @@ dwv.tool.ChangeGroupCommand = function (name, func, startAnchor, endAnchor, laye
func( startAnchor, image );
// draw
layer.draw();
// callback
this.onUndo({'type': 'draw-change'});
};
}; // ChangeShapeCommand class
}; // ChangeGroupCommand class

/**
* Handle an execute event.
* @method onExecute
* @param {Object} event The execute event with type and id.
*/
dwv.tool.ChangeGroupCommand.prototype.onExecute = function (/*event*/)
{
// default does nothing.
};
/**
* Handle an undo event.
* @method onUndo
* @param {Object} event The undo event with type and id.
*/
dwv.tool.ChangeGroupCommand.prototype.onUndo = function (/*event*/)
{
// default does nothing.
};

/**
* Delete group command.
Expand All @@ -145,6 +214,8 @@ dwv.tool.DeleteGroupCommand = function (group, name, layer)
group.remove();
// draw
layer.draw();
// callback
this.onExecute({'type': 'draw-delete', 'id': group.id});
};
/**
* Undo the command.
Expand All @@ -155,8 +226,29 @@ dwv.tool.DeleteGroupCommand = function (group, name, layer)
layer.add(group);
// draw
layer.draw();
// callback
this.onUndo({'type': 'draw-create', 'id': group.id});
};
}; // DeleteShapeCommand class
}; // DeleteGroupCommand class

/**
* Handle an execute event.
* @method onExecute
* @param {Object} event The execute event with type and id.
*/
dwv.tool.DeleteGroupCommand.prototype.onExecute = function (/*event*/)
{
// default does nothing.
};
/**
* Handle an undo event.
* @method onUndo
* @param {Object} event The undo event with type and id.
*/
dwv.tool.DeleteGroupCommand.prototype.onUndo = function (/*event*/)
{
// default does nothing.
};

/**
* Drawing tool.
Expand Down Expand Up @@ -395,11 +487,12 @@ dwv.tool.Draw = function (app, shapeFactoryList)
drawLayer.hitGraphEnabled(true);
// draw shape command
command = new dwv.tool.DrawGroupCommand(group, self.shapeName, drawLayer);
command.onExecute = fireEvent;
command.onUndo = fireEvent;
// execute it
command.execute();
// save it in undo stack
app.getUndoStack().add(command);
fireEvent({'type': 'draw-create'});

// set shape on
var shape = group.getChildren( function (node) {
Expand Down Expand Up @@ -656,17 +749,21 @@ dwv.tool.Draw = function (app, shapeFactoryList)
document.body.style.cursor = 'default';
// delete command
var delcmd = new dwv.tool.DeleteGroupCommand(this.getParent(), cmdName, drawLayer);
delcmd.onExecute = fireEvent;
delcmd.onUndo = fireEvent;
delcmd.execute();
app.getUndoStack().add(delcmd);
fireEvent({'type': 'draw-delete'});
}
else {
// save drag move
var translation = {'x': pos.x - dragStartPos.x,
'y': pos.y - dragStartPos.y};
if ( translation.x !== 0 || translation.y !== 0 ) {
var mvcmd = new dwv.tool.MoveGroupCommand(this.getParent(), cmdName, translation, drawLayer);
mvcmd.onExecute = fireEvent;
mvcmd.onUndo = fireEvent;
app.getUndoStack().add(mvcmd);
// the move is handled by kinetic, trigger an event manually
fireEvent({'type': 'draw-move'});
}
// reset anchors
Expand Down
3 changes: 2 additions & 1 deletion src/tools/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ dwv.tool.ShapeEditor = function (app)
// store the change command
var chgcmd = new dwv.tool.ChangeGroupCommand(
cmdName, updateFunction, startAnchor, endAnchor, this.getLayer(), image);
chgcmd.onExecute = drawEventCallback;
chgcmd.onUndo = drawEventCallback;
chgcmd.execute();
app.getUndoStack().add(chgcmd);
drawEventCallback({'type': 'draw-change'});
// reset start anchor
startAnchor = endAnchor;
});
Expand Down

0 comments on commit 97357ef

Please sign in to comment.