guiGameSettingsCtrl.cpp 33 KB

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