2
0

guiStackCtrl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #include "console/engineAPI.h"
  23. #include "gui/containers/guiStackCtrl.h"
  24. IMPLEMENT_CONOBJECT(GuiStackControl);
  25. ConsoleDocClass( GuiStackControl,
  26. "@brief A container that stacks its children horizontally or vertically.\n\n"
  27. "This container maintains a horizontal or vertical stack of GUI controls. If "
  28. "one is added, deleted, or resized, then the stack is resized to fit. The "
  29. "order of the stack is determined by the internal order of the children (ie. "
  30. "the order of addition).<br>"
  31. "@tsexample\n"
  32. "new GuiStackControl()\n"
  33. "{\n"
  34. " stackingType = \"Dynamic\";\n"
  35. " horizStacking = \"Left to Right\";\n"
  36. " vertStacking = \"Top to Bottom\";\n"
  37. " padding = \"4\";\n"
  38. " dynamicSize = \"1\";\n"
  39. " dynamicNonStackExtent = \"0\";\n"
  40. " dynamicPos = \"0\";\n"
  41. " changeChildSizeToFit = \"1\";\n"
  42. " changeChildPosition = \"1\";\n"
  43. " //Properties not specific to this control have been omitted from this example.\n"
  44. "};\n"
  45. "@endtsexample\n\n"
  46. "@ingroup GuiContainers"
  47. );
  48. ImplementEnumType( GuiStackingType,
  49. "Stacking method used to position child controls.\n\n"
  50. "@ingroup GuiContainers" )
  51. { GuiStackControl::stackingTypeVert, "Vertical", "Stack children vertically by setting their Y position" },
  52. { GuiStackControl::stackingTypeHoriz,"Horizontal", "Stack children horizontall by setting their X position" },
  53. { GuiStackControl::stackingTypeDyn,"Dynamic", "Automatically switch between "
  54. "Vertical and Horizontal stacking. Vertical stacking is chosen when the "
  55. "stack control is taller than it is wide, horizontal stacking is chosen "
  56. "when the stack control is wider than it is tall." }
  57. EndImplementEnumType;
  58. ImplementEnumType( GuiHorizontalStackingType,
  59. "Determines how child controls are stacked horizontally.\n\n"
  60. "@ingroup GuiContainers" )
  61. { GuiStackControl::horizStackLeft, "Left to Right", "Child controls are positioned in order from left to right (left-most control is first)" },
  62. { GuiStackControl::horizStackRight,"Right to Left", "Child controls are positioned in order from right to left (right-most control is first)" }
  63. EndImplementEnumType;
  64. ImplementEnumType( GuiVerticalStackingType,
  65. "Determines how child controls are stacked vertically.\n\n"
  66. "@ingroup GuiContainers" )
  67. { GuiStackControl::vertStackTop, "Top to Bottom", "Child controls are positioned in order from top to bottom (top-most control is first)" },
  68. { GuiStackControl::vertStackBottom,"Bottom to Top", "Child controls are positioned in order from bottom to top (bottom-most control is first)" }
  69. EndImplementEnumType;
  70. GuiStackControl::GuiStackControl()
  71. {
  72. setMinExtent(Point2I(16,16));
  73. mResizing = false;
  74. mStackingType = stackingTypeVert;
  75. mStackVertSizing = vertStackTop;
  76. mStackHorizSizing = horizStackLeft;
  77. mPadding = 0;
  78. mIsContainer = true;
  79. mDynamicSize = true;
  80. mDynamicNonStackExtent = false;
  81. mDynamicPos = false;
  82. mChangeChildSizeToFit = true;
  83. mChangeChildPosition = true;
  84. }
  85. void GuiStackControl::initPersistFields()
  86. {
  87. addGroup( "Stacking" );
  88. addField( "stackingType", TYPEID< StackingType >(), Offset(mStackingType, GuiStackControl),
  89. "Determines the method used to position the child controls.\n\n" );
  90. addField( "horizStacking", TYPEID< HorizontalType >(), Offset(mStackHorizSizing, GuiStackControl),
  91. "Controls the type of horizontal stacking to use (<i>Left to Right</i> or "
  92. "<i>Right to Left</i>)" );
  93. addField( "vertStacking", TYPEID< VerticalType >(), Offset(mStackVertSizing, GuiStackControl),
  94. "Controls the type of vertical stacking to use (<i>Top to Bottom</i> or "
  95. "<i>Bottom to Top</i>)" );
  96. addField( "padding", TypeS32, Offset(mPadding, GuiStackControl),
  97. "Distance (in pixels) between stacked child controls." );
  98. addField( "dynamicSize", TypeBool, Offset(mDynamicSize, GuiStackControl),
  99. "Determines whether to resize the stack control along the stack axis (change "
  100. "width for horizontal stacking, change height for vertical stacking).\n\n"
  101. "If true, the stack width/height will be resized to the sum of the child control widths/heights. "
  102. "If false, the stack will not be resized." );
  103. addField( "dynamicNonStackExtent", TypeBool, Offset(mDynamicNonStackExtent, GuiStackControl),
  104. "Determines whether to resize the stack control along the non-stack axis (change "
  105. "height for horizontal stacking, change width for vertical stacking). No effect "
  106. "if dynamicSize is false.\n\n"
  107. "If true, the stack will be resized to the maximum of the child control widths/heights. "
  108. "If false, the stack will not be resized." );
  109. addField( "dynamicPos", TypeBool, Offset(mDynamicPos, GuiStackControl),
  110. "Determines whether to reposition the stack along the stack axis when it is "
  111. "auto-resized. No effect if dynamicSize is false.\n\n"
  112. "If true, the stack will grow left for horizontal stacking, and grow up for vertical stacking.\n"
  113. "If false, the stack will grow right for horizontal stacking, and grow down for vertical stacking.\n" );
  114. addField( "changeChildSizeToFit", TypeBool, Offset(mChangeChildSizeToFit, GuiStackControl),
  115. "Determines whether to resize child controls.\n\n"
  116. "If true, horizontally stacked children keep their width, but have their "
  117. "height set to the stack control height. Vertically stacked children keep "
  118. "their height, but have their width set to the stack control width. If "
  119. "false, child controls are not resized." );
  120. addField( "changeChildPosition", TypeBool, Offset(mChangeChildPosition, GuiStackControl),
  121. "Determines whether to reposition child controls.\n\n"
  122. "If true, horizontally stacked children are aligned along the top edge of "
  123. "the stack control. Vertically stacked children are aligned along the left "
  124. "edge of the stack control. If false, horizontally stacked children retain "
  125. "their Y position, and vertically stacked children retain their X position." );
  126. endGroup( "Stacking" );
  127. Parent::initPersistFields();
  128. }
  129. DefineEngineMethod( GuiStackControl, isFrozen, bool, (),,
  130. "Return whether or not this control is frozen" )
  131. {
  132. return object->isFrozen();
  133. }
  134. DefineEngineMethod( GuiStackControl, freeze, void, ( bool freeze ),,
  135. "Prevents control from restacking - useful when adding or removing child controls\n"
  136. "@param freeze True to freeze the control, false to unfreeze it\n\n"
  137. "@tsexample\n"
  138. "%stackCtrl.freeze(true);\n"
  139. "// add controls to stack\n"
  140. "%stackCtrl.freeze(false);\n"
  141. "@endtsexample\n" )
  142. {
  143. object->freeze( freeze );
  144. }
  145. DefineEngineMethod( GuiStackControl, updateStack, void, (),,
  146. "Restack the child controls.\n" )
  147. {
  148. object->updatePanes();
  149. }
  150. bool GuiStackControl::onWake()
  151. {
  152. if ( !Parent::onWake() )
  153. return false;
  154. updatePanes();
  155. return true;
  156. }
  157. void GuiStackControl::onSleep()
  158. {
  159. Parent::onSleep();
  160. }
  161. void GuiStackControl::updatePanes()
  162. {
  163. // Prevent recursion
  164. if(mResizing)
  165. return;
  166. // Set Resizing.
  167. mResizing = true;
  168. Point2I extent = getExtent();
  169. // Do we need to stack horizontally?
  170. if( ( extent.x > extent.y && mStackingType == stackingTypeDyn ) || mStackingType == stackingTypeHoriz )
  171. {
  172. stackHorizontal( mStackHorizSizing == horizStackLeft );
  173. }
  174. // Or, vertically?
  175. else if( ( extent.y > extent.x && mStackingType == stackingTypeDyn ) || mStackingType == stackingTypeVert)
  176. {
  177. stackVertical( mStackVertSizing == vertStackTop );
  178. }
  179. // Clear Sizing Flag.
  180. mResizing = false;
  181. }
  182. void GuiStackControl::freeze(bool _shouldfreeze)
  183. {
  184. mResizing = _shouldfreeze;
  185. }
  186. void GuiStackControl::stackVertical(bool fromTop)
  187. {
  188. if( empty() )
  189. return;
  190. S32 begin, end, step;
  191. if ( fromTop )
  192. {
  193. // Stack from Child0 at top to ChildN at bottom
  194. begin = 0;
  195. end = size();
  196. step = 1;
  197. }
  198. else
  199. {
  200. // Stack from ChildN at top to Child0 at bottom
  201. begin = size()-1;
  202. end = -1;
  203. step = -1;
  204. }
  205. // Place each visible child control
  206. S32 maxWidth = 0;
  207. Point2I curPos(0, 0);
  208. for ( S32 i = begin; i != end; i += step )
  209. {
  210. GuiControl * gc = dynamic_cast<GuiControl*>( at(i) );
  211. if ( gc && gc->isVisible() )
  212. {
  213. // Add padding between controls
  214. if ( curPos.y > 0 )
  215. curPos.y += mPadding;
  216. Point2I childPos = curPos;
  217. if ( !mChangeChildPosition )
  218. childPos.x = gc->getLeft();
  219. Point2I childSize( gc->getExtent() );
  220. if ( mChangeChildSizeToFit )
  221. childSize.x = getWidth();
  222. gc->resize( childPos, childSize );
  223. curPos.y += gc->getHeight();
  224. maxWidth = getMax( maxWidth, childPos.x + childSize.x );
  225. }
  226. }
  227. if ( mDynamicSize )
  228. {
  229. // Conform our size to the sum of the child sizes.
  230. Point2I newPos( getPosition() );
  231. Point2I newSize( mDynamicNonStackExtent ? maxWidth : getWidth(), curPos.y );
  232. newSize.setMax( getMinExtent() );
  233. // Grow the stack up instead of down?
  234. if ( mDynamicPos )
  235. newPos.y -= ( newSize.y - getHeight() );
  236. resize( newPos, newSize );
  237. }
  238. }
  239. void GuiStackControl::stackHorizontal(bool fromLeft)
  240. {
  241. if( empty() )
  242. return;
  243. S32 begin, end, step;
  244. if ( fromLeft )
  245. {
  246. // Stack from Child0 at left to ChildN at right
  247. begin = 0;
  248. end = size();
  249. step = 1;
  250. }
  251. else
  252. {
  253. // Stack from ChildN at left to Child0 at right
  254. begin = size()-1;
  255. end = -1;
  256. step = -1;
  257. }
  258. // Place each visible child control
  259. S32 maxHeight = 0;
  260. Point2I curPos(0, 0);
  261. for ( S32 i = begin; i != end; i += step )
  262. {
  263. GuiControl * gc = dynamic_cast<GuiControl*>( at(i) );
  264. if ( gc && gc->isVisible() )
  265. {
  266. // Add padding between controls
  267. if ( curPos.x > 0 )
  268. curPos.x += mPadding;
  269. Point2I childPos = curPos;
  270. if ( !mChangeChildPosition )
  271. childPos.y = gc->getTop();
  272. Point2I childSize( gc->getExtent() );
  273. if ( mChangeChildSizeToFit )
  274. childSize.y = getHeight();
  275. gc->resize( childPos, childSize );
  276. curPos.x += gc->getWidth();
  277. maxHeight = getMax( maxHeight, childPos.y + childSize.y );
  278. }
  279. }
  280. if ( mDynamicSize )
  281. {
  282. // Conform our size to the sum of the child sizes.
  283. Point2I newPos( getPosition() );
  284. Point2I newSize( curPos.x, mDynamicNonStackExtent ? maxHeight : getHeight() );
  285. newSize.setMax( getMinExtent() );
  286. // Grow the stack left instead of right?
  287. if ( mDynamicPos )
  288. newPos.x -= ( newSize.x - getWidth() );
  289. resize( newPos, newSize );
  290. }
  291. }
  292. bool GuiStackControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  293. {
  294. if( !Parent::resize( newPosition, newExtent ) )
  295. return false;
  296. updatePanes();
  297. // CodeReview This logic should be updated to correctly return true/false
  298. // based on whether it sized it's children. [7/1/2007 justind]
  299. return true;
  300. }
  301. void GuiStackControl::addObject(SimObject *obj)
  302. {
  303. Parent::addObject(obj);
  304. updatePanes();
  305. }
  306. void GuiStackControl::removeObject(SimObject *obj)
  307. {
  308. Parent::removeObject(obj);
  309. updatePanes();
  310. }
  311. bool GuiStackControl::reOrder(SimObject* obj, SimObject* target)
  312. {
  313. bool ret = Parent::reOrder(obj, target);
  314. if (ret)
  315. updatePanes();
  316. return ret;
  317. }
  318. void GuiStackControl::childResized(GuiControl *child)
  319. {
  320. updatePanes();
  321. }