guiGameSettingsCtrl.cpp 32 KB

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