guiGameSettingsCtrl.cpp 35 KB

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