guiGameSettingsCtrl.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. Point2I button;
  662. button.x = columnSplit + (columnSplit / 2.5)/* + (optionWidth / 2)*/;
  663. button.y = 0;
  664. Point2I buttonSize;
  665. buttonSize.x = height;
  666. buttonSize.y = height;
  667. RectI rect(button, buttonSize);
  668. onChange_callback();
  669. if (rect.pointInRect(Point2I(xPos, getHeight()/2)))
  670. {
  671. if (mScriptCallback != StringTable->EmptyString())
  672. {
  673. Con::executef(mScriptCallback, this);
  674. }
  675. }
  676. }
  677. F32 GuiGameSettingsCtrl::getValue()
  678. {
  679. return mValue;
  680. }
  681. void GuiGameSettingsCtrl::setValue(F32 value)
  682. {
  683. mValue = value;
  684. }
  685. F32 GuiGameSettingsCtrl::getIncrement()
  686. {
  687. return mStepSize;
  688. }
  689. Point2F GuiGameSettingsCtrl::getRange()
  690. {
  691. return mRange;
  692. }
  693. const char* GuiGameSettingsCtrl::getTooltip()
  694. {
  695. return mTooltip;
  696. }
  697. ConsoleDocClass( GuiGameSettingsCtrl,
  698. "@brief A base class for cross platform menu controls that are gamepad friendly.\n\n"
  699. "This class is used to build row-based menu GUIs that can be easily navigated "
  700. "using the keyboard, mouse or gamepad. The desired row can be selected using "
  701. "the mouse, or by navigating using the Up and Down buttons.\n\n"
  702. "@tsexample\n\n"
  703. "new GuiGameSettingsCtrl()\n"
  704. "{\n"
  705. " debugRender = \"0\";\n"
  706. " callbackOnA = \"applyOptions();\";\n"
  707. " callbackOnB = \"Canvas.setContent(MainMenuGui);\";\n"
  708. " callbackOnX = \"\";\n"
  709. " callbackOnY = \"revertOptions();\";\n"
  710. " //Properties not specific to this control have been omitted from this example.\n"
  711. "};\n"
  712. "@endtsexample\n\n"
  713. "@see GuiGameSettingsProfile\n\n"
  714. "@ingroup GuiGame"
  715. );
  716. IMPLEMENT_CALLBACK( GuiGameSettingsCtrl, onChange, void, (), (),
  717. "Called when the setting's value changes." );
  718. IMPLEMENT_CALLBACK(GuiGameSettingsCtrl, onInputEvent, void, (const char* device, const char* action, bool state),
  719. (device, action, state),
  720. "@brief Callback that occurs when an input is triggered on this control\n\n"
  721. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  722. "@param action The actual event occuring, such as a key or button\n"
  723. "@param state True if the action is being pressed, false if it is being release\n\n");
  724. IMPLEMENT_CALLBACK(GuiGameSettingsCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  725. (device, action, axisValue),
  726. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  727. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  728. "@param action The ActionMap code for the axis\n"
  729. "@param axisValue The current value of the axis\n\n");
  730. void GuiGameSettingsCtrl::initPersistFields()
  731. {
  732. INITPERSISTFIELD_IMAGEASSET(KeybindBitmap, GuiGameSettingsCtrl, "Bitmap used to display the bound key for this keybind option.");
  733. INITPERSISTFIELD_IMAGEASSET(PreviousBitmap, GuiGameSettingsCtrl, "Bitmap used for the previous button when in list mode.");
  734. INITPERSISTFIELD_IMAGEASSET(NextBitmap, GuiGameSettingsCtrl, "Bitmap used for the next button when in list mode.");
  735. addField("arrowSize", TypeS32, Offset(mArrowSize, GuiGameSettingsCtrl),
  736. "Size of the arrow buttons' extents");
  737. addField("columnSplit", TypeS32, Offset(mColumnSplit, GuiGameSettingsCtrl),
  738. "Position of the split between the leftside label and the rightside setting parts");
  739. addField("rightPad", TypeS32, Offset(mRightPad, GuiGameSettingsCtrl),
  740. "Padding between the rightmost edge of the control and right arrow.");
  741. addField("callbackOnA", TypeString, Offset(mCallbackOnA, GuiGameSettingsCtrl),
  742. "Script callback when the 'A' button is pressed. 'A' inputs are Keyboard: A, Return, Space; Gamepad: A, Start" );
  743. addField("callbackOnB", TypeString, Offset(mCallbackOnB, GuiGameSettingsCtrl),
  744. "Script callback when the 'B' button is pressed. 'B' inputs are Keyboard: B, Esc, Backspace, Delete; Gamepad: B, Back" );
  745. addField("callbackOnX", TypeString, Offset(mCallbackOnX, GuiGameSettingsCtrl),
  746. "Script callback when the 'X' button is pressed. 'X' inputs are Keyboard: X; Gamepad: X" );
  747. addField("callbackOnY", TypeString, Offset(mCallbackOnY, GuiGameSettingsCtrl),
  748. "Script callback when the 'Y' button is pressed. 'Y' inputs are Keyboard: Y; Gamepad: Y" );
  749. addField("callbackOnInputs", TypeBool, Offset(mCallbackOnInputs, GuiGameSettingsCtrl),
  750. "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.");
  751. addField("consumeKeyInputEvents", TypeBool, Offset(mConsumeKeyInputEvents, GuiGameSettingsCtrl),
  752. "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.");
  753. Parent::initPersistFields();
  754. }
  755. DefineEngineMethod( GuiGameSettingsCtrl, isEnabled, bool, (),,
  756. "Determines if the control is enabled or disabled.\n\n"
  757. "@return True if the control is enabled. False if the control is not enabled." )
  758. {
  759. return object->isEnabled();
  760. }
  761. DefineEngineMethod( GuiGameSettingsCtrl, setEnabled, void, ( bool enabled ),,
  762. "Sets the control's enabled status according to the given parameters.\n\n"
  763. "@param enabled Indicate true to enable the control or false to disable it." )
  764. {
  765. object->setEnabled( enabled );
  766. }
  767. DefineEngineMethod( GuiGameSettingsCtrl, activate, void, (),,
  768. "Activates the control. The script callback of the control will be called (if it has one)." )
  769. {
  770. object->activate();
  771. }
  772. DefineEngineMethod(GuiGameSettingsCtrl, getLabel, const char *, (),,
  773. "Gets the label displayed.\n\n"
  774. "@return The label." )
  775. {
  776. return object->getLabel();
  777. }
  778. DefineEngineMethod(GuiGameSettingsCtrl, setLabel, void, ( const char* label ),,
  779. "Sets the label.\n\n"
  780. "@param label Text to set as the label.\n" )
  781. {
  782. object->setLabel(label );
  783. }
  784. DefineEngineMethod( GuiGameSettingsCtrl, setSelected, void, (),,
  785. "Sets the control as selected. Can only select enabled controls." )
  786. {
  787. object->setSelected();
  788. }
  789. DefineEngineMethod( GuiGameSettingsCtrl, getSelected, bool, (),,
  790. "Gets if the control is currently selected.\n\n"
  791. "@return if the control is selected." )
  792. {
  793. return object->isSelected();
  794. }
  795. DefineEngineMethod(GuiGameSettingsCtrl, clear, void, (), ,
  796. "Clears the current options.\n\n")
  797. {
  798. return object->clear();
  799. }
  800. DefineEngineMethod(GuiGameSettingsCtrl, getMode, S32, (), ,
  801. "Gets this control's options mode.\n\n")
  802. {
  803. GuiGameSettingsCtrl::Mode mode = object->getMode();
  804. if (mode == GuiGameSettingsCtrl::Mode::OptionList)
  805. return 0;
  806. else if (mode == GuiGameSettingsCtrl::Mode::Slider)
  807. return 1;
  808. else if (mode == GuiGameSettingsCtrl::Mode::Keybind)
  809. return 2;
  810. else
  811. return -1;
  812. }
  813. DefineEngineMethod(GuiGameSettingsCtrl, setListSetting, void,
  814. (const char* label, const char* options, bool wrapOptions, const char* callback, bool enabled, const char* tooltip, const char* defaultValue),
  815. (true, "", ""),
  816. "Sets this setting to a list.\n\n"
  817. "@param label The text to display as a label.\n"
  818. "@param options A tab separated list of options.\n"
  819. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  820. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  821. "@param enabled [optional] If this control is initially enabled.")
  822. {
  823. object->setListSetting(label, options, wrapOptions, callback, enabled, tooltip, defaultValue);
  824. }
  825. DefineEngineMethod(GuiGameSettingsCtrl, setSliderSetting, void,
  826. (const char* label, F32 defaultValue, F32 increment, Point2F range, const char* callback, bool enabled, const char* tooltip),
  827. (true, ""),
  828. "Sets this setting to a slider.\n\n"
  829. "@param label The text to display as a label.\n"
  830. "@param options A tab separated list of options.\n"
  831. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  832. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  833. "@param enabled [optional] If this control is initially enabled.")
  834. {
  835. object->setSliderSetting(label, defaultValue, increment, range, callback, enabled, tooltip);
  836. }
  837. DefineEngineMethod(GuiGameSettingsCtrl, setKeybindSetting, void,
  838. (const char* label, const char* bitmapName, const char* callback, bool enabled, const char* tooltip),
  839. (true, ""),
  840. "Sets this setting to a keybind.\n\n"
  841. "@param label The text to display as a label.\n"
  842. "@param options A tab separated list of options.\n"
  843. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  844. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  845. "@param enabled [optional] If this control is initially enabled.")
  846. {
  847. object->setKeybindSetting(label, bitmapName, callback, enabled, tooltip);
  848. }
  849. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOption, const char*, (), ,
  850. "Gets the text for the currently selected option .\n\n"
  851. "@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.")
  852. {
  853. return object->getCurrentOption();
  854. }
  855. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOptionKey, const char*, (), ,
  856. "Gets the key string for the currently selected option.\n\n"
  857. "@return The key (or id) that was assigned to the selected option. If there is no selected option then the empty string is returned.")
  858. {
  859. return object->getCurrentOptionKey();
  860. }
  861. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOptionIndex, S32, (), ,
  862. "Gets the index into the option list for the currently selected option.\n\n"
  863. "@return The index of the selected option. If there is no selected option then -1 is returned.")
  864. {
  865. return object->getCurrentOptionIndex();
  866. }
  867. DefineEngineMethod(GuiGameSettingsCtrl, selectOption, bool, (const char* option), ,
  868. "Set the control's current option to the one specified\n\n"
  869. "@param option The option to be made active.\n"
  870. "@return True if the control contained the option and was set, false otherwise.")
  871. {
  872. return object->selectOption(option);
  873. }
  874. DefineEngineMethod(GuiGameSettingsCtrl, selectOptionByKey, bool, (const char* optionKey), ,
  875. "Set the control's current option to the one with the specified key.\n\n"
  876. "@param optionKey The key string that was assigned to the option to be made active.\n"
  877. "@return True if the control contained the key and the option and was set, false otherwise.")
  878. {
  879. return object->selectOptionByKey(optionKey);
  880. }
  881. DefineEngineMethod(GuiGameSettingsCtrl, selectOptionByIndex, bool, (S32 optionIndex), ,
  882. "Set the control's current option to the one at the specified index.\n\n"
  883. "@param optionIndex The index of the option to be made active.\n"
  884. "@return True if the index was valid and the option and was set, false otherwise.")
  885. {
  886. return object->selectOptionByIndex(optionIndex);
  887. }
  888. DefineEngineMethod(GuiGameSettingsCtrl, setOptions, void, (const char* optionsList), ,
  889. "Sets the list of options on the given control.\n\n"
  890. "@param optionsList A tab separated list of options for the control.")
  891. {
  892. object->setOptions(optionsList);
  893. }
  894. DefineEngineMethod(GuiGameSettingsCtrl, addOption, void, (const char* displayText, const char* keyText), (""),
  895. "Adds an option to the list of options on the given control.\n\n"
  896. "@param displayText The text to display for this option.\n"
  897. "@param keyText [Optional] The id string to associate with this value. "
  898. "If unset, the id will be the same as the display text.\n")
  899. {
  900. object->addOption(displayText, keyText);
  901. }
  902. DefineEngineMethod(GuiGameSettingsCtrl, getValue, F32, (), ,
  903. "Gets the value of the slider on the given control.")
  904. {
  905. return object->getValue();
  906. }
  907. DefineEngineMethod(GuiGameSettingsCtrl, setValue, void, (F32 value), ,
  908. "Sets the value of the slider on the given control.")
  909. {
  910. object->setValue(value);
  911. }
  912. DefineEngineMethod(GuiGameSettingsCtrl, getIncrement, F32, (), ,
  913. "Gets the increment amount of the slider on a given control.")
  914. {
  915. return object->getIncrement();
  916. }
  917. DefineEngineMethod(GuiGameSettingsCtrl, getRange, Point2F, (), ,
  918. "Gets the min and max values for the range of the slider on a given control.")
  919. {
  920. return object->getRange();
  921. }
  922. DefineEngineMethod(GuiGameSettingsCtrl, getTooltip, const char*, (), ,
  923. "Gets the tooltip on the given control.")
  924. {
  925. return object->getTooltip();
  926. }