guiGameListMenuCtrl.cpp 59 KB

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