Skip to content

Commit d781ccc

Browse files
committed
Added keyboard event handling
1 parent 5bc71c4 commit d781ccc

3 files changed

Lines changed: 97 additions & 9 deletions

File tree

src/libs/cocos2d/Director.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var Director = Thing.extend(/** @scope cocos.Director# */{
3030
_nextScene:null,
3131

3232
init: function() {
33+
@super;
34+
3335
this.set('sceneStack', []);
3436
},
3537

@@ -91,20 +93,22 @@ var Director = Thing.extend(/** @scope cocos.Director# */{
9193
canvas.addEventListener('mousedown', mouseDown, false);
9294
canvas.addEventListener('mousemove', mouseMoved, false);
9395

94-
/*
9596
// Keyboard events
96-
var keyboardDispatcher = KeyboardDispatcher.get('sharedDispatcher');
9797
function keyDown(evt) {
98-
keyboardDispatcher.keyDown({event: evt})
98+
this._keysDown = this._keysDown || {};
99+
eventDispatcher.keyDown(evt)
99100
}
100101
function keyUp(evt) {
101-
keyboardDispatcher.keyUp({event: evt})
102+
eventDispatcher.keyUp(evt)
102103
}
104+
/*
103105
function keyPress(evt) {
104-
keyboardDispatcher.keyPress({event: evt})
106+
eventDispatcher.keyPress(evt)
105107
}
108+
*/
106109
document.documentElement.addEventListener('keydown', keyDown, false);
107110
document.documentElement.addEventListener('keyup', keyUp, false);
111+
/*
108112
document.documentElement.addEventListener('keypress', keyPress, false);
109113
*/
110114
},

src/libs/cocos2d/EventDispatcher.js

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ var EventDispatcher = Thing.extend(/** @scope cocos.EventDispatcher# */ {
1313
dispatchEvents: true,
1414
keyboardDelegates: null,
1515
mouseDelegates: null,
16+
_keysDown: null,
1617

1718
init: function() {
1819
@super;
1920

2021
this.keyboardDelegates = [];
2122
this.mouseDelegates = [];
23+
24+
this._keysDown = {};
2225
},
2326

2427
addDelegate: function(opts) {
@@ -82,19 +85,41 @@ var EventDispatcher = Thing.extend(/** @scope cocos.EventDispatcher# */ {
8285

8386
// TODO flags
8487

85-
this.addDelegate({delegate: delegate, priority: priority, flags: flags, list:this.mouseDelegates});
88+
this.addDelegate({delegate: delegate, priority: priority, flags: flags, list: this.mouseDelegates});
8689
},
8790

8891
removeMouseDelegate: function(opts) {
8992
var delegate = opts['delegate'];
9093

91-
this.removeDelegate({delegate: delegate, list:this.mouseDelegates});
94+
this.removeDelegate({delegate: delegate, list: this.mouseDelegates});
9295
},
9396

9497
removeAllMouseDelegate: function() {
95-
this.removeAllDelegates({list:this.mouseDelegates});
98+
this.removeAllDelegates({list: this.mouseDelegates});
9699
},
97100

101+
addKeyboardDelegate: function(opts) {
102+
var delegate = opts['delegate'],
103+
priority = opts['priority'];
104+
105+
var flags = 0;
106+
107+
// TODO flags
108+
109+
this.addDelegate({delegate: delegate, priority: priority, flags: flags, list: this.keyboardDelegates});
110+
},
111+
112+
removeKeyboardDelegate: function(opts) {
113+
var delegate = opts['delegate'];
114+
115+
this.removeDelegate({delegate: delegate, list: this.keyboardDelegates});
116+
},
117+
118+
removeAllKeyboardDelegate: function() {
119+
this.removeAllDelegates({list: this.keyboardDelegates});
120+
},
121+
122+
98123

99124
// Mouse Events
100125

@@ -178,6 +203,47 @@ var EventDispatcher = Thing.extend(/** @scope cocos.EventDispatcher# */ {
178203
}
179204
}
180205
}
206+
},
207+
208+
// Keyboard events
209+
keyDown: function(evt) {
210+
var kc = evt.keyCode;
211+
if (!this.dispatchEvents || this._keysDown[kc]) {
212+
return;
213+
}
214+
215+
this._keysDown[kc] = true;
216+
217+
for (var i = 0; i < this.keyboardDelegates.length; i++) {
218+
var entry = this.keyboardDelegates[i];
219+
if (entry.delegate.keyDown) {
220+
var swallows = entry.delegate.keyDown(evt);
221+
if (swallows) {
222+
break;
223+
}
224+
}
225+
}
226+
},
227+
228+
keyUp: function(evt) {
229+
if (!this.dispatchEvents) {
230+
return;
231+
}
232+
233+
var kc = evt.keyCode;
234+
if (this._keysDown[kc]) {
235+
delete this._keysDown[kc];
236+
}
237+
238+
for (var i = 0; i < this.keyboardDelegates.length; i++) {
239+
var entry = this.keyboardDelegates[i];
240+
if (entry.delegate.keyUp) {
241+
var swallows = entry.delegate.keyUp(evt);
242+
if (swallows) {
243+
break;
244+
}
245+
}
246+
}
181247
}
182248

183249
});

src/libs/cocos2d/Layer.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ var Node = require('./Node').Node,
88
*/
99
var Layer = Node.extend(/** @scope cocos.Layer# */{
1010
isMouseEnabled: false,
11+
isKeyboardEnabled: false,
1112
mouseDelegatePriority: 0,
13+
keyboardDelegatePriority: 0,
1214

1315
init: function() {
1416
@super;
@@ -24,6 +26,9 @@ var Layer = Node.extend(/** @scope cocos.Layer# */{
2426
if (this.isMouseEnabled) {
2527
EventDispatcher.get('sharedDispatcher').addMouseDelegate({delegate: this, priority:this.get('mouseDelegatePriority')});
2628
}
29+
if (this.isKeyboardEnabled) {
30+
EventDispatcher.get('sharedDispatcher').addKeyboardDelegate({delegate: this, priority:this.get('keyboardDelegatePriority')});
31+
}
2732

2833
@super;
2934
},
@@ -32,6 +37,9 @@ var Layer = Node.extend(/** @scope cocos.Layer# */{
3237
if (this.isMouseEnabled) {
3338
EventDispatcher.get('sharedDispatcher').removeMouseDelegate({delegate: this});
3439
}
40+
if (this.isKeyboardEnabled) {
41+
EventDispatcher.get('sharedDispatcher').removeKeyboardDelegate({delegate: this});
42+
}
3543

3644
@super;
3745
},
@@ -44,7 +52,17 @@ var Layer = Node.extend(/** @scope cocos.Layer# */{
4452
EventDispatcher.get('sharedDispatcher').removeMouseDelegate({delegate: this});
4553
}
4654
}
47-
}.observes('isMouseEnabled')
55+
}.observes('isMouseEnabled'),
56+
57+
_updateIsKeyboardEnabled: function() {
58+
if (this.isRunning) {
59+
if (this.isKeyboardEnabled) {
60+
EventDispatcher.get('sharedDispatcher').addKeyboardDelegate({delegate: this, priority:this.get('keyboardDelegatePriority')});
61+
} else {
62+
EventDispatcher.get('sharedDispatcher').removeKeyboardDelegate({delegate: this});
63+
}
64+
}
65+
}.observes('isKeyboardEnabled')
4866
});
4967

5068
module.exports.Layer = Layer;

0 commit comments

Comments
 (0)