guiGridCtrl.cc 13 KB

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