PageRenderTime 449ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/editor/ui/Window.as

http://github.com/MattThorson/Ogmo-Editor
ActionScript | 305 lines | 242 code | 55 blank | 8 comment | 8 complexity | 360d2de43b37ac5b86704618f81a36b2 MD5 | raw file
  1. package editor.ui
  2. {
  3. import editor.*;
  4. import flash.text.TextField;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.MouseEvent;
  8. public class Window extends Sprite
  9. {
  10. private var _bodyWidth:int;
  11. private var _bodyHeight:int;
  12. private var dragging:Boolean;
  13. private var moveX:Number;
  14. private var moveY:Number;
  15. private var _active:Boolean;
  16. private var minimized:Boolean;
  17. protected var oldWidth:uint;
  18. protected var oldHeight:uint;
  19. //Contained stuff
  20. private var bg:Sprite;
  21. private var bar:Sprite;
  22. private var border:Sprite;
  23. private var _title:TextField;
  24. public var ui:Sprite;
  25. //Constants
  26. static public const BAR_HEIGHT:int = 24;
  27. static private const BORDER:uint = 2;
  28. static private const C_BG:uint = 0x666666;
  29. static private const C_BAR:uint = 0xBB8888;
  30. static private const C_BARHOLD:uint = 0xFF6666;
  31. static private const C_BORDER:uint = 0x000000;
  32. static private const C_BARM:uint = 0x774444;
  33. static private const C_BARMHOLD:uint = 0xBB2222;
  34. static private const C_TITLE:uint = 0xFFFFFF;
  35. public function Window( bodyWidth:int, bodyHeight:int, titleText:String )
  36. {
  37. _bodyWidth = bodyWidth;
  38. _bodyHeight = bodyHeight;
  39. addChild( bg = new Sprite );
  40. addChild( bar = new Sprite );
  41. addChild( border = new Sprite );
  42. bar.doubleClickEnabled = true;
  43. hitArea = new Sprite;
  44. hitArea.width = _bodyWidth;
  45. hitArea.height = _bodyHeight;
  46. drawBG();
  47. drawBar();
  48. drawBorder();
  49. _title = new TextField();
  50. _title.mouseEnabled = false;
  51. _title.selectable = false;
  52. _title.text = titleText;
  53. _title.y = (-BAR_HEIGHT / 2) - (_title.textHeight / 2) + (Ogmo.mac?2:-2);
  54. _title.textColor = C_TITLE;
  55. _title.width = _bodyWidth;
  56. _title.height = BAR_HEIGHT;
  57. _title.x = 10;
  58. addChild( _title );
  59. addChild( ui = new Sprite );
  60. alpha = 0.8;
  61. dragging = false;
  62. minimized = false;
  63. _active = false;
  64. addEventListener( Event.ADDED_TO_STAGE, init );
  65. }
  66. private function init( e:Event ):void
  67. {
  68. removeEventListener( Event.ADDED_TO_STAGE, init );
  69. addEventListener( Event.REMOVED_FROM_STAGE, destroy );
  70. stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
  71. stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
  72. bar.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
  73. bar.addEventListener( MouseEvent.DOUBLE_CLICK, onDoubleClick );
  74. addEventListener( MouseEvent.MOUSE_DOWN, onClickAnywhere );
  75. oldWidth = stage.stageWidth;
  76. oldHeight = stage.stageHeight;
  77. enforceBounds();
  78. }
  79. private function destroy( e:Event ):void
  80. {
  81. removeEventListener( Event.REMOVED_FROM_STAGE, destroy );
  82. stage.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp );
  83. stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
  84. bar.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
  85. bar.removeEventListener( MouseEvent.DOUBLE_CLICK, onDoubleClick );
  86. removeEventListener( MouseEvent.MOUSE_DOWN, onClickAnywhere );
  87. }
  88. private function drawBG():void
  89. {
  90. bg.graphics.clear();
  91. bg.graphics.beginFill( C_BG );
  92. if (minimized)
  93. bg.graphics.drawRect( 0, -BAR_HEIGHT, _bodyWidth, BAR_HEIGHT );
  94. else
  95. bg.graphics.drawRect( 0, -BAR_HEIGHT, _bodyWidth, _bodyHeight + BAR_HEIGHT );
  96. bg.graphics.endFill();
  97. }
  98. private function drawBar():void
  99. {
  100. bar.graphics.clear();
  101. if (minimized)
  102. bar.graphics.beginFill( C_BARM );
  103. else
  104. bar.graphics.beginFill( C_BAR );
  105. bar.graphics.drawRect( 0, -BAR_HEIGHT, _bodyWidth, BAR_HEIGHT );
  106. bar.graphics.endFill();
  107. }
  108. private function drawBarHold():void
  109. {
  110. bar.graphics.clear();
  111. if (minimized)
  112. bar.graphics.beginFill( C_BARMHOLD );
  113. else
  114. bar.graphics.beginFill( C_BARHOLD );
  115. bar.graphics.drawRect( 0, -BAR_HEIGHT, _bodyWidth, BAR_HEIGHT );
  116. bar.graphics.endFill();
  117. }
  118. private function drawBorder():void
  119. {
  120. border.graphics.clear();
  121. border.graphics.beginFill( C_BORDER );
  122. if (minimized)
  123. {
  124. border.graphics.drawRect( 0, -BAR_HEIGHT + BORDER, BORDER, BAR_HEIGHT - BORDER*2 );
  125. border.graphics.drawRect( _bodyWidth - BORDER, -BAR_HEIGHT + BORDER, BORDER, BAR_HEIGHT - BORDER*2 );
  126. border.graphics.drawRect( 0, -BAR_HEIGHT, _bodyWidth, BORDER );
  127. border.graphics.drawRect( 0, -BORDER, _bodyWidth, BORDER );
  128. }
  129. else
  130. {
  131. border.graphics.drawRect( 0, -BAR_HEIGHT + BORDER, BORDER, _bodyHeight + BAR_HEIGHT - BORDER*2 );
  132. border.graphics.drawRect( _bodyWidth - BORDER, -BAR_HEIGHT + BORDER, BORDER, _bodyHeight + BAR_HEIGHT - BORDER*2 );
  133. border.graphics.drawRect( 0, -BAR_HEIGHT, _bodyWidth, BORDER );
  134. border.graphics.drawRect( 0, _bodyHeight - BORDER, _bodyWidth, BORDER );
  135. }
  136. border.graphics.endFill();
  137. }
  138. public function enforceBounds():void
  139. {
  140. //Calculate the distance to the edge
  141. var edgeX:int = (stage.stageWidth - Ogmo.STAGE_DEFAULT_WIDTH) / 2;
  142. var edgeY:int = (stage.stageHeight - Ogmo.STAGE_DEFAULT_HEIGHT) / 2;
  143. //Stick to edges
  144. stickToEdges( oldWidth, oldHeight );
  145. //Actually enforce the bounds
  146. x = Utils.within( -edgeX, x, Ogmo.STAGE_DEFAULT_WIDTH + edgeX - _bodyWidth );
  147. y = Utils.within( -edgeY + BAR_HEIGHT, y, Ogmo.STAGE_DEFAULT_HEIGHT + edgeY );
  148. }
  149. public function stickToEdges( oldWidth:int, oldHeight:int ):void
  150. {
  151. if (x + (_bodyWidth/2) < Ogmo.STAGE_DEFAULT_WIDTH / 2)
  152. x -= (stage.stageWidth - oldWidth) / 2;
  153. else
  154. x += (stage.stageWidth - oldWidth) / 2;
  155. if (y + (_bodyHeight/2) < Ogmo.STAGE_DEFAULT_HEIGHT / 2)
  156. y -= (stage.stageHeight - oldHeight) / 2;
  157. else
  158. y += (stage.stageHeight - oldHeight) / 2;
  159. //update
  160. this.oldWidth = stage.stageWidth;
  161. this.oldHeight = stage.stageHeight;
  162. }
  163. public function emptyUI():void
  164. {
  165. while (ui.numChildren > 0)
  166. ui.removeChildAt(0);
  167. }
  168. /* ========================== GETS/SETS ========================== */
  169. public function set title( to:String ):void
  170. {
  171. _title.text = to;
  172. _title.y = (-BAR_HEIGHT / 2) - (_title.textHeight / 2) - 2;
  173. }
  174. public function get title():String
  175. {
  176. return _title.text;
  177. }
  178. public function set active( to:Boolean ):void
  179. {
  180. _active = to;
  181. visible = to;
  182. mouseEnabled = to;
  183. }
  184. public function get active():Boolean
  185. {
  186. return _active;
  187. }
  188. public function set bodyWidth( to:int ):void
  189. {
  190. _bodyWidth = to;
  191. hitArea.width = _bodyWidth;
  192. drawBG();
  193. drawBar();
  194. drawBorder();
  195. _title.x = (_bodyWidth / 2) - (_title.textWidth / 2) - 3;
  196. }
  197. public function get bodyWidth():int
  198. {
  199. return _bodyWidth;
  200. }
  201. public function set bodyHeight( to:int ):void
  202. {
  203. _bodyHeight = to;
  204. hitArea.height = _bodyHeight;
  205. drawBG();
  206. drawBar();
  207. drawBorder();
  208. }
  209. public function get bodyHeight():int
  210. {
  211. return _bodyHeight;
  212. }
  213. /* ========================== EVENTS ========================== */
  214. private function onMouseDown( e:MouseEvent ):void
  215. {
  216. dragging = true;
  217. moveX = e.localX;
  218. moveY = e.localY;
  219. drawBarHold();
  220. }
  221. private function onMouseMove( e:MouseEvent ):void
  222. {
  223. if (dragging)
  224. {
  225. x = e.stageX - moveX;
  226. y = e.stageY - moveY;
  227. enforceBounds();
  228. }
  229. }
  230. private function onMouseUp( e:MouseEvent ):void
  231. {
  232. dragging = false;
  233. drawBar();
  234. }
  235. private function onDoubleClick( e:MouseEvent ):void
  236. {
  237. minimized = !minimized;
  238. ui.visible = !minimized;
  239. ui.mouseChildren = !minimized;
  240. ui.mouseEnabled = !minimized;
  241. drawBG();
  242. drawBar();
  243. drawBorder();
  244. }
  245. private function onClickAnywhere( e:MouseEvent ):void
  246. {
  247. parent.setChildIndex( this, parent.numChildren - 1 );
  248. }
  249. }
  250. }