objectSelection.ed.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // Common code for object selection dialogs.
  23. //=============================================================================================
  24. // Initialization.
  25. //=============================================================================================
  26. //---------------------------------------------------------------------------------------------
  27. function EObjectSelection::init( %this )
  28. {
  29. // Initialize the class list.
  30. %classList = %this-->classList;
  31. if( isObject( %classList ) )
  32. %this.initClassList();
  33. // Initialize the filter list.
  34. %filterList = %this-->filterList;
  35. if( isObject( %filterList ) )
  36. %this.initFilterList();
  37. // Initialize the group list.
  38. %groupList = %this-->groupList;
  39. if( isObject( %groupList ) )
  40. %this.initGroupList();
  41. }
  42. //---------------------------------------------------------------------------------------------
  43. function EObjectSelection::cleanup( %this )
  44. {
  45. // Clear the class list.
  46. %classList = %this-->classList;
  47. if( isObject( %classList ) )
  48. %classList.clear();
  49. // Clear the filter list.
  50. %filterList = %this-->filterList;
  51. if( isObject( %filterList ) )
  52. %filterList.clear();
  53. // Clear the group list.
  54. %groupList = %this-->groupList;
  55. if( isObject( %groupList ) )
  56. %groupList.clear();
  57. // Delete the class array.
  58. if( isObject( %this.classArray ) )
  59. %this.classArray.delete();
  60. }
  61. //=============================================================================================
  62. // Methods to override in a subclass.
  63. //=============================================================================================
  64. //---------------------------------------------------------------------------------------------
  65. /// Return the group object where onSelectObjects should begin searching for objects.
  66. function EObjectSelection::getRootGroup( %this )
  67. {
  68. return RootGroup;
  69. }
  70. //---------------------------------------------------------------------------------------------
  71. /// Return a set that contains all filter objects to include in the filter list.
  72. /// Returning 0 will leave the filter list empty.
  73. function EObjectSelection::getFilterSet( %this )
  74. {
  75. return 0;
  76. }
  77. //---------------------------------------------------------------------------------------------
  78. /// Return true if the given class should be included in the class list.
  79. function EObjectSelection::includeClass( %this, %className )
  80. {
  81. return true;
  82. }
  83. //---------------------------------------------------------------------------------------------
  84. /// The object has met the given criteria. Select or deselect it depending on %val.
  85. function EObjectSelection::selectObject( %this, %object, %val )
  86. {
  87. }
  88. //=============================================================================================
  89. // Events.
  90. //=============================================================================================
  91. //---------------------------------------------------------------------------------------------
  92. function EObjectSelection::onSelectObjects( %this, %val )
  93. {
  94. // Get the root group to search in.
  95. %groupList = %this-->groupList;
  96. if( !isObject( %groupList ) )
  97. %root = %this.getRootGroup();
  98. else
  99. %root = %groupList.getSelected();
  100. if( !isObject( %root ) )
  101. return;
  102. // Fetch the object name pattern.
  103. %namePatternField = %this-->namePattern;
  104. if( isObject( %namePatternField ) )
  105. %this.namePattern = %namePatternField.getText();
  106. else
  107. %this.namePattern = "";
  108. // Clear current selection first, if need be.
  109. if( %val )
  110. {
  111. %retainSelectionBox = %this-->retainSelection;
  112. if( isObject( %retainSelectionBox ) && !%retainSelectionBox.isStateOn() )
  113. %this.clearSelection();
  114. }
  115. // (De)Select all matching objects in it.
  116. %this.selectObjectsIn( %root, %val, true );
  117. }
  118. //=============================================================================================
  119. // Selection.
  120. //=============================================================================================
  121. //---------------------------------------------------------------------------------------------
  122. function EObjectSelection::selectObjectsIn( %this, %group, %val, %excludeGroup )
  123. {
  124. // Match to the group itself.
  125. if( !%excludeGroup && %this.objectMatchesCriteria( %group ) )
  126. %this.selectObject( %group, %val );
  127. // Recursively match all children.
  128. foreach( %obj in %group )
  129. {
  130. if( %obj.isMemberOfClass( "SimSet" ) )
  131. %this.selectObjectsIn( %obj, %val );
  132. else if( %this.objectMatchesCriteria( %obj ) )
  133. %this.selectObject( %obj, %val );
  134. }
  135. }
  136. //---------------------------------------------------------------------------------------------
  137. function EObjectSelection::objectMatchesCriteria( %this, %object )
  138. {
  139. // Check name.
  140. if( %this.namePattern !$= "" && !strIsMatchExpr( %this.namePattern, %object.getName() ) )
  141. return false;
  142. // Check class.
  143. if( !%this.isClassEnabled( %object.getClassName() ) )
  144. return false;
  145. return true;
  146. }
  147. //=============================================================================================
  148. // Groups.
  149. //=============================================================================================
  150. //---------------------------------------------------------------------------------------------
  151. function EObjectSelection::initGroupList( %this )
  152. {
  153. %groupList = %this-->groupList;
  154. %selected = 0;
  155. if( %groupList.size() > 0 )
  156. %selected = %groupList.getSelected();
  157. %groupList.clear();
  158. %root = %this.getRootGroup();
  159. if( !isObject( %root ) )
  160. return;
  161. // Add all non-empty groups.
  162. %this.scanGroup( %root, %groupList, 0 );
  163. // Select initial group.
  164. if( %selected != 0 && isObject( %selected ) )
  165. %groupList.setSelected( %selected );
  166. else
  167. %groupList.setSelected( %root.getId() );
  168. }
  169. //---------------------------------------------------------------------------------------------
  170. function EObjectSelection::scanGroup( %this, %group, %list, %indentLevel )
  171. {
  172. // Create a display name for the group.
  173. %text = %group.getName();
  174. if( %text $= "" )
  175. %text = %group.getClassName();
  176. %internalName = %group.getInternalName();
  177. if( %internalName !$= "" )
  178. %text = %text @ " [" @ %internalName @ "]";
  179. // Indent the name according to the depth in the hierarchy.
  180. if( %indentLevel > 0 )
  181. %text = strrepeat( " ", %indentLevel ) @ %text;
  182. // Add it to the list.
  183. %list.add( %text, %group.getId() );
  184. // Recurse into SimSets with at least one child.
  185. foreach ( %obj in %group )
  186. {
  187. if( !%obj.isMemberOfClass( "SimSet" )
  188. || %obj.getCount() == 0 )
  189. continue;
  190. %this.scanGroup( %obj, %list, %indentLevel + 1 );
  191. }
  192. }
  193. //=============================================================================================
  194. // Filters.
  195. //=============================================================================================
  196. //---------------------------------------------------------------------------------------------
  197. function EObjectSelection::initFilterList( %this )
  198. {
  199. %filterList = %this-->filterList;
  200. }
  201. //=============================================================================================
  202. // Classes.
  203. //=============================================================================================
  204. //---------------------------------------------------------------------------------------------
  205. /// Initialize the list of class toggles.
  206. function EObjectSelection::initClassList( %this )
  207. {
  208. %classArray = new ArrayObject();
  209. %this.classArray = %classArray;
  210. // Add all classes to the array.
  211. %classes = enumerateConsoleClasses();
  212. foreach$( %className in %classes )
  213. {
  214. if( !%this.includeClass( %className ) )
  215. continue;
  216. %classArray.push_back( %className, true );
  217. }
  218. // Sort the class list.
  219. %classArray.sortk( true );
  220. // Add checkboxes for all classes to the list.
  221. %classList = %this-->classList;
  222. %count = %classArray.count();
  223. for( %i = 0; %i < %count; %i ++ )
  224. {
  225. %className = %classArray.getKey( %i );
  226. %textLength = strlen( %className );
  227. %text = " " @ %className;
  228. %checkBox = new GuiCheckBoxCtrl()
  229. {
  230. canSaveDynamicFields = "0";
  231. isContainer = "0";
  232. Profile = "ToolsGuiCheckBoxListFlipedProfile";
  233. HorizSizing = "right";
  234. VertSizing = "bottom";
  235. Position = "0 0";
  236. Extent = ( %textLength * 4 ) @ " 18";
  237. MinExtent = "8 2";
  238. canSave = "0";
  239. Visible = "1";
  240. tooltipprofile = "ToolsGuiToolTipProfile";
  241. hovertime = "1000";
  242. tooltip = "Include/exclude all " @ %className @ " objects.";
  243. text = %text;
  244. groupNum = "-1";
  245. buttonType = "ToggleButton";
  246. useMouseEvents = "0";
  247. useInactiveState = "0";
  248. command = %classArray @ ".setValue( $ThisControl.getValue(), " @ %i @ " );";
  249. };
  250. %checkBox.setStateOn( true );
  251. %classList.addGuiControl( %checkBox );
  252. }
  253. }
  254. //---------------------------------------------------------------------------------------------
  255. function EObjectSelection::selectAllInClassList( %this, %state )
  256. {
  257. %classList = %this-->classList;
  258. foreach( %ctrl in %classList )
  259. {
  260. if( %ctrl.getValue() == %state )
  261. %ctrl.performClick();
  262. }
  263. }
  264. //---------------------------------------------------------------------------------------------
  265. function EObjectSelection::isClassEnabled( %this, %className )
  266. {
  267. // Look up the class entry in the array.
  268. %index = %this.classArray.getIndexFromKey( %className );
  269. if( %index == -1 )
  270. return false;
  271. // Return the flag.
  272. return %this.classArray.getValue( %index );
  273. }