From 78598ae54aea9d2bc9defc7f3bcd45dcb5fa95d8 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sun, 8 Sep 2013 11:24:41 +0100 Subject: [PATCH] Implemented my own LinkedList class to make the new Input Handler easier to work with. And also just generally useful to have too. --- Phaser.sublime-workspace | 610 ++----------------------------------- examples/js.php | 2 + examples/linkedlist1.php | 74 +++++ src/core/Group.js | 3 + src/core/LinkedList.js | 179 +++++++++++ src/gameobjects/Sprite.js | 5 +- src/input/Input.js | 3 + src/input/InputHandler.js | 69 +++++ src/physics/arcade/Body.js | 2 - 9 files changed, 360 insertions(+), 587 deletions(-) create mode 100644 examples/linkedlist1.php create mode 100644 src/core/LinkedList.js create mode 100644 src/input/InputHandler.js diff --git a/Phaser.sublime-workspace b/Phaser.sublime-workspace index e4d2a6d8..d25a1ce6 100644 --- a/Phaser.sublime-workspace +++ b/Phaser.sublime-workspace @@ -523,40 +523,7 @@ "file": "src/core/Group.js", "settings": { - "buffer_size": 17183, - "line_ending": "Windows", - "name": "Phaser.Group = function () {" - } - }, - { - "file": "examples/anchor1.php", - "settings": - { - "buffer_size": 721, - "line_ending": "Windows" - } - }, - { - "file": "src/gameobjects/GameObjectFactory.js", - "settings": - { - "buffer_size": 2737, - "line_ending": "Windows" - } - }, - { - "file": "src/physics/arcade/Body.js", - "settings": - { - "buffer_size": 5298, - "line_ending": "Windows" - } - }, - { - "file": "src/gameobjects/Sprite.js", - "settings": - { - "buffer_size": 12810, + "buffer_size": 17255, "line_ending": "Windows" } } @@ -589,19 +556,27 @@ }, "file_history": [ - "/D/wamp/www/phaser/src/utils/Debug.js", - "/D/wamp/www/phaser/src/tween/Tween.js", - "/D/wamp/www/phaser/src/core/World.js", - "/D/wamp/www/phaser/src/physics/arcade/Body.js", "/D/wamp/www/phaser/src/gameobjects/Sprite.js", + "/D/wamp/www/phaser/src/physics/arcade/Body.js", + "/D/wamp/www/phaser/src/core/World.js", + "/D/wamp/www/phaser/src/core/Game.js", + "/D/wamp/www/phaser/examples/quadtree2.php", + "/D/wamp/www/phaser/examples/body3.php", + "/D/wamp/www/phaser/examples/body2.php", + "/D/wamp/www/phaser/src/math/QuadTree.js", + "/D/wamp/www/phaser/examples/invaders.php", + "/D/wamp/www/phaser/examples/anchor1.php", + "/D/wamp/www/phaser/src/physics/arcade/ArcadePhysics.js", + "/D/wamp/www/phaser/src/geom/Rectangle.js", + "/D/wamp/www/phaser/src/utils/Debug.js", + "/D/wamp/www/phaser/src/gameobjects/GameObjectFactory.js", + "/D/wamp/www/phaser/src/core/Group.js", + "/D/wamp/www/phaser/src/tween/Tween.js", "/D/wamp/www/phaser/TS Source/core/Group.ts", "/D/wamp/www/phaser/TS Source/core/Group.js", "/D/wamp/www/phaser/src/pixi/display/DisplayObjectContainer.js", - "/D/wamp/www/phaser/src/core/Game.js", - "/D/wamp/www/phaser/src/gameobjects/GameObjectFactory.js", "/D/wamp/www/phaser/examples/consoleBanner3.php", "/D/wamp/www/consoledemo/index.html", - "/D/wamp/www/phaser/src/physics/arcade/ArcadePhysics.js", "/D/wamp/www/phaser/src/core/Stage.js", "/D/wamp/www/phaser/examples/js.php", "/D/wamp/www/phaser/src/system/Canvas.js", @@ -621,9 +596,7 @@ "/D/wamp/www/phaser/TS Source/input/InputHandler.js", "/D/wamp/www/phaser/src/input/Input.js", "/D/wamp/www/phaser/src/input/Pointer.js", - "/D/wamp/www/phaser/src/math/QuadTree.js", "/D/wamp/www/phaser/examples/motion1.php", - "/D/wamp/www/phaser/src/geom/Rectangle.js", "/D/wamp/www/phaser/todo/phaser clean up/Collision.js", "/D/wamp/www/phaser/todo/physics/Collision.js", "/D/wamp/www/phaser/src/time/Time.js", @@ -710,13 +683,7 @@ "/D/wamp/www/phaser/TS Source/math/Vec2Utils.js", "/D/wamp/www/phaser/TS Source/input/InputManager.js", "/D/wamp/www/phaser/TS Source/input/Keyboard.ts", - "/D/wamp/www/phaser/TS Source/input/Keyboard.js", - "/D/wamp/www/phaser/src/pixi/primitives/Graphics.js", - "/D/wamp/www/pixi.js/examples/example 11 - RenderTexture/index.html", - "/D/wamp/www/phaser/src/pixi/textures/RenderTexture.js", - "/D/wamp/www/phaser/examples/assets/fonts/desyrel.json", - "/D/wamp/www/phaser/src/math/RandomDataGenerator.js", - "/D/wamp/www/phaser/src/system/Device.js" + "/D/wamp/www/phaser/TS Source/input/Keyboard.js" ], "find": { @@ -754,6 +721,10 @@ "case_sensitive": true, "find_history": [ + "bounds", + "body.bounds", + "right", + "x", "_iPrev", "first", "child1", @@ -877,11 +848,7 @@ "4", "256", "Phaser.Sprite", - "\n});\n", - "Phaser.Sprite", - "tilingSprite", - "()", - "this." + "\n});\n" ], "highlight": true, "in_selection": false, @@ -889,6 +856,7 @@ "regex": false, "replace_history": [ + "body", "_stage", ")',", "'),", @@ -983,7 +951,7 @@ "groups": [ { - "selected": 3, + "selected": 0, "sheets": [ { @@ -991,20 +959,19 @@ "file": "src/core/Group.js", "settings": { - "buffer_size": 17183, + "buffer_size": 17255, "regions": { }, "selection": [ [ - 310, - 310 + 4441, + 4441 ] ], "settings": { - "auto_name": "Phaser.Group = function () {", "codeintel": true, "codeintel_config": { @@ -1112,528 +1079,7 @@ "translate_tabs_to_spaces": false }, "translation.x": 0.0, - "translation.y": 0.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 1, - "file": "examples/anchor1.php", - "settings": - { - "buffer_size": 721, - "regions": - { - }, - "selection": - [ - [ - 674, - 670 - ] - ], - "settings": - { - "codeintel": true, - "codeintel_config": - { - "JavaScript": - { - "codeintel_max_recursive_dir_depth": 2, - "codeintel_scan_files_in_project": false, - "javascriptExtraPaths": - [ - ] - }, - "PHP": - { - "codeintel_max_recursive_dir_depth": 5, - "codeintel_scan_files_in_project": false, - "phpExtraPaths": - [ - ] - }, - "Python": - { - "env": - { - } - } - }, - "codeintel_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_live": true, - "codeintel_live_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_max_recursive_dir_depth": 10, - "codeintel_scan_exclude_dir": - { - "JavaScript": - [ - "/build/", - "/min/" - ] - }, - "codeintel_scan_files_in_project": true, - "codeintel_selected_catalogs": - [ - "PyWin32", - "jQuery", - "Rails" - ], - "codeintel_snippets": true, - "codeintel_syntax_map": - { - "Python Django": "Python" - }, - "sublime_auto_complete": true, - "syntax": "Packages/PHP/PHP.tmLanguage", - "translate_tabs_to_spaces": false - }, - "translation.x": 0.0, - "translation.y": 0.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 2, - "file": "src/gameobjects/GameObjectFactory.js", - "settings": - { - "buffer_size": 2737, - "regions": - { - }, - "selection": - [ - [ - 2254, - 2254 - ] - ], - "settings": - { - "codeintel": true, - "codeintel_config": - { - "JavaScript": - { - "codeintel_max_recursive_dir_depth": 2, - "codeintel_scan_files_in_project": false, - "javascriptExtraPaths": - [ - ] - }, - "PHP": - { - "codeintel_max_recursive_dir_depth": 5, - "codeintel_scan_files_in_project": false, - "phpExtraPaths": - [ - ] - }, - "Python": - { - "env": - { - } - } - }, - "codeintel_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_live": true, - "codeintel_live_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_max_recursive_dir_depth": 10, - "codeintel_scan_exclude_dir": - { - "JavaScript": - [ - "/build/", - "/min/" - ] - }, - "codeintel_scan_files_in_project": true, - "codeintel_selected_catalogs": - [ - "PyWin32", - "jQuery", - "Rails" - ], - "codeintel_snippets": true, - "codeintel_syntax_map": - { - "Python Django": "Python" - }, - "sublime_auto_complete": true, - "syntax": "Packages/JavaScript/JavaScript.tmLanguage", - "tab_size": 4, - "translate_tabs_to_spaces": true - }, - "translation.x": 0.0, - "translation.y": 668.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 3, - "file": "src/physics/arcade/Body.js", - "settings": - { - "buffer_size": 5298, - "regions": - { - }, - "selection": - [ - [ - 3665, - 3665 - ] - ], - "settings": - { - "codeintel": true, - "codeintel_config": - { - "JavaScript": - { - "codeintel_max_recursive_dir_depth": 2, - "codeintel_scan_files_in_project": false, - "javascriptExtraPaths": - [ - ] - }, - "PHP": - { - "codeintel_max_recursive_dir_depth": 5, - "codeintel_scan_files_in_project": false, - "phpExtraPaths": - [ - ] - }, - "Python": - { - "env": - { - } - } - }, - "codeintel_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_live": true, - "codeintel_live_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_max_recursive_dir_depth": 10, - "codeintel_scan_exclude_dir": - { - "JavaScript": - [ - "/build/", - "/min/" - ] - }, - "codeintel_scan_files_in_project": true, - "codeintel_selected_catalogs": - [ - "PyWin32", - "jQuery", - "Rails" - ], - "codeintel_snippets": true, - "codeintel_syntax_map": - { - "Python Django": "Python" - }, - "sublime_auto_complete": true, - "syntax": "Packages/JavaScript/JavaScript.tmLanguage" - }, - "translation.x": 0.0, - "translation.y": 1602.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 4, - "file": "src/gameobjects/Sprite.js", - "settings": - { - "buffer_size": 12810, - "regions": - { - }, - "selection": - [ - [ - 9001, - 9001 - ] - ], - "settings": - { - "codeintel": true, - "codeintel_config": - { - "JavaScript": - { - "codeintel_max_recursive_dir_depth": 2, - "codeintel_scan_files_in_project": false, - "javascriptExtraPaths": - [ - ] - }, - "PHP": - { - "codeintel_max_recursive_dir_depth": 5, - "codeintel_scan_files_in_project": false, - "phpExtraPaths": - [ - ] - }, - "Python": - { - "env": - { - } - } - }, - "codeintel_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_live": true, - "codeintel_live_enabled_languages": - [ - "JavaScript", - "Mason", - "XBL", - "XUL", - "RHTML", - "SCSS", - "Python", - "HTML", - "Ruby", - "Python3", - "XML", - "Sass", - "XSLT", - "Django", - "HTML5", - "Perl", - "CSS", - "Twig", - "Less", - "Smarty", - "Node.js", - "Tcl", - "TemplateToolkit", - "PHP" - ], - "codeintel_max_recursive_dir_depth": 10, - "codeintel_scan_exclude_dir": - { - "JavaScript": - [ - "/build/", - "/min/" - ] - }, - "codeintel_scan_files_in_project": true, - "codeintel_selected_catalogs": - [ - "PyWin32", - "jQuery", - "Rails" - ], - "codeintel_snippets": true, - "codeintel_syntax_map": - { - "Python Django": "Python" - }, - "sublime_auto_complete": true, - "syntax": "Packages/JavaScript/JavaScript.tmLanguage", - "tab_size": 4, - "translate_tabs_to_spaces": true - }, - "translation.x": 0.0, - "translation.y": 4132.0, + "translation.y": 3550.0, "zoom_level": 1.0 }, "type": "text" diff --git a/examples/js.php b/examples/js.php index 208d8425..7a6cc384 100644 --- a/examples/js.php +++ b/examples/js.php @@ -45,6 +45,7 @@ + @@ -59,6 +60,7 @@ + diff --git a/examples/linkedlist1.php b/examples/linkedlist1.php new file mode 100644 index 00000000..ec3f2daf --- /dev/null +++ b/examples/linkedlist1.php @@ -0,0 +1,74 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/core/Group.js b/src/core/Group.js index 89673ab2..67b07bda 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -205,6 +205,9 @@ Phaser.Group.prototype = { * @param {string} index The string name of the member variable you want to sort on. Default value is "z". * @param {number} order A Group constant that defines the sort order. Possible values are Group.ASCENDING and Group.DESCENDING. Default value is Group.ASCENDING. */ + + // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.c + sort: function (index, order) { // if (typeof index === "undefined") { index = 'z'; } // if (typeof order === "undefined") { order = Phaser.Types.SORT_ASCENDING; } diff --git a/src/core/LinkedList.js b/src/core/LinkedList.js new file mode 100644 index 00000000..be4966ea --- /dev/null +++ b/src/core/LinkedList.js @@ -0,0 +1,179 @@ +Phaser.LinkedList = function () { +}; + +Phaser.LinkedList.prototype = { + + _iNext: null, + _iPrev: null, + first: null, + last: null, + sprite: { name: 'HD' }, + + add: function (child) { + + // If the list is empty + if (this.first == null && this.last == null) + { + this.first = child; + this.last = child; + this._iNext = child; + child._iPrev = this; + return; + } + + // Get gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list) + this.last._iNext = child; + + child._iPrev = this.last; + + this.last = child; + + }, + + remove: function (child) { + + // If the list is empty + if (this.first == null && this.last == null) + { + return; + } + + // The only node? + if (this.first == child && this.last == child) + { + this.first = null; + this.last = null; + this._iNext = null; + child._iNext = null; + child._iPrev = null; + return; + } + + var childPrev = child._iPrev; + + // Tail node? + if (child._iNext) + { + // Has another node after it? + child._iNext._iPrev = child._iPrev; + } + + childPrev._iNext = child._iNext; + + }, + + dump: function () { + + console.log("\nNode\t\t|\t\tNext\t\t|\t\tPrev\t\t|\t\tFirst\t\t|\t\tLast"); + console.log("\t\t\t|\t\t\t\t\t|\t\t\t\t\t|\t\t\t\t\t|"); + + var nameNext = '-'; + var namePrev = '-'; + var nameFirst = '-'; + var nameLast = '-'; + + if (this._iNext) + { + nameNext = this._iNext.sprite.name; + } + + if (this._iPrev) + { + namePrev = this._iPrev.sprite.name; + } + + if (this.first) + { + nameFirst = this.first.sprite.name; + } + + if (this.last) + { + nameLast = this.last.sprite.name; + } + + if (typeof nameNext === 'undefined') + { + nameNext = '-'; + } + + if (typeof namePrev === 'undefined') + { + namePrev = '-'; + } + + if (typeof nameFirst === 'undefined') + { + nameFirst = '-'; + } + + if (typeof nameLast === 'undefined') + { + nameLast = '-'; + } + + console.log('HD' + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast); + + var entity = this; + + var testObject = entity.last._iNext; + entity = entity.first; + + do + { + var name = entity.sprite.name || '*'; + var nameNext = '-'; + var namePrev = '-'; + var nameFirst = '-'; + var nameLast = '-'; + + if (entity._iNext) + { + nameNext = entity._iNext.sprite.name; + } + + if (entity._iPrev) + { + namePrev = entity._iPrev.sprite.name; + } + + if (entity.first) + { + nameFirst = entity.first.sprite.name; + } + + if (entity.last) + { + nameLast = entity.last.sprite.name; + } + + if (typeof nameNext === 'undefined') + { + nameNext = '-'; + } + + if (typeof namePrev === 'undefined') + { + namePrev = '-'; + } + + if (typeof nameFirst === 'undefined') + { + nameFirst = '-'; + } + + if (typeof nameLast === 'undefined') + { + nameLast = '-'; + } + + console.log(name + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast); + + entity = entity._iNext; + + } + while(entity != testObject) + + } + +}; \ No newline at end of file diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 92953252..eb9eae9f 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -10,9 +10,6 @@ Phaser.Sprite = function (game, x, y, key, frame) { // If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all this.exists = true; - // An "invisible" sprite isn't rendered at all - this.visible = true; - // This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering this.alive = true; @@ -58,6 +55,8 @@ Phaser.Sprite = function (game, x, y, key, frame) { this.currentFrame = this.game.cache.getFrame(key); } + this.input = new Phaser.InputHandler(this); + /** * The anchor sets the origin point of the texture. * The default is 0,0 this means the textures origin is the top left diff --git a/src/input/Input.js b/src/input/Input.js index 5ec213ca..4553fcb8 100644 --- a/src/input/Input.js +++ b/src/input/Input.js @@ -253,6 +253,9 @@ Phaser.Input.prototype = { onTap: null, onHold: null, + // A linked list of interactive objects + interactiveItems: new Phaser.LinkedList(), + /** * Starts the Input Manager running * @method start diff --git a/src/input/InputHandler.js b/src/input/InputHandler.js new file mode 100644 index 00000000..102fa69e --- /dev/null +++ b/src/input/InputHandler.js @@ -0,0 +1,69 @@ +Phaser.InputHandler = function (sprite) { + + this.game = sprite.game; + this.sprite = sprite; + + this.enabled = false; + + // Linked list references + this.last = this; + this.first = this; + + /** + * The PriorityID controls which Sprite receives an Input event first if they should overlap. + */ + this.priorityID = 0; + + this.isDragged = false; + this.dragPixelPerfect = false; + this.allowHorizontalDrag = true; + this.allowVerticalDrag = true; + this.bringToTop = false; + this.snapOnDrag = false; + this.snapOnRelease = false; + this.snapX = 0; + this.snapY = 0; + + /** + * Is this sprite allowed to be dragged by the mouse? true = yes, false = no + * @default false + */ + this.draggable = false; + + /** + * A region of the game world within which the sprite is restricted during drag + * @default null + */ + this.boundsRect = null; + + /** + * An Sprite the bounds of which this sprite is restricted during drag + * @default null + */ + this.boundsSprite = null; + + /** + * If this object is set to consume the pointer event then it will stop all propogation from this object on. + * For example if you had a stack of 6 sprites with the same priority IDs and one consumed the event, none of the others would receive it. + * @type {bool} + */ + this.consumePointerEvent = false; + +}; + +Phaser.InputHandler.prototype = { + + game: null, + sprite: null, + + // Linked list references + parent: null, + _iNext: null, + _iPrev: null, + first: null, + last: null, + + enable: function () { + }, + +}; \ No newline at end of file diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index eec10cda..a320404b 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -122,7 +122,6 @@ Phaser.Physics.Arcade.Body.prototype = { }, - /* postUpdate: function () { this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width); @@ -134,7 +133,6 @@ Phaser.Physics.Arcade.Body.prototype = { } }, - */ checkWorldBounds: function () {