123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011 Zynga Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_DOWN = 1 << 0;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_MOVED = 1 << 1;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_DRAGGED = 1 << 2;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_UP = 1 << 3;
- /**
- * @constant
- * @type Number
- */
- cc.RIGHT_MOUSE_DOWN = 1 << 4;
- /**
- * @constant
- * @type Number
- */
- cc.RIGHT_MOUSE_DRAGGED = 1 << 5;
- /**
- * @constant
- * @type Number
- */
- cc.RIGHT_MOUSE_UP = 1 << 6;
- /**
- * @constant
- * @type Number
- */
- cc.OTHER_MOUSE_DOWN = 1 << 7;
- /**
- * @constant
- * @type Number
- */
- cc.OTHER_MOUSE_DRAGGED = 1 << 8;
- /**
- * @constant
- * @type Number
- */
- cc.OTHER_MOUSE_UP = 1 << 9;
- /**
- * @constant
- * @type Number
- */
- cc.SCROLL_WHEEL = 1 << 10;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_ENTERED = 1 << 11;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_EXITED = 1 << 12;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_LEFTBUTTON = 0;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_MIDDLEBUTTON = 1;
- /**
- * @constant
- * @type Number
- */
- cc.MOUSE_RIGHTBUTTON = 2;
- /**
- * CCMouseEventDelegate protocol.
- * Implement it in your node to receive any of mouse events
- */
- cc.MouseEventDelegate = cc.Class.extend({
- /**
- * <p>called when the "mouseDown" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onMouseDown:function (event) {
- return false;
- },
- /**
- * <p>called when the "mouseDragged" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates.</p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onMouseDragged:function (event) {
- return false;
- },
- /**
- * <p> called when the "mouseMoved" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onMouseMoved:function (event) {
- return false;
- },
- /**
- * <p> called when the "mouseUp" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onMouseUp:function (event) {
- return false;
- },
- //right
- /**
- * <p> called when the "rightMouseDown" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onRightMouseDown:function (event) {
- return false;
- },
- /**
- * <p> called when the "rightMouseDragged" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onRightMouseDragged:function (event) {
- return false;
- },
- /**
- * <p> called when the "rightMouseUp" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onRightMouseUp:function (event) {
- return false;
- },
- //other
- /**
- * <p>called when the "otherMouseDown" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onOtherMouseDown:function (event) {
- return false;
- },
- /**
- * <p> called when the "otherMouseDragged" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onOtherMouseDragged:function (event) {
- return false;
- },
- /**
- * <p> called when the "otherMouseUp" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onOtherMouseUp:function (event) {
- return false;
- },
- //scroll wheel
- /**
- * <p> called when the "scrollWheel" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} event
- * @return {Boolean}
- */
- onScrollWheel:function (event) {
- return false;
- },
- // enter / exit
- /**
- * <p> called when the "mouseEntered" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} theEvent
- * @return {Boolean}
- */
- onMouseEntered:function (theEvent) {
- return false;
- },
- /**
- * <p> called when the "mouseExited" event is received. <br/>
- * Return YES to avoid propagating the event to other delegates. </p>
- * @param {cc.Mouse} theEvent
- * @return {Boolean}
- */
- onMouseExited:function (theEvent) {
- return false;
- }
- });
- /**
- * The data of Mouse event
- * @class
- * @extends cc.Touch
- */
- cc.Mouse = cc.Touch.extend(/** @lends cc.Mouse# */{
- _wheelDelta: 0,
- _button: cc.MOUSE_LEFTBUTTON,
- /**
- * Gets mouse wheel delta
- * @returns {number}
- */
- getWheelDelta: function () {
- return this._wheelDelta;
- },
- /**
- * Sets mouse wheel delta
- * @param delta
- */
- setWheelDelta: function (delta) {
- this._wheelDelta = delta;
- },
- /**
- * Gets mouse button
- * @returns {number}
- */
- getButton: function () {
- return this._button;
- },
- /**
- * Sets mouse button
- * @param {number} button
- */
- setButton: function (button) {
- this._button = button;
- }
- });
- /**
- * cc.MouseHandler
- * Object than contains the delegate and priority of the event handler.
- * @class
- * @extends cc.Class
- */
- cc.MouseHandler = cc.Class.extend(/** @lends cc.MouseHandler# */{
- _delegate:null,
- _priority:0,
- _enabledSelectors:0,
- /**
- * @return {cc.MouseEventDelegate}
- */
- getDelegate:function () {
- return this._delegate;
- },
- /**
- * @param {cc.TouchDelegate} delegate
- */
- setDelegate:function (delegate) {
- this._delegate = delegate;
- },
- /**
- * @return {Number}
- */
- getPriority:function () {
- return this._priority;
- },
- /**
- * @param {Number} priority
- */
- setPriority:function (priority) {
- this._priority = priority;
- },
- /**
- * Enabled selectors
- * @return {Number}
- */
- getEnabledSelectors:function () {
- return this._enabledSelectors;
- },
- /**
- * @param {Number} value
- */
- setEnalbedSelectors:function (value) {
- this._enabledSelectors = value;
- },
- initWithDelegate:function (delegate, priority) {
- this._delegate = delegate;
- this._priority = priority;
- }
- });
- cc.MouseHandler.create = function (delegate, priority) {
- var handler = new cc.MouseHandler();
- handler.initWithDelegate(delegate, priority);
- return handler;
- };
- /**
- * cc.MouseDispatcher. <br/>
- * Singleton that handles all the mouse events.
- * @class
- * @extends cc.Class
- */
- cc.MouseDispatcher = cc.Class.extend(/** @lends cc.MouseDispatcher# */{
- _mousePressed:false,
- _rightMousePressed:false,
- _mouseDelegateHandlers:null,
- _dispatchEvents:false,
- init:function () {
- this._dispatchEvents = true;
- this._mouseDelegateHandlers = [];
- this._mousePressed = false;
- this._rightMousePressed = false;
- cc.MouseDispatcher._registerHtmlElementEvent(cc.canvas);
- return true;
- },
- _setMousePressed:function (pressed) {
- this._mousePressed = pressed;
- },
- _getMousePressed:function () {
- return this._mousePressed;
- },
-
- _setRightMousePressed:function (pressed) {
- this._rightMousePressed = pressed;
- },
- _getRightMousePressed:function () {
- return this._rightMousePressed;
- },
- /**
- * Adds a mouse delegate to the dispatcher's list. <br/>
- * Delegates with a lower priority value will be called before higher priority values. <br/>
- * All the events will be propagated to all the delegates, unless the one delegate returns YES. </br>
- * @param delegate
- * @param priority
- */
- addMouseDelegate:function (delegate, priority) {
- var handler = cc.MouseHandler.create(delegate, priority);
- this._mouseDelegateHandlers = this.forceAddHandler(handler, this._mouseDelegateHandlers);
- },
- /**
- * Force add handler
- * @param {cc.TouchHandler} handler
- * @param {Array} array
- * @return {Array}
- */
- forceAddHandler:function (handler, array) {
- var u = 0;
- for (var i = 0; i < array.length; i++) {
- var h = array[i];
- if (h) {
- if (h.getPriority() < handler.getPriority())
- ++u;
- if (h.getDelegate() == handler.getDelegate()) {
- cc.log("cc.MouseDispatcher.forceAddHandler(): handler has been added.") ;
- return array;
- }
- }
- }
- return cc.ArrayAppendObjectToIndex(array, handler, u);
- },
- /**
- * removes a mouse delegate
- * @param delegate
- */
- removeMouseDelegate:function (delegate) {
- if (delegate == null)
- return;
- for (var i = 0; i < this._mouseDelegateHandlers.length; i++) {
- var handler = this._mouseDelegateHandlers[i];
- if (handler && handler.getDelegate() == delegate) {
- cc.ArrayRemoveObject(this._mouseDelegateHandlers, handler);
- break;
- }
- }
- },
- _findHandler:function (delegate) {
- for (var i = 0; i < this._mouseDelegateHandlers.length; i++) {
- if (this._mouseDelegateHandlers[i] && this._mouseDelegateHandlers[i].getDelegate() == delegate) {
- return this._mouseDelegateHandlers[i];
- }
- }
- return null;
- },
- setPriority:function (priority, delegate) {
- if(!delegate)
- throw "cc.MouseDispatcher.setPriority(): delegate should be non-null";
- var handler = this._findHandler(delegate);
- if(!handler) {
- cc.log("cc.MouseDispatcher.setPriority(): Can't find MouseHandler in array");
- return;
- }
- if (handler.getPriority() != priority) {
- handler.setPriority(priority);
- this._mouseDelegateHandlers.sort(cc.less);
- }
- },
- /**
- * Removes all mouse delegates, releasing all the delegates
- */
- removeAllMouseDelegates:function () {
- this._mouseDelegateHandlers.length = 0;
- },
- mouseHandle:function (mouseObj, event, index) {
- for (var i = 0; i < this._mouseDelegateHandlers.length; i++) {
- var handler = this._mouseDelegateHandlers[i];
- switch (index) {
- case cc.MOUSE_DOWN:
- if (mouseObj.getButton() == cc.MOUSE_RIGHTBUTTON) {
- if (handler.getDelegate().onRightMouseDown)
- handler.getDelegate().onRightMouseDown(mouseObj);
- } else {
- if (handler.getDelegate().onMouseDown)
- handler.getDelegate().onMouseDown(mouseObj);
- }
- break;
- case cc.MOUSE_UP:
- if (mouseObj.getButton() == cc.MOUSE_RIGHTBUTTON) {
- if (handler.getDelegate().onRightMouseUp)
- handler.getDelegate().onRightMouseUp(mouseObj);
- } else {
- if (handler.getDelegate().onMouseUp)
- handler.getDelegate().onMouseUp(mouseObj);
- }
- break;
- case cc.MOUSE_MOVED:
- if (this._mousePressed) {
- if (handler.getDelegate().onMouseDragged)
- handler.getDelegate().onMouseDragged(mouseObj);
- } else if (this._rightMousePressed) {
- if (handler.getDelegate().onRightMouseDragged)
- handler.getDelegate().onRightMouseDragged(mouseObj);
- } else {
- if (handler.getDelegate().onMouseMoved)
- handler.getDelegate().onMouseMoved(mouseObj);
- }
- break;
- case cc.MOUSE_ENTERED:
- if (handler.getDelegate().onMouseEntered)
- handler.getDelegate().onMouseEntered(mouseObj);
- break;
- case cc.MOUSE_EXITED:
- if (handler.getDelegate().onMouseExited)
- handler.getDelegate().onMouseExited(mouseObj);
- break;
- case cc.SCROLL_WHEEL:
- if (handler.getDelegate().onScrollWheel)
- handler.getDelegate().onScrollWheel(mouseObj);
- break;
- }
- }
- }
- });
- cc.MouseDispatcher._preMousePoint = cc.p(0, 0);
- cc.MouseDispatcher._isRegisterEvent = false;
- cc.MouseDispatcher._registerHtmlElementEvent = function (element) {
- if (cc.MouseDispatcher._isRegisterEvent)
- return;
- window.addEventListener('mousedown', function (event) {
- if (event.button == cc.MOUSE_RIGHTBUTTON) {
- cc.Director.getInstance().getMouseDispatcher()._setRightMousePressed(true);
- } else {
- cc.Director.getInstance().getMouseDispatcher()._setMousePressed(true);
- }
- });
- window.addEventListener('mouseup', function (event) {
- if (event.button == cc.MOUSE_RIGHTBUTTON) {
- cc.Director.getInstance().getMouseDispatcher()._setRightMousePressed(false);
- } else {
- cc.Director.getInstance().getMouseDispatcher()._setMousePressed(false);
- }
- });
- function getMouseByEvent(event) {
- var pos = cc.getHTMLElementPosition(element);
- var tx = event.pageX;
- var ty = event.pageY;
- var eglViewer = cc.EGLView.getInstance();
- var pixelRatio = eglViewer.getDevicePixelRatio();
- var mouseX = (tx - pos.left) * pixelRatio / eglViewer.getScaleX();
- var mouseY = (pos.height - (ty - pos.top)) * pixelRatio / eglViewer.getScaleY();
- var mouse = new cc.Mouse(mouseX, mouseY);
- mouse._setPrevPoint(cc.MouseDispatcher._preMousePoint.x, cc.MouseDispatcher._preMousePoint.y);
- mouse.setButton(event.button);
- cc.MouseDispatcher._preMousePoint.x = mouseX;
- cc.MouseDispatcher._preMousePoint.y = mouseY;
- return mouse;
- }
- //register canvas mouse event
- element.addEventListener("mousedown", function (event) {
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(getMouseByEvent(event), event, cc.MOUSE_DOWN);
- });
- element.addEventListener("mouseup", function (event) {
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(getMouseByEvent(event), event, cc.MOUSE_UP);
- });
- element.addEventListener("mousemove", function (event) {
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(getMouseByEvent(event), event, cc.MOUSE_MOVED);
- });
- element.addEventListener("mousewheel", function (event) {
- var mouse = getMouseByEvent(event);
- mouse.setWheelDelta(event.wheelDelta);
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(mouse, event, cc.SCROLL_WHEEL);
- }, false);
-
- /* firefox fix */
- element.addEventListener("DOMMouseScroll", function(event) {
- var mouse = getMouseByEvent(event);
- mouse.setWheelDelta(event.detail * -120);
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(mouse, event, cc.SCROLL_WHEEL);
- });
- element.addEventListener("mouseout", function (event) {
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(getMouseByEvent(event), event, cc.MOUSE_EXITED);
- }, false);
- element.addEventListener("mouseover", function (event) {
- cc.Director.getInstance().getMouseDispatcher().mouseHandle(getMouseByEvent(event), event, cc.MOUSE_ENTERED);
- }, false);
- };
|