PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cocktail/mouse/as3/MouseCursor.hx

http://github.com/silexlabs/Cocktail
Haxe | 116 lines | 53 code | 18 blank | 45 comment | 3 complexity | 5ce169aff92bc3b0c487f7e892cf0738 MD5 | raw file
  1/*This file is part of Silex - see http://projects.silexlabs.org/?/silex
  2
  3Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
  4
  5This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 
  6
  7This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8
  9To read the license please visit http://www.gnu.org/copyleft/gpl.html
 10*/
 11package cocktail.mouse.as3;
 12
 13import flash.display.BitmapData;
 14import flash.ui.MouseCursorData;
 15import flash.ui.Mouse;
 16import flash.Vector;
 17import cocktail.domElement.ImageDOMElement;
 18import cocktail.mouse.abstract.AbstractMouseCursor;
 19import cocktail.geom.GeomData;
 20import cocktail.mouse.MouseData;
 21
 22/**
 23 * This is the flash AVM2 implementation of the mouse cursor
 24 * 
 25 * It exposes method to set and get the mouse cursor
 26 * 
 27 * @author Yannick DOMINGUEZ
 28 */
 29class MouseCursor extends AbstractMouseCursor
 30{
 31	/**
 32	 * class constructor
 33	 */
 34	public function new() 
 35	{
 36		super();
 37	}
 38	
 39	
 40	//////////////////////////////////////////////////////////////////////////////////////////
 41	// Overriden private mouse cursor methods
 42	//////////////////////////////////////////////////////////////////////////////////////////
 43	
 44	/**
 45	 * Set a bitmap as mouse cursor using flash mouse API
 46	 */
 47	override private function setBitmapCursor(imageDOMElement:ImageDOMElement, hotSpot:Point):Void
 48	{
 49		//init the hotSpot if null
 50		//to the top left of the cursor
 51		if (hotSpot == null)
 52		{
 53			hotSpot = { x:0.0, y:0.0 };
 54		}
 55		
 56		//draw the image dom element onto a 32x32 transparent bitmap data
 57		var mouseCursorBitmapData:BitmapData = new BitmapData(32, 32, true, 0x00FFFFFF);
 58		mouseCursorBitmapData.draw(imageDOMElement.nativeElement);
 59		
 60		//set the flash mouse cursor data with the drawn bitmap data
 61		//and the cursor hot spot
 62		var mouseCursorData:MouseCursorData = new MouseCursorData();
 63		mouseCursorData.data = new Vector<BitmapData>(1, true);
 64		mouseCursorData.data[0] = mouseCursorBitmapData;
 65		mouseCursorData.hotSpot = new flash.geom.Point(hotSpot.x, hotSpot.y);
 66		
 67		//generate a random ID for the new cursor
 68		var randomID:String = Std.string(Math.round(Math.random() * 1000));
 69		
 70		
 71		
 72		//register the cursor and set it
 73		Mouse.registerCursor(randomID, mouseCursorData);
 74		Mouse.cursor = randomID;
 75		
 76		//show the cursor if it was previously hidden
 77		Mouse.show();
 78	}
 79	
 80	/**
 81	 * Hides the mouse cursor using flash mouse API
 82	 */
 83	override private function hideCursor():Void
 84	{
 85		Mouse.hide();
 86	}
 87	
 88	/**
 89	 * Set the default cursor using flash mouse API
 90	 */
 91	override private function setDefaultCursor():Void
 92	{
 93		//show the cursor if it was previously hidden
 94		Mouse.show();
 95		Mouse.cursor = flash.ui.MouseCursor.AUTO;
 96	}
 97	
 98	/**
 99	 * Set an OS native cursor using flash mouse API
100	 */ 
101	override private function setNativeOSCursor(value:NativeOSMouseCursorValue):Void 
102	{
103		//show the cursor if it was previously hidden
104		Mouse.show();
105		
106		switch value
107		{
108			case pointer:
109				Mouse.cursor = flash.ui.MouseCursor.BUTTON;
110				
111			case text:
112				Mouse.cursor = flash.ui.MouseCursor.IBEAM;
113		}
114	}
115	
116}