guiGridCtrl.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #include "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "gui/containers/guiGridCtrl.h"
  25. #include "guiGridCtrl_ScriptBinding.h"
  26. static EnumTable::Enums cellModeEnums[] =
  27. {
  28. { GuiGridCtrl::Absolute, "absolute" },
  29. { GuiGridCtrl::Variable, "variable" },
  30. { GuiGridCtrl::Percent, "percent" }
  31. };
  32. static EnumTable gCellModeTable(3, &cellModeEnums[0]);
  33. static EnumTable::Enums orderModeEnums[] =
  34. {
  35. { GuiGridCtrl::LRTB, "lrtb" },
  36. { GuiGridCtrl::RLTB, "rltb" },
  37. { GuiGridCtrl::TBLR, "tblr" },
  38. { GuiGridCtrl::TBRL, "tbrl" },
  39. { GuiGridCtrl::LRBT, "lrbt" },
  40. { GuiGridCtrl::RLBT, "rlbt" },
  41. { GuiGridCtrl::BTLR, "btlr" },
  42. { GuiGridCtrl::BTRL, "btrl" }
  43. };
  44. static EnumTable gOrderModeTable(8, &orderModeEnums[0]);
  45. //------------------------------------------------------------------------------
  46. IMPLEMENT_CONOBJECT(GuiGridCtrl);
  47. //------------------------------------------------------------------------------
  48. GuiGridCtrl::GuiGridCtrl()
  49. {
  50. mCellModeX = CellMode::Absolute;
  51. mCellModeY = CellMode::Absolute;
  52. mCellSizeX = 20;
  53. mCellSizeY = 20;
  54. mCellSpacingX = 0;
  55. mCellSpacingY = 0;
  56. mMaxColCount = 0;
  57. mMaxRowCount = 0;
  58. mOrderMode = OrderMode::LRTB;
  59. mIsExtentDynamic = true;
  60. setField("profile", "GuiDefaultProfile");
  61. mResizeGuard = false;
  62. }
  63. //------------------------------------------------------------------------------
  64. void GuiGridCtrl::initPersistFields()
  65. {
  66. Parent::initPersistFields();
  67. addField("CellModeX", TypeEnum, Offset(mCellModeX, GuiGridCtrl), 1, &gCellModeTable);
  68. addField("CellModeY", TypeEnum, Offset(mCellModeY, GuiGridCtrl), 1, &gCellModeTable);
  69. addField("CellSizeX", TypeF32, Offset(mCellSizeX, GuiGridCtrl));
  70. addField("CellSizeY", TypeF32, Offset(mCellSizeY, GuiGridCtrl));
  71. addField("CellSpacingX", TypeF32, Offset(mCellSpacingX, GuiGridCtrl));
  72. addField("CellSpacingY", TypeF32, Offset(mCellSpacingY, GuiGridCtrl));
  73. addField("MaxColCount", TypeS32, Offset(mMaxColCount, GuiGridCtrl));
  74. addField("MaxRowCount", TypeS32, Offset(mMaxRowCount, GuiGridCtrl));
  75. addField("OrderMode", TypeEnum, Offset(mOrderMode, GuiGridCtrl), 1, &gOrderModeTable);
  76. addField("IsExtentDynamic", TypeBool, Offset(mIsExtentDynamic, GuiGridCtrl));
  77. }
  78. //------------------------------------------------------------------------------
  79. bool GuiGridCtrl::onWake()
  80. {
  81. if (!Parent::onWake())
  82. return false;
  83. return true;
  84. }
  85. //------------------------------------------------------------------------------
  86. void GuiGridCtrl::onSleep()
  87. {
  88. Parent::onSleep();
  89. }
  90. //------------------------------------------------------------------------------
  91. void GuiGridCtrl::inspectPostApply()
  92. {
  93. resize(getPosition(), getExtent());
  94. Parent::inspectPostApply();
  95. }
  96. //------------------------------------------------------------------------------
  97. void GuiGridCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  98. {
  99. if (!mResizeGuard)//Prevent circular resizing
  100. {
  101. mResizeGuard = true;
  102. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  103. getMax(mMinExtent.y, newExtent.y));
  104. //call set update both before and after
  105. setUpdate();
  106. Point2I zero = mBounds.point.Zero;
  107. RectI innerRect = getInnerRect(zero, actualNewExtent, NormalState, mProfile);
  108. if (!innerRect.isValidRect() && !mIsExtentDynamic)
  109. {
  110. return;
  111. }
  112. AdjustGrid(innerRect.extent);
  113. iterator i;
  114. U16 cellNumber = 0;
  115. mChainNumber = 0;
  116. mRunningChainHeight = 0;
  117. mCurrentChainHeight = 0;
  118. for (i = begin(); i != end(); i++, cellNumber++)
  119. {
  120. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  121. Point2I cellPos = getCellPosition(cellNumber, innerRect.extent, ctrl);
  122. Point2I cellExt = getCellExtent(ctrl);
  123. ctrl->resize(cellPos, cellExt);
  124. }
  125. mRunningChainHeight += mCurrentChainHeight;
  126. Point2I actualNewPosition = Point2I(newPosition);
  127. if(mIsExtentDynamic)
  128. {
  129. if(isEditMode())
  130. {
  131. mRunningChainHeight += 20;
  132. }
  133. if (IsVertical())
  134. {
  135. innerRect.extent.y = mRunningChainHeight;
  136. }
  137. else
  138. {
  139. innerRect.extent.x = mRunningChainHeight;
  140. }
  141. actualNewExtent = getOuterExtent(innerRect.extent, NormalState, mProfile);
  142. }
  143. mBounds.set(actualNewPosition, actualNewExtent);
  144. GuiControl *parent = getParent();
  145. if (parent)
  146. parent->childResized(this);
  147. setUpdate();
  148. mResizeGuard = false;
  149. }
  150. }
  151. //------------------------------------------------------------------------------
  152. Point2I GuiGridCtrl::getCellPosition(const U16 cellNumber, const Point2I &innerExtent, GuiControl *ctrl)
  153. {
  154. if(mCalcChainLength == 0)
  155. return Point2I(0, 0);
  156. Point2I result(0,0);
  157. U16 y = (U16)mFloor(cellNumber / mCalcChainLength);
  158. U16 x = (U16)(cellNumber % mCalcChainLength);
  159. F32 ChainCount = mCeil((F32)size() / (F32)mCalcChainLength);
  160. if (y != mChainNumber)
  161. {
  162. mRunningChainHeight += (mCurrentChainHeight + (U32)(IsVertical() ? mCalcCellSpace.y : mCalcCellSpace.x));
  163. mCurrentChainHeight = 0;
  164. mChainNumber = y;
  165. }
  166. if (mOrderMode == LRTB)
  167. {
  168. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), mRunningChainHeight);
  169. }
  170. else if (mOrderMode == RLTB)
  171. {
  172. x = (mCalcChainLength - 1) - x;
  173. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  174. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, mRunningChainHeight);
  175. }
  176. else if (mOrderMode == TBLR)
  177. {
  178. result.set(mRunningChainHeight, x * (mCalcCellExt.y + mCalcCellSpace.y));
  179. }
  180. else if (mOrderMode == BTLR)
  181. {
  182. x = (mCalcChainLength - 1) - x;
  183. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  184. result.set(mRunningChainHeight, (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  185. }
  186. else if (mOrderMode == LRBT)
  187. {
  188. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), (innerExtent.y - (HasVariableChainHeight() ? ctrl->getExtent().y : mCalcCellExt.y)) - (S32)mRunningChainHeight);
  189. }
  190. else if (mOrderMode == RLBT)
  191. {
  192. x = (mCalcChainLength - 1) - x;
  193. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  194. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, (innerExtent.y - (HasVariableChainHeight() ? ctrl->getExtent().y : mCalcCellExt.y)) - (S32)mRunningChainHeight);
  195. }
  196. else if (mOrderMode == TBRL)
  197. {
  198. result.set((innerExtent.x - (HasVariableChainHeight() ? ctrl->getExtent().x : mCalcCellExt.x)) - (S32)mRunningChainHeight, x * (mCalcCellExt.y + mCalcCellSpace.y));
  199. }
  200. else if (mOrderMode == BTRL)
  201. {
  202. x = (mCalcChainLength - 1) - x;
  203. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  204. result.set((innerExtent.x - (HasVariableChainHeight() ? ctrl->getExtent().x : mCalcCellExt.x)) - (S32)mRunningChainHeight, (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  205. }
  206. return result;
  207. }
  208. Point2I GuiGridCtrl::getCellExtent(GuiControl *ctrl)
  209. {
  210. if (!HasVariableChainHeight())
  211. {
  212. mCurrentChainHeight = (U32)(IsVertical() ? mCalcCellExt.y : mCalcCellExt.x);
  213. return mCalcCellExt;
  214. }
  215. S32 CellHeight = getMax(IsVertical() ? ctrl->getExtent().y : ctrl->getExtent().x, IsVertical() ? mCalcCellExt.y : mCalcCellExt.x);
  216. mCurrentChainHeight = getMax(mCurrentChainHeight, (U32)CellHeight);
  217. return IsVertical() ? Point2I(mCalcCellExt.x, CellHeight) : Point2I(CellHeight, mCalcCellExt.y);
  218. }
  219. //------------------------------------------------------------------------------
  220. void GuiGridCtrl::AdjustGrid(const Point2I& innerExtent)
  221. {
  222. Point2F cellExtX, cellExtY;
  223. if (IsVertical())
  224. {
  225. cellExtX = GetGridItemWidth(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  226. cellExtY = GetGridItemHeight(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  227. }
  228. else
  229. {
  230. cellExtY = GetGridItemWidth(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  231. cellExtX = GetGridItemHeight(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  232. }
  233. mCalcCellExt.set(cellExtX.x, cellExtY.x);
  234. mCalcCellSpace.set(cellExtX.y, cellExtY.y);
  235. }
  236. Point2F GuiGridCtrl::GetGridItemWidth(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  237. {
  238. //The two values returned are the extent and spacing held in the x and y respectively
  239. if (cellMode == GuiGridCtrl::Percent)
  240. {
  241. mCalcChainLength = (U16)getMax((100 + spaceSize) / (itemSize + spaceSize), 1.f);
  242. }
  243. else
  244. {
  245. mCalcChainLength = (U16)getMax((totalArea + spaceSize) / (itemSize + spaceSize), 1.f);
  246. }
  247. if (maxChainLength > 0)
  248. {
  249. mCalcChainLength = (U16)getMin((S32)mCalcChainLength, maxChainLength);
  250. }
  251. if (cellMode == GuiGridCtrl::Absolute)
  252. {
  253. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  254. }
  255. else if (cellMode == GuiGridCtrl::Variable)
  256. {
  257. F32 remainder = totalArea - ((mCalcChainLength * itemSize) + ((mCalcChainLength - 1) * spaceSize));
  258. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  259. }
  260. else if (cellMode == GuiGridCtrl::Percent)
  261. {
  262. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  263. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  264. return Point2F(calcCellSize, calcSpaceSize);
  265. }
  266. return Point2F(1,1);
  267. }
  268. Point2F GuiGridCtrl::GetGridItemHeight(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  269. {
  270. if (mIsExtentDynamic || cellMode == CellMode::Absolute)
  271. {
  272. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  273. }
  274. else if (cellMode == CellMode::Variable)
  275. {
  276. U16 length = (U16)mClamp((totalArea + spaceSize) / (itemSize + spaceSize), 1, maxChainLength);
  277. F32 remainder = totalArea - ((length * itemSize) + ((length - 1) * spaceSize));
  278. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  279. }
  280. else if (cellMode == CellMode::Percent)
  281. {
  282. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  283. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  284. return Point2F(calcCellSize, calcSpaceSize);
  285. }
  286. return Point2F(1, 1);
  287. }
  288. void GuiGridCtrl::onChildAdded(GuiControl* child)
  289. {
  290. //Ensure the child isn't positioned to the center
  291. child->preventResizeModeCenter();
  292. child->preventResizeModeFill();
  293. resize(getPosition(), getExtent());
  294. Parent::onChildAdded(child);
  295. }
  296. void GuiGridCtrl::onChildRemoved(GuiControl* child)
  297. {
  298. resize(getPosition(), getExtent());
  299. }
  300. void GuiGridCtrl::childResized(GuiControl* child)
  301. {
  302. resize(getPosition(), getExtent());
  303. Parent::childResized(child);
  304. }
  305. void GuiGridCtrl::childMoved(GuiControl* child)
  306. {
  307. resize(getPosition(), getExtent());
  308. Parent::childMoved(child);
  309. }
  310. void GuiGridCtrl::childrenReordered()
  311. {
  312. resize(getPosition(), getExtent());
  313. Parent::childrenReordered();
  314. }
  315. void GuiGridCtrl::setCellSize(F32 width, F32 height)
  316. {
  317. mCellSizeX = mAbs(width);
  318. mCellSizeY = mAbs(height);
  319. resize(getPosition(), getExtent());
  320. }
  321. void GuiGridCtrl::setCellSpacing(F32 x, F32 y)
  322. {
  323. mCellSpacingX = mAbs(x);
  324. mCellSpacingY = mAbs(y);
  325. resize(getPosition(), getExtent());
  326. }
  327. void GuiGridCtrl::setCellModeX(const CellMode mode)
  328. {
  329. // Sanity!
  330. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  331. mCellModeX = mode;
  332. resize(getPosition(), getExtent());
  333. }
  334. void GuiGridCtrl::setCellModeY(const CellMode mode)
  335. {
  336. // Sanity!
  337. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  338. mCellModeY = mode;
  339. resize(getPosition(), getExtent());
  340. }
  341. GuiGridCtrl::CellMode GuiGridCtrl::getCellModeEnum(const char* label)
  342. {
  343. // Search for Mnemonic.
  344. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  345. {
  346. if (dStricmp(cellModeEnums[i].label, label) == 0)
  347. return (CellMode)cellModeEnums[i].index;
  348. }
  349. // Warn.
  350. Con::warnf("GuiGridCtrl::getCellModeEnum() - Invalid cell mode of '%s'", label);
  351. return (CellMode)-1;
  352. }
  353. const char* GuiGridCtrl::getCellModeDescription(const GuiGridCtrl::CellMode mode)
  354. {
  355. // Search for Mnemonic.
  356. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  357. {
  358. if (cellModeEnums[i].index == mode)
  359. return cellModeEnums[i].label;
  360. }
  361. // Warn.
  362. Con::warnf("GuiGridCtrl::getCellModeDescription() - Invalid cell mode.");
  363. return StringTable->EmptyString;
  364. }
  365. const char* GuiGridCtrl::getOrderModeDescription(const GuiGridCtrl::OrderMode mode)
  366. {
  367. // Search for Mnemonic.
  368. for (U32 i = 0; i < (sizeof(orderModeEnums) / sizeof(EnumTable::Enums)); i++)
  369. {
  370. if (orderModeEnums[i].index == mode)
  371. return orderModeEnums[i].label;
  372. }
  373. // Warn.
  374. Con::warnf("GuiGridCtrl::getOrderModeDescription() - Invalid order mode.");
  375. return StringTable->EmptyString;
  376. }