guiGameListMenuCtrl.cpp 59 KB

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