guiGameSettingsCtrl.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 "guiGameSettingsCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "console/script.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "gui/containers/guiScrollCtrl.h"
  28. #include "core/strings/stringUnit.h"
  29. #include "gui/core/guiDefaultControlRender.h"
  30. #include "console/typeValidators.h"
  31. //-----------------------------------------------------------------------------
  32. // GuiGameSettingsCtrl
  33. //-----------------------------------------------------------------------------
  34. GuiGameSettingsCtrl::GuiGameSettingsCtrl() :
  35. mLabel(StringTable->EmptyString()),
  36. mScriptCallback(StringTable->EmptyString()),
  37. mTooltip(StringTable->EmptyString()),
  38. mEnabled(true),
  39. mSelected(false),
  40. mSelectedOption(0),
  41. mWrapOptions(false),
  42. mMode(Mode::Default),
  43. mValue(0),
  44. mStepSize(1),
  45. mRange(Point2F(0, 1)),
  46. mCallbackOnInputs(false),
  47. mConsumeKeyInputEvents(false),
  48. mArrowSize(30),
  49. mColumnSplit(250),
  50. mRightPad(20)
  51. {
  52. VECTOR_SET_ASSOCIATION(mOptions);
  53. // initialize the control callbacks
  54. mCallbackOnA = StringTable->EmptyString();
  55. mCallbackOnB = mCallbackOnA;
  56. mCallbackOnX = mCallbackOnA;
  57. mCallbackOnY = mCallbackOnA;
  58. INIT_ASSET(KeybindBitmap);
  59. INIT_ASSET(PreviousBitmap);
  60. INIT_ASSET(NextBitmap);
  61. }
  62. GuiGameSettingsCtrl::~GuiGameSettingsCtrl()
  63. {
  64. mOptions.clear();
  65. }
  66. void GuiGameSettingsCtrl::onMouseMove(const GuiEvent& event)
  67. {
  68. //check if we're inside an arrow/slider/etc and kick a highlight action
  69. Parent::onMouseMove(event);
  70. }
  71. void GuiGameSettingsCtrl::onMouseUp(const GuiEvent& event)
  72. {
  73. Parent::onMouseUp(event);
  74. if (isEnabled())
  75. {
  76. if (mMode == Mode::Default)
  77. {
  78. activate();
  79. }
  80. else if (mMode == Mode::OptionList)
  81. {
  82. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  83. clickOption(xPos);
  84. }
  85. else if (mMode == Mode::Slider)
  86. {
  87. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  88. clickSlider(xPos);
  89. }
  90. else if (mMode == Mode::Keybind)
  91. {
  92. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  93. clickKeybind(xPos);
  94. }
  95. }
  96. }
  97. void GuiGameSettingsCtrl::onRender(Point2I offset, const RectI &updateRect)
  98. {
  99. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  100. F32 xScale = (float) getWidth();
  101. S32 height = getHeight();
  102. Point2I currentOffset = offset;
  103. Point2I extent = getExtent();
  104. Point2I textOffset(mProfile->mTextOffset.x * xScale, mProfile->mTextOffset.y);
  105. Point2I textExtent(mColumnSplit, height);
  106. Point2I iconExtent, iconOffset(0.0f, 0.0f);
  107. bool highlight = mHighlighted;
  108. ColorI fontColor = mActive ? (highlight ? mProfile->mFontColorHL : mProfile->mFontColor) : mProfile->mFontColorNA;
  109. ColorI fillColor = mActive ? (highlight ? mProfile->mFillColorHL : mProfile->mFillColor) : mProfile->mFillColorNA;
  110. ColorI borderColor = mActive ? (highlight ? mProfile->mBorderColorHL : mProfile->mBorderColor) : mProfile->mBorderColorNA;
  111. RectI boundsRect(offset, getExtent());
  112. if (!mHasTheme)
  113. {
  114. if (mProfile->mBorder != 0)
  115. renderFilledBorder(boundsRect, borderColor, fillColor, mProfile->mBorderThickness);
  116. else
  117. GFX->getDrawUtil()->drawRectFill(boundsRect, fillColor);
  118. }
  119. else
  120. {
  121. S32 indexMultiplier = 1;
  122. if (!mActive)
  123. indexMultiplier = 4;
  124. else if (mDepressed || mStateOn)
  125. indexMultiplier = 2;
  126. else if (mHighlighted)
  127. indexMultiplier = 3;
  128. renderSizableBitmapBordersFilled(boundsRect, indexMultiplier, mProfile);
  129. }
  130. // render the text
  131. drawUtil->setBitmapModulation(fontColor);
  132. renderJustifiedText(currentOffset + textOffset, textExtent, mLabel);
  133. if (mMode == Mode::OptionList)
  134. {
  135. onRenderListOption(currentOffset);
  136. }
  137. else if (mMode == Mode::Slider)
  138. {
  139. onRenderSliderOption(currentOffset);
  140. }
  141. else if (mMode == Mode::Keybind)
  142. {
  143. onRenderKeybindOption(currentOffset);
  144. }
  145. renderChildControls(offset, updateRect);
  146. }
  147. void GuiGameSettingsCtrl::onRenderListOption(Point2I currentOffset)
  148. {
  149. F32 xScale = (float)getWidth();
  150. S32 height = getHeight();
  151. GFXDrawUtil* drawer = GFX->getDrawUtil();
  152. Point2I arrowOffset;
  153. S32 arrowOffsetY = 0;
  154. bool hasOptions = (mOptions.size() > 0) && mSelectedOption > -1;
  155. if (hasOptions)
  156. {
  157. // do we render the left or right arrows?
  158. bool arrowOnL = (isSelected() || isHighlighted()) && (mWrapOptions || (mSelectedOption > 0));
  159. bool arrowOnR = (isSelected() || isHighlighted()) && (mWrapOptions || (mSelectedOption < mOptions.size() - 1));
  160. if (arrowOnL)
  161. {
  162. if (mPreviousBitmapAsset.notNull())
  163. {
  164. arrowOffset.x = currentOffset.x + mColumnSplit;
  165. arrowOffset.y = currentOffset.y + arrowOffsetY;
  166. drawer->clearBitmapModulation();
  167. drawer->drawBitmapStretch(mPreviousBitmap, RectI(arrowOffset, Point2I(mArrowSize, mArrowSize)), GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  168. }
  169. else
  170. {
  171. arrowOffset.x = currentOffset.x + mColumnSplit;
  172. arrowOffset.y = currentOffset.y + height / 2;
  173. drawer->clearBitmapModulation();
  174. drawer->drawLine(arrowOffset, Point2I(arrowOffset.x + mArrowSize, currentOffset.y), ColorI::WHITE);
  175. drawer->drawLine(arrowOffset, Point2I(arrowOffset.x + mArrowSize, currentOffset.y + height), ColorI::WHITE);
  176. }
  177. }
  178. if (arrowOnR)
  179. {
  180. if (mNextBitmapAsset.notNull())
  181. {
  182. arrowOffset.x = currentOffset.x + getWidth() - mRightPad - mArrowSize;
  183. arrowOffset.y = currentOffset.y + arrowOffsetY;
  184. drawer->clearBitmapModulation();
  185. drawer->drawBitmapStretch(mNextBitmap, RectI(arrowOffset, Point2I(mArrowSize, mArrowSize)), GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  186. }
  187. else
  188. {
  189. arrowOffset.x = currentOffset.x + getWidth() - mRightPad;
  190. arrowOffset.y = currentOffset.y + height / 2;
  191. drawer->clearBitmapModulation();
  192. drawer->drawLine(arrowOffset, Point2I(arrowOffset.x - mArrowSize, currentOffset.y), ColorI::WHITE);
  193. drawer->drawLine(arrowOffset, Point2I(arrowOffset.x - mArrowSize, currentOffset.y + height), ColorI::WHITE);
  194. }
  195. }
  196. // get the appropriate font color
  197. ColorI fontColor;
  198. if (!mEnabled)
  199. {
  200. fontColor = mProfile->mFontColorNA;
  201. }
  202. else if (isSelected())
  203. {
  204. fontColor = mProfile->mFontColorSEL;
  205. }
  206. else if (isHighlighted())
  207. {
  208. fontColor = mProfile->mFontColorHL;
  209. }
  210. else
  211. {
  212. fontColor = mProfile->mFontColor;
  213. }
  214. // calculate text to be at the center between the arrows
  215. GFont* font = mProfile->mFont;
  216. StringTableEntry text = mOptions[mSelectedOption].mDisplayText;
  217. S32 textWidth = font->getStrWidth(text);
  218. S32 columnWidth = xScale - mRightPad - mColumnSplit;
  219. S32 columnCenter = mColumnSplit + (columnWidth >> 1);
  220. S32 textStartX = columnCenter - (textWidth >> 1);
  221. Point2I textOffset(textStartX, 0);
  222. // render the option text itself
  223. Point2I textExtent(columnWidth, height);
  224. drawer->setBitmapModulation(fontColor);
  225. renderJustifiedText(currentOffset + Point2I(mColumnSplit, 0), textExtent, text);
  226. }
  227. }
  228. void GuiGameSettingsCtrl::onRenderSliderOption(Point2I currentOffset)
  229. {
  230. S32 height = getHeight();
  231. GFXDrawUtil* drawer = GFX->getDrawUtil();
  232. //Point2I arrowOffset;
  233. S32 columnSplit = mColumnSplit;
  234. //Draw the slider bar
  235. RectI sliderRect;
  236. S32 sliderOffset = 5;
  237. RectI optionRect;
  238. sliderRect.point.x = currentOffset.x + columnSplit + mArrowSize;
  239. sliderRect.point.y = currentOffset.y + sliderOffset;
  240. sliderRect.extent.x = (currentOffset.x + getWidth() - mRightPad - mArrowSize) - sliderRect.point.x;
  241. sliderRect.extent.y = height - sliderOffset*2;
  242. optionRect = sliderRect;
  243. S32 textWidth = sliderRect.extent.x * 0.3;
  244. sliderRect.extent.x -= textWidth;
  245. //Now adjust the bar to match-to our value
  246. S32 barStart = sliderRect.point.x;
  247. S32 barEnd = sliderRect.point.x + sliderRect.extent.x;
  248. S32 xPosFill = (((mValue - mRange.x) * (barEnd - barStart)) / (mRange.y - mRange.x)) + barStart;
  249. RectI fillRect = sliderRect;
  250. fillRect.extent.x = xPosFill - sliderRect.point.x;
  251. ColorI barColor;
  252. ColorI barOutlineColor;
  253. if (isSelected())
  254. {
  255. barColor = mProfile->mFontColor;
  256. barOutlineColor = mProfile->mFontColorSEL;
  257. }
  258. else
  259. {
  260. barColor = mProfile->mFontColor;
  261. barOutlineColor = mProfile->mFontColorHL;
  262. }
  263. drawer->drawRectFill(fillRect, barColor);
  264. drawer->drawRect(sliderRect, barOutlineColor);
  265. // get the appropriate font color
  266. ColorI fontColor;
  267. if (!mEnabled)
  268. {
  269. fontColor = mProfile->mFontColorNA;
  270. }
  271. else if (isSelected())
  272. {
  273. fontColor = mProfile->mFontColorSEL;
  274. }
  275. else if (isHighlighted())
  276. {
  277. fontColor = mProfile->mFontColorHL;
  278. }
  279. else
  280. {
  281. fontColor = mProfile->mFontColor;
  282. }
  283. char stringVal[32];
  284. dSprintf(stringVal, 32, "%.1f", mValue);
  285. //S32 stringWidth = font->getStrWidth(stringVal); //adaptive width
  286. Point2I textOffset(sliderRect.point.x + sliderRect.extent.x, 0);
  287. // render the option text itself
  288. Point2I textExtent(textWidth, height);
  289. RectI textRect = optionRect;
  290. textRect.point.x = sliderRect.point.x + sliderRect.extent.x;
  291. textRect.extent.x = optionRect.extent.x * 0.3;
  292. drawer->setBitmapModulation(fontColor);
  293. renderJustifiedText(textRect.point, textRect.extent, stringVal);
  294. //drawer->drawRectFill(textRect, ColorI::RED);
  295. }
  296. void GuiGameSettingsCtrl::onRenderKeybindOption(Point2I currentOffset)
  297. {
  298. S32 columnSplit = mColumnSplit;
  299. S32 height = getHeight();
  300. GFXDrawUtil* drawer = GFX->getDrawUtil();
  301. //drawer->drawBitmap(mBitmap, )
  302. Point2I button;
  303. button.x = currentOffset.x + columnSplit + (columnSplit / 2.5)/* + (optionWidth / 2)*/;
  304. button.y = currentOffset.y;
  305. Point2I buttonSize;
  306. buttonSize.x = height;
  307. buttonSize.y = height;
  308. if (mKeybindBitmapAsset.notNull())
  309. {
  310. RectI rect(button, buttonSize);
  311. drawer->clearBitmapModulation();
  312. drawer->drawBitmapStretch(mKeybindBitmap, rect, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  313. }
  314. //drawer->drawRectFill(button, ColorI::BLUE);
  315. }
  316. void GuiGameSettingsCtrl::set(const char* label, const char* callback, bool useHighlightIcon, bool enabled, S32 mode, const char* tooltip)
  317. {
  318. mScriptCallback = (dStrlen(callback) > 0) ? StringTable->insert(callback, true) : NULL;
  319. mEnabled = enabled;
  320. mMode = (Mode)mode;
  321. mTooltip = StringTable->insert(tooltip);
  322. mLabel = StringTable->insert(label, true);
  323. }
  324. void GuiGameSettingsCtrl::setListSetting(const char* label, const char* optionsList, bool wrapOptions, const char* callback, bool enabled, const char* tooltip, const char* defaultValue)
  325. {
  326. static StringTableEntry DELIM = StringTable->insert("\t", true);
  327. Vector<OptionEntry> options(__FILE__, __LINE__);
  328. S32 defaultOption = 0;
  329. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  330. for (S32 i = 0; i < count; ++i)
  331. {
  332. OptionEntry e;
  333. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  334. e.mDisplayText = StringTable->insert(option, true);
  335. e.mKeyString = e.mDisplayText;
  336. options.push_back(e);
  337. if (String::compare(option, defaultValue) == 0)
  338. defaultOption = options.size() - 1;
  339. }
  340. mOptions = options;
  341. bool hasOptions = mOptions.size() > 0;
  342. mSelectedOption = (hasOptions) ? defaultOption : NO_OPTION;
  343. mWrapOptions = wrapOptions;
  344. set(label, callback, true, (hasOptions) ? enabled : false, Mode::OptionList, tooltip);
  345. }
  346. void GuiGameSettingsCtrl::setSliderSetting(const char* label, F32 defaultValue, F32 increments, Point2F range, const char* callback, bool enabled, const char* tooltip)
  347. {
  348. static StringTableEntry DELIM = StringTable->insert("\t", true);
  349. mValue = defaultValue;
  350. mStepSize = increments;
  351. mRange = range;
  352. set(label, callback, true, enabled, Mode::Slider, tooltip);
  353. }
  354. void GuiGameSettingsCtrl::setKeybindSetting(const char* label, const char* bitmapName, const char* callback, bool enabled, const char* tooltip)
  355. {
  356. static StringTableEntry DELIM = StringTable->insert("\t", true);
  357. _setKeybindBitmap(StringTable->insert(bitmapName));
  358. //if(mBitmap != StringTable->EmptyString())
  359. // mBitmapTex.set(mBitmap, &GFXDefaultGUIProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
  360. set(label, callback, true, enabled, Mode::Keybind, tooltip);
  361. }
  362. bool GuiGameSettingsCtrl::onAdd()
  363. {
  364. if( !Parent::onAdd() )
  365. return false;
  366. return true;
  367. }
  368. bool GuiGameSettingsCtrl::onWake()
  369. {
  370. if( !Parent::onWake() )
  371. return false;
  372. _setNextBitmap(getNextBitmap());
  373. _setPreviousBitmap(getPreviousBitmap());
  374. _setKeybindBitmap(getKeybindBitmap());
  375. return true;
  376. }
  377. void GuiGameSettingsCtrl::onSleep()
  378. {
  379. if (mNextBitmapAsset.notNull())
  380. mNextBitmap = NULL;
  381. if (mPreviousBitmapAsset.notNull())
  382. mPreviousBitmap = NULL;
  383. if (mKeybindBitmapAsset.notNull())
  384. mKeybindBitmap = NULL;
  385. Parent::onSleep();
  386. }
  387. void GuiGameSettingsCtrl::activate()
  388. {
  389. if(isSelected() && isEnabled() && (mScriptCallback != StringTable->EmptyString()))
  390. {
  391. setThisControl();
  392. if (Con::isFunction(mScriptCallback))
  393. {
  394. Con::executef(mScriptCallback);
  395. }
  396. }
  397. }
  398. void GuiGameSettingsCtrl::setSelected()
  399. {
  400. if (!isEnabled())
  401. return;
  402. mSelected = true;
  403. }
  404. bool GuiGameSettingsCtrl::isEnabled() const
  405. {
  406. return mEnabled;
  407. }
  408. void GuiGameSettingsCtrl::setEnabled(bool enabled)
  409. {
  410. mEnabled = enabled;
  411. }
  412. void GuiGameSettingsCtrl::doScriptCommand(StringTableEntry command)
  413. {
  414. if (command && command[0])
  415. {
  416. setThisControl();
  417. Con::evaluate(command, false, __FILE__);
  418. }
  419. }
  420. void GuiGameSettingsCtrl::setThisControl()
  421. {
  422. smThisControl = this;
  423. }
  424. StringTableEntry GuiGameSettingsCtrl::getLabel() const
  425. {
  426. return mLabel;
  427. }
  428. void GuiGameSettingsCtrl::setLabel( const char * label)
  429. {
  430. mLabel = StringTable->insert(label, true);
  431. }
  432. void GuiGameSettingsCtrl::clear()
  433. {
  434. mOptions.clear();
  435. }
  436. //-----------------------------------------------------------------------------
  437. // Console stuff (GuiGameSettingsCtrl)
  438. //-----------------------------------------------------------------------------
  439. StringTableEntry GuiGameSettingsCtrl::getCurrentOption() const
  440. {
  441. if (mSelectedOption != NO_OPTION && !mOptions.empty())
  442. {
  443. return mOptions[mSelectedOption].mDisplayText;
  444. }
  445. return StringTable->insert("", false);
  446. }
  447. StringTableEntry GuiGameSettingsCtrl::getCurrentOptionKey() const
  448. {
  449. if (mSelectedOption != NO_OPTION)
  450. {
  451. return mOptions[mSelectedOption].mKeyString;
  452. }
  453. return StringTable->insert("", false);
  454. }
  455. S32 GuiGameSettingsCtrl::getCurrentOptionIndex() const
  456. {
  457. if (mSelectedOption != NO_OPTION)
  458. {
  459. return mSelectedOption;
  460. }
  461. return S32(-1);
  462. }
  463. bool GuiGameSettingsCtrl::selectOption(const char* theOption)
  464. {
  465. for (Vector<OptionEntry>::iterator anOption = mOptions.begin(); anOption < mOptions.end(); ++anOption)
  466. {
  467. if (String::compare((*anOption).mDisplayText, theOption) == 0)
  468. {
  469. S32 newIndex = anOption - mOptions.begin();
  470. mSelectedOption = newIndex;
  471. return true;
  472. }
  473. }
  474. return false;
  475. }
  476. bool GuiGameSettingsCtrl::selectOptionByKey(const char* optionKey)
  477. {
  478. for (Vector<OptionEntry>::iterator anOption = mOptions.begin(); anOption < mOptions.end(); ++anOption)
  479. {
  480. if (String::compare((*anOption).mKeyString, optionKey) == 0)
  481. {
  482. S32 newIndex = anOption - mOptions.begin();
  483. mSelectedOption = newIndex;
  484. return true;
  485. }
  486. }
  487. return false;
  488. }
  489. bool GuiGameSettingsCtrl::selectOptionByIndex(S32 optionIndex)
  490. {
  491. if (optionIndex < mOptions.size() && optionIndex >= 0)
  492. {
  493. mSelectedOption = optionIndex;
  494. return true;
  495. }
  496. return false;
  497. }
  498. void GuiGameSettingsCtrl::setOptions(const char* optionsList)
  499. {
  500. static StringTableEntry DELIM = StringTable->insert("\t", true);
  501. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  502. mOptions.setSize(count);
  503. for (S32 i = 0; i < count; ++i)
  504. {
  505. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  506. OptionEntry e;
  507. e.mDisplayText = StringTable->insert(option, true);
  508. e.mKeyString = e.mDisplayText;
  509. mOptions[i] = e;
  510. }
  511. if (mSelectedOption >= mOptions.size())
  512. {
  513. mSelectedOption = mOptions.size() - 1;
  514. }
  515. }
  516. void GuiGameSettingsCtrl::addOption(const char* displayText, const char* keyText)
  517. {
  518. OptionEntry e;
  519. e.mDisplayText = StringTable->insert(displayText, true);
  520. e.mKeyString = (keyText[0] == '\0') ? e.mDisplayText : StringTable->insert(keyText, true);
  521. mOptions.push_back(e);
  522. }
  523. void GuiGameSettingsCtrl::clickOption(S32 xPos)
  524. {
  525. S32 leftArrowX1 = mColumnSplit;
  526. S32 leftArrowX2 = leftArrowX1 + mArrowSize;
  527. S32 rightArrowX2 = getWidth() - mRightPad;
  528. S32 rightArrowX1 = rightArrowX2 - mArrowSize;
  529. if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  530. {
  531. changeOption(-1);
  532. }
  533. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  534. {
  535. changeOption(1);
  536. }
  537. }
  538. void GuiGameSettingsCtrl::changeOption(S32 delta)
  539. {
  540. S32 optionCount = mOptions.size();
  541. S32 newSelection = mSelectedOption + delta;
  542. if (optionCount == 0)
  543. {
  544. newSelection = NO_OPTION;
  545. }
  546. else if (!mWrapOptions)
  547. {
  548. newSelection = mClamp(newSelection, 0, optionCount - 1);
  549. }
  550. else if (newSelection < 0)
  551. {
  552. newSelection = optionCount - 1;
  553. }
  554. else if (newSelection >= optionCount)
  555. {
  556. newSelection = 0;
  557. }
  558. mSelectedOption = newSelection;
  559. if (mMode == GuiGameSettingsCtrl::Slider)
  560. {
  561. mValue += mStepSize * delta;
  562. mValue = mRound(mValue / mStepSize) * mStepSize;
  563. if (mValue < mRange.x)
  564. mValue = mRange.x;
  565. if (mValue > mRange.y)
  566. mValue = mRange.y;
  567. }
  568. static StringTableEntry LEFT = StringTable->insert("LEFT", true);
  569. static StringTableEntry RIGHT = StringTable->insert("RIGHT", true);
  570. onChange_callback();
  571. if (mScriptCallback != NULL && (mSelectedOption != NO_OPTION && mMode != GuiGameSettingsCtrl::Slider))
  572. {
  573. setThisControl();
  574. StringTableEntry direction = StringTable->EmptyString();
  575. if (delta < 0)
  576. {
  577. direction = LEFT;
  578. }
  579. else if (delta > 0)
  580. {
  581. direction = RIGHT;
  582. }
  583. if ((direction != StringTable->EmptyString()) && (Con::isFunction(mScriptCallback)))
  584. {
  585. Con::executef(mScriptCallback, direction);
  586. }
  587. }
  588. }
  589. IMPLEMENT_CONOBJECT(GuiGameSettingsCtrl);
  590. void GuiGameSettingsCtrl::clickSlider(S32 xPos)
  591. {
  592. RectI sliderRect;
  593. S32 sliderOffset = 5;
  594. S32 height = getHeight();
  595. RectI optionRect;
  596. sliderRect.point.x = mColumnSplit + mArrowSize;
  597. sliderRect.point.y = sliderOffset;
  598. sliderRect.extent.x = (getWidth() - mRightPad - mArrowSize) - sliderRect.point.x;
  599. sliderRect.extent.y = height - sliderOffset * 2;
  600. optionRect = sliderRect;
  601. S32 textWidth = sliderRect.extent.x * 0.3;
  602. sliderRect.extent.x -= textWidth;
  603. //Now adjust the bar to match-to our value
  604. S32 barStart = sliderRect.point.x;
  605. S32 barEnd = sliderRect.point.x + sliderRect.extent.x;
  606. if (xPos >= barStart && xPos <= barEnd)
  607. {
  608. //find the position
  609. F32 newValue = (((xPos - barStart) * (mRange.y - mRange.x)) / (barEnd - barStart)) + mRange.x;
  610. newValue = mRound(newValue / mStepSize) * mStepSize;
  611. mValue = newValue;
  612. }
  613. onChange_callback();
  614. }
  615. void GuiGameSettingsCtrl::clickKeybind(S32 xPos)
  616. {
  617. S32 height = getHeight();
  618. S32 width = getWidth();
  619. RectI rect(Point2I::Zero, Point2I(width, height));
  620. onChange_callback();
  621. if (rect.pointInRect(Point2I(xPos, getHeight()/2)))
  622. {
  623. if (mScriptCallback != StringTable->EmptyString())
  624. {
  625. Con::executef(mScriptCallback, this);
  626. }
  627. }
  628. }
  629. F32 GuiGameSettingsCtrl::getValue()
  630. {
  631. return mValue;
  632. }
  633. void GuiGameSettingsCtrl::setValue(F32 value)
  634. {
  635. mValue = value;
  636. }
  637. F32 GuiGameSettingsCtrl::getIncrement()
  638. {
  639. return mStepSize;
  640. }
  641. Point2F GuiGameSettingsCtrl::getRange()
  642. {
  643. return mRange;
  644. }
  645. const char* GuiGameSettingsCtrl::getTooltip()
  646. {
  647. return mTooltip;
  648. }
  649. ConsoleDocClass( GuiGameSettingsCtrl,
  650. "@brief A base class for cross platform menu controls that are gamepad friendly.\n\n"
  651. "This class is used to build row-based menu GUIs that can be easily navigated "
  652. "using the keyboard, mouse or gamepad. The desired row can be selected using "
  653. "the mouse, or by navigating using the Up and Down buttons.\n\n"
  654. "@tsexample\n\n"
  655. "new GuiGameSettingsCtrl()\n"
  656. "{\n"
  657. " debugRender = \"0\";\n"
  658. " callbackOnA = \"applyOptions();\";\n"
  659. " callbackOnB = \"Canvas.setContent(MainMenuGui);\";\n"
  660. " callbackOnX = \"\";\n"
  661. " callbackOnY = \"revertOptions();\";\n"
  662. " //Properties not specific to this control have been omitted from this example.\n"
  663. "};\n"
  664. "@endtsexample\n\n"
  665. "@see GuiGameSettingsProfile\n\n"
  666. "@ingroup GuiGame"
  667. );
  668. IMPLEMENT_CALLBACK( GuiGameSettingsCtrl, onChange, void, (), (),
  669. "Called when the setting's value changes." );
  670. IMPLEMENT_CALLBACK(GuiGameSettingsCtrl, onInputEvent, void, (const char* device, const char* action, bool state),
  671. (device, action, state),
  672. "@brief Callback that occurs when an input is triggered on this control\n\n"
  673. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  674. "@param action The actual event occuring, such as a key or button\n"
  675. "@param state True if the action is being pressed, false if it is being release\n\n");
  676. IMPLEMENT_CALLBACK(GuiGameSettingsCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  677. (device, action, axisValue),
  678. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  679. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  680. "@param action The ActionMap code for the axis\n"
  681. "@param axisValue The current value of the axis\n\n");
  682. void GuiGameSettingsCtrl::initPersistFields()
  683. {
  684. docsURL;
  685. INITPERSISTFIELD_IMAGEASSET(KeybindBitmap, GuiGameSettingsCtrl, "Bitmap used to display the bound key for this keybind option.");
  686. INITPERSISTFIELD_IMAGEASSET(PreviousBitmap, GuiGameSettingsCtrl, "Bitmap used for the previous button when in list mode.");
  687. INITPERSISTFIELD_IMAGEASSET(NextBitmap, GuiGameSettingsCtrl, "Bitmap used for the next button when in list mode.");
  688. addFieldV("arrowSize", TypeRangedS32, Offset(mArrowSize, GuiGameSettingsCtrl), &CommonValidators::PositiveInt,
  689. "Size of the arrow buttons' extents");
  690. addFieldV("columnSplit", TypeRangedS32, Offset(mColumnSplit, GuiGameSettingsCtrl), &CommonValidators::NaturalNumber,
  691. "Position of the split between the leftside label and the rightside setting parts");
  692. addFieldV("rightPad", TypeRangedS32, Offset(mRightPad, GuiGameSettingsCtrl), &CommonValidators::NaturalNumber,
  693. "Padding between the rightmost edge of the control and right arrow.");
  694. addField("callbackOnA", TypeString, Offset(mCallbackOnA, GuiGameSettingsCtrl),
  695. "Script callback when the 'A' button is pressed. 'A' inputs are Keyboard: A, Return, Space; Gamepad: A, Start" );
  696. addField("callbackOnB", TypeString, Offset(mCallbackOnB, GuiGameSettingsCtrl),
  697. "Script callback when the 'B' button is pressed. 'B' inputs are Keyboard: B, Esc, Backspace, Delete; Gamepad: B, Back" );
  698. addField("callbackOnX", TypeString, Offset(mCallbackOnX, GuiGameSettingsCtrl),
  699. "Script callback when the 'X' button is pressed. 'X' inputs are Keyboard: X; Gamepad: X" );
  700. addField("callbackOnY", TypeString, Offset(mCallbackOnY, GuiGameSettingsCtrl),
  701. "Script callback when the 'Y' button is pressed. 'Y' inputs are Keyboard: Y; Gamepad: Y" );
  702. addField("callbackOnInputs", TypeBool, Offset(mCallbackOnInputs, GuiGameSettingsCtrl),
  703. "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.");
  704. addField("consumeKeyInputEvents", TypeBool, Offset(mConsumeKeyInputEvents, GuiGameSettingsCtrl),
  705. "When callbackOnInputs is active, this indicates if the input event should be consumed, or allowed 'through' to let other things respond to the event as well.");
  706. Parent::initPersistFields();
  707. }
  708. DefineEngineMethod( GuiGameSettingsCtrl, isEnabled, bool, (),,
  709. "Determines if the control is enabled or disabled.\n\n"
  710. "@return True if the control is enabled. False if the control is not enabled." )
  711. {
  712. return object->isEnabled();
  713. }
  714. DefineEngineMethod( GuiGameSettingsCtrl, setEnabled, void, ( bool enabled ),,
  715. "Sets the control's enabled status according to the given parameters.\n\n"
  716. "@param enabled Indicate true to enable the control or false to disable it." )
  717. {
  718. object->setEnabled( enabled );
  719. }
  720. DefineEngineMethod( GuiGameSettingsCtrl, activate, void, (),,
  721. "Activates the control. The script callback of the control will be called (if it has one)." )
  722. {
  723. object->activate();
  724. }
  725. DefineEngineMethod(GuiGameSettingsCtrl, getLabel, const char *, (),,
  726. "Gets the label displayed.\n\n"
  727. "@return The label." )
  728. {
  729. return object->getLabel();
  730. }
  731. DefineEngineMethod(GuiGameSettingsCtrl, setLabel, void, ( const char* label ),,
  732. "Sets the label.\n\n"
  733. "@param label Text to set as the label.\n" )
  734. {
  735. object->setLabel(label );
  736. }
  737. DefineEngineMethod( GuiGameSettingsCtrl, setSelected, void, (),,
  738. "Sets the control as selected. Can only select enabled controls." )
  739. {
  740. object->setSelected();
  741. }
  742. DefineEngineMethod( GuiGameSettingsCtrl, getSelected, bool, (),,
  743. "Gets if the control is currently selected.\n\n"
  744. "@return if the control is selected." )
  745. {
  746. return object->isSelected();
  747. }
  748. DefineEngineMethod(GuiGameSettingsCtrl, clear, void, (), ,
  749. "Clears the current options.\n\n")
  750. {
  751. return object->clear();
  752. }
  753. DefineEngineMethod(GuiGameSettingsCtrl, getMode, S32, (), ,
  754. "Gets this control's options mode.\n\n")
  755. {
  756. GuiGameSettingsCtrl::Mode mode = object->getMode();
  757. if (mode == GuiGameSettingsCtrl::Mode::OptionList)
  758. return 0;
  759. else if (mode == GuiGameSettingsCtrl::Mode::Slider)
  760. return 1;
  761. else if (mode == GuiGameSettingsCtrl::Mode::Keybind)
  762. return 2;
  763. else
  764. return -1;
  765. }
  766. DefineEngineMethod(GuiGameSettingsCtrl, setListSetting, void,
  767. (const char* label, const char* options, bool wrapOptions, const char* callback, bool enabled, const char* tooltip, const char* defaultValue),
  768. (true, "", ""),
  769. "Sets this setting to a list.\n\n"
  770. "@param label The text to display as a label.\n"
  771. "@param options A tab separated list of options.\n"
  772. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  773. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  774. "@param enabled [optional] If this control is initially enabled.")
  775. {
  776. object->setListSetting(label, options, wrapOptions, callback, enabled, tooltip, defaultValue);
  777. }
  778. DefineEngineMethod(GuiGameSettingsCtrl, setSliderSetting, void,
  779. (const char* label, F32 defaultValue, F32 increment, Point2F range, const char* callback, bool enabled, const char* tooltip),
  780. (true, ""),
  781. "Sets this setting to a slider.\n\n"
  782. "@param label The text to display as a label.\n"
  783. "@param options A tab separated list of options.\n"
  784. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  785. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  786. "@param enabled [optional] If this control is initially enabled.")
  787. {
  788. object->setSliderSetting(label, defaultValue, increment, range, callback, enabled, tooltip);
  789. }
  790. DefineEngineMethod(GuiGameSettingsCtrl, setKeybindSetting, void,
  791. (const char* label, const char* bitmapName, const char* callback, bool enabled, const char* tooltip),
  792. (true, ""),
  793. "Sets this setting to a keybind.\n\n"
  794. "@param label The text to display as a label.\n"
  795. "@param options A tab separated list of options.\n"
  796. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  797. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  798. "@param enabled [optional] If this control is initially enabled.")
  799. {
  800. object->setKeybindSetting(label, bitmapName, callback, enabled, tooltip);
  801. }
  802. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOption, const char*, (), ,
  803. "Gets the text for the currently selected option .\n\n"
  804. "@return A string representing the text currently displayed as the selected option. If there is no such displayed text then the empty string is returned.")
  805. {
  806. return object->getCurrentOption();
  807. }
  808. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOptionKey, const char*, (), ,
  809. "Gets the key string for the currently selected option.\n\n"
  810. "@return The key (or id) that was assigned to the selected option. If there is no selected option then the empty string is returned.")
  811. {
  812. return object->getCurrentOptionKey();
  813. }
  814. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOptionIndex, S32, (), ,
  815. "Gets the index into the option list for the currently selected option.\n\n"
  816. "@return The index of the selected option. If there is no selected option then -1 is returned.")
  817. {
  818. return object->getCurrentOptionIndex();
  819. }
  820. DefineEngineMethod(GuiGameSettingsCtrl, selectOption, bool, (const char* option), ,
  821. "Set the control's current option to the one specified\n\n"
  822. "@param option The option to be made active.\n"
  823. "@return True if the control contained the option and was set, false otherwise.")
  824. {
  825. return object->selectOption(option);
  826. }
  827. DefineEngineMethod(GuiGameSettingsCtrl, selectOptionByKey, bool, (const char* optionKey), ,
  828. "Set the control's current option to the one with the specified key.\n\n"
  829. "@param optionKey The key string that was assigned to the option to be made active.\n"
  830. "@return True if the control contained the key and the option and was set, false otherwise.")
  831. {
  832. return object->selectOptionByKey(optionKey);
  833. }
  834. DefineEngineMethod(GuiGameSettingsCtrl, selectOptionByIndex, bool, (S32 optionIndex), ,
  835. "Set the control's current option to the one at the specified index.\n\n"
  836. "@param optionIndex The index of the option to be made active.\n"
  837. "@return True if the index was valid and the option and was set, false otherwise.")
  838. {
  839. return object->selectOptionByIndex(optionIndex);
  840. }
  841. DefineEngineMethod(GuiGameSettingsCtrl, setOptions, void, (const char* optionsList), ,
  842. "Sets the list of options on the given control.\n\n"
  843. "@param optionsList A tab separated list of options for the control.")
  844. {
  845. object->setOptions(optionsList);
  846. }
  847. DefineEngineMethod(GuiGameSettingsCtrl, addOption, void, (const char* displayText, const char* keyText), (""),
  848. "Adds an option to the list of options on the given control.\n\n"
  849. "@param displayText The text to display for this option.\n"
  850. "@param keyText [Optional] The id string to associate with this value. "
  851. "If unset, the id will be the same as the display text.\n")
  852. {
  853. object->addOption(displayText, keyText);
  854. }
  855. DefineEngineMethod(GuiGameSettingsCtrl, getValue, F32, (), ,
  856. "Gets the value of the slider on the given control.")
  857. {
  858. return object->getValue();
  859. }
  860. DefineEngineMethod(GuiGameSettingsCtrl, setValue, void, (F32 value), ,
  861. "Sets the value of the slider on the given control.")
  862. {
  863. object->setValue(value);
  864. }
  865. DefineEngineMethod(GuiGameSettingsCtrl, getIncrement, F32, (), ,
  866. "Gets the increment amount of the slider on a given control.")
  867. {
  868. return object->getIncrement();
  869. }
  870. DefineEngineMethod(GuiGameSettingsCtrl, getRange, Point2F, (), ,
  871. "Gets the min and max values for the range of the slider on a given control.")
  872. {
  873. return object->getRange();
  874. }
  875. DefineEngineMethod(GuiGameSettingsCtrl, getTooltip, const char*, (), ,
  876. "Gets the tooltip on the given control.")
  877. {
  878. return object->getTooltip();
  879. }