guiGameListMenuCtrl.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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 "guiGameListMenuCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "gui/containers/guiScrollCtrl.h"
  27. #include "sim/actionMap.h"
  28. #include "core/strings/stringUnit.h"
  29. //-----------------------------------------------------------------------------
  30. // GuiGameListMenuCtrl
  31. //-----------------------------------------------------------------------------
  32. GuiGameListMenuCtrl::GuiGameListMenuCtrl()
  33. : mSelected(NO_ROW),
  34. mDebugRender(false),
  35. mHighlighted(NO_ROW),
  36. mCallbackOnInputs(false),
  37. mConsumeKeyInputEvents(false)
  38. {
  39. VECTOR_SET_ASSOCIATION(mRows);
  40. // initialize the control callbacks
  41. mCallbackOnA = StringTable->EmptyString();
  42. mCallbackOnB = mCallbackOnA;
  43. mCallbackOnX = mCallbackOnA;
  44. mCallbackOnY = mCallbackOnA;
  45. }
  46. GuiGameListMenuCtrl::~GuiGameListMenuCtrl()
  47. {
  48. for (S32 i = 0; i < mRows.size(); ++i)
  49. {
  50. delete mRows[i];
  51. }
  52. }
  53. void GuiGameListMenuCtrl::onRender(Point2I offset, const RectI &updateRect)
  54. {
  55. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  56. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  57. F32 xScale = (float) getWidth() / profile->getRowWidth();
  58. bool profileHasIcons = profile->hasArrows();
  59. S32 rowHeight = profile->getRowHeight();
  60. Point2I currentOffset = offset;
  61. Point2I extent = getExtent();
  62. Point2I rowExtent(extent.x, rowHeight);
  63. Point2I textOffset(profile->mTextOffset.x * xScale, profile->mTextOffset.y);
  64. Point2I textExtent(extent.x - textOffset.x, rowHeight);
  65. Point2I iconExtent, iconOffset(0.0f, 0.0f);
  66. if (profileHasIcons)
  67. {
  68. iconExtent = profile->getIconExtent();
  69. // icon is centered vertically plus any specified offset
  70. S32 iconOffsetY = (rowHeight - iconExtent.y) >> 1;
  71. iconOffsetY += profile->mIconOffset.y;
  72. iconOffset = Point2I(profile->mIconOffset.x * xScale, iconOffsetY);
  73. }
  74. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  75. {
  76. if (row != mRows.begin())
  77. {
  78. // rows other than the first can have padding above them
  79. currentOffset.y += (*row)->mHeightPad;
  80. currentOffset.y += rowHeight;
  81. }
  82. // select appropriate colors and textures
  83. ColorI fontColor;
  84. U32 buttonTextureIndex;
  85. S32 iconIndex = (*row)->mIconIndex;
  86. bool useHighlightIcon = (*row)->mUseHighlightIcon;
  87. if (!(*row)->mEnabled)
  88. {
  89. buttonTextureIndex = Profile::TEX_DISABLED;
  90. fontColor = profile->mFontColorNA;
  91. }
  92. else if (row == &mRows[mSelected])
  93. {
  94. if (iconIndex != NO_ICON)
  95. {
  96. iconIndex++;
  97. }
  98. buttonTextureIndex = Profile::TEX_SELECTED;
  99. fontColor = profile->mFontColorSEL;
  100. }
  101. else if ((mHighlighted != NO_ROW) && (row == &mRows[mHighlighted]))
  102. {
  103. if (iconIndex != NO_ICON && useHighlightIcon)
  104. {
  105. iconIndex++;
  106. }
  107. buttonTextureIndex = Profile::TEX_HIGHLIGHT;
  108. fontColor = profile->mFontColorHL;
  109. }
  110. else
  111. {
  112. buttonTextureIndex = Profile::TEX_NORMAL;
  113. fontColor = profile->mFontColor;
  114. }
  115. // render the row bitmap
  116. drawUtil->clearBitmapModulation();
  117. drawUtil->drawBitmapStretchSR(profile->mBitmap, RectI(currentOffset, rowExtent), profile->getBitmapArrayRect(buttonTextureIndex));
  118. // render the row icon if it has one
  119. if ((iconIndex != NO_ICON) && profileHasIcons && (!profile->getBitmapArrayRect((U32)iconIndex).extent.isZero()))
  120. {
  121. iconIndex += Profile::TEX_FIRST_ICON;
  122. drawUtil->clearBitmapModulation();
  123. drawUtil->drawBitmapStretchSR(profile->mBitmap, RectI(currentOffset + iconOffset, iconExtent), profile->getBitmapArrayRect(iconIndex));
  124. }
  125. // render the row text
  126. drawUtil->setBitmapModulation(fontColor);
  127. renderJustifiedText(currentOffset + textOffset, textExtent, (*row)->mLabel);
  128. if ((*row)->mMode == Row::Mode::OptionList)
  129. {
  130. onRenderListOption((*row), currentOffset);
  131. }
  132. else if ((*row)->mMode == Row::Mode::Slider)
  133. {
  134. onRenderSliderOption((*row), currentOffset);
  135. }
  136. else if ((*row)->mMode == Row::Mode::Keybind)
  137. {
  138. onRenderKeybindOption((*row), currentOffset);
  139. }
  140. }
  141. if (mDebugRender)
  142. {
  143. onDebugRender(offset);
  144. }
  145. renderChildControls(offset, updateRect);
  146. }
  147. void GuiGameListMenuCtrl::onRenderListOption(Row* row, Point2I currentOffset)
  148. {
  149. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  150. F32 xScale = (float)getWidth() / profile->getRowWidth();
  151. S32 rowHeight = profile->getRowHeight();
  152. bool profileHasArrows = profile->hasArrows();
  153. Point2I arrowExtent;
  154. S32 arrowOffsetY(0);
  155. if (profileHasArrows)
  156. {
  157. arrowExtent = profile->getArrowExtent();
  158. // icon is centered vertically
  159. arrowOffsetY = (rowHeight - arrowExtent.y) >> 1;
  160. }
  161. GFXDrawUtil* drawer = GFX->getDrawUtil();
  162. Point2I arrowOffset;
  163. S32 columnSplit = profile->mColumnSplit * xScale;
  164. S32 iconIndex;
  165. bool hasOptions = (row->mOptions.size() > 0) && row->mSelectedOption > -1;
  166. if (hasOptions)
  167. {
  168. bool isRowSelected = (getSelected() != NO_ROW) && (row == mRows[getSelected()]);
  169. bool isRowHighlighted = (getHighlighted() != NO_ROW) ? ((row == mRows[getHighlighted()]) && (row->mEnabled)) : false;
  170. if (profileHasArrows)
  171. {
  172. // render the left arrow
  173. bool arrowOnL = (isRowSelected || isRowHighlighted) && (row->mWrapOptions || (row->mSelectedOption > 0));
  174. iconIndex = (arrowOnL) ? Profile::TEX_L_ARROW_ON : Profile::TEX_L_ARROW_OFF;
  175. arrowOffset.x = currentOffset.x + columnSplit;
  176. arrowOffset.y = currentOffset.y + arrowOffsetY;
  177. drawer->clearBitmapModulation();
  178. drawer->drawBitmapStretchSR(profile->getBitmapResource(), RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  179. // render the right arrow
  180. bool arrowOnR = (isRowSelected || isRowHighlighted) && (row->mWrapOptions || (row->mSelectedOption < row->mOptions.size() - 1));
  181. iconIndex = (arrowOnR) ? Profile::TEX_R_ARROW_ON : Profile::TEX_R_ARROW_OFF;
  182. arrowOffset.x = currentOffset.x + (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale - arrowExtent.x;
  183. arrowOffset.y = currentOffset.y + arrowOffsetY;
  184. drawer->clearBitmapModulation();
  185. drawer->drawBitmapStretchSR(profile->getBitmapResource(), RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  186. }
  187. // get the appropriate font color
  188. ColorI fontColor;
  189. if (!row->mEnabled)
  190. {
  191. fontColor = profile->mFontColorNA;
  192. }
  193. else if (isRowSelected)
  194. {
  195. fontColor = profile->mFontColorSEL;
  196. }
  197. else if (isRowHighlighted)
  198. {
  199. fontColor = profile->mFontColorHL;
  200. }
  201. else
  202. {
  203. fontColor = profile->mFontColor;
  204. }
  205. // calculate text to be at the center between the arrows
  206. GFont* font = profile->mFont;
  207. StringTableEntry text = row->mOptions[row->mSelectedOption].mDisplayText;
  208. S32 textWidth = font->getStrWidth(text);
  209. S32 columnWidth = profile->mHitAreaLowerRight.x * xScale - profile->mRightPad - columnSplit;
  210. S32 columnCenter = columnSplit + (columnWidth >> 1);
  211. S32 textStartX = columnCenter - (textWidth >> 1);
  212. Point2I textOffset(textStartX, 0);
  213. // render the option text itself
  214. Point2I textExtent(columnWidth, rowHeight);
  215. drawer->setBitmapModulation(fontColor);
  216. renderJustifiedText(currentOffset + textOffset, textExtent, text);
  217. }
  218. }
  219. void GuiGameListMenuCtrl::onRenderSliderOption(Row* row, Point2I currentOffset)
  220. {
  221. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  222. F32 xScale = (float)getWidth() / profile->getRowWidth();
  223. S32 rowHeight = profile->getRowHeight();
  224. bool profileHasArrows = profile->hasArrows();
  225. Point2I arrowExtent;
  226. S32 arrowOffsetY(0);
  227. if (profileHasArrows)
  228. {
  229. arrowExtent = profile->getArrowExtent();
  230. // icon is centered vertically
  231. arrowOffsetY = (rowHeight - arrowExtent.y) >> 1;
  232. }
  233. GFXDrawUtil* drawer = GFX->getDrawUtil();
  234. Point2I arrowOffset;
  235. S32 columnSplit = profile->mColumnSplit * xScale;
  236. bool isRowSelected = (getSelected() != NO_ROW) && (row == mRows[getSelected()]);
  237. bool isRowHighlighted = (getHighlighted() != NO_ROW) ? ((row == mRows[getHighlighted()]) && (row->mEnabled)) : false;
  238. /*if (profileHasArrows)
  239. {
  240. // render the left arrow
  241. bool arrowOnL = (isRowSelected || isRowHighlighted) && (row->mValue > row->mRange.x);
  242. iconIndex = (arrowOnL) ? Profile::TEX_L_ARROW_ON : Profile::TEX_L_ARROW_OFF;
  243. arrowOffset.x = currentOffset.x + columnSplit;
  244. arrowOffset.y = currentOffset.y + arrowOffsetY;
  245. drawer->clearBitmapModulation();
  246. drawer->drawBitmapStretchSR(profile->mTextureObject, RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  247. // render the right arrow
  248. bool arrowOnR = (isRowSelected || isRowHighlighted) && (row->mValue < row->mRange.y);
  249. iconIndex = (arrowOnR) ? Profile::TEX_R_ARROW_ON : Profile::TEX_R_ARROW_OFF;
  250. arrowOffset.x = currentOffset.x + (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale - arrowExtent.x;
  251. arrowOffset.y = currentOffset.y + arrowOffsetY;
  252. drawer->clearBitmapModulation();
  253. drawer->drawBitmapStretchSR(profile->mTextureObject, RectI(arrowOffset, arrowExtent), profile->getBitmapArrayRect((U32)iconIndex));
  254. }*/
  255. //Draw the slider bar
  256. if (row->mEnabled)
  257. {
  258. RectI sliderRect;
  259. sliderRect.point.x = currentOffset.x + columnSplit + arrowExtent.x;
  260. sliderRect.point.y = currentOffset.y + arrowOffsetY;
  261. sliderRect.extent.x = (currentOffset.x + (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale - arrowExtent.x) - sliderRect.point.x;
  262. sliderRect.extent.y = arrowExtent.y;
  263. //Now adjust the bar to match-to our value
  264. S32 barStart = sliderRect.point.x;
  265. S32 barEnd = sliderRect.point.x + sliderRect.extent.x;
  266. S32 xPosFill = (((row->mValue - row->mRange.x) * (barEnd - barStart)) / (row->mRange.y - row->mRange.x)) + barStart;
  267. RectI fillRect = sliderRect;
  268. fillRect.extent.x = xPosFill - sliderRect.point.x;
  269. ColorI barColor;
  270. ColorI barOutlineColor;
  271. if (isRowSelected)
  272. {
  273. barColor = profile->mFillColor;
  274. barOutlineColor = profile->mFillColorSEL;
  275. }
  276. else
  277. {
  278. barColor = profile->mFillColor;
  279. barOutlineColor = profile->mFillColorHL;
  280. }
  281. drawer->drawRectFill(fillRect, barColor);
  282. drawer->drawRect(sliderRect, barOutlineColor);
  283. }
  284. // get the appropriate font color
  285. ColorI fontColor;
  286. if (!row->mEnabled)
  287. {
  288. fontColor = profile->mFontColorNA;
  289. }
  290. else if (isRowSelected)
  291. {
  292. fontColor = profile->mFontColorSEL;
  293. }
  294. else if (isRowHighlighted)
  295. {
  296. fontColor = profile->mFontColorHL;
  297. }
  298. else
  299. {
  300. fontColor = profile->mFontColor;
  301. }
  302. // calculate text to be at the center between the arrows
  303. GFont* font = profile->mFont;
  304. char stringVal[32];
  305. dSprintf(stringVal, 32, "%f", row->mValue);
  306. S32 textWidth = font->getStrWidth(stringVal);
  307. S32 columnWidth = profile->mHitAreaLowerRight.x * xScale - profile->mRightPad - columnSplit;
  308. S32 columnCenter = columnSplit + (columnWidth >> 1);
  309. S32 textStartX = columnCenter - (textWidth >> 1);
  310. Point2I textOffset(textStartX, 0);
  311. // render the option text itself
  312. Point2I textExtent(columnWidth, rowHeight);
  313. drawer->setBitmapModulation(fontColor);
  314. renderJustifiedText(currentOffset + textOffset, textExtent, stringVal);
  315. }
  316. void GuiGameListMenuCtrl::onRenderKeybindOption(Row* row, Point2I currentOffset)
  317. {
  318. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  319. F32 xScale = (float)getWidth() / profile->getRowWidth();
  320. S32 columnSplit = profile->mColumnSplit * xScale;
  321. S32 rowHeight = profile->getRowHeight();
  322. GFXDrawUtil* drawer = GFX->getDrawUtil();
  323. //drawer->drawBitmap(row->mBitmap, )
  324. Point2I button;
  325. button.x = currentOffset.x + columnSplit + (columnSplit / 2.5)/* + (optionWidth / 2)*/;
  326. button.y = currentOffset.y + (rowHeight / 4);
  327. Point2I buttonSize;
  328. buttonSize.x = rowHeight / 2;
  329. buttonSize.y = rowHeight / 2;
  330. if (row->mBitmapTex.isValid())
  331. {
  332. GFXTextureObject* texture = row->mBitmapTex;
  333. RectI rect(button, buttonSize);
  334. drawer->clearBitmapModulation();
  335. drawer->drawBitmapStretch(texture, rect, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  336. }
  337. //drawer->drawRectFill(button, ColorI::BLUE);
  338. }
  339. void GuiGameListMenuCtrl::onDebugRender(Point2I offset)
  340. {
  341. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  342. F32 xScale = (float) getWidth() / profile->getRowWidth();
  343. ColorI controlBorderColor(200, 200, 200); // gray
  344. ColorI rowBorderColor(255, 127, 255); // magenta
  345. ColorI hitBorderColor(255, 0, 0); // red
  346. Point2I shrinker(-1, -1);
  347. Point2I extent = getExtent();
  348. // render a border around the entire control
  349. RectI borderRect(offset, extent + shrinker);
  350. GFX->getDrawUtil()->drawRect(borderRect, controlBorderColor);
  351. S32 rowHeight = profile->getRowHeight();
  352. Point2I currentOffset(offset);
  353. Point2I rowExtent(extent.x, rowHeight);
  354. rowExtent += shrinker;
  355. Point2I hitAreaExtent(profile->getHitAreaExtent());
  356. hitAreaExtent.x *= xScale;
  357. hitAreaExtent += shrinker;
  358. Point2I hitAreaOffset = profile->mHitAreaUpperLeft;
  359. hitAreaOffset.x *= xScale;
  360. Point2I upperLeft;
  361. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  362. {
  363. // set the top of the current row
  364. if (row != mRows.begin())
  365. {
  366. // rows other than the first can have padding above them
  367. currentOffset.y += (*row)->mHeightPad;
  368. currentOffset.y += rowHeight;
  369. }
  370. // draw the box around the whole row's extent
  371. upperLeft = currentOffset;
  372. borderRect.point = upperLeft;
  373. borderRect.extent = rowExtent;
  374. GFX->getDrawUtil()->drawRect(borderRect, rowBorderColor);
  375. // draw the box around the hit area of the row
  376. upperLeft = currentOffset + hitAreaOffset;
  377. borderRect.point = upperLeft;
  378. borderRect.extent = hitAreaExtent;
  379. GFX->getDrawUtil()->drawRect(borderRect, hitBorderColor);
  380. }
  381. }
  382. void GuiGameListMenuCtrl::addRow(const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, S32 mode, const char* tooltip)
  383. {
  384. Row * row = new Row();
  385. addRow(row, label, callback, icon, yPad, useHighlightIcon, enabled, mode, tooltip);
  386. }
  387. void GuiGameListMenuCtrl::addRow(Row * row, const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, S32 mode, const char* tooltip)
  388. {
  389. row->mLabel = StringTable->insert(label, true);
  390. row->mScriptCallback = (dStrlen(callback) > 0) ? StringTable->insert(callback, true) : NULL;
  391. row->mIconIndex = (icon < 0) ? NO_ICON : icon;
  392. row->mHeightPad = yPad;
  393. row->mUseHighlightIcon = useHighlightIcon;
  394. row->mEnabled = enabled;
  395. row->mMode = (Row::Mode)mode;
  396. row->mTooltip = StringTable->insert(tooltip);
  397. mRows.push_back(row);
  398. updateHeight();
  399. if (mSelected == NO_ROW)
  400. {
  401. selectFirstEnabledRow();
  402. }
  403. }
  404. void GuiGameListMenuCtrl::addRow(const char* label, const char* optionsList, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip, const char* defaultValue)
  405. {
  406. static StringTableEntry DELIM = StringTable->insert("\t", true);
  407. Row* row = new Row();
  408. Vector<OptionEntry> options(__FILE__, __LINE__);
  409. S32 defaultOption = 0;
  410. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  411. for (S32 i = 0; i < count; ++i)
  412. {
  413. OptionEntry e;
  414. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  415. e.mDisplayText = StringTable->insert(option, true);
  416. e.mKeyString = e.mDisplayText;
  417. options.push_back(e);
  418. if (String::compare(option, defaultValue) == 0)
  419. defaultOption = options.size() - 1;
  420. }
  421. row->mOptions = options;
  422. bool hasOptions = row->mOptions.size() > 0;
  423. row->mSelectedOption = (hasOptions) ? defaultOption : NO_OPTION;
  424. row->mWrapOptions = wrapOptions;
  425. addRow(row, label, callback, icon, yPad, true, (hasOptions) ? enabled : false, Row::Mode::OptionList, tooltip);
  426. }
  427. void GuiGameListMenuCtrl::addRow(const char* label, F32 defaultValue, F32 increments, Point2F range, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip)
  428. {
  429. static StringTableEntry DELIM = StringTable->insert("\t", true);
  430. Row* row = new Row();
  431. row->mValue = defaultValue;
  432. row->mStepSize = increments;
  433. row->mRange = range;
  434. addRow(row, label, callback, icon, yPad, true, enabled, Row::Mode::Slider, tooltip);
  435. }
  436. void GuiGameListMenuCtrl::addRow(const char* label, const char* bitmapName, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip)
  437. {
  438. static StringTableEntry DELIM = StringTable->insert("\t", true);
  439. Row* row = new Row();
  440. row->mBitmap = StringTable->insert(bitmapName);
  441. if(row->mBitmap != StringTable->EmptyString())
  442. row->mBitmapTex.set(row->mBitmap, &GFXDefaultGUIProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
  443. addRow(row, label, callback, icon, yPad, true, enabled, Row::Mode::Keybind, tooltip);
  444. }
  445. void GuiGameListMenuCtrl::removeRow(const S32& row)
  446. {
  447. if (row == -1 || row >= mRows.size())
  448. return;
  449. mRows.erase(row);
  450. }
  451. Point2I GuiGameListMenuCtrl::getMinExtent() const
  452. {
  453. Point2I parentMin = Parent::getMinExtent();
  454. GuiGameListMenuProfile * profile = dynamic_cast<GuiGameListMenuProfile*>(mProfile);
  455. AssertFatal(profile, "Invalid profile for GuiGameListMenuCtrl!");
  456. S32 minHeight = 0;
  457. S32 rowHeight = profile->getRowHeight();
  458. for (Vector<Row *>::const_iterator row = mRows.begin(); row < mRows.end(); ++row)
  459. {
  460. minHeight += rowHeight;
  461. if (row != mRows.begin())
  462. {
  463. minHeight += (*row)->mHeightPad;
  464. }
  465. }
  466. if (minHeight > parentMin.y)
  467. parentMin.y = minHeight;
  468. return parentMin;
  469. }
  470. bool GuiGameListMenuCtrl::onAdd()
  471. {
  472. if( !Parent::onAdd() )
  473. return false;
  474. // If we have a non-GuiGameListMenuProfile profile, try to
  475. // substitute it for DefaultListMenuProfile.
  476. if( !hasValidProfile() )
  477. {
  478. GuiGameListMenuProfile* profile;
  479. if( !Sim::findObject( "DefaultListMenuProfile", profile ) )
  480. {
  481. Con::errorf( "GuiGameListMenuCtrl: %s can't be created with a profile of type %s. Please create it with a profile of type GuiGameListMenuProfile.",
  482. getName(), mProfile->getClassName() );
  483. return false;
  484. }
  485. else
  486. Con::warnf( "GuiGameListMenuCtrl: substituted non-GuiGameListMenuProfile in %s for DefaultListMenuProfile", getName() );
  487. setControlProfile( profile );
  488. }
  489. return true;
  490. }
  491. bool GuiGameListMenuCtrl::onWake()
  492. {
  493. if( !Parent::onWake() )
  494. return false;
  495. if( !hasValidProfile() )
  496. return false;
  497. /*if( mRows.empty() )
  498. {
  499. Con::errorf( "GuiGameListMenuCtrl: %s can't be woken up without any rows. Please use \"addRow\" to add at least one row to the control before pushing it to the canvas.",
  500. getName() );
  501. return false;
  502. }*/
  503. enforceConstraints();
  504. selectFirstEnabledRow();
  505. setFirstResponder();
  506. mHighlighted = NO_ROW;
  507. return true;
  508. }
  509. bool GuiGameListMenuCtrl::hasValidProfile() const
  510. {
  511. GuiGameListMenuProfile * profile = dynamic_cast<GuiGameListMenuProfile *>(mProfile);
  512. return profile;
  513. }
  514. void GuiGameListMenuCtrl::enforceConstraints()
  515. {
  516. if( hasValidProfile() )
  517. {
  518. ((GuiGameListMenuProfile *)mProfile)->enforceConstraints();
  519. }
  520. updateHeight();
  521. }
  522. void GuiGameListMenuCtrl::updateHeight()
  523. {
  524. if (hasValidProfile())
  525. {
  526. S32 minHeight = getMinExtent().y;
  527. if (getHeight() < minHeight)
  528. {
  529. setHeight(minHeight);
  530. }
  531. }
  532. }
  533. void GuiGameListMenuCtrl::onMouseDown(const GuiEvent &event)
  534. {
  535. S32 hitRow = getRow(event.mousePoint);
  536. if (hitRow != NO_ROW)
  537. {
  538. S32 delta = (mSelected != NO_ROW) ? (hitRow - mSelected) : (mSelected + 1);
  539. changeRow(delta);
  540. }
  541. }
  542. void GuiGameListMenuCtrl::onMouseLeave(const GuiEvent &event)
  543. {
  544. mHighlighted = NO_ROW;
  545. }
  546. void GuiGameListMenuCtrl::onMouseMove(const GuiEvent &event)
  547. {
  548. S32 hitRow = getRow(event.mousePoint);
  549. // allow mHighligetd to be set to NO_ROW so rows can be unhighlighted
  550. mHighlighted = hitRow;
  551. }
  552. void GuiGameListMenuCtrl::onMouseUp(const GuiEvent &event)
  553. {
  554. S32 hitRow = getRow(event.mousePoint);
  555. if ((hitRow != NO_ROW) && isRowEnabled(hitRow) && (hitRow == getSelected()))
  556. {
  557. if (mRows[hitRow]->mMode == Row::Mode::Default)
  558. {
  559. activateRow();
  560. }
  561. else if (mRows[hitRow]->mMode == Row::Mode::OptionList)
  562. {
  563. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  564. clickOption((Row*)mRows[getSelected()], xPos);
  565. }
  566. else if (mRows[hitRow]->mMode == Row::Mode::Slider)
  567. {
  568. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  569. clickSlider((Row*)mRows[getSelected()], xPos);
  570. }
  571. else if (mRows[hitRow]->mMode == Row::Mode::Keybind)
  572. {
  573. S32 xPos = globalToLocalCoord(event.mousePoint).x;
  574. clickKeybind((Row*)mRows[getSelected()], xPos);
  575. }
  576. }
  577. }
  578. void GuiGameListMenuCtrl::activateRow()
  579. {
  580. S32 row = getSelected();
  581. if ((row != NO_ROW) && isRowEnabled(row) && (mRows[row]->mScriptCallback != NULL))
  582. {
  583. setThisControl();
  584. if (Con::isFunction(mRows[row]->mScriptCallback))
  585. {
  586. Con::executef(mRows[row]->mScriptCallback);
  587. }
  588. }
  589. }
  590. S32 GuiGameListMenuCtrl::getRow(Point2I globalPoint)
  591. {
  592. Point2I localPoint = globalToLocalCoord(globalPoint);
  593. GuiGameListMenuProfile * profile = (GuiGameListMenuProfile *) mProfile;
  594. F32 xScale = (float) getWidth() / profile->getRowWidth();
  595. S32 rowHeight = profile->getRowHeight();
  596. Point2I currentOffset(0, 0);
  597. Point2I hitAreaUpperLeft = profile->mHitAreaUpperLeft;
  598. hitAreaUpperLeft.x *= xScale;
  599. Point2I hitAreaLowerRight = profile->mHitAreaLowerRight;
  600. hitAreaLowerRight.x *= xScale;
  601. Point2I upperLeft, lowerRight;
  602. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  603. {
  604. if (row != mRows.begin())
  605. {
  606. // rows other than the first can have padding above them
  607. currentOffset.y += (*row)->mHeightPad;
  608. }
  609. upperLeft = currentOffset + hitAreaUpperLeft;
  610. lowerRight = currentOffset + hitAreaLowerRight;
  611. if ((upperLeft.x <= localPoint.x) && (localPoint.x < lowerRight.x) &&
  612. (upperLeft.y <= localPoint.y) && (localPoint.y < lowerRight.y))
  613. {
  614. return row - mRows.begin();
  615. }
  616. currentOffset.y += rowHeight;
  617. }
  618. return NO_ROW;
  619. }
  620. void GuiGameListMenuCtrl::setSelected(S32 index)
  621. {
  622. if (index == NO_ROW)
  623. {
  624. // deselection
  625. mSelected = NO_ROW;
  626. return;
  627. }
  628. if (! isValidRowIndex(index))
  629. {
  630. return;
  631. }
  632. if (! isRowEnabled(index))
  633. {
  634. // row is disabled, it can't be selected
  635. return;
  636. }
  637. mSelected = mClamp(index, 0, mRows.size() - 1);
  638. //If we're childed to a scroll container, make sure us changing rows has our new position visible
  639. GuiScrollCtrl* scroll = dynamic_cast<GuiScrollCtrl*>(getParent());
  640. if (scroll)
  641. {
  642. scroll->scrollRectVisible(getRowBounds(mSelected));
  643. }
  644. }
  645. bool GuiGameListMenuCtrl::isRowEnabled(S32 index) const
  646. {
  647. if (! isValidRowIndex(index))
  648. {
  649. return false;
  650. }
  651. return mRows[index]->mEnabled;
  652. }
  653. void GuiGameListMenuCtrl::setRowEnabled(S32 index, bool enabled)
  654. {
  655. if (! isValidRowIndex(index))
  656. {
  657. return;
  658. }
  659. mRows[index]->mEnabled = enabled;
  660. if (getSelected() == index)
  661. {
  662. selectFirstEnabledRow();
  663. }
  664. }
  665. bool GuiGameListMenuCtrl::isValidRowIndex(S32 index) const
  666. {
  667. return ((0 <= index) && (index < mRows.size()));
  668. }
  669. void GuiGameListMenuCtrl::selectFirstEnabledRow()
  670. {
  671. setSelected(NO_ROW);
  672. for (Vector<Row *>::iterator row = mRows.begin(); row < mRows.end(); ++row)
  673. {
  674. if ((*row)->mEnabled)
  675. {
  676. setSelected(row - mRows.begin());
  677. return;
  678. }
  679. }
  680. }
  681. bool GuiGameListMenuCtrl::onInputEvent(const InputEventInfo& event)
  682. {
  683. if (mCallbackOnInputs)
  684. {
  685. char deviceStr[32];
  686. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceStr))
  687. return false;
  688. String deviceString = deviceStr;
  689. if (event.action == SI_MAKE || event.action == SI_BREAK)
  690. {
  691. bool isModifier = false;
  692. bool state = event.action == SI_MAKE;
  693. switch (event.objInst)
  694. {
  695. case KEY_LCONTROL:
  696. case KEY_RCONTROL:
  697. case KEY_LALT:
  698. case KEY_RALT:
  699. case KEY_LSHIFT:
  700. case KEY_RSHIFT:
  701. case KEY_MAC_LOPT:
  702. case KEY_MAC_ROPT:
  703. isModifier = true;
  704. }
  705. if ((event.objType == SI_KEY) && isModifier)
  706. {
  707. char keyString[32];
  708. if (!ActionMap::getKeyString(event.objInst, keyString))
  709. return false;
  710. onInputEvent_callback(deviceString.c_str(), keyString, state);
  711. if (mConsumeKeyInputEvents)
  712. {
  713. if(deviceString.startsWith("keyboard"))
  714. return true;
  715. }
  716. }
  717. else
  718. {
  719. const char* actionString = ActionMap::buildActionString(&event);
  720. onInputEvent_callback(deviceString.c_str(), actionString, state);
  721. if (mConsumeKeyInputEvents)
  722. {
  723. if (deviceString.startsWith("keyboard") || deviceString.startsWith("gamepad"))
  724. return true;
  725. }
  726. }
  727. }
  728. else if (event.objType == SI_AXIS || event.objType == SI_INT || event.objType == SI_FLOAT)
  729. {
  730. F32 fValue = event.fValue;
  731. if (event.objType == SI_INT)
  732. fValue = (F32)event.iValue;
  733. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceStr))
  734. return false;
  735. const char* actionString = ActionMap::buildActionString(&event);
  736. onAxisEvent_callback(deviceStr, actionString, fValue);
  737. }
  738. }
  739. return false;
  740. }
  741. bool GuiGameListMenuCtrl::onKeyDown(const GuiEvent &event)
  742. {
  743. switch (event.keyCode)
  744. {
  745. case KEY_UP:
  746. changeRow(-1);
  747. return true;
  748. case KEY_DOWN:
  749. changeRow(1);
  750. return true;
  751. case KEY_LEFT:
  752. changeOption(-1);
  753. return true;
  754. case KEY_RIGHT:
  755. changeOption(1);
  756. return true;
  757. case KEY_A:
  758. case KEY_RETURN:
  759. case KEY_NUMPADENTER:
  760. case KEY_SPACE:
  761. case XI_A:
  762. case XI_START:
  763. doScriptCommand(mCallbackOnA);
  764. return true;
  765. case KEY_B:
  766. case KEY_ESCAPE:
  767. case KEY_BACKSPACE:
  768. case KEY_DELETE:
  769. case XI_B:
  770. case XI_BACK:
  771. doScriptCommand(mCallbackOnB);
  772. return true;
  773. case KEY_X:
  774. case XI_X:
  775. doScriptCommand(mCallbackOnX);
  776. return true;
  777. case KEY_Y:
  778. case XI_Y:
  779. doScriptCommand(mCallbackOnY);
  780. return true;
  781. default:
  782. break;
  783. }
  784. return Parent::onKeyDown(event);
  785. }
  786. bool GuiGameListMenuCtrl::onGamepadAxisUp(const GuiEvent &event)
  787. {
  788. changeRow(-1);
  789. return true;
  790. }
  791. bool GuiGameListMenuCtrl::onGamepadAxisDown(const GuiEvent &event)
  792. {
  793. changeRow(1);
  794. return true;
  795. }
  796. bool GuiGameListMenuCtrl::onGamepadAxisLeft(const GuiEvent& event)
  797. {
  798. changeOption(-1);
  799. return true;
  800. }
  801. bool GuiGameListMenuCtrl::onGamepadAxisRight(const GuiEvent& event)
  802. {
  803. changeOption(1);
  804. return true;
  805. }
  806. void GuiGameListMenuCtrl::doScriptCommand(StringTableEntry command)
  807. {
  808. if (command && command[0])
  809. {
  810. setThisControl();
  811. Con::evaluate(command, false, __FILE__);
  812. }
  813. }
  814. void GuiGameListMenuCtrl::changeRow(S32 delta)
  815. {
  816. S32 oldRowIndex = getSelected();
  817. S32 newRowIndex = oldRowIndex;
  818. do
  819. {
  820. newRowIndex += delta;
  821. if (newRowIndex >= mRows.size())
  822. {
  823. newRowIndex = 0;
  824. }
  825. else if (newRowIndex < 0)
  826. {
  827. newRowIndex = mRows.size() - 1;
  828. }
  829. }
  830. while ((! mRows[newRowIndex]->mEnabled) && (newRowIndex != oldRowIndex));
  831. setSelected(newRowIndex);
  832. // do the callback
  833. onChange_callback();
  834. }
  835. void GuiGameListMenuCtrl::setThisControl()
  836. {
  837. smThisControl = this;
  838. }
  839. StringTableEntry GuiGameListMenuCtrl::getRowLabel(S32 rowIndex) const
  840. {
  841. if (! isValidRowIndex(rowIndex))
  842. {
  843. // not a valid row index, don't do anything
  844. return StringTable->EmptyString();
  845. }
  846. return mRows[rowIndex]->mLabel;
  847. }
  848. void GuiGameListMenuCtrl::setRowLabel(S32 rowIndex, const char * label)
  849. {
  850. if (! isValidRowIndex(rowIndex))
  851. {
  852. // not a valid row index, don't do anything
  853. return;
  854. }
  855. mRows[rowIndex]->mLabel = StringTable->insert(label, true);
  856. }
  857. void GuiGameListMenuCtrl::clearRows()
  858. {
  859. for (U32 i = 0; i < mRows.size(); i++)
  860. {
  861. if (mRows[i]->mBitmap != StringTable->EmptyString())
  862. mRows[i]->mBitmapTex = nullptr;
  863. }
  864. mRows.clear();
  865. setSelected(-1);
  866. setHeight(mMinExtent.y);
  867. }
  868. void GuiGameListMenuCtrl::refresh()
  869. {
  870. enforceConstraints();
  871. }
  872. RectI GuiGameListMenuCtrl::getRowBounds(S32 rowIndex)
  873. {
  874. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  875. S32 rowHeight = profile->getRowHeight();
  876. Point2I currentOffset = Point2I::Zero;
  877. Point2I extent = getExtent();
  878. Point2I rowExtent(extent.x, rowHeight);
  879. for (U32 i = 1; i <= rowIndex; i++)
  880. {
  881. //the top row can't pad, so we'll ignore it
  882. GuiGameListMenuCtrl::Row* row = mRows[i];
  883. // rows other than the first can have padding above them
  884. currentOffset.y += row->mHeightPad;
  885. currentOffset.y += rowHeight;
  886. }
  887. return RectI(currentOffset, rowExtent);
  888. }
  889. //-----------------------------------------------------------------------------
  890. // Console stuff (GuiGameListMenuCtrl)
  891. //-----------------------------------------------------------------------------
  892. StringTableEntry GuiGameListMenuCtrl::getCurrentOption(S32 rowIndex) const
  893. {
  894. if (isValidRowIndex(rowIndex))
  895. {
  896. Row* row = (Row*)mRows[rowIndex];
  897. if (row->mSelectedOption != NO_OPTION)
  898. {
  899. return row->mOptions[row->mSelectedOption].mDisplayText;
  900. }
  901. }
  902. return StringTable->insert("", false);
  903. }
  904. StringTableEntry GuiGameListMenuCtrl::getCurrentOptionKey(S32 rowIndex) const
  905. {
  906. if (isValidRowIndex(rowIndex))
  907. {
  908. Row* row = (Row*)mRows[rowIndex];
  909. if (row->mSelectedOption != NO_OPTION)
  910. {
  911. return row->mOptions[row->mSelectedOption].mKeyString;
  912. }
  913. }
  914. return StringTable->insert("", false);
  915. }
  916. S32 GuiGameListMenuCtrl::getCurrentOptionIndex(S32 rowIndex) const
  917. {
  918. if (isValidRowIndex(rowIndex))
  919. {
  920. Row* row = (Row*)mRows[rowIndex];
  921. if (row->mSelectedOption != NO_OPTION)
  922. {
  923. return row->mSelectedOption;
  924. }
  925. }
  926. return S32(-1);
  927. }
  928. bool GuiGameListMenuCtrl::selectOption(S32 rowIndex, const char* theOption)
  929. {
  930. if (!isValidRowIndex(rowIndex))
  931. {
  932. return false;
  933. }
  934. Row* row = (Row*)mRows[rowIndex];
  935. for (Vector<OptionEntry>::iterator anOption = row->mOptions.begin(); anOption < row->mOptions.end(); ++anOption)
  936. {
  937. if (String::compare((*anOption).mDisplayText, theOption) == 0)
  938. {
  939. S32 newIndex = anOption - row->mOptions.begin();
  940. row->mSelectedOption = newIndex;
  941. return true;
  942. }
  943. }
  944. return false;
  945. }
  946. bool GuiGameListMenuCtrl::selectOptionByKey(S32 rowIndex, const char* optionKey)
  947. {
  948. if (!isValidRowIndex(rowIndex))
  949. {
  950. return false;
  951. }
  952. Row* row = (Row*)mRows[rowIndex];
  953. for (Vector<OptionEntry>::iterator anOption = row->mOptions.begin(); anOption < row->mOptions.end(); ++anOption)
  954. {
  955. if (String::compare((*anOption).mKeyString, optionKey) == 0)
  956. {
  957. S32 newIndex = anOption - row->mOptions.begin();
  958. row->mSelectedOption = newIndex;
  959. return true;
  960. }
  961. }
  962. return false;
  963. }
  964. bool GuiGameListMenuCtrl::selectOptionByIndex(S32 rowIndex, S32 optionIndex)
  965. {
  966. if (!isValidRowIndex(rowIndex) || (optionIndex < 0))
  967. {
  968. return false;
  969. }
  970. Row* row = (Row*)mRows[rowIndex];
  971. if (optionIndex < row->mOptions.size())
  972. {
  973. row->mSelectedOption = optionIndex;
  974. return true;
  975. }
  976. return false;
  977. }
  978. void GuiGameListMenuCtrl::setOptions(S32 rowIndex, const char* optionsList)
  979. {
  980. static StringTableEntry DELIM = StringTable->insert("\t", true);
  981. if (!isValidRowIndex(rowIndex))
  982. {
  983. return;
  984. }
  985. Row* row = (Row*)mRows[rowIndex];
  986. S32 count = StringUnit::getUnitCount(optionsList, DELIM);
  987. row->mOptions.setSize(count);
  988. for (S32 i = 0; i < count; ++i)
  989. {
  990. const char* option = StringUnit::getUnit(optionsList, i, DELIM);
  991. OptionEntry e;
  992. e.mDisplayText = StringTable->insert(option, true);
  993. e.mKeyString = e.mDisplayText;
  994. row->mOptions[i] = e;
  995. }
  996. if (row->mSelectedOption >= row->mOptions.size())
  997. {
  998. row->mSelectedOption = row->mOptions.size() - 1;
  999. }
  1000. }
  1001. void GuiGameListMenuCtrl::addOption(S32 rowIndex, const char* displayText, const char* keyText)
  1002. {
  1003. if (!isValidRowIndex(rowIndex))
  1004. {
  1005. return;
  1006. }
  1007. OptionEntry e;
  1008. e.mDisplayText = StringTable->insert(displayText, true);
  1009. e.mKeyString = (keyText[0] == '\0') ? e.mDisplayText : StringTable->insert(keyText, true);
  1010. Row* row = (Row*)mRows[rowIndex];
  1011. row->mOptions.push_back(e);
  1012. }
  1013. void GuiGameListMenuCtrl::clickOption(Row* row, S32 xPos)
  1014. {
  1015. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  1016. if (!profile->hasArrows())
  1017. {
  1018. return;
  1019. }
  1020. F32 xScale = (float)getWidth() / profile->getRowWidth();
  1021. S32 bitmapArrowWidth = mProfile->getBitmapArrayRect(Profile::TEX_FIRST_ARROW).extent.x;
  1022. S32 leftArrowX1 = profile->mColumnSplit * xScale;
  1023. S32 leftArrowX2 = leftArrowX1 + bitmapArrowWidth;
  1024. S32 rightArrowX2 = (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale;
  1025. S32 rightArrowX1 = rightArrowX2 - bitmapArrowWidth;
  1026. if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  1027. {
  1028. changeOption(row, -1);
  1029. }
  1030. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  1031. {
  1032. changeOption(row, 1);
  1033. }
  1034. }
  1035. void GuiGameListMenuCtrl::changeOption(S32 delta)
  1036. {
  1037. if (getSelected() != NO_ROW)
  1038. {
  1039. Row* row = (Row*)mRows[getSelected()];
  1040. changeOption(row, delta);
  1041. }
  1042. }
  1043. void GuiGameListMenuCtrl::changeOption(Row* row, S32 delta)
  1044. {
  1045. S32 optionCount = row->mOptions.size();
  1046. S32 newSelection = row->mSelectedOption + delta;
  1047. if (optionCount == 0)
  1048. {
  1049. newSelection = NO_OPTION;
  1050. }
  1051. else if (!row->mWrapOptions)
  1052. {
  1053. newSelection = mClamp(newSelection, 0, optionCount - 1);
  1054. }
  1055. else if (newSelection < 0)
  1056. {
  1057. newSelection = optionCount - 1;
  1058. }
  1059. else if (newSelection >= optionCount)
  1060. {
  1061. newSelection = 0;
  1062. }
  1063. row->mSelectedOption = newSelection;
  1064. if (row->mMode == GuiGameListMenuCtrl::Row::Slider)
  1065. {
  1066. row->mValue += row->mStepSize * delta;
  1067. row->mValue = mRound(row->mValue / row->mStepSize) * row->mStepSize;
  1068. if (row->mValue < row->mRange.x)
  1069. row->mValue = row->mRange.x;
  1070. if (row->mValue > row->mRange.y)
  1071. row->mValue = row->mRange.y;
  1072. }
  1073. static StringTableEntry LEFT = StringTable->insert("LEFT", true);
  1074. static StringTableEntry RIGHT = StringTable->insert("RIGHT", true);
  1075. if (row->mScriptCallback != NULL && (row->mSelectedOption != NO_OPTION && row->mMode != GuiGameListMenuCtrl::Row::Slider))
  1076. {
  1077. setThisControl();
  1078. StringTableEntry direction = NULL;
  1079. if (delta < 0)
  1080. {
  1081. direction = LEFT;
  1082. }
  1083. else if (delta > 0)
  1084. {
  1085. direction = RIGHT;
  1086. }
  1087. if ((direction != NULL) && (Con::isFunction(row->mScriptCallback)))
  1088. {
  1089. Con::executef(row->mScriptCallback, direction);
  1090. }
  1091. }
  1092. }
  1093. IMPLEMENT_CONOBJECT(GuiGameListMenuCtrl);
  1094. void GuiGameListMenuCtrl::clickSlider(Row* row, S32 xPos)
  1095. {
  1096. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  1097. if (!profile->hasArrows())
  1098. {
  1099. return;
  1100. }
  1101. F32 xScale = (float)getWidth() / profile->getRowWidth();
  1102. S32 bitmapArrowWidth = mProfile->getBitmapArrayRect(Profile::TEX_FIRST_ARROW).extent.x;
  1103. S32 leftArrowX1 = profile->mColumnSplit * xScale;
  1104. S32 leftArrowX2 = leftArrowX1 + bitmapArrowWidth;
  1105. S32 rightArrowX2 = (profile->mHitAreaLowerRight.x - profile->mRightPad) * xScale;
  1106. S32 rightArrowX1 = rightArrowX2 - bitmapArrowWidth;
  1107. if ((leftArrowX1 <= xPos) && (xPos <= leftArrowX2))
  1108. {
  1109. row->mValue -= row->mStepSize;
  1110. row->mValue = mRound(row->mValue / row->mStepSize) * row->mStepSize;
  1111. if (row->mValue < row->mRange.x)
  1112. row->mValue = row->mRange.x;
  1113. }
  1114. else if ((rightArrowX1 <= xPos) && (xPos <= rightArrowX2))
  1115. {
  1116. //F32 snap = row->mValue % row->mStepSize;
  1117. //row->mValue.y -= snap;
  1118. row->mValue += row->mStepSize;
  1119. row->mValue = mRound(row->mValue / row->mStepSize) * row->mStepSize;
  1120. if (row->mValue > row->mRange.y)
  1121. row->mValue = row->mRange.y;
  1122. }
  1123. else
  1124. {
  1125. //see if we clicked on the sliderbar itself
  1126. S32 barStart = leftArrowX2;
  1127. S32 barEnd = rightArrowX1;
  1128. if (xPos >= barStart && xPos <= barEnd)
  1129. {
  1130. //find the position
  1131. F32 newValue = (((xPos - barStart) * (row->mRange.y - row->mRange.x)) / (barEnd - barStart)) + row->mRange.x;
  1132. newValue = mRound(newValue / row->mStepSize) * row->mStepSize;
  1133. row->mValue = newValue;
  1134. }
  1135. }
  1136. }
  1137. void GuiGameListMenuCtrl::clickKeybind(Row* row, S32 xPos)
  1138. {
  1139. GuiGameListMenuProfile* profile = (GuiGameListMenuProfile*)mProfile;
  1140. F32 xScale = (float)getWidth() / profile->getRowWidth();
  1141. S32 columnSplit = profile->mColumnSplit * xScale;
  1142. S32 rowHeight = profile->getRowHeight();
  1143. //drawer->drawBitmap(row->mBitmap, )
  1144. Point2I button;
  1145. button.x = columnSplit + (columnSplit / 2)/* + (optionWidth / 2)*/;
  1146. button.y = rowHeight / 4;
  1147. Point2I buttonSize;
  1148. buttonSize.x = rowHeight / 2;
  1149. buttonSize.y = rowHeight / 2;
  1150. RectI rect(button, buttonSize);
  1151. if (rect.pointInRect(Point2I(xPos, rowHeight / 2)))
  1152. {
  1153. if (row->mScriptCallback != StringTable->EmptyString())
  1154. {
  1155. S32 rowId = getSelected();
  1156. Con::executef(row->mScriptCallback, rowId);
  1157. }
  1158. }
  1159. }
  1160. F32 GuiGameListMenuCtrl::getValue(S32 rowIndex)
  1161. {
  1162. if (!isValidRowIndex(rowIndex))
  1163. {
  1164. return 0;
  1165. }
  1166. Row* row = (Row*)mRows[rowIndex];
  1167. return row->mValue;
  1168. }
  1169. void GuiGameListMenuCtrl::setValue(S32 rowIndex, F32 value)
  1170. {
  1171. if (!isValidRowIndex(rowIndex))
  1172. {
  1173. return;
  1174. }
  1175. Row* row = (Row*)mRows[rowIndex];
  1176. row->mValue = value;
  1177. }
  1178. const char* GuiGameListMenuCtrl::getTooltip(S32 rowIndex)
  1179. {
  1180. if (!isValidRowIndex(rowIndex))
  1181. {
  1182. return "";
  1183. }
  1184. Row* row = (Row*)mRows[rowIndex];
  1185. return row->mTooltip;
  1186. }
  1187. ConsoleDocClass( GuiGameListMenuCtrl,
  1188. "@brief A base class for cross platform menu controls that are gamepad friendly.\n\n"
  1189. "This class is used to build row-based menu GUIs that can be easily navigated "
  1190. "using the keyboard, mouse or gamepad. The desired row can be selected using "
  1191. "the mouse, or by navigating using the Up and Down buttons.\n\n"
  1192. "@tsexample\n\n"
  1193. "new GuiGameListMenuCtrl()\n"
  1194. "{\n"
  1195. " debugRender = \"0\";\n"
  1196. " callbackOnA = \"applyOptions();\";\n"
  1197. " callbackOnB = \"Canvas.setContent(MainMenuGui);\";\n"
  1198. " callbackOnX = \"\";\n"
  1199. " callbackOnY = \"revertOptions();\";\n"
  1200. " //Properties not specific to this control have been omitted from this example.\n"
  1201. "};\n"
  1202. "@endtsexample\n\n"
  1203. "@see GuiGameListMenuProfile\n\n"
  1204. "@ingroup GuiGame"
  1205. );
  1206. IMPLEMENT_CALLBACK( GuiGameListMenuCtrl, onChange, void, (), (),
  1207. "Called when the selected row changes." );
  1208. IMPLEMENT_CALLBACK(GuiGameListMenuCtrl, onInputEvent, void, (const char* device, const char* action, bool state),
  1209. (device, action, state),
  1210. "@brief Callback that occurs when an input is triggered on this control\n\n"
  1211. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  1212. "@param action The actual event occuring, such as a key or button\n"
  1213. "@param state True if the action is being pressed, false if it is being release\n\n");
  1214. IMPLEMENT_CALLBACK(GuiGameListMenuCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  1215. (device, action, axisValue),
  1216. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  1217. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  1218. "@param action The ActionMap code for the axis\n"
  1219. "@param axisValue The current value of the axis\n\n");
  1220. void GuiGameListMenuCtrl::initPersistFields()
  1221. {
  1222. addField("debugRender", TypeBool, Offset(mDebugRender, GuiGameListMenuCtrl),
  1223. "Enable debug rendering" );
  1224. addField("callbackOnA", TypeString, Offset(mCallbackOnA, GuiGameListMenuCtrl),
  1225. "Script callback when the 'A' button is pressed. 'A' inputs are Keyboard: A, Return, Space; Gamepad: A, Start" );
  1226. addField("callbackOnB", TypeString, Offset(mCallbackOnB, GuiGameListMenuCtrl),
  1227. "Script callback when the 'B' button is pressed. 'B' inputs are Keyboard: B, Esc, Backspace, Delete; Gamepad: B, Back" );
  1228. addField("callbackOnX", TypeString, Offset(mCallbackOnX, GuiGameListMenuCtrl),
  1229. "Script callback when the 'X' button is pressed. 'X' inputs are Keyboard: X; Gamepad: X" );
  1230. addField("callbackOnY", TypeString, Offset(mCallbackOnY, GuiGameListMenuCtrl),
  1231. "Script callback when the 'Y' button is pressed. 'Y' inputs are Keyboard: Y; Gamepad: Y" );
  1232. addField("callbackOnInputs", TypeBool, Offset(mCallbackOnInputs, GuiGameListMenuCtrl),
  1233. "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.");
  1234. addField("consumeKeyInputEvents", TypeBool, Offset(mConsumeKeyInputEvents, GuiGameListMenuCtrl),
  1235. "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.");
  1236. Parent::initPersistFields();
  1237. }
  1238. DefineEngineMethod( GuiGameListMenuCtrl, addRow, void,
  1239. ( const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, int mode ),
  1240. ( -1, 0, true, true, 0 ),
  1241. "Add a row to the list control.\n\n"
  1242. "@param label The text to display on the row as a label.\n"
  1243. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  1244. "@param icon [optional] Index of the icon to use as a marker.\n"
  1245. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  1246. "@param useHighlightIcon [optional] Does this row use the highlight icon?.\n"
  1247. "@param enabled [optional] If this row is initially enabled.\n"
  1248. "@param mode [optional] What option mode the row is in. 0 = Default, 1 = OptionList, 2 == Keybind")
  1249. {
  1250. object->addRow( label, callback, icon, yPad, useHighlightIcon, enabled, mode);
  1251. }
  1252. DefineEngineMethod( GuiGameListMenuCtrl, isRowEnabled, bool, ( S32 row ),,
  1253. "Determines if the specified row is enabled or disabled.\n\n"
  1254. "@param row The row to set the enabled status of.\n"
  1255. "@return True if the specified row is enabled. False if the row is not enabled or the given index was not valid." )
  1256. {
  1257. return object->isRowEnabled( row );
  1258. }
  1259. DefineEngineMethod( GuiGameListMenuCtrl, setRowEnabled, void, ( S32 row, bool enabled ),,
  1260. "Sets a row's enabled status according to the given parameters.\n\n"
  1261. "@param row The index to check for validity.\n"
  1262. "@param enabled Indicate true to enable the row or false to disable it." )
  1263. {
  1264. object->setRowEnabled( row, enabled );
  1265. }
  1266. DefineEngineMethod( GuiGameListMenuCtrl, activateRow, void, (),,
  1267. "Activates the current row. The script callback of the current row will be called (if it has one)." )
  1268. {
  1269. object->activateRow();
  1270. }
  1271. DefineEngineMethod( GuiGameListMenuCtrl, getRowCount, S32, (),,
  1272. "Gets the number of rows on the control.\n\n"
  1273. "@return (int) The number of rows on the control." )
  1274. {
  1275. return object->getRowCount();
  1276. }
  1277. DefineEngineMethod( GuiGameListMenuCtrl, getRowLabel, const char *, ( S32 row ),,
  1278. "Gets the label displayed on the specified row.\n\n"
  1279. "@param row Index of the row to get the label of.\n"
  1280. "@return The label for the row." )
  1281. {
  1282. return object->getRowLabel( row );
  1283. }
  1284. DefineEngineMethod( GuiGameListMenuCtrl, setRowLabel, void, ( S32 row, const char* label ),,
  1285. "Sets the label on the given row.\n\n"
  1286. "@param row Index of the row to set the label on.\n"
  1287. "@param label Text to set as the label of the row.\n" )
  1288. {
  1289. object->setRowLabel( row, label );
  1290. }
  1291. DefineEngineMethod( GuiGameListMenuCtrl, setSelected, void, ( S32 row ),,
  1292. "Sets the selected row. Only rows that are enabled can be selected.\n\n"
  1293. "@param row Index of the row to set as selected." )
  1294. {
  1295. object->setSelected( row );
  1296. }
  1297. DefineEngineMethod( GuiGameListMenuCtrl, getSelectedRow, S32, (),,
  1298. "Gets the index of the currently selected row.\n\n"
  1299. "@return Index of the selected row." )
  1300. {
  1301. return object->getSelected();
  1302. }
  1303. DefineEngineMethod(GuiGameListMenuCtrl, clearRows, void, (), ,
  1304. "Gets the index of the currently selected row.\n\n"
  1305. "@return Index of the selected row.")
  1306. {
  1307. return object->clearRows();
  1308. }
  1309. DefineEngineMethod(GuiGameListMenuCtrl, refresh, void, (), ,
  1310. "Gets the index of the currently selected row.\n\n"
  1311. "@return Index of the selected row.")
  1312. {
  1313. return object->refresh();
  1314. }
  1315. DefineEngineMethod(GuiGameListMenuCtrl, addOptionRow, void,
  1316. (const char* label, const char* options, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip, const char* defaultValue),
  1317. (-1, 0, true, "", ""),
  1318. "Add a row to the list control.\n\n"
  1319. "@param label The text to display on the row as a label.\n"
  1320. "@param options A tab separated list of options.\n"
  1321. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  1322. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  1323. "@param icon [optional] Index of the icon to use as a marker.\n"
  1324. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  1325. "@param enabled [optional] If this row is initially enabled.")
  1326. {
  1327. object->addRow(label, options, wrapOptions, callback, icon, yPad, enabled, tooltip, defaultValue);
  1328. }
  1329. DefineEngineMethod(GuiGameListMenuCtrl, addSliderRow, void,
  1330. (const char* label, F32 defaultValue, F32 increment, Point2F range, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip),
  1331. (-1, 0, true, ""),
  1332. "Add a row to the list control.\n\n"
  1333. "@param label The text to display on the row as a label.\n"
  1334. "@param options A tab separated list of options.\n"
  1335. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  1336. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  1337. "@param icon [optional] Index of the icon to use as a marker.\n"
  1338. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  1339. "@param enabled [optional] If this row is initially enabled.")
  1340. {
  1341. object->addRow(label, defaultValue, increment, range, callback, icon, yPad, enabled, tooltip);
  1342. }
  1343. DefineEngineMethod(GuiGameListMenuCtrl, addKeybindRow, void,
  1344. (const char* label, const char* bitmapName, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip),
  1345. (-1, 0, true, ""),
  1346. "Add a row to the list control.\n\n"
  1347. "@param label The text to display on the row as a label.\n"
  1348. "@param options A tab separated list of options.\n"
  1349. "@param wrapOptions Specify true to allow options to wrap at each end or false to prevent wrapping.\n"
  1350. "@param callback Name of a script function to use as a callback when this row is activated.\n"
  1351. "@param icon [optional] Index of the icon to use as a marker.\n"
  1352. "@param yPad [optional] An extra amount of height padding before the row. Does nothing on the first row.\n"
  1353. "@param enabled [optional] If this row is initially enabled.")
  1354. {
  1355. object->addRow(label, bitmapName, callback, icon, yPad, enabled, tooltip);
  1356. }
  1357. DefineEngineMethod(GuiGameListMenuCtrl, removeRow, void, (S32 row),,
  1358. "Removes the row at the provided index")
  1359. {
  1360. object->removeRow(row);
  1361. }
  1362. DefineEngineMethod(GuiGameListMenuCtrl, getCurrentOption, const char*, (S32 row), ,
  1363. "Gets the text for the currently selected option of the given row.\n\n"
  1364. "@param row Index of the row to get the option from.\n"
  1365. "@return A string representing the text currently displayed as the selected option on the given row. If there is no such displayed text then the empty string is returned.")
  1366. {
  1367. return object->getCurrentOption(row);
  1368. }
  1369. DefineEngineMethod(GuiGameListMenuCtrl, getCurrentOptionKey, const char*, (S32 row), ,
  1370. "Gets the key string for the currently selected option of the given row.\n\n"
  1371. "@param row Index of the row to get the option from.\n"
  1372. "@return The key (or id) that was assigned to the selected option on the given row. If there is no selected option then the empty string is returned.")
  1373. {
  1374. return object->getCurrentOptionKey(row);
  1375. }
  1376. DefineEngineMethod(GuiGameListMenuCtrl, getCurrentOptionIndex, S32, (S32 row), ,
  1377. "Gets the index into the option list for the currently selected option of the given row.\n\n"
  1378. "@param row Index of the row to get the option from.\n"
  1379. "@return The index of the selected option on the given row. If there is no selected option then -1 is returned.")
  1380. {
  1381. return object->getCurrentOptionIndex(row);
  1382. }
  1383. DefineEngineMethod(GuiGameListMenuCtrl, selectOption, bool, (S32 row, const char* option), ,
  1384. "Set the row's current option to the one specified\n\n"
  1385. "@param row Index of the row to set an option on.\n"
  1386. "@param option The option to be made active.\n"
  1387. "@return True if the row contained the option and was set, false otherwise.")
  1388. {
  1389. return object->selectOption(row, option);
  1390. }
  1391. DefineEngineMethod(GuiGameListMenuCtrl, selectOptionByKey, bool, (S32 row, const char* optionKey), ,
  1392. "Set the row's current option to the one with the specified key.\n\n"
  1393. "@param row Index of the row to set an option on.\n"
  1394. "@param optionKey The key string that was assigned to the option to be made active.\n"
  1395. "@return True if the row contained the key and the option and was set, false otherwise.")
  1396. {
  1397. return object->selectOptionByKey(row, optionKey);
  1398. }
  1399. DefineEngineMethod(GuiGameListMenuCtrl, selectOptionByIndex, bool, (S32 row, S32 optionIndex), ,
  1400. "Set the row's current option to the one at the specified index.\n\n"
  1401. "@param row Index of the row to set an option on.\n"
  1402. "@param optionIndex The index of the option to be made active.\n"
  1403. "@return True if the index was valid and the option and was set, false otherwise.")
  1404. {
  1405. return object->selectOptionByIndex(row, optionIndex);
  1406. }
  1407. DefineEngineMethod(GuiGameListMenuCtrl, setOptions, void, (S32 row, const char* optionsList), ,
  1408. "Sets the list of options on the given row.\n\n"
  1409. "@param row Index of the row to set options on."
  1410. "@param optionsList A tab separated list of options for the control.")
  1411. {
  1412. object->setOptions(row, optionsList);
  1413. }
  1414. DefineEngineMethod(GuiGameListMenuCtrl, addOption, void, (S32 row, const char* displayText, const char* keyText), (""),
  1415. "Adds an option to the list of options on the given row.\n\n"
  1416. "@param row Index of the row to add the option on.\n"
  1417. "@param displayText The text to display for this option.\n"
  1418. "@param keyText [Optional] The id string to associate with this value. "
  1419. "If unset, the id will be the same as the display text.\n")
  1420. {
  1421. object->addOption(row, displayText, keyText);
  1422. }
  1423. DefineEngineMethod(GuiGameListMenuCtrl, getValue, F32, (S32 row), ,
  1424. "Sets the list of options on the given row.\n\n"
  1425. "@param row Index of the row to set options on."
  1426. "@param optionsList A tab separated list of options for the control.")
  1427. {
  1428. return object->getValue(row);
  1429. }
  1430. DefineEngineMethod(GuiGameListMenuCtrl, setValue, void, (S32 row, F32 value), ,
  1431. "Sets the list of options on the given row.\n\n"
  1432. "@param row Index of the row to set options on."
  1433. "@param optionsList A tab separated list of options for the control.")
  1434. {
  1435. object->setValue(row, value);
  1436. }
  1437. DefineEngineMethod(GuiGameListMenuCtrl, getTooltip, const char*, (S32 row), ,
  1438. "Sets the list of options on the given row.\n\n"
  1439. "@param row Index of the row to set options on."
  1440. "@param optionsList A tab separated list of options for the control.")
  1441. {
  1442. return object->getTooltip(row);
  1443. }
  1444. //-----------------------------------------------------------------------------
  1445. // GuiGameListMenuProfile
  1446. //-----------------------------------------------------------------------------
  1447. GuiGameListMenuProfile::GuiGameListMenuProfile()
  1448. : mHitAreaUpperLeft(0, 0),
  1449. mHitAreaLowerRight(0, 0),
  1450. mIconOffset(0, 0),
  1451. mRowSize(0, 0),
  1452. mRowScale(1.0f, 1.0f),
  1453. mColumnSplit(0),
  1454. mRightPad(0)
  1455. {
  1456. }
  1457. bool GuiGameListMenuProfile::onAdd()
  1458. {
  1459. if (! Parent::onAdd())
  1460. {
  1461. return false;
  1462. }
  1463. // We can't call enforceConstraints() here because incRefCount initializes
  1464. // some of the things to enforce. Do a basic sanity check here instead.
  1465. if(mBitmapAsset.isNull())
  1466. {
  1467. Con::errorf( "GuiGameListMenuProfile: %s can't be created without a bitmap. Please add a 'Bitmap' property to the object definition.", getName() );
  1468. return false;
  1469. }
  1470. if( mRowSize.x < 0 )
  1471. {
  1472. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row width. Please change the row width to be non-negative.", getName() );
  1473. return false;
  1474. }
  1475. if( mRowSize.y < 0 )
  1476. {
  1477. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row height. Please change the row height to be non-negative.", getName() );
  1478. return false;
  1479. }
  1480. return true;
  1481. }
  1482. void GuiGameListMenuProfile::enforceConstraints()
  1483. {
  1484. if( getBitmapArrayRect(0).extent.isZero() )
  1485. Con::errorf( "GuiGameListMenuCtrl: %s can't be created without a bitmap. Please add a bitmap to the profile's definition.", getName() );
  1486. if( mRowSize.x < 0 )
  1487. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row width. Please change the row width to be non-negative.", getName() );
  1488. mRowSize.x = getMax(mRowSize.x, 0);
  1489. if( mRowSize.y < 0 )
  1490. Con::errorf( "GuiGameListMenuProfile: %s can't have a negative row height. Please change the row height to be non-negative.", getName() );
  1491. mRowSize.y = getMax(mRowSize.y, 0);
  1492. Point2I rowTexExtent = getBitmapArrayRect(TEX_NORMAL).extent;
  1493. mRowScale.x = (float) getRowWidth() / rowTexExtent.x;
  1494. mRowScale.y = (float) getRowHeight() / rowTexExtent.y;
  1495. if (mHitAreaUpperLeft.x > mColumnSplit || mColumnSplit > mHitAreaLowerRight.x)
  1496. Con::errorf("GuiGameListOptionsProfile: You can't create %s with a ColumnSplit outside the hit area. You set the split to %d. Please change the ColumnSplit to be in the range [%d, %d].",
  1497. getName(), mColumnSplit, mHitAreaUpperLeft.x, mHitAreaLowerRight.x);
  1498. mColumnSplit = mClamp(mColumnSplit, mHitAreaUpperLeft.x, mHitAreaLowerRight.x);
  1499. }
  1500. Point2I GuiGameListMenuProfile::getIconExtent()
  1501. {
  1502. Point2I iconExtent = getBitmapArrayRect(TEX_FIRST_ICON).extent;
  1503. // scale both by y to keep the aspect ratio
  1504. iconExtent.x *= mRowScale.y;
  1505. iconExtent.y *= mRowScale.y;
  1506. return iconExtent;
  1507. }
  1508. Point2I GuiGameListMenuProfile::getArrowExtent()
  1509. {
  1510. Point2I arrowExtent = getBitmapArrayRect(TEX_FIRST_ARROW).extent;
  1511. // scale both by y to keep the aspect ratio
  1512. arrowExtent.x *= mRowScale.y;
  1513. arrowExtent.y *= mRowScale.y;
  1514. return arrowExtent;
  1515. }
  1516. Point2I GuiGameListMenuProfile::getHitAreaExtent()
  1517. {
  1518. if (mHitAreaLowerRight == mHitAreaUpperLeft)
  1519. {
  1520. return mRowSize;
  1521. }
  1522. else
  1523. {
  1524. return mHitAreaLowerRight - mHitAreaUpperLeft;
  1525. }
  1526. }
  1527. //-----------------------------------------------------------------------------
  1528. // Console stuff (GuiGameListMenuProfile)
  1529. //-----------------------------------------------------------------------------
  1530. IMPLEMENT_CONOBJECT(GuiGameListMenuProfile);
  1531. ConsoleDocClass( GuiGameListMenuProfile,
  1532. "@brief A GuiControlProfile with additional fields specific to GuiGameListMenuCtrl.\n\n"
  1533. "@tsexample\n"
  1534. "new GuiGameListMenuProfile()\n"
  1535. "{\n"
  1536. " hitAreaUpperLeft = \"10 2\";\n"
  1537. " hitAreaLowerRight = \"190 18\";\n"
  1538. " iconOffset = \"10 2\";\n"
  1539. " rowSize = \"200 20\";\n"
  1540. " columnSplit = \"100\";\n"
  1541. " rightPad = \"4\";\n"
  1542. " //Properties not specific to this control have been omitted from this example.\n"
  1543. "};\n"
  1544. "@endtsexample\n\n"
  1545. "@ingroup GuiGame"
  1546. );
  1547. void GuiGameListMenuProfile::initPersistFields()
  1548. {
  1549. addField( "hitAreaUpperLeft", TypePoint2I, Offset(mHitAreaUpperLeft, GuiGameListMenuProfile),
  1550. "Position of the upper left corner of the row hit area (relative to row's top left corner)" );
  1551. addField( "hitAreaLowerRight", TypePoint2I, Offset(mHitAreaLowerRight, GuiGameListMenuProfile),
  1552. "Position of the lower right corner of the row hit area (relative to row's top left corner)" );
  1553. addField( "iconOffset", TypePoint2I, Offset(mIconOffset, GuiGameListMenuProfile),
  1554. "Offset from the row's top left corner at which to render the row icon" );
  1555. addField( "rowSize", TypePoint2I, Offset(mRowSize, GuiGameListMenuProfile),
  1556. "The base size (\"width height\") of a row" );
  1557. addField("columnSplit", TypeS32, Offset(mColumnSplit, GuiGameListMenuProfile),
  1558. "Padding between the leftmost edge of the control, and the row's left arrow.");
  1559. addField("rightPad", TypeS32, Offset(mRightPad, GuiGameListMenuProfile),
  1560. "Padding between the rightmost edge of the control and the row's right arrow.");
  1561. Parent::initPersistFields();
  1562. removeField("tab");
  1563. removeField("mouseOverSelected");
  1564. removeField("modal");
  1565. removeField("opaque");
  1566. removeField("border");
  1567. removeField("borderThickness");
  1568. removeField("borderColor");
  1569. removeField("borderColorHL");
  1570. removeField("borderColorNA");
  1571. removeField("bevelColorHL");
  1572. removeField("bevelColorLL");
  1573. removeField("fontColorLink");
  1574. removeField("fontColorLinkHL");
  1575. removeField("justify");
  1576. removeField("returnTab");
  1577. removeField("numbersOnly");
  1578. removeField("cursorColor");
  1579. removeField("profileForChildren");
  1580. }