guiGameListMenuCtrl.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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 "guiGameListMenuCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "gui/containers/guiScrollCtrl.h"
  27. #include "sim\actionMap.h"
  28. #include "core\strings\stringUnit.h"
  29. //-----------------------------------------------------------------------------
  30. // GuiGameListMenuCtrl
  31. //-----------------------------------------------------------------------------
  32. GuiGameListMenuCtrl::GuiGameListMenuCtrl()
  33. : mSelected(NO_ROW),
  34. mDebugRender(false),
  35. mHighlighted(NO_ROW),
  36. mCallbackOnInputs(false)
  37. {
  38. VECTOR_SET_ASSOCIATION(mRows);
  39. // initialize the control callbacks
  40. mCallbackOnA = StringTable->EmptyString();
  41. mCallbackOnB = mCallbackOnA;
  42. mCallbackOnX = mCallbackOnA;
  43. mCallbackOnY = mCallbackOnA;
  44. }
  45. GuiGameListMenuCtrl::~GuiGameListMenuCtrl()
  46. {
  47. for (S32 i = 0; i < mRows.size(); ++i)
  48. {
  49. delete mRows[i];
  50. }
  51. }
  52. void GuiGameListMenuCtrl::onRender(Point2I offset, const RectI &updateRect)
  53. {
  54. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  55. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  56. F32 xScale = (float) getWidth() / profile->getRowWidth();
  57. bool profileHasIcons = profile->hasArrows();
  58. S32 rowHeight = profile->getRowHeight();
  59. Point2I currentOffset = offset;
  60. Point2I extent = getExtent();
  61. Point2I rowExtent(extent.x, rowHeight);
  62. Point2I textOffset(profile->mTextOffset.x * xScale, profile->mTextOffset.y);
  63. Point2I textExtent(extent.x - textOffset.x, rowHeight);
  64. Point2I iconExtent, iconOffset(0.0f, 0.0f);
  65. if (profileHasIcons)
  66. {
  67. iconExtent = profile->getIconExtent();
  68. // icon is centered vertically plus any specified offset
  69. S32 iconOffsetY = (rowHeight - iconExtent.y) >> 1;
  70. iconOffsetY += profile->mIconOffset.y;
  71. iconOffset = Point2I(profile->mIconOffset.x * xScale, iconOffsetY);
  72. }
  73. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  74. {
  75. if (row != mRows.begin())
  76. {
  77. // rows other than the first can have padding above them
  78. currentOffset.y += (*row)->mHeightPad;
  79. currentOffset.y += rowHeight;
  80. }
  81. // select appropriate colors and textures
  82. ColorI fontColor;
  83. U32 buttonTextureIndex;
  84. S32 iconIndex = (*row)->mIconIndex;
  85. bool useHighlightIcon = (*row)->mUseHighlightIcon;
  86. if (!(*row)->mEnabled)
  87. {
  88. buttonTextureIndex = Profile::TEX_DISABLED;
  89. fontColor = profile->mFontColorNA;
  90. }
  91. else if (row == &mRows[mSelected])
  92. {
  93. if (iconIndex != NO_ICON)
  94. {
  95. iconIndex++;
  96. }
  97. buttonTextureIndex = Profile::TEX_SELECTED;
  98. fontColor = profile->mFontColorSEL;
  99. }
  100. else if ((mHighlighted != NO_ROW) && (row == &mRows[mHighlighted]))
  101. {
  102. if (iconIndex != NO_ICON && useHighlightIcon)
  103. {
  104. iconIndex++;
  105. }
  106. buttonTextureIndex = Profile::TEX_HIGHLIGHT;
  107. fontColor = profile->mFontColorHL;
  108. }
  109. else
  110. {
  111. buttonTextureIndex = Profile::TEX_NORMAL;
  112. fontColor = profile->mFontColor;
  113. }
  114. // render the row bitmap
  115. drawUtil->clearBitmapModulation();
  116. drawUtil->drawBitmapStretchSR(profile->mTextureObject, RectI(currentOffset, rowExtent), profile->getBitmapArrayRect(buttonTextureIndex));
  117. // render the row icon if it has one
  118. if ((iconIndex != NO_ICON) && profileHasIcons && (!profile->getBitmapArrayRect((U32)iconIndex).extent.isZero()))
  119. {
  120. iconIndex += Profile::TEX_FIRST_ICON;
  121. drawUtil->clearBitmapModulation();
  122. drawUtil->drawBitmapStretchSR(profile->mTextureObject, RectI(currentOffset + iconOffset, iconExtent), profile->getBitmapArrayRect(iconIndex));
  123. }
  124. // render the row text
  125. drawUtil->setBitmapModulation(fontColor);
  126. renderJustifiedText(currentOffset + textOffset, textExtent, (*row)->mLabel);
  127. if ((*row)->mMode == Row::Mode::OptionList)
  128. {
  129. onRenderOptionList((*row), currentOffset);
  130. }
  131. }
  132. if (mDebugRender)
  133. {
  134. onDebugRender(offset);
  135. }
  136. renderChildControls(offset, updateRect);
  137. }
  138. void GuiGameListMenuCtrl::onRenderOptionList(Row* row, Point2I currentOffset)
  139. {
  140. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  141. F32 xScale = (float)getWidth() / profile->getRowWidth();
  142. S32 rowHeight = profile->getRowHeight();
  143. bool profileHasArrows = profile->hasArrows();
  144. Point2I arrowExtent;
  145. S32 arrowOffsetY(0);
  146. if (profileHasArrows)
  147. {
  148. arrowExtent = profile->getArrowExtent();
  149. // icon is centered vertically
  150. arrowOffsetY = (rowHeight - arrowExtent.y) >> 1;
  151. }
  152. GFXDrawUtil* drawer = GFX->getDrawUtil();
  153. Point2I arrowOffset;
  154. S32 columnSplit = profile->mColumnSplit * xScale;
  155. S32 iconIndex;
  156. bool hasOptions = (row->mOptions.size() > 0) && row->mSelectedOption > -1;
  157. if (hasOptions)
  158. {
  159. bool isRowSelected = (getSelected() != NO_ROW) && (row == mRows[getSelected()]);
  160. bool isRowHighlighted = (getHighlighted() != NO_ROW) ? ((row == mRows[getHighlighted()]) && (row->mEnabled)) : false;
  161. if (profileHasArrows)
  162. {
  163. // render the left arrow
  164. bool arrowOnL = (isRowSelected || isRowHighlighted) && (row->mWrapOptions || (row->mSelectedOption > 0));
  165. iconIndex = (arrowOnL) ? Profile::TEX_L_ARROW_ON : Profile::TEX_L_ARROW_OFF;
  166. arrowOffset.x = currentOffset.x + columnSplit;
  167. arrowOffset.y = currentOffset.y + arrowOffsetY;
  168. drawer->clearBitmapModulation();
  169. drawer->drawBitmapStretchSR(profile->mTextureObject, RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  170. // render the right arrow
  171. bool arrowOnR = (isRowSelected || isRowHighlighted) && (row->mWrapOptions || (row->mSelectedOption < row->mOptions.size() - 1));
  172. iconIndex = (arrowOnR) ? Profile::TEX_R_ARROW_ON : Profile::TEX_R_ARROW_OFF;
  173. arrowOffset.x = currentOffset.x + (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale - arrowExtent.x;
  174. arrowOffset.y = currentOffset.y + arrowOffsetY;
  175. drawer->clearBitmapModulation();
  176. drawer->drawBitmapStretchSR(profile->mTextureObject, RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  177. }
  178. // get the appropriate font color
  179. ColorI fontColor;
  180. if (!row->mEnabled)
  181. {
  182. fontColor = profile->mFontColorNA;
  183. }
  184. else if (isRowSelected)
  185. {
  186. fontColor = profile->mFontColorSEL;
  187. }
  188. else if (isRowHighlighted)
  189. {
  190. fontColor = profile->mFontColorHL;
  191. }
  192. else
  193. {
  194. fontColor = profile->mFontColor;
  195. }
  196. // calculate text to be at the center between the arrows
  197. GFont* font = profile->mFont;
  198. StringTableEntry text = row->mOptions[row->mSelectedOption];
  199. S32 textWidth = font->getStrWidth(text);
  200. S32 columnWidth = profile->mHitAreaLowerRight.x * xScale - profile->mRightPad - columnSplit;
  201. S32 columnCenter = columnSplit + (columnWidth >> 1);
  202. S32 textStartX = columnCenter - (textWidth >> 1);
  203. Point2I textOffset(textStartX, 0);
  204. // render the option text itself
  205. Point2I textExtent(columnWidth, rowHeight);
  206. drawer->setBitmapModulation(fontColor);
  207. renderJustifiedText(currentOffset + textOffset, textExtent, text);
  208. }
  209. }
  210. void GuiGameListMenuCtrl::onDebugRender(Point2I offset)
  211. {
  212. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  213. F32 xScale = (float) getWidth() / profile->getRowWidth();
  214. ColorI controlBorderColor(200, 200, 200); // gray
  215. ColorI rowBorderColor(255, 127, 255); // magenta
  216. ColorI hitBorderColor(255, 0, 0); // red
  217. Point2I shrinker(-1, -1);
  218. Point2I extent = getExtent();
  219. // render a border around the entire control
  220. RectI borderRect(offset, extent + shrinker);
  221. GFX->getDrawUtil()->drawRect(borderRect, controlBorderColor);
  222. S32 rowHeight = profile->getRowHeight();
  223. Point2I currentOffset(offset);
  224. Point2I rowExtent(extent.x, rowHeight);
  225. rowExtent += shrinker;
  226. Point2I hitAreaExtent(profile->getHitAreaExtent());
  227. hitAreaExtent.x *= xScale;
  228. hitAreaExtent += shrinker;
  229. Point2I hitAreaOffset = profile->mHitAreaUpperLeft;
  230. hitAreaOffset.x *= xScale;
  231. Point2I upperLeft;
  232. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  233. {
  234. // set the top of the current row
  235. if (row != mRows.begin())
  236. {
  237. // rows other than the first can have padding above them
  238. currentOffset.y += (*row)->mHeightPad;
  239. currentOffset.y += rowHeight;
  240. }
  241. // draw the box around the whole row's extent
  242. upperLeft = currentOffset;
  243. borderRect.point = upperLeft;
  244. borderRect.extent = rowExtent;
  245. GFX->getDrawUtil()->drawRect(borderRect, rowBorderColor);
  246. // draw the box around the hit area of the row
  247. upperLeft = currentOffset + hitAreaOffset;
  248. borderRect.point = upperLeft;
  249. borderRect.extent = hitAreaExtent;
  250. GFX->getDrawUtil()->drawRect(borderRect, hitBorderColor);
  251. }
  252. }
  253. void GuiGameListMenuCtrl::addRow(const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, S32 mode)
  254. {
  255. Row * row = new Row();
  256. addRow(row, label, callback, icon, yPad, useHighlightIcon, enabled, mode);
  257. }
  258. void GuiGameListMenuCtrl::addRow(Row * row, const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, S32 mode)
  259. {
  260. row->mLabel = StringTable->insert(label, true);
  261. row->mScriptCallback = (dStrlen(callback) > 0) ? StringTable->insert(callback, true) : NULL;
  262. row->mIconIndex = (icon < 0) ? NO_ICON : icon;
  263. row->mHeightPad = yPad;
  264. row->mUseHighlightIcon = useHighlightIcon;
  265. row->mEnabled = enabled;
  266. row->mMode = (Row::Mode)mode;
  267. mRows.push_back(row);
  268. updateHeight();
  269. if (mSelected == NO_ROW)
  270. {
  271. selectFirstEnabledRow();
  272. }
  273. }
  274. void GuiGameListMenuCtrl::addRow(const char* label, const char* optionsList, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled)
  275. {
  276. static StringTableEntry DELIM = StringTable->insert("\t", true);
  277. Row* row = new Row();
  278. Vector<StringTableEntry> options(__FILE__, __LINE__);
  279. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  280. for (S32 i = 0; i < count; ++i)
  281. {
  282. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  283. options.push_back(StringTable->insert(option, true));
  284. }
  285. row->mOptions = options;
  286. bool hasOptions = row->mOptions.size() > 0;
  287. row->mSelectedOption = (hasOptions) ? 0 : NO_OPTION;
  288. row->mWrapOptions = wrapOptions;
  289. addRow(row, label, callback, icon, yPad, true, (hasOptions) ? enabled : false, Row::Mode::OptionList);
  290. }
  291. Point2I GuiGameListMenuCtrl::getMinExtent() const
  292. {
  293. Point2I parentMin = Parent::getMinExtent();
  294. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  295. S32 minHeight = 0;
  296. S32 rowHeight = profile->getRowHeight();
  297. for (Vector<Row *>::const_iterator row = mRows.begin(); row < mRows.end(); ++row)
  298. {
  299. minHeight += rowHeight;
  300. if (row != mRows.begin())
  301. {
  302. minHeight += (*row)->mHeightPad;
  303. }
  304. }
  305. if (minHeight > parentMin.y)
  306. parentMin.y = minHeight;
  307. return parentMin;
  308. }
  309. bool GuiGameListMenuCtrl::onAdd()
  310. {
  311. if( !Parent::onAdd() )
  312. return false;
  313. // If we have a non-GuiGameListMenuProfile profile, try to
  314. // substitute it for DefaultListMenuProfile.
  315. if( !hasValidProfile() )
  316. {
  317. GuiGameListMenuProfile* profile;
  318. if( !Sim::findObject( "DefaultListMenuProfile", profile ) )
  319. {
  320. Con::errorf( "GuiGameListMenuCtrl: %s can't be created with a profile of type %s. Please create it with a profile of type GuiGameListMenuProfile.",
  321. getName(), mProfile->getClassName() );
  322. return false;
  323. }
  324. else
  325. Con::warnf( "GuiGameListMenuCtrl: substituted non-GuiGameListMenuProfile in %s for DefaultListMenuProfile", getName() );
  326. setControlProfile( profile );
  327. }
  328. return true;
  329. }
  330. bool GuiGameListMenuCtrl::onWake()
  331. {
  332. if( !Parent::onWake() )
  333. return false;
  334. if( !hasValidProfile() )
  335. return false;
  336. /*if( mRows.empty() )
  337. {
  338. Con::errorf( "GuiGameListMenuCtrl: %s can't be woken up without any rows. Please use \"addRow\" to add at least one row to the control before pushing it to the canvas.",
  339. getName() );
  340. return false;
  341. }*/
  342. enforceConstraints();
  343. selectFirstEnabledRow();
  344. setFirstResponder();
  345. mHighlighted = NO_ROW;
  346. return true;
  347. }
  348. bool GuiGameListMenuCtrl::hasValidProfile() const
  349. {
  350. GuiGameListMenuProfile * profile = dynamic_cast<GuiGameListMenuProfile *>(mProfile);
  351. return profile;
  352. }
  353. void GuiGameListMenuCtrl::enforceConstraints()
  354. {
  355. if( hasValidProfile() )
  356. {
  357. ((GuiGameListMenuProfile *)mProfile)->enforceConstraints();
  358. }
  359. updateHeight();
  360. }
  361. void GuiGameListMenuCtrl::updateHeight()
  362. {
  363. S32 minHeight = getMinExtent().y;
  364. if (getHeight() < minHeight)
  365. {
  366. setHeight(minHeight);
  367. }
  368. }
  369. void GuiGameListMenuCtrl::onMouseDown(const GuiEvent &event)
  370. {
  371. S32 hitRow = getRow(event.mousePoint);
  372. if (hitRow != NO_ROW)
  373. {
  374. S32 delta = (mSelected != NO_ROW) ? (hitRow - mSelected) : (mSelected + 1);
  375. changeRow(delta);
  376. }
  377. }
  378. void GuiGameListMenuCtrl::onMouseLeave(const GuiEvent &event)
  379. {
  380. mHighlighted = NO_ROW;
  381. }
  382. void GuiGameListMenuCtrl::onMouseMove(const GuiEvent &event)
  383. {
  384. S32 hitRow = getRow(event.mousePoint);
  385. // allow mHighligetd to be set to NO_ROW so rows can be unhighlighted
  386. mHighlighted = hitRow;
  387. }
  388. void GuiGameListMenuCtrl::onMouseUp(const GuiEvent &event)
  389. {
  390. S32 hitRow = getRow(event.mousePoint);
  391. if ((hitRow != NO_ROW) && isRowEnabled(hitRow) && (hitRow == getSelected()))
  392. {
  393. if (mRows[hitRow]->mMode == Row::Mode::Default)
  394. {
  395. activateRow();
  396. }
  397. else if (mRows[hitRow]->mMode == Row::Mode::OptionList)
  398. {
  399. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  400. clickOption((Row*)mRows[getSelected()], xPos);
  401. }
  402. }
  403. }
  404. void GuiGameListMenuCtrl::activateRow()
  405. {
  406. S32 row = getSelected();
  407. if ((row != NO_ROW) && isRowEnabled(row) && (mRows[row]->mScriptCallback != NULL))
  408. {
  409. setThisControl();
  410. if (Con::isFunction(mRows[row]->mScriptCallback))
  411. {
  412. Con::executef(mRows[row]->mScriptCallback);
  413. }
  414. }
  415. }
  416. S32 GuiGameListMenuCtrl::getRow(Point2I globalPoint)
  417. {
  418. Point2I localPoint = globalToLocalCoord(globalPoint);
  419. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  420. F32 xScale = (float) getWidth() / profile->getRowWidth();
  421. S32 rowHeight = profile->getRowHeight();
  422. Point2I currentOffset(0, 0);
  423. Point2I hitAreaUpperLeft = profile->mHitAreaUpperLeft;
  424. hitAreaUpperLeft.x *= xScale;
  425. Point2I hitAreaLowerRight = profile->mHitAreaLowerRight;
  426. hitAreaLowerRight.x *= xScale;
  427. Point2I upperLeft, lowerRight;
  428. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  429. {
  430. if (row != mRows.begin())
  431. {
  432. // rows other than the first can have padding above them
  433. currentOffset.y += (*row)->mHeightPad;
  434. }
  435. upperLeft = currentOffset + hitAreaUpperLeft;
  436. lowerRight = currentOffset + hitAreaLowerRight;
  437. if ((upperLeft.x <= localPoint.x) && (localPoint.x < lowerRight.x) &&
  438. (upperLeft.y <= localPoint.y) && (localPoint.y < lowerRight.y))
  439. {
  440. return row - mRows.begin();
  441. }
  442. currentOffset.y += rowHeight;
  443. }
  444. return NO_ROW;
  445. }
  446. void GuiGameListMenuCtrl::setSelected(S32 index)
  447. {
  448. if (index == NO_ROW)
  449. {
  450. // deselection
  451. mSelected = NO_ROW;
  452. return;
  453. }
  454. if (! isValidRowIndex(index))
  455. {
  456. return;
  457. }
  458. if (! isRowEnabled(index))
  459. {
  460. // row is disabled, it can't be selected
  461. return;
  462. }
  463. mSelected = mClamp(index, 0, mRows.size() - 1);
  464. //If we're childed to a scroll container, make sure us changing rows has our new position visible
  465. GuiScrollCtrl* scroll = dynamic_cast<GuiScrollCtrl*>(getParent());
  466. if (scroll)
  467. {
  468. scroll->scrollRectVisible(getRowBounds(mSelected));
  469. }
  470. }
  471. bool GuiGameListMenuCtrl::isRowEnabled(S32 index) const
  472. {
  473. if (! isValidRowIndex(index))
  474. {
  475. return false;
  476. }
  477. return mRows[index]->mEnabled;
  478. }
  479. void GuiGameListMenuCtrl::setRowEnabled(S32 index, bool enabled)
  480. {
  481. if (! isValidRowIndex(index))
  482. {
  483. return;
  484. }
  485. mRows[index]->mEnabled = enabled;
  486. if (getSelected() == index)
  487. {
  488. selectFirstEnabledRow();
  489. }
  490. }
  491. bool GuiGameListMenuCtrl::isValidRowIndex(S32 index) const
  492. {
  493. return ((0 <= index) && (index < mRows.size()));
  494. }
  495. void GuiGameListMenuCtrl::selectFirstEnabledRow()
  496. {
  497. setSelected(NO_ROW);
  498. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  499. {
  500. if ((*row)->mEnabled)
  501. {
  502. setSelected(row - mRows.begin());
  503. return;
  504. }
  505. }
  506. }
  507. bool GuiGameListMenuCtrl::onInputEvent(const InputEventInfo& event)
  508. {
  509. if (mCallbackOnInputs)
  510. {
  511. char deviceString[32];
  512. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  513. return false;
  514. if (event.action == SI_MAKE || event.action == SI_BREAK)
  515. {
  516. bool isModifier = false;
  517. switch (event.objInst)
  518. {
  519. case KEY_LCONTROL:
  520. case KEY_RCONTROL:
  521. case KEY_LALT:
  522. case KEY_RALT:
  523. case KEY_LSHIFT:
  524. case KEY_RSHIFT:
  525. case KEY_MAC_LOPT:
  526. case KEY_MAC_ROPT:
  527. isModifier = true;
  528. }
  529. if ((event.objType == SI_KEY) && isModifier)
  530. {
  531. char keyString[32];
  532. if (!ActionMap::getKeyString(event.objInst, keyString))
  533. return false;
  534. onInputEvent_callback(deviceString, keyString, event.action);
  535. }
  536. else
  537. {
  538. const char* actionString = ActionMap::buildActionString(&event);
  539. onInputEvent_callback(deviceString, actionString, event.action);
  540. }
  541. }
  542. else if (event.objType == SI_AXIS || event.objType == SI_INT || event.objType == SI_FLOAT)
  543. {
  544. F32 fValue = event.fValue;
  545. if (event.objType == SI_INT)
  546. fValue = (F32)event.iValue;
  547. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  548. return false;
  549. const char* actionString = ActionMap::buildActionString(&event);
  550. onAxisEvent_callback(deviceString, actionString, fValue);
  551. }
  552. }
  553. return false;
  554. }
  555. bool GuiGameListMenuCtrl::onKeyDown(const GuiEvent &event)
  556. {
  557. switch (event.keyCode)
  558. {
  559. case KEY_UP:
  560. changeRow(-1);
  561. return true;
  562. case KEY_DOWN:
  563. changeRow(1);
  564. return true;
  565. case KEY_A:
  566. case KEY_RETURN:
  567. case KEY_NUMPADENTER:
  568. case KEY_SPACE:
  569. case XI_A:
  570. case XI_START:
  571. doScriptCommand(mCallbackOnA);
  572. return true;
  573. case KEY_B:
  574. case KEY_ESCAPE:
  575. case KEY_BACKSPACE:
  576. case KEY_DELETE:
  577. case XI_B:
  578. case XI_BACK:
  579. doScriptCommand(mCallbackOnB);
  580. return true;
  581. case KEY_X:
  582. case XI_X:
  583. doScriptCommand(mCallbackOnX);
  584. return true;
  585. case KEY_Y:
  586. case XI_Y:
  587. doScriptCommand(mCallbackOnY);
  588. return true;
  589. default:
  590. break;
  591. }
  592. return Parent::onKeyDown(event);
  593. }
  594. bool GuiGameListMenuCtrl::onGamepadAxisUp(const GuiEvent &event)
  595. {
  596. changeRow(-1);
  597. return true;
  598. }
  599. bool GuiGameListMenuCtrl::onGamepadAxisDown(const GuiEvent &event)
  600. {
  601. changeRow(1);
  602. return true;
  603. }
  604. bool GuiGameListMenuCtrl::onGamepadAxisLeft(const GuiEvent& event)
  605. {
  606. changeOption(-1);
  607. return true;
  608. }
  609. bool GuiGameListMenuCtrl::onGamepadAxisRight(const GuiEvent& event)
  610. {
  611. changeOption(1);
  612. return true;
  613. }
  614. void GuiGameListMenuCtrl::doScriptCommand(StringTableEntry command)
  615. {
  616. if (command && command[0])
  617. {
  618. setThisControl();
  619. Con::evaluate(command, false, __FILE__);
  620. }
  621. }
  622. void GuiGameListMenuCtrl::changeRow(S32 delta)
  623. {
  624. S32 oldRowIndex = getSelected();
  625. S32 newRowIndex = oldRowIndex;
  626. do
  627. {
  628. newRowIndex += delta;
  629. if (newRowIndex >= mRows.size())
  630. {
  631. newRowIndex = 0;
  632. }
  633. else if (newRowIndex < 0)
  634. {
  635. newRowIndex = mRows.size() - 1;
  636. }
  637. }
  638. while ((! mRows[newRowIndex]->mEnabled) && (newRowIndex != oldRowIndex));
  639. setSelected(newRowIndex);
  640. // do the callback
  641. onChange_callback();
  642. }
  643. void GuiGameListMenuCtrl::setThisControl()
  644. {
  645. smThisControl = this;
  646. }
  647. StringTableEntry GuiGameListMenuCtrl::getRowLabel(S32 rowIndex) const
  648. {
  649. AssertFatal(isValidRowIndex(rowIndex), avar("GuiGameListMenuCtrl: You can't get the label from row %d of %s because it is not a valid row index. Please specify a valid row index in the range [0, %d).", rowIndex, getName(), getRowCount()));
  650. if (! isValidRowIndex(rowIndex))
  651. {
  652. // not a valid row index, don't do anything
  653. return StringTable->EmptyString();
  654. }
  655. return mRows[rowIndex]->mLabel;
  656. }
  657. void GuiGameListMenuCtrl::setRowLabel(S32 rowIndex, const char * label)
  658. {
  659. AssertFatal(isValidRowIndex(rowIndex), avar("GuiGameListMenuCtrl: You can't set the label on row %d of %s because it is not a valid row index. Please specify a valid row index in the range [0, %d).", rowIndex, getName(), getRowCount()));
  660. if (! isValidRowIndex(rowIndex))
  661. {
  662. // not a valid row index, don't do anything
  663. return;
  664. }
  665. mRows[rowIndex]->mLabel = StringTable->insert(label, true);
  666. }
  667. void GuiGameListMenuCtrl::clearRows()
  668. {
  669. mRows.clear();
  670. }
  671. RectI GuiGameListMenuCtrl::getRowBounds(S32 rowIndex)
  672. {
  673. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  674. F32 xScale = (float)getWidth() / profile->getRowWidth();
  675. S32 rowHeight = profile->getRowHeight();
  676. Point2I currentOffset = Point2I::Zero;
  677. Point2I extent = getExtent();
  678. Point2I rowExtent(extent.x, rowHeight);
  679. for (U32 i = 1; i <= rowIndex; i++)
  680. {
  681. //the top row can't pad, so we'll ignore it
  682. GuiGameListMenuCtrl::Row* row = mRows[i];
  683. // rows other than the first can have padding above them
  684. currentOffset.y += row->mHeightPad;
  685. currentOffset.y += rowHeight;
  686. }
  687. return RectI(currentOffset, rowExtent);
  688. }
  689. //-----------------------------------------------------------------------------
  690. // Console stuff (GuiGameListMenuCtrl)
  691. //-----------------------------------------------------------------------------
  692. StringTableEntry GuiGameListMenuCtrl::getCurrentOption(S32 rowIndex) const
  693. {
  694. if (isValidRowIndex(rowIndex))
  695. {
  696. Row* row = (Row*)mRows[rowIndex];
  697. if (row->mSelectedOption != NO_OPTION)
  698. {
  699. return row->mOptions[row->mSelectedOption];
  700. }
  701. }
  702. return StringTable->insert("", false);
  703. }
  704. bool GuiGameListMenuCtrl::selectOption(S32 rowIndex, const char* theOption)
  705. {
  706. if (!isValidRowIndex(rowIndex))
  707. {
  708. return false;
  709. }
  710. Row* row = (Row*)mRows[rowIndex];
  711. for (Vector<StringTableEntry>::iterator anOption = row->mOptions.begin(); anOption < row->mOptions.end(); ++anOption)
  712. {
  713. if (dStrcmp(*anOption, theOption) == 0)
  714. {
  715. S32 newIndex = anOption - row->mOptions.begin();
  716. row->mSelectedOption = newIndex;
  717. return true;
  718. }
  719. }
  720. return false;
  721. }
  722. void GuiGameListMenuCtrl::setOptions(S32 rowIndex, const char* optionsList)
  723. {
  724. static StringTableEntry DELIM = StringTable->insert("\t", true);
  725. if (!isValidRowIndex(rowIndex))
  726. {
  727. return;
  728. }
  729. Row* row = (Row*)mRows[rowIndex];
  730. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  731. row->mOptions.setSize(count);
  732. for (S32 i = 0; i < count; ++i)
  733. {
  734. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  735. row->mOptions[i] = StringTable->insert(option, true);
  736. }
  737. if (row->mSelectedOption >= row->mOptions.size())
  738. {
  739. row->mSelectedOption = row->mOptions.size() - 1;
  740. }
  741. }
  742. void GuiGameListMenuCtrl::clickOption(Row* row, S32 xPos)
  743. {
  744. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  745. if (!profile->hasArrows())
  746. {
  747. return;
  748. }
  749. F32 xScale = (float)getWidth() / profile->getRowWidth();
  750. S32 bitmapArrowWidth = mProfile->getBitmapArrayRect(Profile::TEX_FIRST_ARROW).extent.x;
  751. S32 leftArrowX1 = profile->mColumnSplit * xScale;
  752. S32 leftArrowX2 = leftArrowX1 + bitmapArrowWidth;
  753. S32 rightArrowX2 = (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale;
  754. S32 rightArrowX1 = rightArrowX2 - bitmapArrowWidth;
  755. if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  756. {
  757. changeOption(row, -1);
  758. }
  759. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  760. {
  761. changeOption(row, 1);
  762. }
  763. }
  764. void GuiGameListMenuCtrl::changeOption(S32 delta)
  765. {
  766. if (getSelected() != NO_ROW)
  767. {
  768. Row* row = (Row*)mRows[getSelected()];
  769. changeOption(row, delta);
  770. }
  771. }
  772. void GuiGameListMenuCtrl::changeOption(Row* row, S32 delta)
  773. {
  774. S32 optionCount = row->mOptions.size();
  775. S32 newSelection = row->mSelectedOption + delta;
  776. if (optionCount == 0)
  777. {
  778. newSelection = NO_OPTION;
  779. }
  780. else if (!row->mWrapOptions)
  781. {
  782. newSelection = mClamp(newSelection, 0, optionCount - 1);
  783. }
  784. else if (newSelection < 0)
  785. {
  786. newSelection = optionCount - 1;
  787. }
  788. else if (newSelection >= optionCount)
  789. {
  790. newSelection = 0;
  791. }
  792. row->mSelectedOption = newSelection;
  793. static StringTableEntry LEFT = StringTable->insert("LEFT", true);
  794. static StringTableEntry RIGHT = StringTable->insert("RIGHT", true);
  795. if (row->mScriptCallback != NULL)
  796. {
  797. setThisControl();
  798. StringTableEntry direction = NULL;
  799. if (delta < 0)
  800. {
  801. direction = LEFT;
  802. }
  803. else if (delta > 0)
  804. {
  805. direction = RIGHT;
  806. }
  807. if ((direction != NULL) && (Con::isFunction(row->mScriptCallback)))
  808. {
  809. Con::executef(row->mScriptCallback, direction);
  810. }
  811. }
  812. }
  813. IMPLEMENT_CONOBJECT(GuiGameListMenuCtrl);
  814. ConsoleDocClass( GuiGameListMenuCtrl,
  815. "@brief A base class for cross platform menu controls that are gamepad friendly.\n\n"
  816. "This class is used to build row-based menu GUIs that can be easily navigated "
  817. "using the keyboard, mouse or gamepad. The desired row can be selected using "
  818. "the mouse, or by navigating using the Up and Down buttons.\n\n"
  819. "@tsexample\n\n"
  820. "new GuiGameListMenuCtrl()\n"
  821. "{\n"
  822. " debugRender = \"0\";\n"
  823. " callbackOnA = \"applyOptions();\";\n"
  824. " callbackOnB = \"Canvas.setContent(MainMenuGui);\";\n"
  825. " callbackOnX = \"\";\n"
  826. " callbackOnY = \"revertOptions();\";\n"
  827. " //Properties not specific to this control have been omitted from this example.\n"
  828. "};\n"
  829. "@endtsexample\n\n"
  830. "@see GuiGameListMenuProfile\n\n"
  831. "@ingroup GuiGame"
  832. );
  833. IMPLEMENT_CALLBACK( GuiGameListMenuCtrl, onChange, void, (), (),
  834. "Called when the selected row changes." );
  835. IMPLEMENT_CALLBACK(GuiGameListMenuCtrl, onInputEvent, void, (const char* device, const char* action, bool state),
  836. (device, action, state),
  837. "@brief Callback that occurs when an input is triggered on this control\n\n"
  838. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  839. "@param action The actual event occuring, such as a key or button\n"
  840. "@param state True if the action is being pressed, false if it is being release\n\n");
  841. IMPLEMENT_CALLBACK(GuiGameListMenuCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  842. (device, action, axisValue),
  843. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  844. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  845. "@param action The ActionMap code for the axis\n"
  846. "@param axisValue The current value of the axis\n\n");
  847. void GuiGameListMenuCtrl::initPersistFields()
  848. {
  849. addField("debugRender", TypeBool, Offset(mDebugRender, GuiGameListMenuCtrl),
  850. "Enable debug rendering" );
  851. addField("callbackOnA", TypeString, Offset(mCallbackOnA, GuiGameListMenuCtrl),
  852. "Script callback when the 'A' button is pressed. 'A' inputs are Keyboard: A, Return, Space; Gamepad: A, Start" );
  853. addField("callbackOnB", TypeString, Offset(mCallbackOnB, GuiGameListMenuCtrl),
  854. "Script callback when the 'B' button is pressed. 'B' inputs are Keyboard: B, Esc, Backspace, Delete; Gamepad: B, Back" );
  855. addField("callbackOnX", TypeString, Offset(mCallbackOnX, GuiGameListMenuCtrl),
  856. "Script callback when the 'X' button is pressed. 'X' inputs are Keyboard: X; Gamepad: X" );
  857. addField("callbackOnY", TypeString, Offset(mCallbackOnY, GuiGameListMenuCtrl),
  858. "Script callback when the 'Y' button is pressed. 'Y' inputs are Keyboard: Y; Gamepad: Y" );
  859. addField("callbackOnInputs", TypeBool, Offset(mCallbackOnInputs, GuiGameListMenuCtrl),
  860. "Script callback when any inputs are detected, even if they aren't the regular 4 face buttons. Useful for secondary/speciality handling of menu navigation.");
  861. Parent::initPersistFields();
  862. }
  863. DefineEngineMethod( GuiGameListMenuCtrl, addRow, void,
  864. ( const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, int mode ),
  865. ( -1, 0, true, true, 0 ),
  866. "Add a row to the list control.\n\n"
  867. "@param label The text to display on the row as a label.\n"
  868. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  869. "@param icon [optional] Index of the icon to use as a marker.\n"
  870. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  871. "@param useHighlightIcon [optional] Does this row use the highlight icon?.\n"
  872. "@param enabled [optional] If this row is initially enabled.\n"
  873. "@param mode [optional] What option mode the row is in. 0 = Default, 1 = OptionList, 2 == Keybind")
  874. {
  875. object->addRow( label, callback, icon, yPad, useHighlightIcon, enabled, mode);
  876. }
  877. DefineEngineMethod( GuiGameListMenuCtrl, isRowEnabled, bool, ( S32 row ),,
  878. "Determines if the specified row is enabled or disabled.\n\n"
  879. "@param row The row to set the enabled status of.\n"
  880. "@return True if the specified row is enabled. False if the row is not enabled or the given index was not valid." )
  881. {
  882. return object->isRowEnabled( row );
  883. }
  884. DefineEngineMethod( GuiGameListMenuCtrl, setRowEnabled, void, ( S32 row, bool enabled ),,
  885. "Sets a row's enabled status according to the given parameters.\n\n"
  886. "@param row The index to check for validity.\n"
  887. "@param enabled Indicate true to enable the row or false to disable it." )
  888. {
  889. object->setRowEnabled( row, enabled );
  890. }
  891. DefineEngineMethod( GuiGameListMenuCtrl, activateRow, void, (),,
  892. "Activates the current row. The script callback of the current row will be called (if it has one)." )
  893. {
  894. object->activateRow();
  895. }
  896. DefineEngineMethod( GuiGameListMenuCtrl, getRowCount, S32, (),,
  897. "Gets the number of rows on the control.\n\n"
  898. "@return (int) The number of rows on the control." )
  899. {
  900. return object->getRowCount();
  901. }
  902. DefineEngineMethod( GuiGameListMenuCtrl, getRowLabel, const char *, ( S32 row ),,
  903. "Gets the label displayed on the specified row.\n\n"
  904. "@param row Index of the row to get the label of.\n"
  905. "@return The label for the row." )
  906. {
  907. return object->getRowLabel( row );
  908. }
  909. DefineEngineMethod( GuiGameListMenuCtrl, setRowLabel, void, ( S32 row, const char* label ),,
  910. "Sets the label on the given row.\n\n"
  911. "@param row Index of the row to set the label on.\n"
  912. "@param label Text to set as the label of the row.\n" )
  913. {
  914. object->setRowLabel( row, label );
  915. }
  916. DefineEngineMethod( GuiGameListMenuCtrl, setSelected, void, ( S32 row ),,
  917. "Sets the selected row. Only rows that are enabled can be selected.\n\n"
  918. "@param row Index of the row to set as selected." )
  919. {
  920. object->setSelected( row );
  921. }
  922. DefineEngineMethod( GuiGameListMenuCtrl, getSelectedRow, S32, (),,
  923. "Gets the index of the currently selected row.\n\n"
  924. "@return Index of the selected row." )
  925. {
  926. return object->getSelected();
  927. }
  928. DefineEngineMethod(GuiGameListMenuCtrl, clearRows, void, (), ,
  929. "Gets the index of the currently selected row.\n\n"
  930. "@return Index of the selected row.")
  931. {
  932. return object->clearRows();
  933. }
  934. DefineEngineMethod(GuiGameListMenuCtrl, addOptionRow, void,
  935. (const char* label, const char* options, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled),
  936. (-1, 0, true),
  937. "Add a row to the list control.\n\n"
  938. "@param label The text to display on the row as a label.\n"
  939. "@param options A tab separated list of options.\n"
  940. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  941. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  942. "@param icon [optional] Index of the icon to use as a marker.\n"
  943. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  944. "@param enabled [optional] If this row is initially enabled.")
  945. {
  946. object->addRow(label, options, wrapOptions, callback, icon, yPad, enabled);
  947. }
  948. DefineEngineMethod(GuiGameListMenuCtrl, getCurrentOption, const char*, (S32 row), ,
  949. "Gets the text for the currently selected option of the given row.\n\n"
  950. "@param row Index of the row to get the option from.\n"
  951. "@return A string representing the text currently displayed as the selected option on the given row. If there is no such displayed text then the empty string is returned.")
  952. {
  953. return object->getCurrentOption(row);
  954. }
  955. DefineEngineMethod(GuiGameListMenuCtrl, selectOption, bool, (S32 row, const char* option), ,
  956. "Set the row's current option to the one specified\n\n"
  957. "@param row Index of the row to set an option on.\n"
  958. "@param option The option to be made active.\n"
  959. "@return True if the row contained the option and was set, false otherwise.")
  960. {
  961. return object->selectOption(row, option);
  962. }
  963. DefineEngineMethod(GuiGameListMenuCtrl, setOptions, void, (S32 row, const char* optionsList), ,
  964. "Sets the list of options on the given row.\n\n"
  965. "@param row Index of the row to set options on."
  966. "@param optionsList A tab separated list of options for the control.")
  967. {
  968. object->setOptions(row, optionsList);
  969. }
  970. //-----------------------------------------------------------------------------
  971. // GuiGameListMenuProfile
  972. //-----------------------------------------------------------------------------
  973. GuiGameListMenuProfile::GuiGameListMenuProfile()
  974. : mHitAreaUpperLeft(0, 0),
  975. mHitAreaLowerRight(0, 0),
  976. mIconOffset(0, 0),
  977. mRowSize(0, 0),
  978. mRowScale(1.0f, 1.0f),
  979. mColumnSplit(0),
  980. mRightPad(0)
  981. {
  982. }
  983. bool GuiGameListMenuProfile::onAdd()
  984. {
  985. if (! Parent::onAdd())
  986. {
  987. return false;
  988. }
  989. // We can't call enforceConstraints() here because incRefCount initializes
  990. // some of the things to enforce. Do a basic sanity check here instead.
  991. if( !mBitmapName || !dStrlen(mBitmapName) )
  992. {
  993. Con::errorf( "GuiGameListMenuProfile: %s can't be created without a bitmap. Please add a 'Bitmap' property to the object definition.", getName() );
  994. return false;
  995. }
  996. if( mRowSize.x < 0 )
  997. {
  998. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row width. Please change the row width to be non-negative.", getName() );
  999. return false;
  1000. }
  1001. if( mRowSize.y < 0 )
  1002. {
  1003. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row height. Please change the row height to be non-negative.", getName() );
  1004. return false;
  1005. }
  1006. return true;
  1007. }
  1008. void GuiGameListMenuProfile::enforceConstraints()
  1009. {
  1010. if( getBitmapArrayRect(0).extent.isZero() )
  1011. Con::errorf( "GuiGameListMenuCtrl: %s can't be created without a bitmap. Please add a bitmap to the profile's definition.", getName() );
  1012. if( mRowSize.x < 0 )
  1013. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row width. Please change the row width to be non-negative.", getName() );
  1014. mRowSize.x = getMax(mRowSize.x, 0);
  1015. if( mRowSize.y < 0 )
  1016. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row height. Please change the row height to be non-negative.", getName() );
  1017. mRowSize.y = getMax(mRowSize.y, 0);
  1018. Point2I rowTexExtent = getBitmapArrayRect(TEX_NORMAL).extent;
  1019. mRowScale.x = (float) getRowWidth() / rowTexExtent.x;
  1020. mRowScale.y = (float) getRowHeight() / rowTexExtent.y;
  1021. if (mHitAreaUpperLeft.x > mColumnSplit || mColumnSplit > mHitAreaLowerRight.x)
  1022. Con::errorf("GuiGameListOptionsProfile: You can't create %s with a ColumnSplit outside the hit area. You set the split to %d. Please change the ColumnSplit to be in the range [%d, %d].",
  1023. getName(), mColumnSplit, mHitAreaUpperLeft.x, mHitAreaLowerRight.x);
  1024. mColumnSplit = mClamp(mColumnSplit, mHitAreaUpperLeft.x, mHitAreaLowerRight.x);
  1025. }
  1026. Point2I GuiGameListMenuProfile::getIconExtent()
  1027. {
  1028. Point2I iconExtent = getBitmapArrayRect(TEX_FIRST_ICON).extent;
  1029. // scale both by y to keep the aspect ratio
  1030. iconExtent.x *= mRowScale.y;
  1031. iconExtent.y *= mRowScale.y;
  1032. return iconExtent;
  1033. }
  1034. Point2I GuiGameListMenuProfile::getArrowExtent()
  1035. {
  1036. Point2I arrowExtent = getBitmapArrayRect(TEX_FIRST_ARROW).extent;
  1037. // scale both by y to keep the aspect ratio
  1038. arrowExtent.x *= mRowScale.y;
  1039. arrowExtent.y *= mRowScale.y;
  1040. return arrowExtent;
  1041. }
  1042. Point2I GuiGameListMenuProfile::getHitAreaExtent()
  1043. {
  1044. if (mHitAreaLowerRight == mHitAreaUpperLeft)
  1045. {
  1046. return mRowSize;
  1047. }
  1048. else
  1049. {
  1050. return mHitAreaLowerRight - mHitAreaUpperLeft;
  1051. }
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. // Console stuff (GuiGameListMenuProfile)
  1055. //-----------------------------------------------------------------------------
  1056. IMPLEMENT_CONOBJECT(GuiGameListMenuProfile);
  1057. ConsoleDocClass( GuiGameListMenuProfile,
  1058. "@brief A GuiControlProfile with additional fields specific to GuiGameListMenuCtrl.\n\n"
  1059. "@tsexample\n"
  1060. "new GuiGameListMenuProfile()\n"
  1061. "{\n"
  1062. " hitAreaUpperLeft = \"10 2\";\n"
  1063. " hitAreaLowerRight = \"190 18\";\n"
  1064. " iconOffset = \"10 2\";\n"
  1065. " rowSize = \"200 20\";\n"
  1066. " columnSplit = \"100\";\n"
  1067. " rightPad = \"4\";\n"
  1068. " //Properties not specific to this control have been omitted from this example.\n"
  1069. "};\n"
  1070. "@endtsexample\n\n"
  1071. "@ingroup GuiGame"
  1072. );
  1073. void GuiGameListMenuProfile::initPersistFields()
  1074. {
  1075. addField( "hitAreaUpperLeft", TypePoint2I, Offset(mHitAreaUpperLeft, GuiGameListMenuProfile),
  1076. "Position of the upper left corner of the row hit area (relative to row's top left corner)" );
  1077. addField( "hitAreaLowerRight", TypePoint2I, Offset(mHitAreaLowerRight, GuiGameListMenuProfile),
  1078. "Position of the lower right corner of the row hit area (relative to row's top left corner)" );
  1079. addField( "iconOffset", TypePoint2I, Offset(mIconOffset, GuiGameListMenuProfile),
  1080. "Offset from the row's top left corner at which to render the row icon" );
  1081. addField( "rowSize", TypePoint2I, Offset(mRowSize, GuiGameListMenuProfile),
  1082. "The base size (\"width height\") of a row" );
  1083. addField("columnSplit", TypeS32, Offset(mColumnSplit, GuiGameListMenuProfile),
  1084. "Padding between the leftmost edge of the control, and the row's left arrow.");
  1085. addField("rightPad", TypeS32, Offset(mRightPad, GuiGameListMenuProfile),
  1086. "Padding between the rightmost edge of the control and the row's right arrow.");
  1087. Parent::initPersistFields();
  1088. removeField("tab");
  1089. removeField("mouseOverSelected");
  1090. removeField("modal");
  1091. removeField("opaque");
  1092. removeField("fillColor");
  1093. removeField("fillColorHL");
  1094. removeField("fillColorNA");
  1095. removeField("border");
  1096. removeField("borderThickness");
  1097. removeField("borderColor");
  1098. removeField("borderColorHL");
  1099. removeField("borderColorNA");
  1100. removeField("bevelColorHL");
  1101. removeField("bevelColorLL");
  1102. removeField("fontColorLink");
  1103. removeField("fontColorLinkHL");
  1104. removeField("justify");
  1105. removeField("returnTab");
  1106. removeField("numbersOnly");
  1107. removeField("cursorColor");
  1108. removeField("profileForChildren");
  1109. }