guiGameListMenuCtrl.cpp 26 KB

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