Mouse handler updates.

This commit is contained in:
photonstorm 2013-11-04 03:16:17 +00:00
parent 9022a2f9d3
commit 19ddad8095
4 changed files with 166 additions and 36 deletions

View file

@ -40,7 +40,13 @@ Change Log
Version 1.1.3 - in build
* New: Sprite.animations.getAnimation will return an animation instance which was added by name.
* New: Added Mouse.button which is set to the button that was pressed: Phaser.Mouse.LEFT_BUTTON, MIDDLE_BUTTON or RIGHT_BUTTON (thanks wKLV)
* New: Added Mouse.pointerLock signal which you can listen to whenever the browser enters or leaves pointer lock mode.
* Fixed: Mouse.stop now uses the true useCapture, which means the event listeners stop listening correctly (thanks beeglebug)
* Updated: RequestAnimationFrame now retains the callbackID which is passed to cancelRequestAnimationFrame.
* Updated: Button now goes back to over state when setFrames used in action (thanks beeglebug)
* Updated: plugins now have a postUpdate callback (thanks cocoademon)
Version 1.1.2 - November 1st 2013

View file

@ -405,28 +405,50 @@ Phaser.InputHandler.prototype = {
/**
* Is the Pointer over this Sprite?
* @method Phaser.InputHandler#pointerOver
* @param {Pointer} pointer
* @return {bool
* @param {number} [index] - The ID number of a Pointer to check. If you don't provide a number it will check all Pointers.
* @return {boolean} True if the given pointer (if a index was given, or any pointer if not) is over this object.
*/
pointerOver: function (pointer) {
pointerOver: function (index) {
pointer = pointer || 0;
return this._pointerData[pointer].isOver;
if (typeof index === 'undefined')
{
for (var i = 0; i < 10; i++)
{
if (this._pointerData[i].isOver)
{
return true;
}
}
}
else
{
return this._pointerData[index].isOver;
}
},
/**
* Is the Pointer outside of this Sprite?
* @method Phaser.InputHandler#pointerOut
* @param {Pointer} pointer
* @return {boolean}
* @param {number} [index] - The ID number of a Pointer to check. If you don't provide a number it will check all Pointers.
* @return {boolean} True if the given pointer (if a index was given, or any pointer if not) is out of this object.
*/
pointerOut: function (pointer) {
pointer = pointer || 0;
return this._pointerData[pointer].isOut;
if (typeof index === 'undefined')
{
for (var i = 0; i < 10; i++)
{
if (this._pointerData[i].isOut)
{
return true;
}
}
}
else
{
return this._pointerData[index].isOut;
}
},
@ -547,6 +569,8 @@ Phaser.InputHandler.prototype = {
return false;
}
// For an enabled sprite that may have been clicked
if (this.draggable && this._draggedPointerID == pointer.id)
{
return this.updateDrag(pointer);

View file

@ -25,27 +25,28 @@ Phaser.Mouse = function (game) {
this.callbackContext = this.game;
/**
* @property {Description} mouseDownCallback - Description.
* @property {function} mouseDownCallback - Description.
* @default
*/
this.mouseDownCallback = null;
/**
* @property {Description} mouseMoveCallback - Description.
* @property {function} mouseMoveCallback - Description.
* @default
*/
this.mouseMoveCallback = null;
/**
* @property {Description} mouseUpCallback - Description.
* @property {function} mouseUpCallback - Description.
* @default
*/
this.mouseUpCallback = null;
/**
* @property {Description} mouseTypeDown - The type of click. -1 for not clicking
* @property {number} button- The type of click, either: Phaser.Mouse.NO_BUTTON, Phaser.Mouse.LEFT_BUTTON, Phaser.Mouse.MIDDLE_BUTTON or Phaser.Mouse.RIGHT_BUTTON.
* @default
*/
this.mouseTypeDown = -1;
this.button = -1;
/**
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
@ -61,6 +62,13 @@ Phaser.Mouse = function (game) {
*/
this.locked = false;
/**
* This event is dispatched when the browser enters or leaves pointer lock state.
* @property {Phaser.Signal} pointerLock
* @default
*/
this.pointerLock = new Phaser.Signal;
};
/**
@ -86,7 +94,6 @@ Phaser.Mouse.MIDDLE_BUTTON = 1;
*/
Phaser.Mouse.RIGHT_BUTTON = 2;
Phaser.Mouse.prototype = {
/**
@ -122,7 +129,7 @@ Phaser.Mouse.prototype = {
},
/**
* Description.
* The internal method that handles the mouse down event from the browser.
* @method Phaser.Mouse#onMouseDown
* @param {MouseEvent} event
*/
@ -130,9 +137,18 @@ Phaser.Mouse.prototype = {
event.preventDefault();
if (event.which === 1) this.mouseTypeDown = Phaser.Mouse.LEFT_BUTTON;
else if (event.which === 2) this.mouseTypeDown = Phaser.Mouse.MIDDLE_BUTTON;
else if (event.which === 3) this.mouseTypeDown = Phaser.Mouse.RIGHT_BUTTON;
if (event.which === 1)
{
this.button = Phaser.Mouse.LEFT_BUTTON;
}
else if (event.which === 2)
{
this.button = Phaser.Mouse.MIDDLE_BUTTON;
}
else if (event.which === 3)
{
this.button = Phaser.Mouse.RIGHT_BUTTON;
}
if (this.mouseDownCallback)
{
@ -151,7 +167,7 @@ Phaser.Mouse.prototype = {
},
/**
* Description
* The internal method that handles the mouse move event from the browser.
* @method Phaser.Mouse#onMouseMove
* @param {MouseEvent} event
*/
@ -176,7 +192,7 @@ Phaser.Mouse.prototype = {
},
/**
* Description.
* The internal method that handles the mouse up event from the browser.
* @method Phaser.Mouse#onMouseUp
* @param {MouseEvent} event
*/
@ -184,7 +200,7 @@ Phaser.Mouse.prototype = {
event.preventDefault();
this.mouseTypeDown = Phaser.Mouse.NO_BUTTON;
this.button = Phaser.Mouse.NO_BUTTON;
if (this.mouseUpCallback)
{
@ -203,7 +219,9 @@ Phaser.Mouse.prototype = {
},
/**
* Description.
* If the browser supports it you can request that the pointer be locked to the browser window.
* This is classically known as 'FPS controls', where the pointer can't leave the browser until the user presses an exit key.
* If the browser successfully enters a locked state the event Phaser.Mouse.pointerLock will be dispatched and the first parameter will be 'true'.
* @method Phaser.Mouse#requestPointerLock
*/
requestPointerLock: function () {
@ -222,15 +240,15 @@ Phaser.Mouse.prototype = {
return _this.pointerLockChange(event);
};
document.addEventListener('pointerlockchange', this._pointerLockChange, false);
document.addEventListener('mozpointerlockchange', this._pointerLockChange, false);
document.addEventListener('webkitpointerlockchange', this._pointerLockChange, false);
document.addEventListener('pointerlockchange', this._pointerLockChange, true);
document.addEventListener('mozpointerlockchange', this._pointerLockChange, true);
document.addEventListener('webkitpointerlockchange', this._pointerLockChange, true);
}
},
/**
* Description.
* Internal pointerLockChange handler.
* @method Phaser.Mouse#pointerLockChange
* @param {MouseEvent} event
*/
@ -242,17 +260,19 @@ Phaser.Mouse.prototype = {
{
// Pointer was successfully locked
this.locked = true;
this.pointerLock.dispatch(true);
}
else
{
// Pointer was unlocked
this.locked = false;
this.pointerLock.dispatch(false);
}
},
/**
* Description.
* Internal release pointer lock handler.
* @method Phaser.Mouse#releasePointerLock
*/
releasePointerLock: function () {
@ -261,9 +281,9 @@ Phaser.Mouse.prototype = {
document.exitPointerLock();
document.removeEventListener('pointerlockchange', this._pointerLockChange);
document.removeEventListener('mozpointerlockchange', this._pointerLockChange);
document.removeEventListener('webkitpointerlockchange', this._pointerLockChange);
document.removeEventListener('pointerlockchange', this._pointerLockChange, true);
document.removeEventListener('mozpointerlockchange', this._pointerLockChange, true);
document.removeEventListener('webkitpointerlockchange', this._pointerLockChange, true);
},
@ -273,9 +293,9 @@ Phaser.Mouse.prototype = {
*/
stop: function () {
this.game.stage.canvas.removeEventListener('mousedown', this._onMouseDown);
this.game.stage.canvas.removeEventListener('mousemove', this._onMouseMove);
this.game.stage.canvas.removeEventListener('mouseup', this._onMouseUp);
this.game.stage.canvas.removeEventListener('mousedown', this._onMouseDown, true);
this.game.stage.canvas.removeEventListener('mousemove', this._onMouseMove, true);
this.game.stage.canvas.removeEventListener('mouseup', this._onMouseUp, true);
}

View file

@ -0,0 +1,80 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Phaser Tutorial 01 - Getting Started</title>
<script src="build/phaser.js" type="text/javascript"></script>
<style>
body {
font-family: Arial;
font-size: 14px;
}
</style>
</head>
<body>
<h1>Getting started with Phaser</h1>
<h2>Part 1 - Setting up your development environment</h2>
<p>In this tutorial we're going to cover setting-up a development enviornment with which you can build your Phaser games. This will include running a local web server, picking an IDE, getting the latest version of Phaser and checking it all works together properly.</p>
<p>"But why do I need a local web server anyway? Can't I just drag the html files onto my browser?"</p>
<p>Not these days, no. I appreciate that it's a bit confusing, even contradictory at times, but it all boils down to browser security in the end. If you're making a static html web site, with a few images in it, then you can happily drag this into your browser and see the end results. You can also "Save As" entire web pages locally and re-open them, with all the contents mostly intact. So why can't you drag an HTML5 game into a browser and have it work?</p>
<p>It's to do with the protocol used to access the files. When you request anything over the web you're using http - and the server level security is enough to ensure you can only access files you're meant to. But when you drag a file in it's loaded via the local file system (technically file://) and that is a massively restricted one, for obvious reasons. Under file:// there's no concept of domains, no server level security - just a raw file system. Ask yourself this: do you really want JavaScript to have the ability to load files from your local file system? The immediate answer should of course be "no way!". If JavaScript had free reign while operating under file:// there would be nothing stopping it loading up any system file it fancied and sending it off over the net to anywhere it liked.</p>
<p>Because this is so dangerous browsers lock themselves down tighter than Alcatraz when running under file://. Every single page becomes treated as a unique local domain. That is why "Save Web page" work, to a degree. They still are under the same cross-site restrictions as if they were on a live server. (to a degree, they still can't save anything or send data out or load data like a game), but also why "Save Web page" cannot make a cross site request, i.e. it can't load or send local data elsewhere. There's a quite detailed post here about it: http://blog.chromium.org/2008/12/security-in-depth-local-web-pages.html
Due to the way that web browser sandbox security works you will need a local web server running. This is so that your games can load in external assets like images and music. By default web browsers are unable to load local files, as if you think about it for a moment you should be grateful for that fact! Although there are ways to modify your browsers start-up settings to bypass the sandbox security we really don't recommend it, and as you're building games for <em>the web</em> it's only sensible that you run a local environment that as closely emulates the web on which your game will be running anyway.</p>
<h2>Download the latest version of Phaser</h2>
<p>In this tutorial we're going to build a game that will introduce some of the core features of Phaser to you. Before we begin there are a few things you'll need to have set-up if you haven't already:</p>
<h2>Download the latest version of Phaser</h2>
<p>The latest version of Phaser is available to download from github. At the time of writing that is version 1.1.2. If you aren't familiar with using git then github offer a handy "Download as zip" option which will download the whole repository in a single zip file. You can then unzip this locally and get to all the files. If you are happy using git then pull down the latest version of the repositiory before starting.</p>
<h2>Set-up your development environment</h2>
<h3>Method 1: Run a local web server</h3>
http://cesanta.com/downloads.html
<p>On Windows there are lots of "single install" bundles available. These are easy-to-install files that package together popular web technologies like Apache, PHP and MySQL and install them all for you at once, often with a handy system-tray icon to manage them too. We would recommend either <a href="http://www.wampserver.com/en/">WAMP Server</a> or <a href="http://www.apachefriends.org/en/xampp.html">XAMPP</a>. Instead of an 'all in one' bundle you could also download just the web server on its own. Microsoft provide IIS and Apache can be downloaded for Windows as well.</p>
<p><strong>Note:</strong> Skype likes to steal port 80 by default. This is the traditional port for a web server to run over and it might interfer with WAMP or similar being able to start. To disable this within Skype go to "Tools - Options - Connection" and uncheck the "Use port 80 and 443 as alternatives for incoming connections" checkbox.</p>
<p>On OS X you could download <a href="http://www.mamp.info/en/index.html">MAMP</a> which comes in two versions (one free, one paid for). Or there are guides for setting up a local web server manually, such as <a href="https://discussions.apple.com/docs/DOC-3083">this guide written for Mountain Lion</a>.
<h3>Method 2: grunt connect</h3>
<p><a href="http://gruntjs.com/">Grunt</a> is an extremely powerful tool to have installed, regardless if you use it as a web server or not. At its essence it's a JavaScript based task runner and allows you to automate tedious time consuming tasks. But it can also be configured with Connect for serving local files and <a href="https://github.com/gruntjs/grunt-contrib-connect">here's a guide on doing just that</a>.
<h3>Method 3: Simple HTTP Server with Python</h3>
<p>If you need a quick web server running and you don't want to mess around with setting up apache or something similar, then Python can help. Python comes with a simple built in HTTP server. With the help of this little HTTP server you can turn any directory in your system into your web server directory. The only thing you need to have installed is Python. <a href="http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python">Read the full guide here</a>.
<h3>Method 4: http-server for node.js</h3>
<p>http-server is a simple, zero-configuration command-line http server for <a href="http://nodejs.org/">node.js</a>. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development, and learning. Or as the web site says "Serving up static files like they were turtles strapped to rockets". Get the npm and instructions from the <a href="https://npmjs.org/package/http-server">http-server web site</a>.</p>
<h3>Method 5: php 5 built-in web server</h3>
<p>As of PHP 5.4.0, the CLI SAPI provides a built-in web server. It's only really suitable for development purposes and servers all files sequentially, but it's easily powerful enough for testing HTML5 games. It's invoked from a single command-line call and you can find details on how to do this in <a href="http://php.net/manual/en/features.commandline.webserver.php">the PHP Manual</a></p>.
<h3>Method 2: Run in the cloud</h3>
</body>
</html>