guiGameSettingsCtrl.cpp 33 KB

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