guiGameListMenuCtrl.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. //-----------------------------------------------------------------------------
  27. // GuiGameListMenuCtrl
  28. //-----------------------------------------------------------------------------
  29. GuiGameListMenuCtrl::GuiGameListMenuCtrl()
  30. : mSelected(NO_ROW),
  31. mHighlighted(NO_ROW),
  32. mDebugRender(false)
  33. {
  34. VECTOR_SET_ASSOCIATION(mRows);
  35. // initialize the control callbacks
  36. mCallbackOnA = StringTable->EmptyString();
  37. mCallbackOnB = mCallbackOnA;
  38. mCallbackOnX = mCallbackOnA;
  39. mCallbackOnY = mCallbackOnA;
  40. }
  41. GuiGameListMenuCtrl::~GuiGameListMenuCtrl()
  42. {
  43. for (S32 i = 0; i < mRows.size(); ++i)
  44. {
  45. delete mRows[i];
  46. }
  47. }
  48. void GuiGameListMenuCtrl::onRender(Point2I offset, const RectI &updateRect)
  49. {
  50. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  51. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  52. F32 xScale = (float) getWidth() / profile->getRowWidth();
  53. bool profileHasIcons = profile->hasArrows();
  54. S32 rowHeight = profile->getRowHeight();
  55. Point2I currentOffset = offset;
  56. Point2I extent = getExtent();
  57. Point2I rowExtent(extent.x, rowHeight);
  58. Point2I textOffset(profile->mTextOffset.x * xScale, profile->mTextOffset.y);
  59. Point2I textExtent(extent.x - textOffset.x, rowHeight);
  60. Point2I iconExtent, iconOffset(0.0f, 0.0f);
  61. if (profileHasIcons)
  62. {
  63. iconExtent = profile->getIconExtent();
  64. // icon is centered vertically plus any specified offset
  65. S32 iconOffsetY = (rowHeight - iconExtent.y) >> 1;
  66. iconOffsetY += profile->mIconOffset.y;
  67. iconOffset = Point2I(profile->mIconOffset.x * xScale, iconOffsetY);
  68. }
  69. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  70. {
  71. if (row != mRows.begin())
  72. {
  73. // rows other than the first can have padding above them
  74. currentOffset.y += (*row)->mHeightPad;
  75. currentOffset.y += rowHeight;
  76. }
  77. // select appropriate colors and textures
  78. ColorI fontColor;
  79. U32 buttonTextureIndex;
  80. S32 iconIndex = (*row)->mIconIndex;
  81. bool useHighlightIcon = (*row)->mUseHighlightIcon;
  82. if (! (*row)->mEnabled)
  83. {
  84. buttonTextureIndex = Profile::TEX_DISABLED;
  85. fontColor = profile->mFontColorNA;
  86. }
  87. else if (row == &mRows[mSelected])
  88. {
  89. if (iconIndex != NO_ICON)
  90. {
  91. iconIndex++;
  92. }
  93. buttonTextureIndex = Profile::TEX_SELECTED;
  94. fontColor = profile->mFontColorSEL;
  95. }
  96. else if ((mHighlighted != NO_ROW) && (row == &mRows[mHighlighted]))
  97. {
  98. if (iconIndex != NO_ICON && useHighlightIcon)
  99. {
  100. iconIndex++;
  101. }
  102. buttonTextureIndex = Profile::TEX_HIGHLIGHT;
  103. fontColor = profile->mFontColorHL;
  104. }
  105. else
  106. {
  107. buttonTextureIndex = Profile::TEX_NORMAL;
  108. fontColor = profile->mFontColor;
  109. }
  110. // render the row bitmap
  111. drawUtil->clearBitmapModulation();
  112. drawUtil->drawBitmapStretchSR(profile->mTextureObject, RectI(currentOffset, rowExtent), profile->getBitmapArrayRect(buttonTextureIndex));
  113. // render the row icon if it has one
  114. if ((iconIndex != NO_ICON) && profileHasIcons && (! profile->getBitmapArrayRect((U32)iconIndex).extent.isZero()))
  115. {
  116. iconIndex += Profile::TEX_FIRST_ICON;
  117. drawUtil->clearBitmapModulation();
  118. drawUtil->drawBitmapStretchSR(profile->mTextureObject, RectI(currentOffset + iconOffset, iconExtent), profile->getBitmapArrayRect(iconIndex));
  119. }
  120. // render the row text
  121. drawUtil->setBitmapModulation(fontColor);
  122. renderJustifiedText(currentOffset + textOffset, textExtent, (*row)->mLabel);
  123. }
  124. if (mDebugRender)
  125. {
  126. onDebugRender(offset);
  127. }
  128. renderChildControls(offset, updateRect);
  129. }
  130. void GuiGameListMenuCtrl::onDebugRender(Point2I offset)
  131. {
  132. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  133. F32 xScale = (float) getWidth() / profile->getRowWidth();
  134. ColorI controlBorderColor(200, 200, 200); // gray
  135. ColorI rowBorderColor(255, 127, 255); // magenta
  136. ColorI hitBorderColor(255, 0, 0); // red
  137. Point2I shrinker(-1, -1);
  138. Point2I extent = getExtent();
  139. // render a border around the entire control
  140. RectI borderRect(offset, extent + shrinker);
  141. GFX->getDrawUtil()->drawRect(borderRect, controlBorderColor);
  142. S32 rowHeight = profile->getRowHeight();
  143. Point2I currentOffset(offset);
  144. Point2I rowExtent(extent.x, rowHeight);
  145. rowExtent += shrinker;
  146. Point2I hitAreaExtent(profile->getHitAreaExtent());
  147. hitAreaExtent.x *= xScale;
  148. hitAreaExtent += shrinker;
  149. Point2I hitAreaOffset = profile->mHitAreaUpperLeft;
  150. hitAreaOffset.x *= xScale;
  151. Point2I upperLeft;
  152. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  153. {
  154. // set the top of the current row
  155. if (row != mRows.begin())
  156. {
  157. // rows other than the first can have padding above them
  158. currentOffset.y += (*row)->mHeightPad;
  159. currentOffset.y += rowHeight;
  160. }
  161. // draw the box around the whole row's extent
  162. upperLeft = currentOffset;
  163. borderRect.point = upperLeft;
  164. borderRect.extent = rowExtent;
  165. GFX->getDrawUtil()->drawRect(borderRect, rowBorderColor);
  166. // draw the box around the hit area of the row
  167. upperLeft = currentOffset + hitAreaOffset;
  168. borderRect.point = upperLeft;
  169. borderRect.extent = hitAreaExtent;
  170. GFX->getDrawUtil()->drawRect(borderRect, hitBorderColor);
  171. }
  172. }
  173. void GuiGameListMenuCtrl::addRow(const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled)
  174. {
  175. Row * row = new Row();
  176. addRow(row, label, callback, icon, yPad, useHighlightIcon, enabled);
  177. }
  178. void GuiGameListMenuCtrl::addRow(Row * row, const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled)
  179. {
  180. row->mLabel = StringTable->insert(label, true);
  181. row->mScriptCallback = (dStrlen(callback) > 0) ? StringTable->insert(callback, true) : NULL;
  182. row->mIconIndex = (icon < 0) ? NO_ICON : icon;
  183. row->mHeightPad = yPad;
  184. row->mUseHighlightIcon = useHighlightIcon;
  185. row->mEnabled = enabled;
  186. mRows.push_back(row);
  187. updateHeight();
  188. if (mSelected == NO_ROW)
  189. {
  190. selectFirstEnabledRow();
  191. }
  192. }
  193. Point2I GuiGameListMenuCtrl::getMinExtent() const
  194. {
  195. Point2I parentMin = Parent::getMinExtent();
  196. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  197. S32 minHeight = 0;
  198. S32 rowHeight = profile->getRowHeight();
  199. for (Vector<Row *>::const_iterator row = mRows.begin(); row < mRows.end(); ++row)
  200. {
  201. minHeight += rowHeight;
  202. if (row != mRows.begin())
  203. {
  204. minHeight += (*row)->mHeightPad;
  205. }
  206. }
  207. if (minHeight > parentMin.y)
  208. parentMin.y = minHeight;
  209. return parentMin;
  210. }
  211. bool GuiGameListMenuCtrl::onAdd()
  212. {
  213. if( !Parent::onAdd() )
  214. return false;
  215. // If we have a non-GuiGameListMenuProfile profile, try to
  216. // substitute it for DefaultListMenuProfile.
  217. if( !hasValidProfile() )
  218. {
  219. GuiGameListMenuProfile* profile;
  220. if( !Sim::findObject( "DefaultListMenuProfile", profile ) )
  221. {
  222. Con::errorf( "GuiGameListMenuCtrl: %s can't be created with a profile of type %s. Please create it with a profile of type GuiGameListMenuProfile.",
  223. getName(), mProfile->getClassName() );
  224. return false;
  225. }
  226. else
  227. Con::warnf( "GuiGameListMenuCtrl: substituted non-GuiGameListMenuProfile in %s for DefaultListMenuProfile", getName() );
  228. setControlProfile( profile );
  229. }
  230. return true;
  231. }
  232. bool GuiGameListMenuCtrl::onWake()
  233. {
  234. if( !Parent::onWake() )
  235. return false;
  236. if( !hasValidProfile() )
  237. return false;
  238. if( mRows.empty() )
  239. {
  240. 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.",
  241. getName() );
  242. return false;
  243. }
  244. enforceConstraints();
  245. selectFirstEnabledRow();
  246. setFirstResponder();
  247. mHighlighted = NO_ROW;
  248. return true;
  249. }
  250. bool GuiGameListMenuCtrl::hasValidProfile() const
  251. {
  252. GuiGameListMenuProfile * profile = dynamic_cast<GuiGameListMenuProfile *>(mProfile);
  253. return profile;
  254. }
  255. void GuiGameListMenuCtrl::enforceConstraints()
  256. {
  257. if( hasValidProfile() )
  258. {
  259. ((GuiGameListMenuProfile *)mProfile)->enforceConstraints();
  260. }
  261. updateHeight();
  262. }
  263. void GuiGameListMenuCtrl::updateHeight()
  264. {
  265. S32 minHeight = getMinExtent().y;
  266. if (getHeight() < minHeight)
  267. {
  268. setHeight(minHeight);
  269. }
  270. }
  271. void GuiGameListMenuCtrl::onMouseDown(const GuiEvent &event)
  272. {
  273. S32 hitRow = getRow(event.mousePoint);
  274. if (hitRow != NO_ROW)
  275. {
  276. S32 delta = (mSelected != NO_ROW) ? (hitRow - mSelected) : (mSelected + 1);
  277. changeRow(delta);
  278. }
  279. }
  280. void GuiGameListMenuCtrl::onMouseLeave(const GuiEvent &event)
  281. {
  282. mHighlighted = NO_ROW;
  283. }
  284. void GuiGameListMenuCtrl::onMouseMove(const GuiEvent &event)
  285. {
  286. S32 hitRow = getRow(event.mousePoint);
  287. // allow mHighligetd to be set to NO_ROW so rows can be unhighlighted
  288. mHighlighted = hitRow;
  289. }
  290. void GuiGameListMenuCtrl::onMouseUp(const GuiEvent &event)
  291. {
  292. S32 hitRow = getRow(event.mousePoint);
  293. if ((hitRow != NO_ROW) && isRowEnabled(hitRow) && (hitRow == getSelected()))
  294. {
  295. activateRow();
  296. }
  297. }
  298. void GuiGameListMenuCtrl::activateRow()
  299. {
  300. S32 row = getSelected();
  301. if ((row != NO_ROW) && isRowEnabled(row) && (mRows[row]->mScriptCallback != NULL))
  302. {
  303. setThisControl();
  304. if (Con::isFunction(mRows[row]->mScriptCallback))
  305. {
  306. Con::executef(mRows[row]->mScriptCallback);
  307. }
  308. }
  309. }
  310. S32 GuiGameListMenuCtrl::getRow(Point2I globalPoint)
  311. {
  312. Point2I localPoint = globalToLocalCoord(globalPoint);
  313. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  314. F32 xScale = (float) getWidth() / profile->getRowWidth();
  315. S32 rowHeight = profile->getRowHeight();
  316. Point2I currentOffset(0, 0);
  317. Point2I hitAreaUpperLeft = profile->mHitAreaUpperLeft;
  318. hitAreaUpperLeft.x *= xScale;
  319. Point2I hitAreaLowerRight = profile->mHitAreaLowerRight;
  320. hitAreaLowerRight.x *= xScale;
  321. Point2I upperLeft, lowerRight;
  322. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  323. {
  324. if (row != mRows.begin())
  325. {
  326. // rows other than the first can have padding above them
  327. currentOffset.y += (*row)->mHeightPad;
  328. }
  329. upperLeft = currentOffset + hitAreaUpperLeft;
  330. lowerRight = currentOffset + hitAreaLowerRight;
  331. if ((upperLeft.x <= localPoint.x) && (localPoint.x < lowerRight.x) &&
  332. (upperLeft.y <= localPoint.y) && (localPoint.y < lowerRight.y))
  333. {
  334. return row - mRows.begin();
  335. }
  336. currentOffset.y += rowHeight;
  337. }
  338. return NO_ROW;
  339. }
  340. void GuiGameListMenuCtrl::setSelected(S32 index)
  341. {
  342. if (index == NO_ROW)
  343. {
  344. // deselection
  345. mSelected = NO_ROW;
  346. return;
  347. }
  348. if (! isValidRowIndex(index))
  349. {
  350. return;
  351. }
  352. if (! isRowEnabled(index))
  353. {
  354. // row is disabled, it can't be selected
  355. return;
  356. }
  357. mSelected = mClamp(index, 0, mRows.size() - 1);
  358. }
  359. bool GuiGameListMenuCtrl::isRowEnabled(S32 index) const
  360. {
  361. if (! isValidRowIndex(index))
  362. {
  363. return false;
  364. }
  365. return mRows[index]->mEnabled;
  366. }
  367. void GuiGameListMenuCtrl::setRowEnabled(S32 index, bool enabled)
  368. {
  369. if (! isValidRowIndex(index))
  370. {
  371. return;
  372. }
  373. mRows[index]->mEnabled = enabled;
  374. if (getSelected() == index)
  375. {
  376. selectFirstEnabledRow();
  377. }
  378. }
  379. bool GuiGameListMenuCtrl::isValidRowIndex(S32 index) const
  380. {
  381. return ((0 <= index) && (index < mRows.size()));
  382. }
  383. void GuiGameListMenuCtrl::selectFirstEnabledRow()
  384. {
  385. setSelected(NO_ROW);
  386. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  387. {
  388. if ((*row)->mEnabled)
  389. {
  390. setSelected(row - mRows.begin());
  391. return;
  392. }
  393. }
  394. }
  395. bool GuiGameListMenuCtrl::onKeyDown(const GuiEvent &event)
  396. {
  397. switch (event.keyCode)
  398. {
  399. case KEY_UP:
  400. changeRow(-1);
  401. return true;
  402. case KEY_DOWN:
  403. changeRow(1);
  404. return true;
  405. case KEY_A:
  406. case KEY_RETURN:
  407. case KEY_NUMPADENTER:
  408. case KEY_SPACE:
  409. case XI_A:
  410. case XI_START:
  411. doScriptCommand(mCallbackOnA);
  412. return true;
  413. case KEY_B:
  414. case KEY_ESCAPE:
  415. case KEY_BACKSPACE:
  416. case KEY_DELETE:
  417. case XI_B:
  418. case XI_BACK:
  419. doScriptCommand(mCallbackOnB);
  420. return true;
  421. case KEY_X:
  422. case XI_X:
  423. doScriptCommand(mCallbackOnX);
  424. return true;
  425. case KEY_Y:
  426. case XI_Y:
  427. doScriptCommand(mCallbackOnY);
  428. return true;
  429. default:
  430. break;
  431. }
  432. return Parent::onKeyDown(event);
  433. }
  434. bool GuiGameListMenuCtrl::onGamepadAxisUp(const GuiEvent &event)
  435. {
  436. changeRow(-1);
  437. return true;
  438. }
  439. bool GuiGameListMenuCtrl::onGamepadAxisDown(const GuiEvent &event)
  440. {
  441. changeRow(1);
  442. return true;
  443. }
  444. void GuiGameListMenuCtrl::doScriptCommand(StringTableEntry command)
  445. {
  446. if (command && command[0])
  447. {
  448. setThisControl();
  449. Con::evaluate(command, false, __FILE__);
  450. }
  451. }
  452. void GuiGameListMenuCtrl::changeRow(S32 delta)
  453. {
  454. S32 oldRowIndex = getSelected();
  455. S32 newRowIndex = oldRowIndex;
  456. do
  457. {
  458. newRowIndex += delta;
  459. if (newRowIndex >= mRows.size())
  460. {
  461. newRowIndex = 0;
  462. }
  463. else if (newRowIndex < 0)
  464. {
  465. newRowIndex = mRows.size() - 1;
  466. }
  467. }
  468. while ((! mRows[newRowIndex]->mEnabled) && (newRowIndex != oldRowIndex));
  469. setSelected(newRowIndex);
  470. // do the callback
  471. onChange_callback();
  472. }
  473. void GuiGameListMenuCtrl::setThisControl()
  474. {
  475. smThisControl = this;
  476. }
  477. StringTableEntry GuiGameListMenuCtrl::getRowLabel(S32 rowIndex) const
  478. {
  479. 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()));
  480. if (! isValidRowIndex(rowIndex))
  481. {
  482. // not a valid row index, don't do anything
  483. return StringTable->EmptyString();
  484. }
  485. return mRows[rowIndex]->mLabel;
  486. }
  487. void GuiGameListMenuCtrl::setRowLabel(S32 rowIndex, const char * label)
  488. {
  489. 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()));
  490. if (! isValidRowIndex(rowIndex))
  491. {
  492. // not a valid row index, don't do anything
  493. return;
  494. }
  495. mRows[rowIndex]->mLabel = StringTable->insert(label, true);
  496. }
  497. //-----------------------------------------------------------------------------
  498. // Console stuff (GuiGameListMenuCtrl)
  499. //-----------------------------------------------------------------------------
  500. IMPLEMENT_CONOBJECT(GuiGameListMenuCtrl);
  501. ConsoleDocClass( GuiGameListMenuCtrl,
  502. "@brief A base class for cross platform menu controls that are gamepad friendly.\n\n"
  503. "This class is used to build row-based menu GUIs that can be easily navigated "
  504. "using the keyboard, mouse or gamepad. The desired row can be selected using "
  505. "the mouse, or by navigating using the Up and Down buttons.\n\n"
  506. "@tsexample\n\n"
  507. "new GuiGameListMenuCtrl()\n"
  508. "{\n"
  509. " debugRender = \"0\";\n"
  510. " callbackOnA = \"applyOptions();\";\n"
  511. " callbackOnB = \"Canvas.setContent(MainMenuGui);\";\n"
  512. " callbackOnX = \"\";\n"
  513. " callbackOnY = \"revertOptions();\";\n"
  514. " //Properties not specific to this control have been omitted from this example.\n"
  515. "};\n"
  516. "@endtsexample\n\n"
  517. "@see GuiGameListMenuProfile\n\n"
  518. "@ingroup GuiGame"
  519. );
  520. IMPLEMENT_CALLBACK( GuiGameListMenuCtrl, onChange, void, (), (),
  521. "Called when the selected row changes." );
  522. void GuiGameListMenuCtrl::initPersistFields()
  523. {
  524. addField("debugRender", TypeBool, Offset(mDebugRender, GuiGameListMenuCtrl),
  525. "Enable debug rendering" );
  526. addField("callbackOnA", TypeString, Offset(mCallbackOnA, GuiGameListMenuCtrl),
  527. "Script callback when the 'A' button is pressed. 'A' inputs are Keyboard: A, Return, Space; Gamepad: A, Start" );
  528. addField("callbackOnB", TypeString, Offset(mCallbackOnB, GuiGameListMenuCtrl),
  529. "Script callback when the 'B' button is pressed. 'B' inputs are Keyboard: B, Esc, Backspace, Delete; Gamepad: B, Back" );
  530. addField("callbackOnX", TypeString, Offset(mCallbackOnX, GuiGameListMenuCtrl),
  531. "Script callback when the 'X' button is pressed. 'X' inputs are Keyboard: X; Gamepad: X" );
  532. addField("callbackOnY", TypeString, Offset(mCallbackOnY, GuiGameListMenuCtrl),
  533. "Script callback when the 'Y' button is pressed. 'Y' inputs are Keyboard: Y; Gamepad: Y" );
  534. Parent::initPersistFields();
  535. }
  536. DefineEngineMethod( GuiGameListMenuCtrl, addRow, void,
  537. ( const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled ),
  538. ( -1, 0, true, true ),
  539. "Add a row to the list control.\n\n"
  540. "@param label The text to display on the row as a label.\n"
  541. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  542. "@param icon [optional] Index of the icon to use as a marker.\n"
  543. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  544. "@param useHighlightIcon [optional] Does this row use the highlight icon?.\n"
  545. "@param enabled [optional] If this row is initially enabled." )
  546. {
  547. object->addRow( label, callback, icon, yPad, useHighlightIcon, enabled );
  548. }
  549. DefineEngineMethod( GuiGameListMenuCtrl, isRowEnabled, bool, ( S32 row ),,
  550. "Determines if the specified row is enabled or disabled.\n\n"
  551. "@param row The row to set the enabled status of.\n"
  552. "@return True if the specified row is enabled. False if the row is not enabled or the given index was not valid." )
  553. {
  554. return object->isRowEnabled( row );
  555. }
  556. DefineEngineMethod( GuiGameListMenuCtrl, setRowEnabled, void, ( S32 row, bool enabled ),,
  557. "Sets a row's enabled status according to the given parameters.\n\n"
  558. "@param row The index to check for validity.\n"
  559. "@param enabled Indicate true to enable the row or false to disable it." )
  560. {
  561. object->setRowEnabled( row, enabled );
  562. }
  563. DefineEngineMethod( GuiGameListMenuCtrl, activateRow, void, (),,
  564. "Activates the current row. The script callback of the current row will be called (if it has one)." )
  565. {
  566. object->activateRow();
  567. }
  568. DefineEngineMethod( GuiGameListMenuCtrl, getRowCount, S32, (),,
  569. "Gets the number of rows on the control.\n\n"
  570. "@return (int) The number of rows on the control." )
  571. {
  572. return object->getRowCount();
  573. }
  574. DefineEngineMethod( GuiGameListMenuCtrl, getRowLabel, const char *, ( S32 row ),,
  575. "Gets the label displayed on the specified row.\n\n"
  576. "@param row Index of the row to get the label of.\n"
  577. "@return The label for the row." )
  578. {
  579. return object->getRowLabel( row );
  580. }
  581. DefineEngineMethod( GuiGameListMenuCtrl, setRowLabel, void, ( S32 row, const char* label ),,
  582. "Sets the label on the given row.\n\n"
  583. "@param row Index of the row to set the label on.\n"
  584. "@param label Text to set as the label of the row.\n" )
  585. {
  586. object->setRowLabel( row, label );
  587. }
  588. DefineEngineMethod( GuiGameListMenuCtrl, setSelected, void, ( S32 row ),,
  589. "Sets the selected row. Only rows that are enabled can be selected.\n\n"
  590. "@param row Index of the row to set as selected." )
  591. {
  592. object->setSelected( row );
  593. }
  594. DefineEngineMethod( GuiGameListMenuCtrl, getSelectedRow, S32, (),,
  595. "Gets the index of the currently selected row.\n\n"
  596. "@return Index of the selected row." )
  597. {
  598. return object->getSelected();
  599. }
  600. //-----------------------------------------------------------------------------
  601. // GuiGameListMenuProfile
  602. //-----------------------------------------------------------------------------
  603. GuiGameListMenuProfile::GuiGameListMenuProfile()
  604. : mHitAreaUpperLeft(0, 0),
  605. mHitAreaLowerRight(0, 0),
  606. mIconOffset(0, 0),
  607. mRowSize(0, 0),
  608. mRowScale(1.0f, 1.0f)
  609. {
  610. }
  611. bool GuiGameListMenuProfile::onAdd()
  612. {
  613. if (! Parent::onAdd())
  614. {
  615. return false;
  616. }
  617. // We can't call enforceConstraints() here because incRefCount initializes
  618. // some of the things to enforce. Do a basic sanity check here instead.
  619. if( !mBitmapName || !dStrlen(mBitmapName) )
  620. {
  621. Con::errorf( "GuiGameListMenuProfile: %s can't be created without a bitmap. Please add a 'Bitmap' property to the object definition.", getName() );
  622. return false;
  623. }
  624. if( mRowSize.x < 0 )
  625. {
  626. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row width. Please change the row width to be non-negative.", getName() );
  627. return false;
  628. }
  629. if( mRowSize.y < 0 )
  630. {
  631. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row height. Please change the row height to be non-negative.", getName() );
  632. return false;
  633. }
  634. return true;
  635. }
  636. void GuiGameListMenuProfile::enforceConstraints()
  637. {
  638. if( getBitmapArrayRect(0).extent.isZero() )
  639. Con::errorf( "GuiGameListMenuCtrl: %s can't be created without a bitmap. Please add a bitmap to the profile's definition.", getName() );
  640. if( mRowSize.x < 0 )
  641. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row width. Please change the row width to be non-negative.", getName() );
  642. mRowSize.x = getMax(mRowSize.x, 0);
  643. if( mRowSize.y < 0 )
  644. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row height. Please change the row height to be non-negative.", getName() );
  645. mRowSize.y = getMax(mRowSize.y, 0);
  646. Point2I rowTexExtent = getBitmapArrayRect(TEX_NORMAL).extent;
  647. mRowScale.x = (float) getRowWidth() / rowTexExtent.x;
  648. mRowScale.y = (float) getRowHeight() / rowTexExtent.y;
  649. }
  650. Point2I GuiGameListMenuProfile::getIconExtent()
  651. {
  652. Point2I iconExtent = getBitmapArrayRect(TEX_FIRST_ICON).extent;
  653. // scale both by y to keep the aspect ratio
  654. iconExtent.x *= mRowScale.y;
  655. iconExtent.y *= mRowScale.y;
  656. return iconExtent;
  657. }
  658. Point2I GuiGameListMenuProfile::getArrowExtent()
  659. {
  660. Point2I arrowExtent = getBitmapArrayRect(TEX_FIRST_ARROW).extent;
  661. // scale both by y to keep the aspect ratio
  662. arrowExtent.x *= mRowScale.y;
  663. arrowExtent.y *= mRowScale.y;
  664. return arrowExtent;
  665. }
  666. Point2I GuiGameListMenuProfile::getHitAreaExtent()
  667. {
  668. if (mHitAreaLowerRight == mHitAreaUpperLeft)
  669. {
  670. return mRowSize;
  671. }
  672. else
  673. {
  674. return mHitAreaLowerRight - mHitAreaUpperLeft;
  675. }
  676. }
  677. //-----------------------------------------------------------------------------
  678. // Console stuff (GuiGameListMenuProfile)
  679. //-----------------------------------------------------------------------------
  680. IMPLEMENT_CONOBJECT(GuiGameListMenuProfile);
  681. ConsoleDocClass( GuiGameListMenuProfile,
  682. "@brief A GuiControlProfile with additional fields specific to GuiGameListMenuCtrl.\n\n"
  683. "@tsexample\n"
  684. "new GuiGameListMenuProfile()\n"
  685. "{\n"
  686. " hitAreaUpperLeft = \"10 2\";\n"
  687. " hitAreaLowerRight = \"190 18\";\n"
  688. " iconOffset = \"10 2\";\n"
  689. " rowSize = \"200 20\";\n"
  690. " //Properties not specific to this control have been omitted from this example.\n"
  691. "};\n"
  692. "@endtsexample\n\n"
  693. "@ingroup GuiGame"
  694. );
  695. void GuiGameListMenuProfile::initPersistFields()
  696. {
  697. addField( "hitAreaUpperLeft", TypePoint2I, Offset(mHitAreaUpperLeft, GuiGameListMenuProfile),
  698. "Position of the upper left corner of the row hit area (relative to row's top left corner)" );
  699. addField( "hitAreaLowerRight", TypePoint2I, Offset(mHitAreaLowerRight, GuiGameListMenuProfile),
  700. "Position of the lower right corner of the row hit area (relative to row's top left corner)" );
  701. addField( "iconOffset", TypePoint2I, Offset(mIconOffset, GuiGameListMenuProfile),
  702. "Offset from the row's top left corner at which to render the row icon" );
  703. addField( "rowSize", TypePoint2I, Offset(mRowSize, GuiGameListMenuProfile),
  704. "The base size (\"width height\") of a row" );
  705. Parent::initPersistFields();
  706. removeField("tab");
  707. removeField("mouseOverSelected");
  708. removeField("modal");
  709. removeField("opaque");
  710. removeField("fillColor");
  711. removeField("fillColorHL");
  712. removeField("fillColorNA");
  713. removeField("border");
  714. removeField("borderThickness");
  715. removeField("borderColor");
  716. removeField("borderColorHL");
  717. removeField("borderColorNA");
  718. removeField("bevelColorHL");
  719. removeField("bevelColorLL");
  720. removeField("fontColorLink");
  721. removeField("fontColorLinkHL");
  722. removeField("justify");
  723. removeField("returnTab");
  724. removeField("numbersOnly");
  725. removeField("cursorColor");
  726. removeField("profileForChildren");
  727. }