guiGameListOptionsCtrl.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 "guiGameListOptionsCtrl.h"
  23. #include "gfx/gfxDrawUtil.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "core/strings/stringUnit.h"
  27. //-----------------------------------------------------------------------------
  28. // GuiGameListOptionsCtrl
  29. //-----------------------------------------------------------------------------
  30. GuiGameListOptionsCtrl::GuiGameListOptionsCtrl()
  31. {
  32. }
  33. GuiGameListOptionsCtrl::~GuiGameListOptionsCtrl()
  34. {
  35. }
  36. bool GuiGameListOptionsCtrl::onAdd()
  37. {
  38. if( !Parent::onAdd() )
  39. return false;
  40. if( !hasValidProfile() )
  41. {
  42. GuiGameListOptionsProfile* profile;
  43. if( !Sim::findObject( "DefaultOptionsMenuProfile", profile ) )
  44. {
  45. Con::errorf( "GuiGameListOptionsCtrl: %s can't be created with a profile of type %s. Please create it with a profile of type GuiGameListOptionsProfile.",
  46. getName(), mProfile->getClassName() );
  47. return false;
  48. }
  49. else
  50. Con::warnf( "GuiGameListOptionsCtrl: substituted non-GuiGameListOptionsProfile in %s for DefaultOptionsMenuProfile", getName() );
  51. setControlProfile( profile );
  52. }
  53. return true;
  54. }
  55. void GuiGameListOptionsCtrl::onRender(Point2I offset, const RectI &updateRect)
  56. {
  57. Parent::onRender(offset, updateRect);
  58. GuiGameListOptionsProfile * profile = (GuiGameListOptionsProfile *) mProfile;
  59. F32 xScale = (float) getWidth() / profile->getRowWidth();
  60. S32 rowHeight = profile->getRowHeight();
  61. bool profileHasArrows = profile->hasArrows();
  62. Point2I arrowExtent;
  63. S32 arrowOffsetY(0);
  64. if (profileHasArrows)
  65. {
  66. arrowExtent = profile->getArrowExtent();
  67. // icon is centered vertically
  68. arrowOffsetY = (rowHeight - arrowExtent.y) >> 1;
  69. }
  70. GFXDrawUtil *drawer = GFX->getDrawUtil();
  71. Point2I currentOffset = offset;
  72. Point2I arrowOffset;
  73. S32 columnSplit = profile->mColumnSplit * xScale;
  74. S32 iconIndex;
  75. for (Vector<Parent::Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  76. {
  77. Row * myRow = (Row *) *row;
  78. if (row != mRows.begin())
  79. {
  80. // rows other than the first can have padding above them
  81. currentOffset.y += myRow->mHeightPad;
  82. currentOffset.y += rowHeight;
  83. }
  84. bool hasOptions = (myRow->mOptions.size() > 0) && myRow->mSelectedOption > -1;
  85. if (hasOptions)
  86. {
  87. bool isRowSelected = (getSelected() != NO_ROW) && (row == &mRows[getSelected()]);
  88. bool isRowHighlighted = (getHighlighted() != NO_ROW) ? ((row == &mRows[getHighlighted()]) && ((*row)->mEnabled)) : false;
  89. if (profileHasArrows)
  90. {
  91. // render the left arrow
  92. bool arrowOnL = (isRowSelected || isRowHighlighted) && (myRow->mWrapOptions || (myRow->mSelectedOption > 0));
  93. iconIndex = (arrowOnL) ? Profile::TEX_L_ARROW_ON : Profile::TEX_L_ARROW_OFF;
  94. arrowOffset.x = currentOffset.x + columnSplit;
  95. arrowOffset.y = currentOffset.y + arrowOffsetY;
  96. drawer->clearBitmapModulation();
  97. drawer->drawBitmapStretchSR(profile->getBitmapResource(), RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  98. // render the right arrow
  99. bool arrowOnR = (isRowSelected || isRowHighlighted) && (myRow->mWrapOptions || (myRow->mSelectedOption < myRow->mOptions.size() - 1));
  100. iconIndex = (arrowOnR) ? Profile::TEX_R_ARROW_ON : Profile::TEX_R_ARROW_OFF;
  101. arrowOffset.x = currentOffset.x + (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale - arrowExtent.x;
  102. arrowOffset.y = currentOffset.y + arrowOffsetY;
  103. drawer->clearBitmapModulation();
  104. drawer->drawBitmapStretchSR(profile->getBitmapResource(), RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  105. }
  106. // get the appropriate font color
  107. ColorI fontColor;
  108. if (! myRow->mEnabled)
  109. {
  110. fontColor = profile->mFontColorNA;
  111. }
  112. else if (isRowSelected)
  113. {
  114. fontColor = profile->mFontColorSEL;
  115. }
  116. else if (isRowHighlighted)
  117. {
  118. fontColor = profile->mFontColorHL;
  119. }
  120. else
  121. {
  122. fontColor = profile->mFontColor;
  123. }
  124. // calculate text to be at the center between the arrows
  125. GFont * font = profile->mFont;
  126. StringTableEntry text = myRow->mOptions[myRow->mSelectedOption];
  127. S32 textWidth = font->getStrWidth(text);
  128. S32 columnWidth = profile->mHitAreaLowerRight.x * xScale - profile->mRightPad - columnSplit;
  129. S32 columnCenter = columnSplit + (columnWidth >> 1);
  130. S32 textStartX = columnCenter - (textWidth >> 1);
  131. Point2I textOffset(textStartX, 0);
  132. // render the option text itself
  133. Point2I textExtent(columnWidth, rowHeight);
  134. drawer->setBitmapModulation(fontColor);
  135. renderJustifiedText(currentOffset + textOffset, textExtent, text);
  136. }
  137. }
  138. }
  139. void GuiGameListOptionsCtrl::onDebugRender(Point2I offset)
  140. {
  141. Parent::onDebugRender(offset);
  142. GuiGameListOptionsProfile * profile = (GuiGameListOptionsProfile *) mProfile;
  143. F32 xScale = (float) getWidth() / profile->getRowWidth();
  144. ColorI column1Color(255, 255, 0); // yellow
  145. ColorI column2Color(0, 255, 0); // green
  146. Point2I shrinker(-1, -1);
  147. S32 rowHeight = profile->getRowHeight();
  148. Point2I currentOffset(offset);
  149. Point2I rowExtent(getExtent().x, rowHeight);
  150. Point2I hitAreaExtent(profile->getHitAreaExtent());
  151. hitAreaExtent.x *= xScale;
  152. hitAreaExtent += shrinker;
  153. Point2I column1Extent((profile->mColumnSplit - profile->mHitAreaUpperLeft.x) * xScale - 1, hitAreaExtent.y);
  154. Point2I column2Extent(hitAreaExtent.x - column1Extent.x - 1, hitAreaExtent.y);
  155. Point2I hitAreaOffset = profile->mHitAreaUpperLeft;
  156. hitAreaOffset.x *= xScale;
  157. RectI borderRect;
  158. Point2I upperLeft;
  159. for (Vector<Parent::Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  160. {
  161. // set the top of the current row
  162. if (row != mRows.begin())
  163. {
  164. // rows other than the first can have padding above them
  165. currentOffset.y += (*row)->mHeightPad;
  166. currentOffset.y += rowHeight;
  167. }
  168. // draw the box around column 1
  169. upperLeft = currentOffset + hitAreaOffset;
  170. borderRect.point = upperLeft;
  171. borderRect.extent = column1Extent;
  172. GFX->getDrawUtil()->drawRect(borderRect, column1Color);
  173. // draw the box around column 2
  174. upperLeft.x += column1Extent.x + 1;
  175. borderRect.point = upperLeft;
  176. borderRect.extent = column2Extent;
  177. GFX->getDrawUtil()->drawRect(borderRect, column2Color);
  178. }
  179. }
  180. void GuiGameListOptionsCtrl::addRow(const char* label, const char* optionsList, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled)
  181. {
  182. static StringTableEntry DELIM = StringTable->insert("\t", true);
  183. Row * row = new Row();
  184. Vector<StringTableEntry> options( __FILE__, __LINE__ );
  185. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  186. for (S32 i = 0; i < count; ++i)
  187. {
  188. const char * option = StringUnit::getUnit(optionsList, i, DELIM);
  189. options.push_back(StringTable->insert(option, true));
  190. }
  191. row->mOptions = options;
  192. bool hasOptions = row->mOptions.size() > 0;
  193. row->mSelectedOption = (hasOptions) ? 0 : NO_OPTION;
  194. row->mWrapOptions = wrapOptions;
  195. Parent::addRow(row, label, callback, icon, yPad, true, (hasOptions) ? enabled : false);
  196. }
  197. void GuiGameListOptionsCtrl::setOptions(S32 rowIndex, const char * optionsList)
  198. {
  199. static StringTableEntry DELIM = StringTable->insert("\t", true);
  200. if (! isValidRowIndex(rowIndex))
  201. {
  202. return;
  203. }
  204. Row * row = (Row *)mRows[rowIndex];
  205. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  206. row->mOptions.setSize(count);
  207. for (S32 i = 0; i < count; ++i)
  208. {
  209. const char * option = StringUnit::getUnit(optionsList, i, DELIM);
  210. row->mOptions[i] = StringTable->insert(option, true);
  211. }
  212. if (row->mSelectedOption >= row->mOptions.size())
  213. {
  214. row->mSelectedOption = row->mOptions.size() - 1;
  215. }
  216. }
  217. bool GuiGameListOptionsCtrl::hasValidProfile() const
  218. {
  219. GuiGameListOptionsProfile * profile = dynamic_cast<GuiGameListOptionsProfile *>(mProfile);
  220. return profile;
  221. }
  222. void GuiGameListOptionsCtrl::enforceConstraints()
  223. {
  224. Parent::enforceConstraints();
  225. }
  226. void GuiGameListOptionsCtrl::onMouseUp(const GuiEvent &event)
  227. {
  228. S32 hitRow = getRow(event.mousePoint);
  229. if ((hitRow != NO_ROW) && isRowEnabled(hitRow) && (hitRow == getSelected()))
  230. {
  231. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  232. clickOption((Row *) mRows[getSelected()], xPos);
  233. }
  234. }
  235. void GuiGameListOptionsCtrl::clickOption(Row * row, S32 xPos)
  236. {
  237. GuiGameListOptionsProfile * profile = (GuiGameListOptionsProfile *) mProfile;
  238. if (! profile->hasArrows())
  239. {
  240. return;
  241. }
  242. F32 xScale = (float) getWidth() / profile->getRowWidth();
  243. S32 bitmapArrowWidth = mProfile->getBitmapArrayRect(Profile::TEX_FIRST_ARROW).extent.x;
  244. S32 leftArrowX1 = profile->mColumnSplit * xScale;
  245. S32 leftArrowX2 = leftArrowX1 + bitmapArrowWidth;
  246. S32 rightArrowX2 = (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale;
  247. S32 rightArrowX1 = rightArrowX2 - bitmapArrowWidth;
  248. if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  249. {
  250. changeOption(row, -1);
  251. }
  252. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  253. {
  254. changeOption(row, 1);
  255. }
  256. }
  257. bool GuiGameListOptionsCtrl::onKeyDown(const GuiEvent &event)
  258. {
  259. switch (event.keyCode)
  260. {
  261. case KEY_LEFT:
  262. changeOption(-1);
  263. return true;
  264. case KEY_RIGHT:
  265. changeOption(1);
  266. return true;
  267. default:
  268. break;
  269. }
  270. return Parent::onKeyDown(event);
  271. }
  272. bool GuiGameListOptionsCtrl::onGamepadAxisLeft( const GuiEvent &event )
  273. {
  274. changeOption(-1);
  275. return true;
  276. }
  277. bool GuiGameListOptionsCtrl::onGamepadAxisRight( const GuiEvent &event )
  278. {
  279. changeOption(1);
  280. return true;
  281. }
  282. void GuiGameListOptionsCtrl::changeOption(S32 delta)
  283. {
  284. if (getSelected() != NO_ROW)
  285. {
  286. Row * row = (Row *) mRows[getSelected()];
  287. changeOption(row, delta);
  288. }
  289. }
  290. void GuiGameListOptionsCtrl::changeOption(Row * row, S32 delta)
  291. {
  292. S32 optionCount = row->mOptions.size();
  293. S32 newSelection = row->mSelectedOption + delta;
  294. if (optionCount == 0)
  295. {
  296. newSelection = NO_OPTION;
  297. }
  298. else if (! row->mWrapOptions)
  299. {
  300. newSelection = mClamp(newSelection, 0, optionCount - 1);
  301. }
  302. else if (newSelection < 0)
  303. {
  304. newSelection = optionCount - 1;
  305. }
  306. else if (newSelection >= optionCount)
  307. {
  308. newSelection = 0;
  309. }
  310. row->mSelectedOption = newSelection;
  311. static StringTableEntry LEFT = StringTable->insert("LEFT", true);
  312. static StringTableEntry RIGHT = StringTable->insert("RIGHT", true);
  313. if (row->mScriptCallback != NULL)
  314. {
  315. setThisControl();
  316. StringTableEntry direction = NULL;
  317. if (delta < 0)
  318. {
  319. direction = LEFT;
  320. }
  321. else if (delta > 0)
  322. {
  323. direction = RIGHT;
  324. }
  325. if ((direction != NULL) && (Con::isFunction(row->mScriptCallback)))
  326. {
  327. Con::executef(row->mScriptCallback, direction);
  328. }
  329. }
  330. }
  331. StringTableEntry GuiGameListOptionsCtrl::getCurrentOption(S32 rowIndex) const
  332. {
  333. if (isValidRowIndex(rowIndex))
  334. {
  335. Row * row = (Row *) mRows[rowIndex];
  336. if (row->mSelectedOption != NO_OPTION)
  337. {
  338. return row->mOptions[row->mSelectedOption];
  339. }
  340. }
  341. return StringTable->insert("", false);
  342. }
  343. bool GuiGameListOptionsCtrl::selectOption(S32 rowIndex, const char * theOption)
  344. {
  345. if (! isValidRowIndex(rowIndex))
  346. {
  347. return false;
  348. }
  349. Row * row = (Row *) mRows[rowIndex];
  350. for (Vector<StringTableEntry>::iterator anOption = row->mOptions.begin(); anOption < row->mOptions.end(); ++anOption)
  351. {
  352. if (String::compare(*anOption, theOption) == 0)
  353. {
  354. S32 newIndex = anOption - row->mOptions.begin();
  355. row->mSelectedOption = newIndex;
  356. return true;
  357. }
  358. }
  359. return false;
  360. }
  361. void GuiGameListOptionsCtrl::clearRows()
  362. {
  363. mRows.clear();
  364. }
  365. //-----------------------------------------------------------------------------
  366. // Console stuff (GuiGameListOptionsCtrl)
  367. //-----------------------------------------------------------------------------
  368. IMPLEMENT_CONOBJECT(GuiGameListOptionsCtrl);
  369. ConsoleDocClass( GuiGameListOptionsCtrl,
  370. "@brief A control for showing pages of options that are gamepad friendly.\n\n"
  371. "Each row in this control allows the selection of one value from a set of "
  372. "options using the keyboard, gamepad or mouse. The row is rendered as 2 "
  373. "columns: the first column contains the row label, the second column "
  374. "contains left and right arrows (for mouse picking) and the currently "
  375. "selected value.\n\n"
  376. "@see GuiGameListOptionsProfile\n\n"
  377. "@ingroup GuiGame"
  378. );
  379. void GuiGameListOptionsCtrl::initPersistFields()
  380. {
  381. Parent::initPersistFields();
  382. }
  383. DefineEngineMethod( GuiGameListOptionsCtrl, addRow, void,
  384. ( const char* label, const char* options, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled ),
  385. ( -1, 0, true ),
  386. "Add a row to the list control.\n\n"
  387. "@param label The text to display on the row as a label.\n"
  388. "@param options A tab separated list of options.\n"
  389. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  390. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  391. "@param icon [optional] Index of the icon to use as a marker.\n"
  392. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  393. "@param enabled [optional] If this row is initially enabled." )
  394. {
  395. object->addRow( label, options, wrapOptions, callback, icon, yPad, enabled );
  396. }
  397. DefineEngineMethod( GuiGameListOptionsCtrl, getCurrentOption, const char *, ( S32 row ),,
  398. "Gets the text for the currently selected option of the given row.\n\n"
  399. "@param row Index of the row to get the option from.\n"
  400. "@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." )
  401. {
  402. return object->getCurrentOption( row );
  403. }
  404. DefineEngineMethod( GuiGameListOptionsCtrl, selectOption, bool, ( S32 row, const char* option ),,
  405. "Set the row's current option to the one specified\n\n"
  406. "@param row Index of the row to set an option on.\n"
  407. "@param option The option to be made active.\n"
  408. "@return True if the row contained the option and was set, false otherwise." )
  409. {
  410. return object->selectOption( row, option );
  411. }
  412. DefineEngineMethod( GuiGameListOptionsCtrl, setOptions, void, ( S32 row, const char* optionsList ),,
  413. "Sets the list of options on the given row.\n\n"
  414. "@param row Index of the row to set options on."
  415. "@param optionsList A tab separated list of options for the control." )
  416. {
  417. object->setOptions( row, optionsList );
  418. }
  419. DefineEngineMethod(GuiGameListOptionsCtrl, clearOptions, void, (), ,
  420. "Sets the list of options on the given row.\n\n"
  421. "@param row Index of the row to set options on."
  422. "@param optionsList A tab separated list of options for the control.")
  423. {
  424. object->clearRows();
  425. }
  426. //-----------------------------------------------------------------------------
  427. // GuiGameListOptionsProfile
  428. //-----------------------------------------------------------------------------
  429. GuiGameListOptionsProfile::GuiGameListOptionsProfile()
  430. : mColumnSplit(0),
  431. mRightPad(0)
  432. {
  433. }
  434. void GuiGameListOptionsProfile::enforceConstraints()
  435. {
  436. Parent::enforceConstraints();
  437. if( mHitAreaUpperLeft.x > mColumnSplit || mColumnSplit > mHitAreaLowerRight.x )
  438. 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].",
  439. getName(), mColumnSplit, mHitAreaUpperLeft.x, mHitAreaLowerRight.x);
  440. mColumnSplit = mClamp(mColumnSplit, mHitAreaUpperLeft.x, mHitAreaLowerRight.x);
  441. }
  442. //-----------------------------------------------------------------------------
  443. // Console stuff (GuiGameListOptionsProfile)
  444. //-----------------------------------------------------------------------------
  445. IMPLEMENT_CONOBJECT(GuiGameListOptionsProfile);
  446. ConsoleDocClass( GuiGameListOptionsProfile,
  447. "@brief A GuiControlProfile with additional fields specific to GuiGameListOptionsCtrl.\n\n"
  448. "@tsexample\n"
  449. "new GuiGameListOptionsProfile()\n"
  450. "{\n"
  451. " columnSplit = \"100\";\n"
  452. " rightPad = \"4\";\n"
  453. " //Properties not specific to this control have been omitted from this example.\n"
  454. "};\n"
  455. "@endtsexample\n\n"
  456. "@ingroup GuiGame"
  457. );
  458. void GuiGameListOptionsProfile::initPersistFields()
  459. {
  460. addField( "columnSplit", TypeS32, Offset(mColumnSplit, GuiGameListOptionsProfile),
  461. "Padding between the leftmost edge of the control, and the row's left arrow." );
  462. addField( "rightPad", TypeS32, Offset(mRightPad, GuiGameListOptionsProfile),
  463. "Padding between the rightmost edge of the control and the row's right arrow." );
  464. Parent::initPersistFields();
  465. }