Skip to content

Commit 4b6df52

Browse files
author
SeanLin
committed
Merge pull request #592 from dingpinglv/iss1518_MouseDispatcher
fixed #1518 implement cc.MouseDispatcher
2 parents 48ffe89 + 2ab4059 commit 4b6df52

File tree

5 files changed

+688
-43
lines changed

5 files changed

+688
-43
lines changed

cocos2d/CCDirector.js

+14
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
164164
_touchDispatcher:null,
165165
_keyboardDispatcher:null,
166166
_accelerometer:null,
167+
_mouseDispatcher:null,
167168

168169
_watcherFun:null,
169170
_watcherSender:null,
@@ -244,6 +245,10 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
244245
//accelerometer
245246
//this._accelerometer = new cc.Accelerometer();
246247

248+
//MouseDispatcher
249+
this._mouseDispatcher = new cc.MouseDispatcher();
250+
this._mouseDispatcher.init();
251+
247252
return true;
248253
},
249254

@@ -1121,6 +1126,15 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
11211126
}
11221127
},
11231128

1129+
getMouseDispatcher:function(){
1130+
return this._mouseDispatcher;
1131+
},
1132+
1133+
setMouseDispatcher:function( mouseDispatcher){
1134+
if(this._mouseDispatcher != mouseDispatcher)
1135+
this._mouseDispatcher = mouseDispatcher;
1136+
},
1137+
11241138
_createStatsLabel:function () {
11251139
this._FPSLabel = cc.LabelTTF.create("00.0", "Arial", 18, cc.size(60, 16), cc.TEXT_ALIGNMENT_RIGHT);
11261140
this._SPFLabel = cc.LabelTTF.create("0.000", "Arial", 18, cc.size(60, 16), cc.TEXT_ALIGNMENT_RIGHT);

cocos2d/build.xml

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<file name="touch_dispatcher/CCTouchDelegateProtocol.js"/>
6969
<file name="touch_dispatcher/CCTouchHandler.js"/>
7070
<file name="touch_dispatcher/CCTouchDispatcher.js"/>
71+
<file name="touch_dispatcher/CCMouseDispatcher.js"/>
7172
<file name="keyboard_dispatcher/CCKeyboardDelegate.js"/>
7273
<file name="keyboard_dispatcher/CCKeyboardDispatcher.js"/>
7374
<file name="text_input_node/CCIMEDispatcher.js"/>

cocos2d/layers_scenes_transitions_nodes/CCLayer.js

+152-43
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
****************************************************************************/
2626

2727
/** Layer will receive all the touches at once The onTouchesXXX API will be called
28-
*/
28+
*/
2929
cc.TOUCH_ALL_AT_ONCE = 0;
3030

3131
/** Layer will receive only one touch at the time. The onTouchXXX API will be called */
@@ -45,6 +45,7 @@ cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
4545
_touchPriority:0,
4646
_touchMode:cc.TOUCH_ALL_AT_ONCE,
4747
_isMouseEnabled:false,
48+
_mousePriority:0,
4849

4950
/**
5051
* Constructor
@@ -88,25 +89,43 @@ cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
8889
* If isTouchEnabled, this method is called onEnter.
8990
*/
9091
registerWithTouchDispatcher:function () {
91-
if( this._touchMode === cc.TOUCH_ALL_AT_ONCE )
92+
if (this._touchMode === cc.TOUCH_ALL_AT_ONCE)
9293
cc.Director.getInstance().getTouchDispatcher().addStandardDelegate(this, this._touchPriority);
9394
else
9495
cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, this._touchPriority, true);
9596
},
9697

97-
isMouseEnabled:function(){
98-
return this._isMouseEnabled;
98+
isMouseEnabled:function () {
99+
return this._isMouseEnabled;
99100
},
100101

101-
setMouseEnabled:function(enabled){
102-
if(this._isMouseEnabled != enabled){
102+
setMouseEnabled:function (enabled) {
103+
if (this._isMouseEnabled != enabled) {
103104
this._isMouseEnabled = enabled;
104-
if(this._isRunning){
105+
if (this._isRunning) {
106+
if (enabled)
107+
cc.Director.getInstance().getMouseDispatcher().addMouseDelegate(this, this._mousePriority);
108+
else
109+
cc.Director.getInstance().getMouseDispatcher().removeMouseDelegate(this);
110+
}
111+
}
112+
},
105113

114+
setMousePriority:function (priority) {
115+
if (this._mousePriority != priority) {
116+
this._mousePriority = priority;
117+
// Update touch priority with handler
118+
if (this._isMouseEnabled) {
119+
this.setMouseEnabled(false);
120+
this.setMouseEnabled(true);
106121
}
107122
}
108123
},
109124

125+
getMousePriority:function () {
126+
return this._mousePriority;
127+
},
128+
110129
/**
111130
* whether or not it will receive Touch events.<br/>
112131
* You can enable / disable touch events with this property.<br/>
@@ -139,41 +158,41 @@ cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
139158
/** returns the priority of the touch event handler
140159
* @return {Number}
141160
*/
142-
getTouchPriority:function() {
161+
getTouchPriority:function () {
143162
return this._touchPriority;
144163
},
145164

146165
/** Sets the touch event handler priority. Default is 0.
147166
* @param {Number} priority
148167
*/
149-
setTouchPriority:function(priority) {
150-
if( this._touchPriority != priority ) {
168+
setTouchPriority:function (priority) {
169+
if (this._touchPriority != priority) {
151170
this._touchPriority = priority;
152171
// Update touch priority with handler
153-
if( this._isTouchEnabled ) {
154-
this.setTouchEnabled( false );
155-
this.setTouchEnabled( true );
172+
if (this._isTouchEnabled) {
173+
this.setTouchEnabled(false);
174+
this.setTouchEnabled(true);
156175
}
157176
}
158177
},
159178

160179
/** returns the touch mode.
161180
* @return {Number}
162181
*/
163-
getTouchMode:function() {
182+
getTouchMode:function () {
164183
return this._touchMode;
165184
},
166185

167186
/** Sets the touch mode.
168187
* @param {Number} mode
169188
*/
170-
setTouchMode:function(mode) {
171-
if( this._touchMode != mode ) {
189+
setTouchMode:function (mode) {
190+
if (this._touchMode != mode) {
172191
this._touchMode = mode;
173192
// update the mode with handler
174-
if( this._isTouchEnabled ) {
175-
this.setTouchEnabled( false );
176-
this.setTouchEnabled( true );
193+
if (this._isTouchEnabled) {
194+
this.setTouchEnabled(false);
195+
this.setTouchEnabled(true);
177196
}
178197
}
179198
},
@@ -249,14 +268,15 @@ cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
249268
this._super();
250269

251270
// add this layer to concern the Accelerometer Sensor
252-
if (this._isAccelerometerEnabled) {
271+
if (this._isAccelerometerEnabled)
253272
director.getAccelerometer().setDelegate(this);
254-
}
255273

256274
// add this layer to concern the kaypad msg
257-
if (this._isKeyboardEnabled) {
275+
if (this._isKeyboardEnabled)
258276
director.getKeyboardDispatcher().addDelegate(this);
259-
}
277+
278+
if (this._isMouseEnabled)
279+
director.getMouseDispatcher().addMouseDelegate(this,this._mousePriority);
260280
},
261281

262282
/**
@@ -278,6 +298,9 @@ cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
278298
director.getKeyboardDispatcher().removeDelegate(this);
279299
}
280300

301+
if (this._isMouseEnabled)
302+
director.getMouseDispatcher().removeMouseDelegate(this);
303+
281304
this._super();
282305
},
283306

@@ -362,52 +385,138 @@ cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
362385

363386
// ---------------------CCMouseEventDelegate interface------------------------------
364387

365-
//
366-
// left
367-
//
368-
onMouseDown:function(event){
388+
/**
389+
* <p>called when the "mouseDown" event is received. <br/>
390+
* Return YES to avoid propagating the event to other delegates. </p>
391+
* @param event
392+
* @return {Boolean}
393+
*/
394+
onMouseDown:function (event) {
395+
return false;
369396
},
370397

371-
onMouseDragged:function(event){
398+
/**
399+
* <p>called when the "mouseDragged" event is received. <br/>
400+
* Return YES to avoid propagating the event to other delegates.</p>
401+
* @param event
402+
* @return {Boolean}
403+
*/
404+
onMouseDragged:function (event) {
405+
return false;
372406
},
373407

374-
onMouseMoved : function(event){
408+
/**
409+
* <p> called when the "mouseMoved" event is received. <br/>
410+
* Return YES to avoid propagating the event to other delegates. </p>
411+
* @param event
412+
* @return {Boolean}
413+
*/
414+
onMouseMoved:function (event) {
415+
return false;
375416
},
376417

377-
onMouseUp:function(event){
418+
/**
419+
* <p> called when the "mouseUp" event is received. <br/>
420+
* Return YES to avoid propagating the event to other delegates. </p>
421+
* @param event
422+
* @return {Boolean}
423+
*/
424+
onMouseUp:function (event) {
425+
return false;
378426
},
379427

380428
//right
381-
382-
onRightMouseDown : function(event){
429+
/**
430+
* <p> called when the "rightMouseDown" event is received. <br/>
431+
* Return YES to avoid propagating the event to other delegates. </p>
432+
* @param event
433+
* @return {Boolean}
434+
*/
435+
onRightMouseDown:function (event) {
436+
return false;
383437
},
384438

385-
onRightMouseDragged : function(event){
439+
/**
440+
* <p> called when the "rightMouseDragged" event is received. <br/>
441+
* Return YES to avoid propagating the event to other delegates. </p>
442+
* @param event
443+
* @return {Boolean}
444+
*/
445+
onRightMouseDragged:function (event) {
446+
return false;
386447
},
387448

388-
onRightMouseUp : function( event ){
449+
/**
450+
* <p> called when the "rightMouseUp" event is received. <br/>
451+
* Return YES to avoid propagating the event to other delegates. </p>
452+
* @param event
453+
* @return {Boolean}
454+
*/
455+
onRightMouseUp:function (event) {
456+
return false;
389457
},
390458

391459
//other
392-
onOtherMouseDown : function(event){
460+
/**
461+
* <p>called when the "otherMouseDown" event is received. <br/>
462+
* Return YES to avoid propagating the event to other delegates. </p>
463+
* @param event
464+
* @return {Boolean}
465+
*/
466+
onOtherMouseDown:function (event) {
467+
return false;
393468
},
394469

395-
onOtherMouseDragged : function( event){
470+
/**
471+
* <p> called when the "otherMouseDragged" event is received. <br/>
472+
* Return YES to avoid propagating the event to other delegates. </p>
473+
* @param event
474+
* @return {Boolean}
475+
*/
476+
onOtherMouseDragged:function (event) {
477+
return false;
396478
},
397479

398-
onOtherMouseUp:function(event){
480+
/**
481+
* <p> called when the "otherMouseUp" event is received. <br/>
482+
* Return YES to avoid propagating the event to other delegates. </p>
483+
* @param event
484+
* @return {Boolean}
485+
*/
486+
onOtherMouseUp:function (event) {
487+
return false;
399488
},
400489

401490
//scroll wheel
402-
onScrollWheel : function( event){
491+
/**
492+
* <p> called when the "scrollWheel" event is received. <br/>
493+
* Return YES to avoid propagating the event to other delegates. </p>
494+
* @param event
495+
* @return {Boolean}
496+
*/
497+
onScrollWheel:function (event) {
498+
return false;
403499
},
404500

405501
// enter / exit
406-
407-
onMouseEntered:function(theEvent){
502+
/**
503+
* <p> called when the "mouseEntered" event is received. <br/>
504+
* Return YES to avoid propagating the event to other delegates. </p>
505+
* @param theEvent
506+
* @return {Boolean}
507+
*/
508+
onMouseEntered:function (theEvent) {
509+
return false;
408510
},
409511

410-
onMouseExited:function(theEvent){
512+
/**
513+
* <p> called when the "mouseExited" event is received. <br/>
514+
* Return YES to avoid propagating the event to other delegates. </p>
515+
* @param theEvent
516+
* @return {Boolean}
517+
*/
518+
onMouseExited:function (theEvent) {
519+
return false;
411520
}
412521
});
413522

@@ -709,7 +818,7 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
709818
layerWidth = layerWidth || this.getContentSize().width;
710819
layerHeight = layerHeight || this.getContentSize().height;
711820

712-
if(!this._sourceGradientCanvas)
821+
if (!this._sourceGradientCanvas)
713822
this._sourceGradientCanvas = document.createElement('canvas');
714823
this._sourceGradientCanvas.width = 2;
715824
this._sourceGradientCanvas.height = 2;
@@ -721,7 +830,7 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
721830
var image_colors = context_colors.getImageData(0, 0, 2, 2);
722831
var data = image_colors.data;
723832

724-
if(!this._drawGradientCanvas)
833+
if (!this._drawGradientCanvas)
725834
this._drawGradientCanvas = document.createElement('canvas');
726835
this._drawGradientCanvas.width = layerWidth;
727836
this._drawGradientCanvas.height = layerHeight;

cocos2d/platform/jsloader.js

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
'touch_dispatcher/CCTouchDelegateProtocol.js',
8383
'touch_dispatcher/CCTouchHandler.js',
8484
'touch_dispatcher/CCTouchDispatcher.js',
85+
'touch_dispatcher/CCMouseDispatcher.js',
8586
'keyboard_dispatcher/CCKeyboardDelegate.js',
8687
'keyboard_dispatcher/CCKeyboardDispatcher.js',
8788
'text_input_node/CCIMEDispatcher.js',

0 commit comments

Comments
 (0)