guiContainer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "platform/platform.h"
  23. #include "gui/containers/guiContainer.h"
  24. #include "gui/containers/guiPanel.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. IMPLEMENT_CONOBJECT( GuiContainer );
  28. ConsoleDocClass( GuiContainer,
  29. "@brief Brief Desc.\n\n"
  30. "@tsexample\n"
  31. "// Comment:\n"
  32. "%okButton = new ClassObject()\n"
  33. "instantiation\n"
  34. "@endtsexample\n\n"
  35. "@ingroup GuiContainers"
  36. );
  37. ImplementEnumType( GuiDockingType,
  38. "\n\n"
  39. "@ingroup GuiContainers" )
  40. { Docking::dockNone, "None" },
  41. { Docking::dockClient, "Client" },
  42. { Docking::dockTop, "Top" },
  43. { Docking::dockBottom, "Bottom" },
  44. { Docking::dockLeft, "Left" },
  45. { Docking::dockRight, "Right" }
  46. EndImplementEnumType;
  47. //-----------------------------------------------------------------------------
  48. GuiContainer::GuiContainer()
  49. {
  50. mUpdateLayout = false;
  51. mValidDockingMask = Docking::dockNone | Docking::dockBottom |
  52. Docking::dockTop | Docking::dockClient |
  53. Docking::dockLeft | Docking::dockRight;
  54. mIsContainer = true;
  55. }
  56. //-----------------------------------------------------------------------------
  57. GuiContainer::~GuiContainer()
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. void GuiContainer::initPersistFields()
  62. {
  63. Con::setIntVariable("$DOCKING_NONE", Docking::dockNone);
  64. Con::setIntVariable("$DOCKING_CLIENT", Docking::dockClient);
  65. Con::setIntVariable("$DOCKING_TOP", Docking::dockTop);
  66. Con::setIntVariable("$DOCKING_BOTTOM", Docking::dockBottom);
  67. Con::setIntVariable("$DOCKING_LEFT", Docking::dockLeft);
  68. Con::setIntVariable("$DOCKING_RIGHT", Docking::dockRight);
  69. addGroup( "Layout" );
  70. addProtectedField("docking", TYPEID< Docking::DockingType >(), Offset(mSizingOptions.mDocking, GuiContainer), &setDockingField, &defaultProtectedGetFn, "" );
  71. addField("margin", TypeRectSpacingI, Offset(mSizingOptions.mPadding, GuiContainer));
  72. addField("padding", TypeRectSpacingI, Offset(mSizingOptions.mInternalPadding, GuiContainer));
  73. addField("anchorTop", TypeBool, Offset(mSizingOptions.mAnchorTop, GuiContainer));
  74. addField("anchorBottom", TypeBool, Offset(mSizingOptions.mAnchorBottom, GuiContainer));
  75. addField("anchorLeft", TypeBool, Offset(mSizingOptions.mAnchorLeft, GuiContainer));
  76. addField("anchorRight", TypeBool, Offset(mSizingOptions.mAnchorRight, GuiContainer));
  77. endGroup( "Layout" );
  78. Parent::initPersistFields();
  79. }
  80. //-----------------------------------------------------------------------------
  81. void GuiContainer::onChildAdded(GuiControl* control)
  82. {
  83. Parent::onChildAdded( control );
  84. setUpdateLayout();
  85. }
  86. //-----------------------------------------------------------------------------
  87. void GuiContainer::onChildRemoved(GuiControl* control)
  88. {
  89. Parent::onChildRemoved( control );
  90. setUpdateLayout();
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool GuiContainer::reOrder(SimObject* obj, SimObject* target)
  94. {
  95. if ( !Parent::reOrder(obj, target) )
  96. return false;
  97. setUpdateLayout();
  98. return true;
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool GuiContainer::resize( const Point2I &newPosition, const Point2I &newExtent )
  102. {
  103. RectI oldBounds = getBounds();
  104. Point2I minExtent = getMinExtent();
  105. if( !Parent::resize( newPosition, newExtent ) )
  106. return false;
  107. RectI clientRect = getClientRect();
  108. layoutControls( clientRect );
  109. GuiControl *parent = getParent();
  110. S32 docking = getDocking();
  111. if( parent && docking != Docking::dockNone && docking != Docking::dockInvalid )
  112. setUpdateLayout( updateParent );
  113. return true;
  114. }
  115. //-----------------------------------------------------------------------------
  116. void GuiContainer::addObject(SimObject *obj)
  117. {
  118. Parent::addObject(obj);
  119. setUpdateLayout();
  120. }
  121. //-----------------------------------------------------------------------------
  122. void GuiContainer::removeObject(SimObject *obj)
  123. {
  124. Parent::removeObject(obj);
  125. setUpdateLayout();
  126. }
  127. //-----------------------------------------------------------------------------
  128. void GuiContainer::parentResized(const RectI &oldParentRect, const RectI &newParentRect)
  129. {
  130. //if(!mCanResize)
  131. // return;
  132. // If it's a control that specifies invalid docking, we'll just treat it as an old GuiControl
  133. if( getDocking() & Docking::dockInvalid || getDocking() & Docking::dockNone)
  134. return Parent::parentResized( oldParentRect, newParentRect );
  135. S32 deltaX = newParentRect.extent.x - oldParentRect.extent.x;
  136. S32 deltaY = newParentRect.extent.y - oldParentRect.extent.y;
  137. // Update Self
  138. RectI oldThisRect = getBounds();
  139. anchorControl( this, Point2I( deltaX, deltaY ) );
  140. RectI newThisRect = getBounds();
  141. // Update Deltas to pass on to children
  142. deltaX = newThisRect.extent.x - oldThisRect.extent.x;
  143. deltaY = newThisRect.extent.y - oldThisRect.extent.y;
  144. // Iterate over all children and update their anchors
  145. iterator nI = begin();
  146. for( ; nI != end(); nI++ )
  147. {
  148. // Sanity
  149. GuiControl *control = dynamic_cast<GuiControl*>( (*nI) );
  150. if( control )
  151. control->parentResized( oldThisRect, newThisRect );
  152. }
  153. }
  154. //-----------------------------------------------------------------------------
  155. void GuiContainer::childResized(GuiControl *child)
  156. {
  157. Parent::childResized( child );
  158. setUpdateLayout();
  159. }
  160. //-----------------------------------------------------------------------------
  161. bool GuiContainer::layoutControls( RectI &clientRect )
  162. {
  163. // This variable is set to the first 'Client' docking
  164. // control that is found. We defer client docking until
  165. // after all other docks have been made since it will consume
  166. // the remaining client area available.
  167. GuiContainer *clientDocking = NULL;
  168. // Iterate over all children and perform docking
  169. iterator nI = begin();
  170. for( ; nI != end(); nI++ )
  171. {
  172. // Layout Content with proper docking (Client Default)
  173. GuiControl *control = static_cast<GuiControl*>(*nI);
  174. // If we're invisible we don't get counted in docking
  175. if( control == NULL || !control->isVisible() )
  176. continue;
  177. S32 dockingMode = Docking::dockNone;
  178. GuiContainer *container = dynamic_cast<GuiContainer*>(control);
  179. if( container != NULL )
  180. dockingMode = container->getDocking();
  181. else
  182. continue;
  183. // See above note about clientDocking pointer
  184. if( dockingMode & Docking::dockClient && clientDocking == NULL )
  185. clientDocking = container;
  186. // Dock Appropriately
  187. if( !(dockingMode & Docking::dockClient) )
  188. dockControl( container, dockingMode, clientRect );
  189. }
  190. // Do client dock
  191. if( clientDocking != NULL )
  192. dockControl( clientDocking, Docking::dockClient, clientRect );
  193. return true;
  194. }
  195. //-----------------------------------------------------------------------------
  196. bool GuiContainer::dockControl( GuiContainer *control, S32 dockingMode, RectI &clientRect )
  197. {
  198. if( !control )
  199. return false;
  200. // Make sure this class support docking of this type
  201. if( !(dockingMode & getValidDockingMask()))
  202. return false;
  203. // If our client rect has run out of room, we can't dock any more
  204. if( !clientRect.isValidRect() )
  205. return false;
  206. // Dock Appropriately
  207. RectI dockRect;
  208. RectSpacingI rectShrinker;
  209. ControlSizing sizingOptions = control->getSizingOptions();
  210. switch( dockingMode )
  211. {
  212. case Docking::dockClient:
  213. // Inset by padding
  214. sizingOptions.mPadding.insetRect(clientRect);
  215. // Dock to entirety of client rectangle
  216. control->resize( clientRect.point, clientRect.extent );
  217. // Remove Client Rect, can only have one client dock
  218. clientRect.set(0,0,0,0);
  219. break;
  220. case Docking::dockTop:
  221. dockRect = clientRect;
  222. dockRect.extent.y = getMin( control->getHeight() + sizingOptions.mPadding.top + sizingOptions.mPadding.bottom , clientRect.extent.y );
  223. // Subtract our rect
  224. clientRect.point.y += dockRect.extent.y;
  225. clientRect.extent.y -= dockRect.extent.y;
  226. // Inset by padding
  227. sizingOptions.mPadding.insetRect(dockRect);
  228. // Resize
  229. control->resize( dockRect.point, dockRect.extent );
  230. break;
  231. case Docking::dockBottom:
  232. dockRect = clientRect;
  233. dockRect.extent.y = getMin( control->getHeight() + sizingOptions.mPadding.top + sizingOptions.mPadding.bottom, clientRect.extent.y );
  234. dockRect.point.y += clientRect.extent.y - dockRect.extent.y;
  235. // Subtract our rect
  236. clientRect.extent.y -= dockRect.extent.y;
  237. // Inset by padding
  238. sizingOptions.mPadding.insetRect(dockRect);
  239. // Resize
  240. control->resize( dockRect.point, dockRect.extent );
  241. break;
  242. case Docking::dockLeft:
  243. dockRect = clientRect;
  244. dockRect.extent.x = getMin( control->getWidth() + sizingOptions.mPadding.left + sizingOptions.mPadding.right, clientRect.extent.x );
  245. // Subtract our rect
  246. clientRect.point.x += dockRect.extent.x;
  247. clientRect.extent.x -= dockRect.extent.x;
  248. // Inset by padding
  249. sizingOptions.mPadding.insetRect(dockRect);
  250. // Resize
  251. control->resize( dockRect.point, dockRect.extent );
  252. break;
  253. case Docking::dockRight:
  254. dockRect = clientRect;
  255. dockRect.extent.x = getMin( control->getWidth() + sizingOptions.mPadding.left + sizingOptions.mPadding.right, clientRect.extent.x );
  256. dockRect.point.x += clientRect.extent.x - dockRect.extent.x;
  257. // Subtract our rect
  258. clientRect.extent.x -= dockRect.extent.x;
  259. // Inset by padding
  260. sizingOptions.mPadding.insetRect(dockRect);
  261. // Resize
  262. control->resize( dockRect.point, dockRect.extent );
  263. break;
  264. case Docking::dockNone:
  265. control->setUpdateLayout();
  266. break;
  267. }
  268. return true;
  269. }
  270. //-----------------------------------------------------------------------------
  271. bool GuiContainer::anchorControl( GuiControl *control, const Point2I &deltaParentExtent )
  272. {
  273. GuiContainer *container = dynamic_cast<GuiContainer*>( control );
  274. if( !control || !container )
  275. return false;
  276. // If we're docked, we don't anchor to anything
  277. if( (container->getDocking() & Docking::dockAny) || !(container->getDocking() & Docking::dockInvalid) )
  278. return false;
  279. if( deltaParentExtent.isZero() )
  280. return false;
  281. RectI oldRect = control->getBounds();
  282. RectI newRect = control->getBounds();
  283. F32 deltaBottom = mSizingOptions.mAnchorBottom ? (F32)deltaParentExtent.y : 0.0f;
  284. F32 deltaRight = mSizingOptions.mAnchorRight ? (F32)deltaParentExtent.x : 0.0f;
  285. F32 deltaLeft = mSizingOptions.mAnchorLeft ? 0.0f : (F32)deltaParentExtent.x;
  286. F32 deltaTop = mSizingOptions.mAnchorTop ? 0.0f : (F32)deltaParentExtent.y;
  287. // Apply Delta's to newRect
  288. newRect.point.x += (S32)deltaLeft;
  289. newRect.extent.x += (S32)(deltaRight - deltaLeft);
  290. newRect.point.y += (S32)deltaTop;
  291. newRect.extent.y += (S32)(deltaBottom - deltaTop);
  292. Point2I minExtent = control->getMinExtent();
  293. // Only resize if our minExtent is satisfied with it.
  294. if( !( newRect.extent.x >= control->getMinExtent().x && newRect.extent.y >= control->getMinExtent().y ) )
  295. return false;
  296. if( newRect.point == oldRect.point && newRect.extent == oldRect.extent )
  297. return false;
  298. // Finally Size the control
  299. control->resize( newRect.point, newRect.extent );
  300. // We made changes
  301. return true;
  302. }
  303. //-----------------------------------------------------------------------------
  304. void GuiContainer::onPreRender()
  305. {
  306. if( mUpdateLayout == updateNone )
  307. return;
  308. RectI clientRect = getClientRect();
  309. if( mUpdateLayout & updateSelf )
  310. layoutControls( clientRect );
  311. GuiContainer *parent = dynamic_cast<GuiContainer*>( getParent() );
  312. if( parent && ( mUpdateLayout & updateParent ) )
  313. parent->setUpdateLayout();
  314. // Always set AFTER layoutControls call to prevent recursive calling of layoutControls - JDD
  315. mUpdateLayout = updateNone;
  316. Parent::onPreRender();
  317. }
  318. //-----------------------------------------------------------------------------
  319. const RectI GuiContainer::getClientRect()
  320. {
  321. RectI resRect = RectI( Point2I(0,0), getExtent() );
  322. // Inset by padding
  323. mSizingOptions.mInternalPadding.insetRect( resRect );
  324. return resRect;
  325. }
  326. //-----------------------------------------------------------------------------
  327. void GuiContainer::setDocking( S32 docking )
  328. {
  329. mSizingOptions.mDocking = docking;
  330. setUpdateLayout( updateParent );
  331. }