menuBuilder.ed.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //-----------------------------------------------------------------------------
  23. // Menu Builder Helper Class
  24. //-----------------------------------------------------------------------------
  25. /// @class MenuBuilder
  26. /// @brief Create Dynamic Context and MenuBar Menus
  27. ///
  28. ///
  29. /// Summary : The MenuBuilder script class exists merely as a helper for creating
  30. /// popup menu's for use in torque editors. It is setup as a single
  31. /// object with dynamic fields starting with item[0]..[n] that describe
  32. /// how to create the menu in question. An example is below.
  33. ///
  34. /// isPopup : isPopup is a persistent field on PopupMenu console class which
  35. /// when specified to true will allow you to perform .showPopup(x,y)
  36. /// commands which allow popupmenu's to be used/reused as menubar menus
  37. /// as well as context menus.
  38. ///
  39. /// barPosition : barPosition indicates which index on the menu bar (0 = leftmost)
  40. /// to place this menu if it is attached. Use the attachToMenuBar() command
  41. /// to attach a menu.
  42. ///
  43. /// barName : barName specifies the visible name of a menu item that is attached
  44. /// to the global menubar.
  45. ///
  46. /// canvas : The GuiCanvas object the menu should be attached to. This defaults to
  47. /// the global Canvas object if unspecified.
  48. ///
  49. /// Remarks : If you wish to use a menu as a context popup menu, isPopup must be
  50. /// specified as true at the creation time of the menu.
  51. ///
  52. ///
  53. /// @li @b item[n] (String) TAB (String) TAB (String) : <c>A Menu Item Definition.</c>
  54. /// @code item[0] = "Open File..." TAB "Ctrl O" TAB "Something::OpenFile"; @endcode
  55. ///
  56. /// @li @b isPopup (bool) : <c>If Specified the menu will be considered a popup menu and should be used via .showPopup()</c>
  57. /// @code isPopup = true; @endcode
  58. ///
  59. ///
  60. /// Example : Creating a @b MenuBar Menu
  61. /// @code
  62. /// %%editMenu = new PopupMenu()
  63. /// {
  64. /// barPosition = 3;
  65. /// barName = "View";
  66. /// superClass = "MenuBuilder";
  67. /// item[0] = "Undo" TAB "Ctrl Z" TAB "levelBuilderUndo(1);";
  68. /// item[1] = "Redo" TAB "Ctrl Y" TAB "levelBuilderRedo(1);";
  69. /// item[2] = "-";
  70. /// };
  71. ///
  72. /// %%editMenu.attachToMenuBar( 1, "Edit" );
  73. ///
  74. /// @endcode
  75. ///
  76. ///
  77. /// Example : Creating a @b Context (Popup) Menu
  78. /// @code
  79. /// %%contextMenu = new PopupMenu()
  80. /// {
  81. /// superClass = MenuBuilder;
  82. /// isPopup = true;
  83. /// item[0] = "My Super Cool Item" TAB "Ctrl 2" TAB "echo(\"Clicked Super Cool Item\");";
  84. /// item[1] = "-";
  85. /// };
  86. ///
  87. /// %%contextMenu.showPopup();
  88. /// @endcode
  89. ///
  90. ///
  91. /// Example : Modifying a Menu
  92. /// @code
  93. /// %%editMenu = new PopupMenu()
  94. /// {
  95. /// item[0] = "Foo" TAB "Ctrl F" TAB "echo(\"clicked Foo\")";
  96. /// item[1] = "-";
  97. /// };
  98. /// %%editMenu.addItem( 2, "Bar" TAB "Ctrl B" TAB "echo(\"clicked Bar\")" );
  99. /// %%editMenu.removeItem( 0 );
  100. /// %%editMenu.addItem( 0, "Modified Foo" TAB "Ctrl F" TAB "echo(\"clicked modified Foo\")" );
  101. /// @endcode
  102. ///
  103. ///
  104. /// @see PopupMenu
  105. ///
  106. //-----------------------------------------------------------------------------
  107. // Adds one item to the menu.
  108. // if %item is skipped or "", we will use %item[#], which was set when the menu was created.
  109. // if %item is provided, then we update %item[#].
  110. function MenuBuilder::addItem(%this, %pos, %item)
  111. {
  112. if(%item $= "")
  113. %item = %this.item[%pos];
  114. if(%item !$= %this.item[%pos])
  115. %this.item[%pos] = %item;
  116. %name = getField(%item, 0);
  117. %accel = getField(%item, 1);
  118. %cmd = getField(%item, 2);
  119. // We replace the [this] token with our object ID
  120. %cmd = strreplace( %cmd, "[this]", %this );
  121. %this.item[%pos] = setField( %item, 2, %cmd );
  122. if(isObject(%accel))
  123. {
  124. // If %accel is an object, we want to add a sub menu
  125. %this.insertSubmenu(%pos, %name, %accel);
  126. }
  127. else
  128. {
  129. %this.insertItem(%pos, %name !$= "-" ? %name : "", %accel, %cmd);
  130. }
  131. }
  132. function MenuBuilder::appendItem(%this, %item)
  133. {
  134. %this.addItem(%this.getItemCount(), %item);
  135. }
  136. function MenuBuilder::onAdd(%this)
  137. {
  138. if(! isObject(%this.canvas))
  139. %this.canvas = Canvas;
  140. for(%i = 0;%this.item[%i] !$= "";%i++)
  141. {
  142. %this.addItem(%i);
  143. }
  144. }
  145. function MenuBuilder::onRemove(%this)
  146. {
  147. %this.removeFromMenuBar();
  148. }
  149. //////////////////////////////////////////////////////////////////////////
  150. function MenuBuilder::onSelectItem(%this, %id, %text)
  151. {
  152. %cmd = getField(%this.item[%id], 2);
  153. if(%cmd !$= "")
  154. {
  155. eval( %cmd );
  156. return true;
  157. }
  158. return false;
  159. }
  160. /// Sets a new name on an existing menu item.
  161. function MenuBuilder::setItemName( %this, %id, %name )
  162. {
  163. %item = %this.item[%id];
  164. %accel = getField(%item, 1);
  165. %this.setItem( %id, %name, %accel );
  166. }
  167. /// Sets a new command on an existing menu item.
  168. function MenuBuilder::setItemCommand( %this, %id, %command )
  169. {
  170. %this.item[%id] = setField( %this.item[%id], 2, %command );
  171. }
  172. /// (SimID this)
  173. /// Wraps the attachToMenuBar call so that it does not require knowledge of
  174. /// barName or barIndex to be removed/attached. This makes the individual
  175. /// MenuBuilder items very easy to add and remove dynamically from a bar.
  176. ///
  177. function MenuBuilder::attachToMenuBar( %this )
  178. {
  179. if( %this.barName $= "" )
  180. {
  181. error("MenuBuilder::attachToMenuBar - Menu property 'barName' not specified.");
  182. return false;
  183. }
  184. if( %this.barPosition < 0 )
  185. {
  186. error("MenuBuilder::attachToMenuBar - Menu " SPC %this.barName SPC "property 'barPosition' is invalid, must be zero or greater.");
  187. return false;
  188. }
  189. Parent::attachToMenuBar( %this, %this.canvas, %this.barPosition, %this.barName );
  190. }
  191. //////////////////////////////////////////////////////////////////////////
  192. // Callbacks from PopupMenu. These callbacks are now passed on to submenus
  193. // in C++, which was previously not the case. Thus, no longer anything to
  194. // do in these. I am keeping the callbacks in case they are needed later.
  195. function MenuBuilder::onAttachToMenuBar(%this, %canvas, %pos, %title)
  196. {
  197. }
  198. function MenuBuilder::onRemoveFromMenuBar(%this, %canvas)
  199. {
  200. }
  201. //////////////////////////////////////////////////////////////////////////
  202. /// Method called to setup default state for the menu. Expected to be overriden
  203. /// on an individual menu basis. See the mission editor for an example.
  204. function MenuBuilder::setupDefaultState(%this)
  205. {
  206. for(%i = 0;%this.item[%i] !$= "";%i++)
  207. {
  208. %name = getField(%this.item[%i], 0);
  209. %accel = getField(%this.item[%i], 1);
  210. %cmd = getField(%this.item[%i], 2);
  211. // Pass on to sub menus
  212. if(isObject(%accel))
  213. %accel.setupDefaultState();
  214. }
  215. }
  216. /// Method called to easily enable or disable all items in a menu.
  217. function MenuBuilder::enableAllItems(%this, %enable)
  218. {
  219. for(%i = 0; %this.item[%i] !$= ""; %i++)
  220. {
  221. %this.enableItem(%i, %enable);
  222. }
  223. }