2
0

guiGameSettingsCtrl.cpp 32 KB

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