PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/DreamYourLevel/src/view/PropertyPanel.hx

https://code.google.com/p/dreamyour2dgame/
Haxe | 451 lines | 384 code | 57 blank | 10 comment | 27 complexity | 264a7abe0694306f6cdc0e259f80676e MD5 | raw file
  1. package view;
  2. import events.ResizeEvent;
  3. import flash.display.DisplayObject;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import flash.events.FocusEvent;
  7. import flash.events.KeyboardEvent;
  8. import flash.events.MouseEvent;
  9. import flash.Lib;
  10. import flash.text.TextField;
  11. import flash.text.TextFieldType;
  12. import graphic.RenderObject;
  13. import gui.buttons.CheckBox;
  14. import gui.buttons.SimpleButton;
  15. import view.events.LevelEditorViewOtherEvent;
  16. enum E_PropertyType
  17. {
  18. INSTANCE_NAME;
  19. X_POSITION;
  20. Y_POSITION;
  21. X_SCALE;
  22. Y_SCALE;
  23. ROTATION;
  24. STATIC;
  25. }
  26. /**
  27. * @author Damien Mabin
  28. */
  29. class PropertyPanel extends Sprite
  30. {
  31. private var m_Style : { backgroundColor : UInt, separatorColor : UInt, buttonBackgroundColor : UInt, buttonForegroundColor : UInt };
  32. private var m_ButtonStyle : { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int, foregroundColor : UInt };
  33. private var m_CheckBoxStyle : { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int };
  34. private var m_AllInfos : Array<{ label : TextField, value : DisplayObject }>;
  35. private var m_DisplayedInfos : Array<{ label : TextField, value : DisplayObject }>;
  36. private var m_Title : TextField;
  37. // All the property available, maybe some can't be shown, depend of the context/mode of edition
  38. private var m_EltType : { label : TextField, value : DisplayObject };
  39. private var m_EltModelName : { label : TextField, value : DisplayObject };
  40. private var m_EltInstanceName : { label : TextField, value : DisplayObject };
  41. private var m_PosX : { label : TextField, value : DisplayObject };
  42. private var m_PosY : { label : TextField, value : DisplayObject };
  43. private var m_ScaleX : { label : TextField, value : DisplayObject };
  44. private var m_ScaleY : { label : TextField, value : DisplayObject };
  45. private var m_Rotation : { label : TextField, value : DisplayObject };
  46. private var m_PhysicalShape : { label : TextField, value : DisplayObject };
  47. private var m_Static : { label : TextField, value : DisplayObject };
  48. // The element for wich we are displaying informations
  49. private var m_CurrentElement : Dynamic;
  50. public function new(?_Style:{ backgroundColor : UInt, separatorColor : UInt, buttonBackgroundColor : UInt, buttonForegroundColor : UInt })
  51. {
  52. super();
  53. if (_Style != null)
  54. {
  55. m_Style = _Style;
  56. }
  57. else
  58. {
  59. m_Style = { backgroundColor : 0x000000, separatorColor : 0xFF0000, buttonBackgroundColor : 0xFFFF00, buttonForegroundColor : 0x00FFFF };
  60. }
  61. m_ButtonStyle = { backgroundColor : m_Style.buttonBackgroundColor, backgroundAlpha : 1.0, borderColor : 0x000000, borderAlpha : 1.0, borderSize : 0, foregroundColor : m_Style.buttonForegroundColor };
  62. m_CheckBoxStyle = { backgroundColor : m_Style.buttonBackgroundColor, backgroundAlpha : 1.0, borderColor : 0x000000, borderAlpha : 1.0, borderSize : 0 };
  63. m_AllInfos = new Array<{ label : TextField, value : DisplayObject }>();
  64. m_DisplayedInfos = new Array<{ label : TextField, value : DisplayObject }>();
  65. this.focusRect = false;
  66. // ==========================================================================================================
  67. // Title :
  68. m_Title = new TextField();
  69. m_Title.text = "Properties :";
  70. m_Title.mouseEnabled = false;
  71. m_Title.selectable = false;
  72. m_Title.height = 20;
  73. // ==========================================================================================================
  74. // Infos :
  75. m_EltType = { label : new TextField(), value : cast new TextField() };
  76. cast(m_EltType.value, TextField).mouseEnabled = false;
  77. m_AllInfos.push(m_EltType);
  78. m_EltInstanceName = { label : new TextField(), value : cast new TextField() };
  79. cast(m_EltInstanceName.value, TextField).mouseEnabled = false;
  80. cast(m_EltInstanceName.value, TextField).addEventListener(FocusEvent.FOCUS_OUT, callback(OnLooseFocusEvent, E_PropertyType.INSTANCE_NAME));
  81. cast(m_EltInstanceName.value, TextField).addEventListener(KeyboardEvent.KEY_DOWN, callback(OnKeyboardTextInputEvent, E_PropertyType.INSTANCE_NAME));
  82. m_AllInfos.push(m_EltInstanceName);
  83. m_EltModelName = { label : new TextField(), value : cast new TextField() };
  84. cast(m_EltModelName.value, TextField).mouseEnabled = false;
  85. m_AllInfos.push(m_EltModelName);
  86. m_PosX = { label : new TextField(), value : cast new TextField() };
  87. cast(m_PosX.value, TextField).mouseEnabled = false;
  88. cast(m_PosX.value, TextField).addEventListener(FocusEvent.FOCUS_OUT, callback(OnLooseFocusEvent, E_PropertyType.X_POSITION));
  89. cast(m_PosX.value, TextField).addEventListener(KeyboardEvent.KEY_DOWN, callback(OnKeyboardTextInputEvent, E_PropertyType.X_POSITION));
  90. m_AllInfos.push(m_PosX);
  91. m_PosY = { label : new TextField(), value : cast new TextField() };
  92. cast(m_PosY.value, TextField).mouseEnabled = false;
  93. cast(m_PosY.value, TextField).addEventListener(FocusEvent.FOCUS_OUT, callback(OnLooseFocusEvent, E_PropertyType.Y_POSITION));
  94. cast(m_PosY.value, TextField).addEventListener(KeyboardEvent.KEY_DOWN, callback(OnKeyboardTextInputEvent, E_PropertyType.Y_POSITION));
  95. m_AllInfos.push(m_PosY);
  96. m_ScaleX = { label : new TextField(), value : cast new TextField() };
  97. cast(m_ScaleX.value, TextField).mouseEnabled = false;
  98. cast(m_ScaleX.value, TextField).addEventListener(FocusEvent.FOCUS_OUT, callback(OnLooseFocusEvent, E_PropertyType.X_SCALE));
  99. cast(m_ScaleX.value, TextField).addEventListener(KeyboardEvent.KEY_DOWN, callback(OnKeyboardTextInputEvent, E_PropertyType.X_SCALE));
  100. m_AllInfos.push(m_ScaleX);
  101. m_ScaleY = { label : new TextField(), value : cast new TextField() };
  102. cast(m_ScaleY.value, TextField).mouseEnabled = false;
  103. cast(m_ScaleY.value, TextField).addEventListener(FocusEvent.FOCUS_OUT, callback(OnLooseFocusEvent, E_PropertyType.Y_SCALE));
  104. cast(m_ScaleY.value, TextField).addEventListener(KeyboardEvent.KEY_DOWN, callback(OnKeyboardTextInputEvent, E_PropertyType.Y_SCALE));
  105. m_AllInfos.push(m_ScaleY);
  106. m_Rotation = { label : new TextField(), value : cast new TextField() };
  107. cast(m_Rotation.value, TextField).mouseEnabled = false;
  108. cast(m_Rotation.value, TextField).addEventListener(FocusEvent.FOCUS_OUT, callback(OnLooseFocusEvent, E_PropertyType.ROTATION));
  109. cast(m_Rotation.value, TextField).addEventListener(KeyboardEvent.KEY_DOWN, callback(OnKeyboardTextInputEvent, E_PropertyType.ROTATION));
  110. m_AllInfos.push(m_Rotation);
  111. m_PhysicalShape = { label : new TextField(), value : cast new SimpleButton("Edit", null, m_ButtonStyle) };
  112. m_PhysicalShape.value.dispatchEvent(new ResizeEvent(35, 18));
  113. m_PhysicalShape.value.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseEvent);
  114. m_PhysicalShape.value.visible = false;
  115. m_AllInfos.push(m_PhysicalShape);
  116. m_Static = { label : new TextField(), value : cast new CheckBox(m_CheckBoxStyle) };
  117. m_Static.value.dispatchEvent(new ResizeEvent(16, 16));
  118. m_Static.value.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseEvent);
  119. m_Static.value.visible = false;
  120. m_AllInfos.push(m_Static);
  121. // ==========================================================================================================
  122. for (info in m_AllInfos)
  123. {
  124. this.addChild(info.label);
  125. info.label.mouseEnabled = false;
  126. info.label.selectable = false;
  127. this.addChild(info.value);
  128. }
  129. this.addChild(m_Title);
  130. this.addEventListener(ResizeEvent.EVENT, OnResizeEvent);
  131. }
  132. private function OnKeyboardTextInputEvent(_Type:E_PropertyType, _Evt:KeyboardEvent):Void
  133. {
  134. switch(_Type)
  135. {
  136. case INSTANCE_NAME :
  137. {
  138. if (_Evt.keyCode == 13 /*Enter*/)
  139. {
  140. Lib.current.stage.focus = this;
  141. }
  142. }
  143. case X_POSITION :
  144. {
  145. if (_Evt.keyCode == 13 /*Enter*/)
  146. {
  147. Lib.current.stage.focus = this;
  148. }
  149. }
  150. case Y_POSITION :
  151. {
  152. if (_Evt.keyCode == 13 /*Enter*/)
  153. {
  154. Lib.current.stage.focus = this;
  155. }
  156. }
  157. case X_SCALE :
  158. {
  159. if (_Evt.keyCode == 13 /*Enter*/)
  160. {
  161. Lib.current.stage.focus = this;
  162. }
  163. }
  164. case Y_SCALE :
  165. {
  166. if (_Evt.keyCode == 13 /*Enter*/)
  167. {
  168. Lib.current.stage.focus = this;
  169. }
  170. }
  171. case ROTATION :
  172. {
  173. if (_Evt.keyCode == 13 /*Enter*/)
  174. {
  175. Lib.current.stage.focus = this;
  176. }
  177. }
  178. case STATIC :
  179. {
  180. }
  181. }
  182. }
  183. private function OnMouseEvent(_Evt:MouseEvent):Void
  184. {
  185. if (_Evt.currentTarget == m_PhysicalShape.value)
  186. {
  187. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.EDIT_RENDER_OBJECT_PHYSIC_BODY(cast m_CurrentElement)));
  188. }
  189. else if(_Evt.currentTarget == m_Static.value)
  190. {
  191. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, STATIC, ""+!cast(m_Static.value, CheckBox).GetState())));
  192. }
  193. }
  194. private function OnLooseFocusEvent(_Type:E_PropertyType, _Evt:Event):Void
  195. {
  196. switch(_Type)
  197. {
  198. case INSTANCE_NAME :
  199. {
  200. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, INSTANCE_NAME, cast(m_EltInstanceName.value, TextField).text)));
  201. }
  202. case X_POSITION :
  203. {
  204. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, X_POSITION, cast(m_PosX.value, TextField).text)));
  205. }
  206. case Y_POSITION :
  207. {
  208. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, Y_POSITION, cast(m_PosY.value, TextField).text)));
  209. }
  210. case X_SCALE :
  211. {
  212. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, X_SCALE, cast(m_ScaleX.value, TextField).text)));
  213. }
  214. case Y_SCALE :
  215. {
  216. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, Y_SCALE, cast(m_ScaleY.value, TextField).text)));
  217. }
  218. case ROTATION :
  219. {
  220. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, ROTATION, cast(m_Rotation.value, TextField).text)));
  221. }
  222. case STATIC :
  223. {
  224. this.dispatchEvent(new LevelEditorViewOtherEvent(E_OtherInteractionType.CHANGE_RENDER_OBJECT_PROPERTY(cast m_CurrentElement, STATIC, ""+cast(m_Static.value, CheckBox).GetState())));
  225. }
  226. }
  227. }
  228. public function DisplayPropertiesForPhysics<T>(_Class:Class<T>, _Elt:T):Void
  229. {
  230. m_CurrentElement = _Elt;
  231. for (info in m_DisplayedInfos)
  232. {
  233. info.value.visible = false;
  234. info.label.visible = false;
  235. }
  236. m_DisplayedInfos = new Array<{ label : TextField, value : DisplayObject }>();
  237. if (_Elt != null)
  238. {
  239. switch(_Class)
  240. {
  241. case cast RenderObject :
  242. {
  243. var ro : RenderObject = cast _Elt;
  244. m_EltModelName.label.text = "Model name";
  245. cast(m_EltModelName.value, TextField).text = ro.GetModelName();
  246. cast(m_EltModelName.value, TextField).height = 20;
  247. m_DisplayedInfos.push(m_EltModelName);
  248. m_EltInstanceName.label.text = "Instance name";
  249. cast(m_EltInstanceName.value, TextField).text = ro.name;
  250. cast(m_EltInstanceName.value, TextField).mouseEnabled = true;
  251. cast(m_EltInstanceName.value, TextField).selectable = true;
  252. cast(m_EltInstanceName.value, TextField).height = 20;
  253. cast(m_EltInstanceName.value, TextField).type = TextFieldType.INPUT;
  254. m_DisplayedInfos.push(m_EltInstanceName);
  255. m_PosX.label.text = "X";
  256. cast(m_PosX.value, TextField).text = "" + ro.GetPosition().x;
  257. cast(m_PosX.value, TextField).mouseEnabled = true;
  258. cast(m_PosX.value, TextField).selectable = true;
  259. cast(m_PosX.value, TextField).height = 20;
  260. cast(m_PosX.value, TextField).type = TextFieldType.INPUT;
  261. m_DisplayedInfos.push(m_PosX);
  262. m_PosY.label.text = "Y";
  263. cast(m_PosY.value, TextField).text = "" + ro.GetPosition().y;
  264. cast(m_PosY.value, TextField).mouseEnabled = true;
  265. cast(m_PosY.value, TextField).selectable = true;
  266. cast(m_PosY.value, TextField).height = 20;
  267. cast(m_PosY.value, TextField).type = TextFieldType.INPUT;
  268. m_DisplayedInfos.push(m_PosY);
  269. m_Rotation.label.text = "Rotation";
  270. cast(m_Rotation.value, TextField).text = "" + ro.GetPhysicalBody().a*180/Math.PI;
  271. cast(m_Rotation.value, TextField).mouseEnabled = true;
  272. cast(m_Rotation.value, TextField).selectable = true;
  273. cast(m_Rotation.value, TextField).height = 20;
  274. cast(m_Rotation.value, TextField).type = TextFieldType.INPUT;
  275. m_DisplayedInfos.push(m_Rotation);
  276. m_Static.label.text = "Static";
  277. cast(m_Static.value, CheckBox).SetState(ro.GetPhysicalBody().isStatic);
  278. m_DisplayedInfos.push(m_Static);
  279. m_PhysicalShape.label.text = "Physic body";
  280. m_DisplayedInfos.push(m_PhysicalShape);
  281. }
  282. default :
  283. {
  284. }
  285. }
  286. }
  287. OnResizeEvent(new ResizeEvent(this.width, this.height));
  288. }
  289. public function DisplayProperties<T>(_Class:Class<T>, _Elt:T):Void
  290. {
  291. m_CurrentElement = _Elt;
  292. for (info in m_DisplayedInfos)
  293. {
  294. info.value.visible = false;
  295. info.label.visible = false;
  296. }
  297. m_DisplayedInfos = new Array<{ label : TextField, value : DisplayObject }>();
  298. if (_Elt != null)
  299. {
  300. switch(_Class)
  301. {
  302. case cast RenderObject :
  303. {
  304. var ro : RenderObject = cast _Elt;
  305. m_EltType.label.text = "Type";
  306. cast(m_EltType.value, TextField).text = "RenderObject";
  307. cast(m_EltType.value, TextField).height = 20;
  308. m_DisplayedInfos.push(m_EltType);
  309. m_EltModelName.label.text = "Model name";
  310. cast(m_EltModelName.value, TextField).text = ro.GetModelName();
  311. cast(m_EltModelName.value, TextField).height = 20;
  312. m_DisplayedInfos.push(m_EltModelName);
  313. m_EltInstanceName.label.text = "Instance name";
  314. cast(m_EltInstanceName.value, TextField).text = ro.name;
  315. cast(m_EltInstanceName.value, TextField).mouseEnabled = true;
  316. cast(m_EltInstanceName.value, TextField).selectable = true;
  317. cast(m_EltInstanceName.value, TextField).height = 20;
  318. cast(m_EltInstanceName.value, TextField).type = TextFieldType.INPUT;
  319. m_DisplayedInfos.push(m_EltInstanceName);
  320. m_PosX.label.text = "X";
  321. cast(m_PosX.value, TextField).text = "" + (Math.floor(ro.GetPosition().x*100)/100);
  322. cast(m_PosX.value, TextField).mouseEnabled = true;
  323. cast(m_PosX.value, TextField).selectable = true;
  324. cast(m_PosX.value, TextField).height = 20;
  325. cast(m_PosX.value, TextField).type = TextFieldType.INPUT;
  326. m_DisplayedInfos.push(m_PosX);
  327. m_PosY.label.text = "Y";
  328. cast(m_PosY.value, TextField).text = "" + (Math.floor(ro.GetPosition().y*100)/100);
  329. cast(m_PosY.value, TextField).mouseEnabled = true;
  330. cast(m_PosY.value, TextField).selectable = true;
  331. cast(m_PosY.value, TextField).height = 20;
  332. cast(m_PosY.value, TextField).type = TextFieldType.INPUT;
  333. m_DisplayedInfos.push(m_PosY);
  334. m_Rotation.label.text = "Rotation";
  335. cast(m_Rotation.value, TextField).text = "" + ro.rotation;
  336. cast(m_Rotation.value, TextField).mouseEnabled = true;
  337. cast(m_Rotation.value, TextField).selectable = true;
  338. cast(m_Rotation.value, TextField).height = 20;
  339. cast(m_Rotation.value, TextField).type = TextFieldType.INPUT;
  340. m_DisplayedInfos.push(m_Rotation);
  341. m_ScaleX.label.text = "Scale X";
  342. cast(m_ScaleX.value, TextField).text = "" + ro.scaleX;
  343. cast(m_ScaleX.value, TextField).mouseEnabled = true;
  344. cast(m_ScaleX.value, TextField).selectable = true;
  345. cast(m_ScaleX.value, TextField).height = 20;
  346. cast(m_ScaleX.value, TextField).type = TextFieldType.INPUT;
  347. m_DisplayedInfos.push(m_ScaleX);
  348. m_ScaleY.label.text = "Scale Y";
  349. cast(m_ScaleY.value, TextField).text = "" + ro.scaleY;
  350. cast(m_ScaleY.value, TextField).mouseEnabled = true;
  351. cast(m_ScaleY.value, TextField).selectable = true;
  352. cast(m_ScaleY.value, TextField).height = 20;
  353. cast(m_ScaleY.value, TextField).type = TextFieldType.INPUT;
  354. m_DisplayedInfos.push(m_ScaleY);
  355. m_PhysicalShape.label.text = "Physic body";
  356. m_DisplayedInfos.push(m_PhysicalShape);
  357. }
  358. default :
  359. {
  360. }
  361. }
  362. }
  363. OnResizeEvent(new ResizeEvent(this.width, this.height));
  364. }
  365. private function OnResizeEvent(_Evt:ResizeEvent):Void
  366. {
  367. this.graphics.clear();
  368. this.graphics.beginFill(m_Style.backgroundColor);
  369. this.graphics.drawRect(0, 0, _Evt.m_Width, _Evt.m_Height);
  370. this.graphics.endFill();
  371. this.graphics.beginFill(m_Style.separatorColor);
  372. this.graphics.drawRect(_Evt.m_Width * 0.45-3, 24, 1, _Evt.m_Height-26);
  373. this.graphics.endFill();
  374. this.graphics.beginFill(m_Style.separatorColor);
  375. this.graphics.drawRect(5, 20, _Evt.m_Width-10, 2);
  376. this.graphics.endFill();
  377. var verticalOffset : Float = 22;
  378. for (info in m_DisplayedInfos)
  379. {
  380. info.label.visible = true;
  381. info.label.y = verticalOffset;
  382. info.label.x = _Evt.m_Width * 0.15;
  383. info.label.height = 20;
  384. info.value.visible = true;
  385. info.value.y = verticalOffset;
  386. info.value.x = _Evt.m_Width * 0.45;
  387. info.label.height = 20;
  388. verticalOffset += 20;
  389. this.graphics.beginFill(m_Style.separatorColor);
  390. this.graphics.drawRect(5, verticalOffset, _Evt.m_Width-10, 1);
  391. this.graphics.endFill();
  392. }
  393. }
  394. }