guiGridCtrl.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. mIsContainer = true;
  51. mCellModeX = CellMode::Absolute;
  52. mCellModeY = CellMode::Absolute;
  53. mCellSizeX = 20;
  54. mCellSizeY = 20;
  55. mCellSpacingX = 0;
  56. mCellSpacingY = 0;
  57. mMaxColCount = 0;
  58. mMaxRowCount = 0;
  59. mOrderMode = OrderMode::LRTB;
  60. mIsExtentDynamic = true;
  61. setField("profile", "GuiDefaultProfile");
  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. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  100. getMax(mMinExtent.y, newExtent.y));
  101. //call set update both before and after
  102. setUpdate();
  103. Point2I zero = mBounds.point.Zero;
  104. RectI innerRect = getInnerRect(zero, actualNewExtent, NormalState, mProfile);
  105. if (!innerRect.isValidRect() && !mIsExtentDynamic)
  106. {
  107. return;
  108. }
  109. AdjustGrid(innerRect.extent);
  110. iterator i;
  111. U16 cellNumber = 0;
  112. mChainNumber = 0;
  113. mRunningChainHeight = 0;
  114. mCurrentChainHeight = 0;
  115. for (i = begin(); i != end(); i++, cellNumber++)
  116. {
  117. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  118. Point2I cellPos = getCellPosition(cellNumber, innerRect.extent, ctrl);
  119. Point2I cellExt = getCellExtent(ctrl);
  120. ctrl->resize(cellPos, cellExt);
  121. }
  122. mRunningChainHeight += mCurrentChainHeight;
  123. Point2I actualNewPosition = Point2I(newPosition);
  124. if(mIsExtentDynamic)
  125. {
  126. if (IsVertical())
  127. {
  128. innerRect.extent.y = mRunningChainHeight;
  129. }
  130. else
  131. {
  132. innerRect.extent.x = mRunningChainHeight;
  133. }
  134. actualNewExtent = getOuterExtent(innerRect.extent, NormalState, mProfile);
  135. }
  136. mBounds.set(actualNewPosition, actualNewExtent);
  137. GuiControl *parent = getParent();
  138. if (parent)
  139. parent->childResized(this);
  140. setUpdate();
  141. }
  142. //------------------------------------------------------------------------------
  143. Point2I GuiGridCtrl::getCellPosition(const U16 cellNumber, const Point2I &innerExtent, GuiControl *ctrl)
  144. {
  145. if(mCalcChainLength == 0)
  146. return Point2I(0, 0);
  147. Point2I result(0,0);
  148. U16 y = (U16)mFloor(cellNumber / mCalcChainLength);
  149. U16 x = (U16)(cellNumber % mCalcChainLength);
  150. F32 ChainCount = mCeil((F32)size() / (F32)mCalcChainLength);
  151. if (y != mChainNumber)
  152. {
  153. mRunningChainHeight += (mCurrentChainHeight + (U32)(IsVertical() ? mCalcCellSpace.y : mCalcCellSpace.x));
  154. mCurrentChainHeight = 0;
  155. mChainNumber = y;
  156. }
  157. if (mOrderMode == LRTB)
  158. {
  159. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), mRunningChainHeight);
  160. }
  161. else if (mOrderMode == RLTB)
  162. {
  163. x = (mCalcChainLength - 1) - x;
  164. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  165. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, mRunningChainHeight);
  166. }
  167. else if (mOrderMode == TBLR)
  168. {
  169. result.set(mRunningChainHeight, x * (mCalcCellExt.y + mCalcCellSpace.y));
  170. }
  171. else if (mOrderMode == BTLR)
  172. {
  173. x = (mCalcChainLength - 1) - x;
  174. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  175. result.set(mRunningChainHeight, (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  176. }
  177. else if (mOrderMode == LRBT)
  178. {
  179. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), (innerExtent.y - (HasVariableChainHeight() ? ctrl->getExtent().y : mCalcCellExt.y)) - (S32)mRunningChainHeight);
  180. }
  181. else if (mOrderMode == RLBT)
  182. {
  183. x = (mCalcChainLength - 1) - x;
  184. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  185. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, (innerExtent.y - (HasVariableChainHeight() ? ctrl->getExtent().y : mCalcCellExt.y)) - (S32)mRunningChainHeight);
  186. }
  187. else if (mOrderMode == TBRL)
  188. {
  189. result.set((innerExtent.x - (HasVariableChainHeight() ? ctrl->getExtent().x : mCalcCellExt.x)) - (S32)mRunningChainHeight, x * (mCalcCellExt.y + mCalcCellSpace.y));
  190. }
  191. else if (mOrderMode == BTRL)
  192. {
  193. x = (mCalcChainLength - 1) - x;
  194. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  195. result.set((innerExtent.x - (HasVariableChainHeight() ? ctrl->getExtent().x : mCalcCellExt.x)) - (S32)mRunningChainHeight, (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  196. }
  197. return result;
  198. }
  199. Point2I GuiGridCtrl::getCellExtent(GuiControl *ctrl)
  200. {
  201. if (!HasVariableChainHeight())
  202. {
  203. mCurrentChainHeight = (U32)(IsVertical() ? mCalcCellExt.y : mCalcCellExt.x);
  204. return mCalcCellExt;
  205. }
  206. S32 CellHeight = getMax(IsVertical() ? ctrl->getExtent().y : ctrl->getExtent().x, IsVertical() ? mCalcCellExt.y : mCalcCellExt.x);
  207. mCurrentChainHeight = getMax(mCurrentChainHeight, (U32)CellHeight);
  208. return IsVertical() ? Point2I(mCalcCellExt.x, CellHeight) : Point2I(CellHeight, mCalcCellExt.y);
  209. }
  210. //------------------------------------------------------------------------------
  211. void GuiGridCtrl::AdjustGrid(const Point2I& innerExtent)
  212. {
  213. Point2F cellExtX, cellExtY;
  214. if (IsVertical())
  215. {
  216. cellExtX = GetGridItemWidth(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  217. cellExtY = GetGridItemHeight(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  218. }
  219. else
  220. {
  221. cellExtY = GetGridItemWidth(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  222. cellExtX = GetGridItemHeight(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  223. }
  224. mCalcCellExt.set(cellExtX.x, cellExtY.x);
  225. mCalcCellSpace.set(cellExtX.y, cellExtY.y);
  226. }
  227. Point2F GuiGridCtrl::GetGridItemWidth(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  228. {
  229. //The two values returned are the extent and spacing held in the x and y respectively
  230. if (cellMode == GuiGridCtrl::Percent)
  231. {
  232. mCalcChainLength = (U16)getMax((100 + spaceSize) / (itemSize + spaceSize), 1.f);
  233. }
  234. else
  235. {
  236. mCalcChainLength = (U16)getMax((totalArea + spaceSize) / (itemSize + spaceSize), 1.f);
  237. }
  238. if (maxChainLength > 0)
  239. {
  240. mCalcChainLength = (U16)getMin((S32)mCalcChainLength, maxChainLength);
  241. }
  242. if (cellMode == GuiGridCtrl::Absolute)
  243. {
  244. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  245. }
  246. else if (cellMode == GuiGridCtrl::Variable)
  247. {
  248. F32 remainder = totalArea - ((mCalcChainLength * itemSize) + ((mCalcChainLength - 1) * spaceSize));
  249. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  250. }
  251. else if (cellMode == GuiGridCtrl::Percent)
  252. {
  253. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  254. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  255. return Point2F(calcCellSize, calcSpaceSize);
  256. }
  257. return Point2F(1,1);
  258. }
  259. Point2F GuiGridCtrl::GetGridItemHeight(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  260. {
  261. if (mIsExtentDynamic || cellMode == CellMode::Absolute)
  262. {
  263. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  264. }
  265. else if (cellMode == CellMode::Variable)
  266. {
  267. U16 length = (U16)mClamp((totalArea + spaceSize) / (itemSize + spaceSize), 1, maxChainLength);
  268. F32 remainder = totalArea - ((length * itemSize) + ((length - 1) * spaceSize));
  269. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  270. }
  271. else if (cellMode == CellMode::Percent)
  272. {
  273. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  274. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  275. return Point2F(calcCellSize, calcSpaceSize);
  276. }
  277. return Point2F(1, 1);
  278. }
  279. void GuiGridCtrl::onChildAdded(GuiControl *child)
  280. {
  281. //Ensure the child isn't positioned to the center
  282. if (child->getHorizSizing() == horizResizeCenter)
  283. {
  284. child->setHorizSizing(horizResizeLeft);
  285. }
  286. if (child->getVertSizing() == vertResizeCenter)
  287. {
  288. child->setVertSizing(vertResizeTop);
  289. }
  290. resize(getPosition(), getExtent());
  291. }
  292. void GuiGridCtrl::onChildRemoved(SimObject *child)
  293. {
  294. resize(getPosition(), getExtent());
  295. }
  296. void GuiGridCtrl::setCellSize(F32 width, F32 height)
  297. {
  298. mCellSizeX = mAbs(width);
  299. mCellSizeY = mAbs(height);
  300. resize(getPosition(), getExtent());
  301. }
  302. void GuiGridCtrl::setCellSpacing(F32 x, F32 y)
  303. {
  304. mCellSpacingX = mAbs(x);
  305. mCellSpacingY = mAbs(y);
  306. resize(getPosition(), getExtent());
  307. }
  308. void GuiGridCtrl::setCellModeX(const CellMode mode)
  309. {
  310. // Sanity!
  311. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  312. mCellModeX = mode;
  313. resize(getPosition(), getExtent());
  314. }
  315. void GuiGridCtrl::setCellModeY(const CellMode mode)
  316. {
  317. // Sanity!
  318. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  319. mCellModeY = mode;
  320. resize(getPosition(), getExtent());
  321. }
  322. GuiGridCtrl::CellMode GuiGridCtrl::getCellModeEnum(const char* label)
  323. {
  324. // Search for Mnemonic.
  325. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  326. {
  327. if (dStricmp(cellModeEnums[i].label, label) == 0)
  328. return (CellMode)cellModeEnums[i].index;
  329. }
  330. // Warn.
  331. Con::warnf("GuiGridCtrl::getCellModeEnum() - Invalid cell mode of '%s'", label);
  332. return (CellMode)-1;
  333. }
  334. const char* GuiGridCtrl::getCellModeDescription(const GuiGridCtrl::CellMode mode)
  335. {
  336. // Search for Mnemonic.
  337. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  338. {
  339. if (cellModeEnums[i].index == mode)
  340. return cellModeEnums[i].label;
  341. }
  342. // Warn.
  343. Con::warnf("GuiGridCtrl::getCellModeDescription() - Invalid cell mode.");
  344. return StringTable->EmptyString;
  345. }
  346. const char* GuiGridCtrl::getOrderModeDescription(const GuiGridCtrl::OrderMode mode)
  347. {
  348. // Search for Mnemonic.
  349. for (U32 i = 0; i < (sizeof(orderModeEnums) / sizeof(EnumTable::Enums)); i++)
  350. {
  351. if (orderModeEnums[i].index == mode)
  352. return orderModeEnums[i].label;
  353. }
  354. // Warn.
  355. Con::warnf("GuiGridCtrl::getOrderModeDescription() - Invalid order mode.");
  356. return StringTable->EmptyString;
  357. }