/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. Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
  3. This 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.
  4. This 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.
  5. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  6. */
  7. package cocktail.mouse.as3;
  8. import flash.display.BitmapData;
  9. import flash.ui.MouseCursorData;
  10. import flash.ui.Mouse;
  11. import flash.Vector;
  12. import cocktail.domElement.ImageDOMElement;
  13. import cocktail.mouse.abstract.AbstractMouseCursor;
  14. import cocktail.geom.GeomData;
  15. import cocktail.mouse.MouseData;
  16. /**
  17. * This is the flash AVM2 implementation of the mouse cursor
  18. *
  19. * It exposes method to set and get the mouse cursor
  20. *
  21. * @author Yannick DOMINGUEZ
  22. */
  23. class MouseCursor extends AbstractMouseCursor
  24. {
  25. /**
  26. * class constructor
  27. */
  28. public function new()
  29. {
  30. super();
  31. }
  32. //////////////////////////////////////////////////////////////////////////////////////////
  33. // Overriden private mouse cursor methods
  34. //////////////////////////////////////////////////////////////////////////////////////////
  35. /**
  36. * Set a bitmap as mouse cursor using flash mouse API
  37. */
  38. override private function setBitmapCursor(imageDOMElement:ImageDOMElement, hotSpot:Point):Void
  39. {
  40. //init the hotSpot if null
  41. //to the top left of the cursor
  42. if (hotSpot == null)
  43. {
  44. hotSpot = { x:0.0, y:0.0 };
  45. }
  46. //draw the image dom element onto a 32x32 transparent bitmap data
  47. var mouseCursorBitmapData:BitmapData = new BitmapData(32, 32, true, 0x00FFFFFF);
  48. mouseCursorBitmapData.draw(imageDOMElement.nativeElement);
  49. //set the flash mouse cursor data with the drawn bitmap data
  50. //and the cursor hot spot
  51. var mouseCursorData:MouseCursorData = new MouseCursorData();
  52. mouseCursorData.data = new Vector<BitmapData>(1, true);
  53. mouseCursorData.data[0] = mouseCursorBitmapData;
  54. mouseCursorData.hotSpot = new flash.geom.Point(hotSpot.x, hotSpot.y);
  55. //generate a random ID for the new cursor
  56. var randomID:String = Std.string(Math.round(Math.random() * 1000));
  57. //register the cursor and set it
  58. Mouse.registerCursor(randomID, mouseCursorData);
  59. Mouse.cursor = randomID;
  60. //show the cursor if it was previously hidden
  61. Mouse.show();
  62. }
  63. /**
  64. * Hides the mouse cursor using flash mouse API
  65. */
  66. override private function hideCursor():Void
  67. {
  68. Mouse.hide();
  69. }
  70. /**
  71. * Set the default cursor using flash mouse API
  72. */
  73. override private function setDefaultCursor():Void
  74. {
  75. //show the cursor if it was previously hidden
  76. Mouse.show();
  77. Mouse.cursor = flash.ui.MouseCursor.AUTO;
  78. }
  79. /**
  80. * Set an OS native cursor using flash mouse API
  81. */
  82. override private function setNativeOSCursor(value:NativeOSMouseCursorValue):Void
  83. {
  84. //show the cursor if it was previously hidden
  85. Mouse.show();
  86. switch value
  87. {
  88. case pointer:
  89. Mouse.cursor = flash.ui.MouseCursor.BUTTON;
  90. case text:
  91. Mouse.cursor = flash.ui.MouseCursor.IBEAM;
  92. }
  93. }
  94. }