guiGameListMenuCtrl.cpp 59 KB

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