Skip to content

Commit

Permalink
CursorController: build while moving
Browse files Browse the repository at this point in the history
  • Loading branch information
newagebegins committed Jun 12, 2012
1 parent bf72bf9 commit 8e35f14
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/CursorControllerSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
describe("CursorController", function () {
it("should subscribe", function () {
var eventManager = new EventManager();
spyOn(eventManager, 'addSubscriber');
var cursor = new Cursor(eventManager);
var cursorController = new CursorController(eventManager, cursor);
expect(eventManager.addSubscriber).toHaveBeenCalledWith(cursorController, [Cursor.Event.MOVED]);
});

it("SPACE pressed - call build()", function () {
var eventManager = new EventManager();
var cursor = new Cursor(eventManager);
Expand All @@ -16,5 +24,14 @@ describe("CursorController", function () {
cursorController.notify({name: Keyboard.Event.KEY_RELEASED, key: Keyboard.Key.SPACE});
cursorController.notify({name: Keyboard.Event.KEY_PRESSED, key: Keyboard.Key.SPACE});
expect(cursor.build).toHaveBeenCalled();
cursor.build.reset();

cursorController.notify({'name': Cursor.Event.MOVED, 'cursor': cursor});
expect(cursor.build).toHaveBeenCalled();
cursor.build.reset();

cursorController.notify({name: Keyboard.Event.KEY_RELEASED, key: Keyboard.Key.SPACE});
cursorController.notify({'name': Cursor.Event.MOVED, 'cursor': cursor});
expect(cursor.build).not.toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions src/Cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Cursor.subclass(Sprite);

Cursor.Event = {};
Cursor.Event.BUILD = 'Cursor.Event.BUILD';
Cursor.Event.MOVED = 'Cursor.Event.MOVED';

Cursor.prototype.toNormalSpeed = function () {
Sprite.prototype.toNormalSpeed.call(this);
Expand Down
9 changes: 9 additions & 0 deletions src/CursorController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
function CursorController(eventManager, cursor) {
SpriteController.call(this, eventManager, cursor);
this._eventManager.addSubscriber(this, [Cursor.Event.MOVED]);
this._spacePressed = false;
}

CursorController.subclass(SpriteController);

CursorController.prototype.notify = function (event) {
SpriteController.prototype.notify.call(this, event);

if (event.name == Cursor.Event.MOVED && this._spacePressed) {
event.cursor.build();
}
};

CursorController.prototype.keyPressed = function (key) {
SpriteController.prototype.keyPressed.call(this, key);

Expand Down

0 comments on commit 8e35f14

Please sign in to comment.