main.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function GuiEditorToy::create( %this )
  23. {
  24. exec("./assets/gui/guiEditorCtrl.gui");
  25. exec("./assets/gui/guiToolbox.gui");
  26. exec("./assets/gui/ColorSelectorTool.gui");
  27. exec("./assets/scripts/GuiEditorCtrlProperties.cs");
  28. // Set the sandbox drag mode availability.
  29. Sandbox.allowManipulation( pan );
  30. Sandbox.allowManipulation( pull );
  31. // Set the manipulation mode.
  32. Sandbox.useManipulation( pull );
  33. SandboxScene.clear();
  34. //SandboxWindow.delete();
  35. GuiEditor.blankGui = new GuiControl();
  36. // Reset the toy initially.
  37. GuiEditorToy.reset();
  38. }
  39. //-----------------------------------------------------------------------------
  40. function GuiEditorToy::destroy( %this )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. function GuiEditorToy::reset(%this)
  45. {
  46. // Clear the scene.
  47. Canvas.setContent(GuiEditorCtrl);
  48. GuiEditorContent.add(GuiEditor.blankGui);
  49. GuiEditorCtrl.add(GuiTreeViewWindow);
  50. $GuiRootElement = GuiBlank;
  51. %rootSize = $GuiRootElement.extent;
  52. GuiEditor.setRoot(GuiBlank);
  53. GuiEditorTreeView.open(GuiBlank);
  54. GuiEditor.setFirstResponder();
  55. GuiEditor.setSnapToGrid("8");
  56. GuiEditorCtrl.add(GuiToolbox);
  57. GuiEditorCtrl.add(GuiInspectorWindow);
  58. //GuiEditorCtrl.add(CBBWindow);
  59. }
  60. function GuiEditorToy::save(%this)
  61. {
  62. %guiObj = GuiEditorContent.getObject(0);
  63. if(%guiObj == -1)
  64. {
  65. return;
  66. }
  67. if(%guiObj.getName() !$= "")
  68. {
  69. %name = %guiObj.getName() @ ".gui";
  70. }
  71. else
  72. {
  73. %name = "untitled.gui";
  74. }
  75. %fo = new FileObject();
  76. %fo.openForWrite(%name);
  77. %fo.writeLine("//--- Created With GUIEDITORTOY ---//");
  78. %fo.writeObject(%guiObj, "%guiContent = ");
  79. %fo.writeLine("//--- GUIEDITORTOY END ---//");
  80. %fo.close();
  81. %fo.delete();
  82. }
  83. //-----------------------------------------------------------------------------
  84. function GuiToolbox::onWake(%this)
  85. {
  86. %controls = enumerateConsoleClasses("GuiControl");
  87. %this-->toolboxList.clearItems();
  88. for(%i = 0; %i < getFieldCount(%controls); %i++)
  89. {
  90. %field = getField(%controls, %i);
  91. %this-->toolboxList.addItem(%field);
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. function GuiEditorToolboxDrag::onTouchDragged(%this, %index, %text)
  96. {
  97. %position = %this.getGlobalPosition();
  98. %cursorpos = Canvas.getCursorPos();
  99. %class = %this.getItemText(%this.getSelectedItem());
  100. %payload = eval("return new " @ %class @ "();");
  101. if(!isObject(%payload))
  102. return;
  103. %xOffset = getWord(%payload.extent, 0) / 2;
  104. %yOffset = getWord(%payload.extent, 1) / 2;
  105. // position where the drag will start, to prevent visible jumping.
  106. %xPos = getWord(%cursorpos, 0) - %xOffset;
  107. %yPos = getWord(%cursorpos, 1) - %yOffset;
  108. %dragCtrl = new GuiDragAndDropCtrl() {
  109. canSaveDynamicFields = "0";
  110. Profile = "GuiDragAndDropProfile";
  111. HorizSizing = "right";
  112. VertSizing = "bottom";
  113. Position = %xPos SPC %yPos;
  114. extent = %payload.extent;
  115. MinExtent = "32 32";
  116. canSave = "1";
  117. Visible = "1";
  118. hovertime = "1000";
  119. Text = %text;
  120. deleteOnMouseUp = true;
  121. };
  122. %dragCtrl.add(%payload);
  123. Canvas.getContent().add(%dragCtrl);
  124. %dragCtrl.startDragging(%xOffset, %yOffset);
  125. }
  126. //-----------------------------------------------------------------------------
  127. function GuiEditor::onControlDragged(%this, %payload, %position)
  128. {
  129. %pos = VectorSub(%position, GuiEditorContent.getGlobalPosition());
  130. %x = getWord(%pos, 0);
  131. %y = getWord(%pos, 1);
  132. %target = GuiEditorContent.findHitControl(%x, %y);
  133. while(! %target.isContainer )
  134. {
  135. %target = %target.getParent();
  136. }
  137. if(%target != %this.getCurrentAddset())
  138. {
  139. %this.setCurrentAddSet(%target);
  140. }
  141. GuiEditorCtrlProperties.update(%ctrl);
  142. }
  143. //-----------------------------------------------------------------------------
  144. function GuiEditor::onControlDropped(%this, %payload, %position)
  145. {
  146. %pos = %payload.getGlobalPosition();
  147. %x = getWord(%pos, 0);
  148. %y = getWord(%pos, 1);
  149. if(%x > $GuiRootElement.extent.x || %y > $GuiRootElement.extent.y)
  150. {
  151. messageBox("Error", "Cannot add a control outside the root gui element!");
  152. return;
  153. }
  154. %this.addNewCtrl(%payload);
  155. %payload.setPositionGlobal(%x, %y);
  156. %this.setFirstResponder();
  157. }
  158. //-----------------------------------------------------------------------------
  159. function GuiEditor::onSelect(%this, %ctrl)
  160. {
  161. GuiEditorCtrlProperties.update(%ctrl);
  162. GuiEditor.clearSelection();
  163. GuiEditor.select(%ctrl);
  164. GuiEditorTreeView.addSelection(%ctrl);
  165. }
  166. function GuiEditor::onSelectionMoved(%this, %ctrl)
  167. {
  168. GuiEditorCtrlProperties.update(%ctrl);
  169. }
  170. function GuiEditor::onClearSelected(%this)
  171. {
  172. GuiEditorTreeView.clearSelection();
  173. }
  174. function GuiEditor::onSelectionParentChange(%this)
  175. {
  176. GuiEditorTreeView.update();
  177. }
  178. function GuiEditor::onDelete(%this)
  179. {
  180. GuiEditorTreeView.update();
  181. GuiEditorCtrlProperties.update(0);
  182. }
  183. function GuiEditor::onAddSelected(%this,%ctrl)
  184. {
  185. GuiEditorTreeView.addSelection(%ctrl);
  186. GuiEditorTreeView.scrollVisibleByObjectId(%ctrl);
  187. }
  188. function GuiEditor::onRemoveSelected(%this,%ctrl)
  189. {
  190. GuiEditorTreeView.removeSelection(%ctrl);
  191. }
  192. //-----------------------------------------------------------------------------
  193. function GuiEditorTreeView::update(%this)
  194. {
  195. %obj = GuiEditorContent.getObject(0);
  196. if(!isObject(%obj))
  197. {
  198. GuiEditorTreeView.clear();
  199. }
  200. else
  201. {
  202. GuiEditorTreeView.open(GuiEditorContent.getObject(0));
  203. }
  204. }
  205. function GuiEditorTreeView::onRightMouseDown(%this, %item, %pts, %obj)
  206. {
  207. if(%obj)
  208. {
  209. GuiEditor.setCurrentAddSet(%obj);
  210. }
  211. }
  212. function GuiEditorTreeView::onAddSelection(%this, %ctrl)
  213. {
  214. GuiEditor.setFirstResponder();
  215. }
  216. function GuiEditorTreeView::onRemoveSelection(%this, %ctrl)
  217. {
  218. GuiEditor.removeSelection(%ctrl);
  219. }
  220. function GuiEditorTreeView::onDeleteSelection(%this)
  221. {
  222. GuiEditor.clearSelection();
  223. }
  224. function GuiEditorTreeView::onSelect(%this, %obj)
  225. {
  226. if(isObject(%obj))
  227. {
  228. GuiEditorCtrlProperties.update(%obj);
  229. GuiEditor.select(%obj);
  230. }
  231. }