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, "%f", 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. return true;
  414. }
  415. void GuiGameSettingsCtrl::activate()
  416. {
  417. if(isSelected() && isEnabled() && (mScriptCallback != StringTable->EmptyString()))
  418. {
  419. setThisControl();
  420. if (Con::isFunction(mScriptCallback))
  421. {
  422. Con::executef(mScriptCallback);
  423. }
  424. }
  425. }
  426. void GuiGameSettingsCtrl::setSelected()
  427. {
  428. if (!isEnabled())
  429. return;
  430. mSelected = true;
  431. }
  432. bool GuiGameSettingsCtrl::isEnabled() const
  433. {
  434. return mEnabled;
  435. }
  436. void GuiGameSettingsCtrl::setEnabled(bool enabled)
  437. {
  438. mEnabled = enabled;
  439. }
  440. void GuiGameSettingsCtrl::doScriptCommand(StringTableEntry command)
  441. {
  442. if (command && command[0])
  443. {
  444. setThisControl();
  445. Con::evaluate(command, false, __FILE__);
  446. }
  447. }
  448. void GuiGameSettingsCtrl::setThisControl()
  449. {
  450. smThisControl = this;
  451. }
  452. StringTableEntry GuiGameSettingsCtrl::getLabel() const
  453. {
  454. return mLabel;
  455. }
  456. void GuiGameSettingsCtrl::setLabel( const char * label)
  457. {
  458. mLabel = StringTable->insert(label, true);
  459. }
  460. void GuiGameSettingsCtrl::clear()
  461. {
  462. mOptions.clear();
  463. }
  464. //-----------------------------------------------------------------------------
  465. // Console stuff (GuiGameSettingsCtrl)
  466. //-----------------------------------------------------------------------------
  467. StringTableEntry GuiGameSettingsCtrl::getCurrentOption() const
  468. {
  469. if (mSelectedOption != NO_OPTION && !mOptions.empty())
  470. {
  471. return mOptions[mSelectedOption].mDisplayText;
  472. }
  473. return StringTable->insert("", false);
  474. }
  475. StringTableEntry GuiGameSettingsCtrl::getCurrentOptionKey() const
  476. {
  477. if (mSelectedOption != NO_OPTION)
  478. {
  479. return mOptions[mSelectedOption].mKeyString;
  480. }
  481. return StringTable->insert("", false);
  482. }
  483. S32 GuiGameSettingsCtrl::getCurrentOptionIndex() const
  484. {
  485. if (mSelectedOption != NO_OPTION)
  486. {
  487. return mSelectedOption;
  488. }
  489. return S32(-1);
  490. }
  491. bool GuiGameSettingsCtrl::selectOption(const char* theOption)
  492. {
  493. for (Vector<OptionEntry>::iterator anOption = mOptions.begin(); anOption < mOptions.end(); ++anOption)
  494. {
  495. if (String::compare((*anOption).mDisplayText, theOption) == 0)
  496. {
  497. S32 newIndex = anOption - mOptions.begin();
  498. mSelectedOption = newIndex;
  499. return true;
  500. }
  501. }
  502. return false;
  503. }
  504. bool GuiGameSettingsCtrl::selectOptionByKey(const char* optionKey)
  505. {
  506. for (Vector<OptionEntry>::iterator anOption = mOptions.begin(); anOption < mOptions.end(); ++anOption)
  507. {
  508. if (String::compare((*anOption).mKeyString, optionKey) == 0)
  509. {
  510. S32 newIndex = anOption - mOptions.begin();
  511. mSelectedOption = newIndex;
  512. return true;
  513. }
  514. }
  515. return false;
  516. }
  517. bool GuiGameSettingsCtrl::selectOptionByIndex(S32 optionIndex)
  518. {
  519. if (optionIndex < mOptions.size() && optionIndex >= 0)
  520. {
  521. mSelectedOption = optionIndex;
  522. return true;
  523. }
  524. return false;
  525. }
  526. void GuiGameSettingsCtrl::setOptions(const char* optionsList)
  527. {
  528. static StringTableEntry DELIM = StringTable->insert("\t", true);
  529. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  530. mOptions.setSize(count);
  531. for (S32 i = 0; i < count; ++i)
  532. {
  533. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  534. OptionEntry e;
  535. e.mDisplayText = StringTable->insert(option, true);
  536. e.mKeyString = e.mDisplayText;
  537. mOptions[i] = e;
  538. }
  539. if (mSelectedOption >= mOptions.size())
  540. {
  541. mSelectedOption = mOptions.size() - 1;
  542. }
  543. }
  544. void GuiGameSettingsCtrl::addOption(const char* displayText, const char* keyText)
  545. {
  546. OptionEntry e;
  547. e.mDisplayText = StringTable->insert(displayText, true);
  548. e.mKeyString = (keyText[0] == '\0') ? e.mDisplayText : StringTable->insert(keyText, true);
  549. mOptions.push_back(e);
  550. }
  551. void GuiGameSettingsCtrl::clickOption(S32 xPos)
  552. {
  553. F32 xScale = (float)getWidth();
  554. S32 leftArrowX1 = mColumnSplit;
  555. S32 leftArrowX2 = leftArrowX1 + mArrowSize;
  556. S32 rightArrowX2 = getWidth() - mRightPad;
  557. S32 rightArrowX1 = rightArrowX2 - mArrowSize;
  558. if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  559. {
  560. changeOption(-1);
  561. }
  562. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  563. {
  564. changeOption(1);
  565. }
  566. }
  567. void GuiGameSettingsCtrl::changeOption(S32 delta)
  568. {
  569. S32 optionCount = mOptions.size();
  570. S32 newSelection = mSelectedOption + delta;
  571. if (optionCount == 0)
  572. {
  573. newSelection = NO_OPTION;
  574. }
  575. else if (!mWrapOptions)
  576. {
  577. newSelection = mClamp(newSelection, 0, optionCount - 1);
  578. }
  579. else if (newSelection < 0)
  580. {
  581. newSelection = optionCount - 1;
  582. }
  583. else if (newSelection >= optionCount)
  584. {
  585. newSelection = 0;
  586. }
  587. mSelectedOption = newSelection;
  588. if (mMode == GuiGameSettingsCtrl::Slider)
  589. {
  590. mValue += mStepSize * delta;
  591. mValue = mRound(mValue / mStepSize) * mStepSize;
  592. if (mValue < mRange.x)
  593. mValue = mRange.x;
  594. if (mValue > mRange.y)
  595. mValue = mRange.y;
  596. }
  597. static StringTableEntry LEFT = StringTable->insert("LEFT", true);
  598. static StringTableEntry RIGHT = StringTable->insert("RIGHT", true);
  599. onChange_callback();
  600. if (mScriptCallback != NULL && (mSelectedOption != NO_OPTION && mMode != GuiGameSettingsCtrl::Slider))
  601. {
  602. setThisControl();
  603. StringTableEntry direction = StringTable->EmptyString();
  604. if (delta < 0)
  605. {
  606. direction = LEFT;
  607. }
  608. else if (delta > 0)
  609. {
  610. direction = RIGHT;
  611. }
  612. if ((direction != StringTable->EmptyString()) && (Con::isFunction(mScriptCallback)))
  613. {
  614. Con::executef(mScriptCallback, direction);
  615. }
  616. }
  617. }
  618. IMPLEMENT_CONOBJECT(GuiGameSettingsCtrl);
  619. void GuiGameSettingsCtrl::clickSlider(S32 xPos)
  620. {
  621. F32 xScale = (float)getWidth();
  622. S32 leftArrowX1 = mColumnSplit;
  623. S32 leftArrowX2 = leftArrowX1 + mArrowSize;
  624. S32 rightArrowX2 = getWidth() - mRightPad;
  625. S32 rightArrowX1 = rightArrowX2 - mArrowSize;
  626. S32 sliderWidth = rightArrowX1 - leftArrowX2;
  627. sliderWidth *= 0.6; //remove the number text spacing
  628. /*if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  629. {
  630. mValue -= mStepSize;
  631. mValue = mRound(mValue / mStepSize) * mStepSize;
  632. if (mValue < mRange.x)
  633. mValue = mRange.x;
  634. }
  635. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  636. {
  637. //F32 snap = mValue % mStepSize;
  638. //mValue.y -= snap;
  639. mValue += mStepSize;
  640. mValue = mRound(mValue / mStepSize) * mStepSize;
  641. if (mValue > mRange.y)
  642. mValue = mRange.y;
  643. }
  644. else
  645. {*/
  646. //see if we clicked on the sliderbar itself
  647. S32 barStart = leftArrowX2;
  648. S32 barEnd = barStart + sliderWidth;
  649. if (xPos >= barStart && xPos <= barEnd)
  650. {
  651. //find the position
  652. F32 newValue = (((xPos - barStart) * (mRange.y - mRange.x)) / (barEnd - barStart)) + mRange.x;
  653. newValue = mRound(newValue / mStepSize) * mStepSize;
  654. mValue = newValue;
  655. }
  656. //}
  657. onChange_callback();
  658. }
  659. void GuiGameSettingsCtrl::clickKeybind(S32 xPos)
  660. {
  661. S32 columnSplit = mColumnSplit;
  662. S32 height = getHeight();
  663. Point2I button;
  664. button.x = columnSplit + (columnSplit / 2.5)/* + (optionWidth / 2)*/;
  665. button.y = 0;
  666. Point2I buttonSize;
  667. buttonSize.x = height;
  668. buttonSize.y = height;
  669. RectI rect(button, buttonSize);
  670. onChange_callback();
  671. if (rect.pointInRect(Point2I(xPos, getHeight()/2)))
  672. {
  673. if (mScriptCallback != StringTable->EmptyString())
  674. {
  675. Con::executef(mScriptCallback, this);
  676. }
  677. }
  678. }
  679. F32 GuiGameSettingsCtrl::getValue()
  680. {
  681. return mValue;
  682. }
  683. void GuiGameSettingsCtrl::setValue(F32 value)
  684. {
  685. mValue = value;
  686. }
  687. F32 GuiGameSettingsCtrl::getIncrement()
  688. {
  689. return mStepSize;
  690. }
  691. Point2F GuiGameSettingsCtrl::getRange()
  692. {
  693. return mRange;
  694. }
  695. const char* GuiGameSettingsCtrl::getTooltip()
  696. {
  697. return mTooltip;
  698. }
  699. ConsoleDocClass( GuiGameSettingsCtrl,
  700. "@brief A base class for cross platform menu controls that are gamepad friendly.\n\n"
  701. "This class is used to build row-based menu GUIs that can be easily navigated "
  702. "using the keyboard, mouse or gamepad. The desired row can be selected using "
  703. "the mouse, or by navigating using the Up and Down buttons.\n\n"
  704. "@tsexample\n\n"
  705. "new GuiGameSettingsCtrl()\n"
  706. "{\n"
  707. " debugRender = \"0\";\n"
  708. " callbackOnA = \"applyOptions();\";\n"
  709. " callbackOnB = \"Canvas.setContent(MainMenuGui);\";\n"
  710. " callbackOnX = \"\";\n"
  711. " callbackOnY = \"revertOptions();\";\n"
  712. " //Properties not specific to this control have been omitted from this example.\n"
  713. "};\n"
  714. "@endtsexample\n\n"
  715. "@see GuiGameSettingsProfile\n\n"
  716. "@ingroup GuiGame"
  717. );
  718. IMPLEMENT_CALLBACK( GuiGameSettingsCtrl, onChange, void, (), (),
  719. "Called when the setting's value changes." );
  720. IMPLEMENT_CALLBACK(GuiGameSettingsCtrl, onInputEvent, void, (const char* device, const char* action, bool state),
  721. (device, action, state),
  722. "@brief Callback that occurs when an input is triggered on this control\n\n"
  723. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  724. "@param action The actual event occuring, such as a key or button\n"
  725. "@param state True if the action is being pressed, false if it is being release\n\n");
  726. IMPLEMENT_CALLBACK(GuiGameSettingsCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  727. (device, action, axisValue),
  728. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  729. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  730. "@param action The ActionMap code for the axis\n"
  731. "@param axisValue The current value of the axis\n\n");
  732. void GuiGameSettingsCtrl::initPersistFields()
  733. {
  734. INITPERSISTFIELD_IMAGEASSET(KeybindBitmap, GuiGameSettingsCtrl, "Bitmap used to display the bound key for this keybind option.");
  735. INITPERSISTFIELD_IMAGEASSET(PreviousBitmap, GuiGameSettingsCtrl, "Bitmap used for the previous button when in list mode.");
  736. INITPERSISTFIELD_IMAGEASSET(NextBitmap, GuiGameSettingsCtrl, "Bitmap used for the next button when in list mode.");
  737. addField("arrowSize", TypeS32, Offset(mArrowSize, GuiGameSettingsCtrl),
  738. "Size of the arrow buttons' extents");
  739. addField("columnSplit", TypeS32, Offset(mColumnSplit, GuiGameSettingsCtrl),
  740. "Position of the split between the leftside label and the rightside setting parts");
  741. addField("rightPad", TypeS32, Offset(mRightPad, GuiGameSettingsCtrl),
  742. "Padding between the rightmost edge of the control and right arrow.");
  743. addField("callbackOnA", TypeString, Offset(mCallbackOnA, GuiGameSettingsCtrl),
  744. "Script callback when the 'A' button is pressed. 'A' inputs are Keyboard: A, Return, Space; Gamepad: A, Start" );
  745. addField("callbackOnB", TypeString, Offset(mCallbackOnB, GuiGameSettingsCtrl),
  746. "Script callback when the 'B' button is pressed. 'B' inputs are Keyboard: B, Esc, Backspace, Delete; Gamepad: B, Back" );
  747. addField("callbackOnX", TypeString, Offset(mCallbackOnX, GuiGameSettingsCtrl),
  748. "Script callback when the 'X' button is pressed. 'X' inputs are Keyboard: X; Gamepad: X" );
  749. addField("callbackOnY", TypeString, Offset(mCallbackOnY, GuiGameSettingsCtrl),
  750. "Script callback when the 'Y' button is pressed. 'Y' inputs are Keyboard: Y; Gamepad: Y" );
  751. addField("callbackOnInputs", TypeBool, Offset(mCallbackOnInputs, GuiGameSettingsCtrl),
  752. "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.");
  753. addField("consumeKeyInputEvents", TypeBool, Offset(mConsumeKeyInputEvents, GuiGameSettingsCtrl),
  754. "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.");
  755. Parent::initPersistFields();
  756. }
  757. DefineEngineMethod( GuiGameSettingsCtrl, isEnabled, bool, (),,
  758. "Determines if the control is enabled or disabled.\n\n"
  759. "@return True if the control is enabled. False if the control is not enabled." )
  760. {
  761. return object->isEnabled();
  762. }
  763. DefineEngineMethod( GuiGameSettingsCtrl, setEnabled, void, ( bool enabled ),,
  764. "Sets the control's enabled status according to the given parameters.\n\n"
  765. "@param enabled Indicate true to enable the control or false to disable it." )
  766. {
  767. object->setEnabled( enabled );
  768. }
  769. DefineEngineMethod( GuiGameSettingsCtrl, activate, void, (),,
  770. "Activates the control. The script callback of the control will be called (if it has one)." )
  771. {
  772. object->activate();
  773. }
  774. DefineEngineMethod(GuiGameSettingsCtrl, getLabel, const char *, (),,
  775. "Gets the label displayed.\n\n"
  776. "@return The label." )
  777. {
  778. return object->getLabel();
  779. }
  780. DefineEngineMethod(GuiGameSettingsCtrl, setLabel, void, ( const char* label ),,
  781. "Sets the label.\n\n"
  782. "@param label Text to set as the label.\n" )
  783. {
  784. object->setLabel(label );
  785. }
  786. DefineEngineMethod( GuiGameSettingsCtrl, setSelected, void, (),,
  787. "Sets the control as selected. Can only select enabled controls." )
  788. {
  789. object->setSelected();
  790. }
  791. DefineEngineMethod( GuiGameSettingsCtrl, getSelected, bool, (),,
  792. "Gets if the control is currently selected.\n\n"
  793. "@return if the control is selected." )
  794. {
  795. return object->isSelected();
  796. }
  797. DefineEngineMethod(GuiGameSettingsCtrl, clear, void, (), ,
  798. "Clears the current options.\n\n")
  799. {
  800. return object->clear();
  801. }
  802. DefineEngineMethod(GuiGameSettingsCtrl, getMode, S32, (), ,
  803. "Gets this control's options mode.\n\n")
  804. {
  805. GuiGameSettingsCtrl::Mode mode = object->getMode();
  806. if (mode == GuiGameSettingsCtrl::Mode::OptionList)
  807. return 0;
  808. else if (mode == GuiGameSettingsCtrl::Mode::Slider)
  809. return 1;
  810. else if (mode == GuiGameSettingsCtrl::Mode::Keybind)
  811. return 2;
  812. else
  813. return -1;
  814. }
  815. DefineEngineMethod(GuiGameSettingsCtrl, setListSetting, void,
  816. (const char* label, const char* options, bool wrapOptions, const char* callback, bool enabled, const char* tooltip, const char* defaultValue),
  817. (true, "", ""),
  818. "Sets this setting to a list.\n\n"
  819. "@param label The text to display as a label.\n"
  820. "@param options A tab separated list of options.\n"
  821. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  822. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  823. "@param enabled [optional] If this control is initially enabled.")
  824. {
  825. object->setListSetting(label, options, wrapOptions, callback, enabled, tooltip, defaultValue);
  826. }
  827. DefineEngineMethod(GuiGameSettingsCtrl, setSliderSetting, void,
  828. (const char* label, F32 defaultValue, F32 increment, Point2F range, const char* callback, bool enabled, const char* tooltip),
  829. (true, ""),
  830. "Sets this setting to a slider.\n\n"
  831. "@param label The text to display as a label.\n"
  832. "@param options A tab separated list of options.\n"
  833. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  834. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  835. "@param enabled [optional] If this control is initially enabled.")
  836. {
  837. object->setSliderSetting(label, defaultValue, increment, range, callback, enabled, tooltip);
  838. }
  839. DefineEngineMethod(GuiGameSettingsCtrl, setKeybindSetting, void,
  840. (const char* label, const char* bitmapName, const char* callback, bool enabled, const char* tooltip),
  841. (true, ""),
  842. "Sets this setting to a keybind.\n\n"
  843. "@param label The text to display as a label.\n"
  844. "@param options A tab separated list of options.\n"
  845. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  846. "@param callback Name of a script function to use as a callback when this control is activated.\n"
  847. "@param enabled [optional] If this control is initially enabled.")
  848. {
  849. object->setKeybindSetting(label, bitmapName, callback, enabled, tooltip);
  850. }
  851. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOption, const char*, (), ,
  852. "Gets the text for the currently selected option .\n\n"
  853. "@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.")
  854. {
  855. return object->getCurrentOption();
  856. }
  857. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOptionKey, const char*, (), ,
  858. "Gets the key string for the currently selected option.\n\n"
  859. "@return The key (or id) that was assigned to the selected option. If there is no selected option then the empty string is returned.")
  860. {
  861. return object->getCurrentOptionKey();
  862. }
  863. DefineEngineMethod(GuiGameSettingsCtrl, getCurrentOptionIndex, S32, (), ,
  864. "Gets the index into the option list for the currently selected option.\n\n"
  865. "@return The index of the selected option. If there is no selected option then -1 is returned.")
  866. {
  867. return object->getCurrentOptionIndex();
  868. }
  869. DefineEngineMethod(GuiGameSettingsCtrl, selectOption, bool, (const char* option), ,
  870. "Set the control's current option to the one specified\n\n"
  871. "@param option The option to be made active.\n"
  872. "@return True if the control contained the option and was set, false otherwise.")
  873. {
  874. return object->selectOption(option);
  875. }
  876. DefineEngineMethod(GuiGameSettingsCtrl, selectOptionByKey, bool, (const char* optionKey), ,
  877. "Set the control's current option to the one with the specified key.\n\n"
  878. "@param optionKey The key string that was assigned to the option to be made active.\n"
  879. "@return True if the control contained the key and the option and was set, false otherwise.")
  880. {
  881. return object->selectOptionByKey(optionKey);
  882. }
  883. DefineEngineMethod(GuiGameSettingsCtrl, selectOptionByIndex, bool, (S32 optionIndex), ,
  884. "Set the control's current option to the one at the specified index.\n\n"
  885. "@param optionIndex The index of the option to be made active.\n"
  886. "@return True if the index was valid and the option and was set, false otherwise.")
  887. {
  888. return object->selectOptionByIndex(optionIndex);
  889. }
  890. DefineEngineMethod(GuiGameSettingsCtrl, setOptions, void, (const char* optionsList), ,
  891. "Sets the list of options on the given control.\n\n"
  892. "@param optionsList A tab separated list of options for the control.")
  893. {
  894. object->setOptions(optionsList);
  895. }
  896. DefineEngineMethod(GuiGameSettingsCtrl, addOption, void, (const char* displayText, const char* keyText), (""),
  897. "Adds an option to the list of options on the given control.\n\n"
  898. "@param displayText The text to display for this option.\n"
  899. "@param keyText [Optional] The id string to associate with this value. "
  900. "If unset, the id will be the same as the display text.\n")
  901. {
  902. object->addOption(displayText, keyText);
  903. }
  904. DefineEngineMethod(GuiGameSettingsCtrl, getValue, F32, (), ,
  905. "Gets the value of the slider on the given control.")
  906. {
  907. return object->getValue();
  908. }
  909. DefineEngineMethod(GuiGameSettingsCtrl, setValue, void, (F32 value), ,
  910. "Sets the value of the slider on the given control.")
  911. {
  912. object->setValue(value);
  913. }
  914. DefineEngineMethod(GuiGameSettingsCtrl, getIncrement, F32, (), ,
  915. "Gets the increment amount of the slider on a given control.")
  916. {
  917. return object->getIncrement();
  918. }
  919. DefineEngineMethod(GuiGameSettingsCtrl, getRange, Point2F, (), ,
  920. "Gets the min and max values for the range of the slider on a given control.")
  921. {
  922. return object->getRange();
  923. }
  924. DefineEngineMethod(GuiGameSettingsCtrl, getTooltip, const char*, (), ,
  925. "Gets the tooltip on the given control.")
  926. {
  927. return object->getTooltip();
  928. }