guiPopUpCtrlEx.cc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. // Revision History:
  23. // December 31, 2003 David Wyand Changed a bunch of stuff. Search for DAW below
  24. // and make better notes here and in the changes doc.
  25. // May 19, 2004 David Wyand Made changes to allow for a coloured rectangle to be
  26. // displayed to the left of the text in the popup.
  27. // May 27, 2004 David Wyand Added a check for mReverseTextList to see if we should
  28. // reverse the text list if we must render it above
  29. // the control. NOTE: there are some issues with setting
  30. // the selected ID using the 'setSelected' command externally
  31. // and the list is rendered in reverse. It seems that the
  32. // entry ID's also get reversed.
  33. // November 16, 2005 David Wyand Added the method setNoneSelected() to set none of the
  34. // items as selected. Use this over setSelected(-1); when
  35. // there are negative IDs in the item list. This also
  36. // includes the setNoneSelected console method.
  37. //
  38. #include "graphics/dgl.h"
  39. #include "gui/guiCanvas.h"
  40. #include "gui/guiPopUpCtrlEx.h"
  41. #include "console/consoleTypes.h"
  42. #include "gui/guiDefaultControlRender.h"
  43. static ColorI colorWhite(255,255,255); // DAW: Added
  44. // Function to return the number of columns in 'string' given delimeters in 'set'
  45. static U32 getColumnCount(const char *string, const char *set)
  46. {
  47. U32 count = 0;
  48. U8 last = 0;
  49. while(*string)
  50. {
  51. last = *string++;
  52. for(U32 i =0; set[i]; i++)
  53. {
  54. if(last == set[i])
  55. {
  56. count++;
  57. last = 0;
  58. break;
  59. }
  60. }
  61. }
  62. if(last)
  63. count++;
  64. return count;
  65. }
  66. // Function to return the 'index' column from 'string' given delimeters in 'set'
  67. static const char *getColumn(const char *string, char* returnbuff, U32 index, const char *set)
  68. {
  69. U32 sz;
  70. while(index--)
  71. {
  72. if(!*string)
  73. return "";
  74. sz = dStrcspn(string, set);
  75. if (string[sz] == 0)
  76. return "";
  77. string += (sz + 1);
  78. }
  79. sz = dStrcspn(string, set);
  80. if (sz == 0)
  81. return "";
  82. char *ret = returnbuff;
  83. dStrncpy(ret, string, sz);
  84. ret[sz] = '\0';
  85. return ret;
  86. }
  87. GuiPopUpBackgroundCtrlEx::GuiPopUpBackgroundCtrlEx(GuiPopUpMenuCtrlEx *ctrl, GuiPopupTextListCtrlEx *textList)
  88. {
  89. mPopUpCtrl = ctrl;
  90. mTextList = textList;
  91. }
  92. void GuiPopUpBackgroundCtrlEx::onMouseDown(const GuiEvent &event)
  93. {
  94. // mTextList->setSelectedCell(Point2I(-1,-1)); // DAW: Removed
  95. mPopUpCtrl->mBackgroundCancel = true; // DAW: Set that the user didn't click within the text list. Replaces the line above.
  96. mPopUpCtrl->closePopUp();
  97. }
  98. //------------------------------------------------------------------------------
  99. GuiPopupTextListCtrlEx::GuiPopupTextListCtrlEx()
  100. {
  101. mPopUpCtrl = NULL;
  102. }
  103. //------------------------------------------------------------------------------
  104. GuiPopupTextListCtrlEx::GuiPopupTextListCtrlEx(GuiPopUpMenuCtrlEx *ctrl)
  105. {
  106. mPopUpCtrl = ctrl;
  107. }
  108. //------------------------------------------------------------------------------
  109. //------------------------------------------------------------------------------
  110. //void GuiPopUpTextListCtrl::onCellSelected( Point2I /*cell*/ )
  111. //{
  112. // // Do nothing, the parent control will take care of everything...
  113. //}
  114. void GuiPopupTextListCtrlEx::onCellSelected( Point2I cell )
  115. {
  116. // DAW: The old function is above. This new one will only call the the select
  117. // functions if we were not cancelled by a background click.
  118. // DAW: Check if we were cancelled by the user clicking on the Background ie: anywhere
  119. // other than within the text list.
  120. if(mPopUpCtrl->mBackgroundCancel)
  121. return;
  122. if( isMethod( "onSelect" ) )
  123. Con::executef(this, 3, "onSelect", Con::getFloatArg(cell.x), Con::getFloatArg(cell.y));
  124. //call the console function
  125. if (mConsoleCommand[0])
  126. Con::evaluate(mConsoleCommand, false);
  127. }
  128. bool GuiPopupTextListCtrlEx::hasCategories()
  129. {
  130. for( S32 i = 0; i < mList.size(); i++)
  131. {
  132. if( mList[i].id == -1)
  133. return true;
  134. }
  135. return false;
  136. }
  137. //------------------------------------------------------------------------------
  138. bool GuiPopupTextListCtrlEx::onKeyDown(const GuiEvent &event)
  139. {
  140. //if the control is a dead end, don't process the input:
  141. if ( !mVisible || !mActive || !mAwake )
  142. return false;
  143. //see if the key down is a <return> or not
  144. if ( event.modifier == 0 )
  145. {
  146. if ( event.keyCode == KEY_RETURN )
  147. {
  148. mPopUpCtrl->closePopUp();
  149. return true;
  150. }
  151. else if ( event.keyCode == KEY_ESCAPE )
  152. {
  153. mSelectedCell.set( -1, -1 );
  154. mPopUpCtrl->closePopUp();
  155. return true;
  156. }
  157. }
  158. //otherwise, pass the event to it's parent
  159. return Parent::onKeyDown(event);
  160. }
  161. void GuiPopupTextListCtrlEx::onMouseUp(const GuiEvent &event)
  162. {
  163. if(!mActive)
  164. return;
  165. Point2I pt = globalToLocalCoord(event.mousePoint);
  166. pt.x -= mHeaderDim.x; pt.y -= mHeaderDim.y;
  167. Point2I cell(
  168. (pt.x < 0 ? -1 : pt.x / mCellSize.x),
  169. (pt.y < 0 ? -1 : pt.y / mCellSize.y)
  170. );
  171. if (cell.x >= 0 && cell.x < mSize.x && cell.y >= 0 && cell.y < mSize.y)
  172. {
  173. if (mList[cell.y].id == -1)
  174. return;
  175. }
  176. Parent::onMouseDown(event);
  177. mPopUpCtrl->closePopUp();
  178. Parent::onMouseUp(event);
  179. }
  180. void GuiPopupTextListCtrlEx::onMouseMove( const GuiEvent &event )
  181. {
  182. if( !mPopUpCtrl || !mPopUpCtrl->isMethod("onHotTrackItem") )
  183. return Parent::onMouseMove( event );
  184. Point2I pt = globalToLocalCoord(event.mousePoint);
  185. pt.x -= mHeaderDim.x; pt.y -= mHeaderDim.y;
  186. Point2I cell( (pt.x < 0 ? -1 : pt.x / mCellSize.x), (pt.y < 0 ? -1 : pt.y / mCellSize.y) );
  187. // Within Bounds?
  188. if (cell.x >= 0 && cell.x < mSize.x && cell.y >= 0 && cell.y < mSize.y)
  189. // Hot Track notification
  190. Con::executef( mPopUpCtrl, 2, "onHotTrackItem", Con::getIntArg(mList[cell.y].id) );
  191. else
  192. // Hot Track -1
  193. Con::executef( mPopUpCtrl, 2, "onHotTrackItem", Con::getIntArg(-1) );
  194. // Call Parent
  195. Parent::onMouseMove(event);
  196. }
  197. //------------------------------------------------------------------------------
  198. void GuiPopupTextListCtrlEx::onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver)
  199. {
  200. // DAW: Get the size of the cells
  201. Point2I size;
  202. getCellSize(size);
  203. if(mouseOver && (mList[cell.y].id != -1))
  204. {
  205. // DAW: Render a background colour for the cell
  206. RectI cellR(offset.x, offset.y, size.x, size.y);
  207. ColorI color(0,0,0);
  208. dglDrawRectFill(cellR, color);
  209. }
  210. else if(selected)
  211. {
  212. // DAW: Render a background colour for the cell
  213. RectI cellR(offset.x, offset.y, size.x, size.y);
  214. ColorI color(128,128,128);
  215. dglDrawRectFill(cellR, color);
  216. }
  217. // DAW: Define the default x offset for the text
  218. U32 textXOffset = offset.x + mProfile->mTextOffset.x;
  219. // DAW: Do we also draw a coloured box beside the text?
  220. ColorI boxColor;
  221. bool drawbox = mPopUpCtrl->getColoredBox( boxColor, mList[cell.y].id);
  222. if(drawbox)
  223. {
  224. Point2I coloredboxsize(15,10);
  225. RectI r(offset.x + mProfile->mTextOffset.x, offset.y+2, coloredboxsize.x, coloredboxsize.y);
  226. dglDrawRectFill( r, boxColor);
  227. dglDrawRect( r, ColorI(0,0,0));
  228. textXOffset += coloredboxsize.x + mProfile->mTextOffset.x;
  229. }
  230. // Render 'Group' background
  231. if(mList[cell.y].id == -1)
  232. {
  233. RectI cellR(offset.x, offset.y, size.x, size.y);
  234. dglDrawRectFill(cellR, mProfile->mFillColorHL );
  235. }
  236. ColorI fontColor;
  237. mPopUpCtrl->getFontColor( fontColor, mList[cell.y].id, selected, mouseOver );
  238. dglSetBitmapModulation( fontColor );
  239. //dglDrawText( mFont, Point2I( offset.x + 4, offset.y ), mList[cell.y].text );
  240. // DAW: Get the number of columns in the cell
  241. S32 colcount = getColumnCount(mList[cell.y].text, "\t");
  242. // DAW: Are there two or more columns?
  243. if(colcount >= 2)
  244. {
  245. char buff[256];
  246. // Draw the first column
  247. getColumn(mList[cell.y].text, buff, 0, "\t");
  248. dglDrawText( mFont, Point2I( textXOffset, offset.y ), buff ); // DAW: Used mTextOffset as a margin for the text list rather than the hard coded value of '4'.
  249. // Draw the second column to the right
  250. getColumn(mList[cell.y].text, buff, 1, "\t");
  251. S32 txt_w = mFont->getStrWidth(buff);
  252. dglDrawText( mFont, Point2I( offset.x+size.x-mProfile->mTextOffset.x-txt_w, offset.y ), buff ); // DAW: Used mTextOffset as a margin for the text list rather than the hard coded value of '4'.
  253. }
  254. else
  255. {
  256. if ((mList[cell.y].id == -1) || (!hasCategories()))
  257. dglDrawText( mFont, Point2I( textXOffset, offset.y ), mList[cell.y].text ); // DAW: Used mTextOffset as a margin for the text list rather than the hard coded value of '4'.
  258. else
  259. dglDrawText( mFont, Point2I( textXOffset + 8, offset.y ), mList[cell.y].text ); // DAW: Used mTextOffset as a margin for the text list rather than the hard coded value of '4'.
  260. }
  261. }
  262. //------------------------------------------------------------------------------
  263. //------------------------------------------------------------------------------
  264. IMPLEMENT_CONOBJECT(GuiPopUpMenuCtrlEx);
  265. GuiPopUpMenuCtrlEx::GuiPopUpMenuCtrlEx(void)
  266. {
  267. VECTOR_SET_ASSOCIATION(mEntries);
  268. VECTOR_SET_ASSOCIATION(mSchemes);
  269. mSelIndex = -1;
  270. mActive = true;
  271. mMaxPopupHeight = 200;
  272. mScrollDir = GuiScrollCtrl::None;
  273. mScrollCount = 0;
  274. mLastYvalue = 0;
  275. mIncValue = 0;
  276. mRevNum = 0;
  277. mInAction = false;
  278. mMouseOver = false; // DAW: Added
  279. mRenderScrollInNA = false; // DAW: Added
  280. mBackgroundCancel = false; // DAW: Added
  281. mReverseTextList = false; // DAW: Added - Don't reverse text list if displaying up
  282. mBitmapName = StringTable->EmptyString; // DAW: Added
  283. mBitmapBounds.set(16, 16); // DAW: Added
  284. mHotTrackItems = false;
  285. }
  286. //------------------------------------------------------------------------------
  287. GuiPopUpMenuCtrlEx::~GuiPopUpMenuCtrlEx()
  288. {
  289. }
  290. //------------------------------------------------------------------------------
  291. void GuiPopUpMenuCtrlEx::initPersistFields(void)
  292. {
  293. Parent::initPersistFields();
  294. addField("maxPopupHeight", TypeS32, Offset(mMaxPopupHeight, GuiPopUpMenuCtrlEx));
  295. addField("sbUsesNAColor", TypeBool, Offset(mRenderScrollInNA, GuiPopUpMenuCtrlEx));
  296. addField("reverseTextList", TypeBool, Offset(mReverseTextList, GuiPopUpMenuCtrlEx));
  297. addField("bitmap", TypeFilename, Offset(mBitmapName, GuiPopUpMenuCtrlEx));
  298. addField("bitmapBounds", TypePoint2I, Offset(mBitmapBounds, GuiPopUpMenuCtrlEx));
  299. addField("hotTrackCallback", TypeBool, Offset(mHotTrackItems, GuiPopUpMenuCtrlEx),
  300. "Whether to provide a 'onHotTrackItem' callback when a list item is hovered over");
  301. }
  302. //------------------------------------------------------------------------------
  303. ConsoleMethod( GuiPopUpMenuCtrlEx, add, void, 4, 5, "(string name, int idNum, int scheme=0) Adds a new pop up menu control\n"
  304. "@param name The control's name\n"
  305. "@param idNum The control's ID\n"
  306. "@param scheme The selected scheme (default 0)\n"
  307. "@return No Return Value")
  308. {
  309. if ( argc > 4 )
  310. object->addEntry(argv[2],dAtoi(argv[3]),dAtoi(argv[4]));
  311. else
  312. object->addEntry(argv[2],dAtoi(argv[3]),0);
  313. }
  314. ConsoleMethod( GuiPopUpMenuCtrlEx, addCategory, void, 3, 3, "(string text) Adds the given category to the list\n"
  315. "@param text The category to add\n"
  316. "@return No return value.")
  317. {
  318. object->addEntry(argv[2], -1, 0);
  319. }
  320. ConsoleMethod( GuiPopUpMenuCtrlEx, addScheme, void, 6, 6, "(int id, ColorI fontColor, ColorI fontColorHL, ColorI fontColorSEL) Adds the defined scheme\n"
  321. "@param id The object id of the scheme.\n"
  322. "@param fontcolor The desired color of the font.\n"
  323. "@param fontColorHL The desired color on highlight.\n"
  324. "@param fontColorSEL The desired color on select.\n"
  325. "@return No return value.")
  326. {
  327. ColorI fontColor, fontColorHL, fontColorSEL;
  328. U32 r, g, b;
  329. char buf[64];
  330. dStrcpy( buf, argv[3] );
  331. char* temp = dStrtok( buf, " \0" );
  332. r = temp ? dAtoi( temp ) : 0;
  333. temp = dStrtok( NULL, " \0" );
  334. g = temp ? dAtoi( temp ) : 0;
  335. temp = dStrtok( NULL, " \0" );
  336. b = temp ? dAtoi( temp ) : 0;
  337. fontColor.set( r, g, b );
  338. dStrcpy( buf, argv[4] );
  339. temp = dStrtok( buf, " \0" );
  340. r = temp ? dAtoi( temp ) : 0;
  341. temp = dStrtok( NULL, " \0" );
  342. g = temp ? dAtoi( temp ) : 0;
  343. temp = dStrtok( NULL, " \0" );
  344. b = temp ? dAtoi( temp ) : 0;
  345. fontColorHL.set( r, g, b );
  346. dStrcpy( buf, argv[5] );
  347. temp = dStrtok( buf, " \0" );
  348. r = temp ? dAtoi( temp ) : 0;
  349. temp = dStrtok( NULL, " \0" );
  350. g = temp ? dAtoi( temp ) : 0;
  351. temp = dStrtok( NULL, " \0" );
  352. b = temp ? dAtoi( temp ) : 0;
  353. fontColorSEL.set( r, g, b );
  354. object->addScheme( dAtoi( argv[2] ), fontColor, fontColorHL, fontColorSEL );
  355. }
  356. ConsoleMethod( GuiPopUpMenuCtrlEx, setText, void, 3, 3, "(string text) Set control text to given string.\n"
  357. "@param text The desired text.\n"
  358. "@return No return vlaue.")
  359. {
  360. object->setText(argv[2]);
  361. }
  362. ConsoleMethod( GuiPopUpMenuCtrlEx, getText, const char*, 2, 2, "()\n @return Returns the control's text")
  363. {
  364. return object->getText();
  365. }
  366. ConsoleMethod( GuiPopUpMenuCtrlEx, clear, void, 2, 2, "() Clear the popup list.\n"
  367. "@return No return value.")
  368. {
  369. object->clear();
  370. }
  371. ConsoleMethod(GuiPopUpMenuCtrlEx, sort, void, 2, 2, "() Sort the list alphabetically.\n"
  372. "@return No return value.")
  373. {
  374. object->sort();
  375. }
  376. // DAW: Added to sort the entries by ID
  377. ConsoleMethod(GuiPopUpMenuCtrlEx, sortID, void, 2, 2, "() Sort the list by ID.\n"
  378. "@return No return value.")
  379. {
  380. object->sortID();
  381. }
  382. ConsoleMethod( GuiPopUpMenuCtrlEx, forceOnAction, void, 2, 2, "")
  383. {
  384. object->onAction();
  385. }
  386. ConsoleMethod( GuiPopUpMenuCtrlEx, forceClose, void, 2, 2, "")
  387. {
  388. object->closePopUp();
  389. }
  390. ConsoleMethod( GuiPopUpMenuCtrlEx, getSelected, S32, 2, 2, "")
  391. {
  392. return object->getSelected();
  393. }
  394. ConsoleMethod( GuiPopUpMenuCtrlEx, setSelected, void, 3, 3, "(int id)")
  395. {
  396. object->setSelected(dAtoi(argv[2]));
  397. }
  398. ConsoleMethod( GuiPopUpMenuCtrlEx, setFirstSelected, void, 2, 2, "")
  399. {
  400. object->setFirstSelected();
  401. }
  402. ConsoleMethod( GuiPopUpMenuCtrlEx, setNoneSelected, void, 2, 2, "")
  403. {
  404. object->setNoneSelected();
  405. }
  406. ConsoleMethod( GuiPopUpMenuCtrlEx, getTextById, const char*, 3, 3, "(int id)")
  407. {
  408. return(object->getTextById(dAtoi(argv[2])));
  409. }
  410. ConsoleMethod( GuiPopUpMenuCtrlEx, setEnumContent, void, 4, 4, "(string class, string enum)"
  411. "This fills the popup with a classrep's field enumeration type info.\n\n"
  412. "More of a helper function than anything. If console access to the field list is added, "
  413. "at least for the enumerated types, then this should go away..")
  414. {
  415. AbstractClassRep * classRep = AbstractClassRep::getClassList();
  416. // walk the class list to get our class
  417. while(classRep)
  418. {
  419. if(!dStricmp(classRep->getClassName(), argv[2]))
  420. break;
  421. classRep = classRep->getNextClass();
  422. }
  423. // get it?
  424. if(!classRep)
  425. {
  426. Con::warnf(ConsoleLogEntry::General, "failed to locate class rep for '%s'", argv[2]);
  427. return;
  428. }
  429. // walk the fields to check for this one (findField checks StringTableEntry ptrs...)
  430. U32 i;
  431. for(i = 0; i < (U32)classRep->mFieldList.size(); i++)
  432. if(!dStricmp(classRep->mFieldList[i].pFieldname, argv[3]))
  433. break;
  434. // found it?
  435. if(i == classRep->mFieldList.size())
  436. {
  437. Con::warnf(ConsoleLogEntry::General, "failed to locate field '%s' for class '%s'", argv[3], argv[2]);
  438. return;
  439. }
  440. const AbstractClassRep::Field & field = classRep->mFieldList[i];
  441. // check the type
  442. if(field.type != TypeEnum)
  443. {
  444. Con::warnf(ConsoleLogEntry::General, "field '%s' is not an enumeration for class '%s'", argv[3], argv[2]);
  445. return;
  446. }
  447. AssertFatal(field.table, avar("enumeration '%s' for class '%s' with NULL ", argv[3], argv[2]));
  448. // fill it
  449. for(i = 0; i < (U32)field.table->size; i++)
  450. object->addEntry(field.table->table[i].label, field.table->table[i].index);
  451. }
  452. //------------------------------------------------------------------------------
  453. ConsoleMethod( GuiPopUpMenuCtrlEx, findText, S32, 3, 3, "(string text)"
  454. "Returns the position of the first entry containing the specified text.")
  455. {
  456. return( object->findText( argv[2] ) );
  457. }
  458. //------------------------------------------------------------------------------
  459. ConsoleMethod( GuiPopUpMenuCtrlEx, size, S32, 2, 2, "Get the size of the menu - the number of entries in it.")
  460. {
  461. return( object->getNumEntries() );
  462. }
  463. //------------------------------------------------------------------------------
  464. ConsoleMethod( GuiPopUpMenuCtrlEx, replaceText, void, 3, 3, "(bool doReplaceText)")
  465. {
  466. object->replaceText(dAtoi(argv[2]));
  467. }
  468. //------------------------------------------------------------------------------
  469. // DAW: Added
  470. bool GuiPopUpMenuCtrlEx::onWake()
  471. {
  472. if (! Parent::onWake())
  473. return false;
  474. // Set the bitmap for the popup.
  475. setBitmap(mBitmapName);
  476. // Now update the Form Control's bitmap array, and possibly the child's too
  477. mProfile->constructBitmapArray();
  478. if(mProfile->mProfileForChildren)
  479. mProfile->mProfileForChildren->constructBitmapArray();
  480. return true;
  481. }
  482. //------------------------------------------------------------------------------
  483. bool GuiPopUpMenuCtrlEx::onAdd()
  484. {
  485. if (!Parent::onAdd())
  486. return false;
  487. mSelIndex = -1;
  488. mReplaceText = true;
  489. return true;
  490. }
  491. //------------------------------------------------------------------------------
  492. void GuiPopUpMenuCtrlEx::onSleep()
  493. {
  494. mTextureNormal = NULL; // DAW: Added
  495. mTextureDepressed = NULL; // DAW: Added
  496. Parent::onSleep();
  497. closePopUp(); // Tests in function.
  498. }
  499. //------------------------------------------------------------------------------
  500. void GuiPopUpMenuCtrlEx::clear()
  501. {
  502. mEntries.setSize(0);
  503. setText("");
  504. mSelIndex = -1;
  505. mRevNum = 0;
  506. }
  507. //------------------------------------------------------------------------------
  508. static S32 QSORT_CALLBACK textCompare(const void *a,const void *b)
  509. {
  510. GuiPopUpMenuCtrlEx::Entry *ea = (GuiPopUpMenuCtrlEx::Entry *) (a);
  511. GuiPopUpMenuCtrlEx::Entry *eb = (GuiPopUpMenuCtrlEx::Entry *) (b);
  512. return (dStricmp(ea->buf, eb->buf));
  513. }
  514. // DAW: Added to sort by entry ID
  515. //------------------------------------------------------------------------------
  516. static S32 QSORT_CALLBACK idCompare(const void *a,const void *b)
  517. {
  518. GuiPopUpMenuCtrlEx::Entry *ea = (GuiPopUpMenuCtrlEx::Entry *) (a);
  519. GuiPopUpMenuCtrlEx::Entry *eb = (GuiPopUpMenuCtrlEx::Entry *) (b);
  520. return ( (ea->id < eb->id) ? -1 : ((ea->id > eb->id) ? 1 : 0) );
  521. }
  522. //------------------------------------------------------------------------------
  523. // DAW: Added
  524. void GuiPopUpMenuCtrlEx::setBitmap(const char *name)
  525. {
  526. mBitmapName = StringTable->insert(name);
  527. if(!isAwake())
  528. return;
  529. if (*mBitmapName)
  530. {
  531. char buffer[1024];
  532. char *p;
  533. dStrcpy(buffer, name);
  534. p = buffer + dStrlen(buffer);
  535. dStrcpy(p, "_n");
  536. mTextureNormal = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  537. dStrcpy(p, "_d");
  538. mTextureDepressed = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  539. if (!mTextureDepressed)
  540. mTextureDepressed = mTextureNormal;
  541. }
  542. else
  543. {
  544. mTextureNormal = NULL;
  545. mTextureDepressed = NULL;
  546. }
  547. setUpdate();
  548. }
  549. //------------------------------------------------------------------------------
  550. void GuiPopUpMenuCtrlEx::sort()
  551. {
  552. if (!mEntries.empty())
  553. dQsort((void *)&(mEntries[0]), mEntries.size(), sizeof(Entry), textCompare);
  554. }
  555. // DAW: Added to sort by entry ID
  556. //------------------------------------------------------------------------------
  557. void GuiPopUpMenuCtrlEx::sortID()
  558. {
  559. if (!mEntries.empty())
  560. dQsort((void *)&(mEntries[0]), mEntries.size(), sizeof(Entry), idCompare);
  561. }
  562. //------------------------------------------------------------------------------
  563. void GuiPopUpMenuCtrlEx::addEntry(const char *buf, S32 id, U32 scheme)
  564. {
  565. if( !buf )
  566. {
  567. //Con::printf( "GuiPopupMenuCtrlEx::addEntry - Invalid buffer!" );
  568. return;
  569. }
  570. Entry e;
  571. dStrcpy(e.buf, buf);
  572. e.id = id;
  573. e.scheme = scheme;
  574. // see if there is a shortcut key
  575. char * cp = dStrchr(e.buf, '~');
  576. e.ascii = cp ? cp[1] : 0;
  577. // DAW: See if there is a colour box defined with the text
  578. char* cb = dStrchr(e.buf, '|');
  579. if(cb)
  580. {
  581. e.usesColorBox = true;
  582. cb[0] = '\0';
  583. char* red = &cb[1];
  584. cb = dStrchr(red, '|');
  585. cb[0] = '\0';
  586. char* green = &cb[1];
  587. cb = dStrchr(green, '|');
  588. cb[0] = '\0';
  589. char* blue = &cb[1];
  590. U32 r = dAtoi(red);
  591. U32 g = dAtoi(green);
  592. U32 b = dAtoi(blue);
  593. e.colorbox = ColorI(r,g,b);
  594. } else
  595. {
  596. e.usesColorBox = false;
  597. }
  598. mEntries.push_back(e);
  599. if ( mInAction && mTl )
  600. {
  601. // Add the new entry:
  602. mTl->addEntry( e.id, e.buf );
  603. repositionPopup();
  604. }
  605. }
  606. //------------------------------------------------------------------------------
  607. void GuiPopUpMenuCtrlEx::addScheme( U32 id, ColorI fontColor, ColorI fontColorHL, ColorI fontColorSEL )
  608. {
  609. if ( !id )
  610. return;
  611. Scheme newScheme;
  612. newScheme.id = id;
  613. newScheme.fontColor = fontColor;
  614. newScheme.fontColorHL = fontColorHL;
  615. newScheme.fontColorSEL = fontColorSEL;
  616. mSchemes.push_back( newScheme );
  617. }
  618. //------------------------------------------------------------------------------
  619. S32 GuiPopUpMenuCtrlEx::getSelected()
  620. {
  621. if (mSelIndex == -1)
  622. return 0;
  623. return mEntries[mSelIndex].id;
  624. }
  625. //------------------------------------------------------------------------------
  626. const char* GuiPopUpMenuCtrlEx::getTextById(S32 id)
  627. {
  628. for ( U32 i = 0; i < (U32)mEntries.size(); i++ )
  629. {
  630. if ( mEntries[i].id == id )
  631. return( mEntries[i].buf );
  632. }
  633. return( "" );
  634. }
  635. //------------------------------------------------------------------------------
  636. S32 GuiPopUpMenuCtrlEx::findText( const char* text )
  637. {
  638. for ( U32 i = 0; i < (U32)mEntries.size(); i++ )
  639. {
  640. if ( dStrcmp( text, mEntries[i].buf ) == 0 )
  641. return( mEntries[i].id );
  642. }
  643. return( -1 );
  644. }
  645. //------------------------------------------------------------------------------
  646. void GuiPopUpMenuCtrlEx::setSelected(S32 id)
  647. {
  648. S32 i;
  649. for (i = 0; i < mEntries.size(); i++)
  650. if (id == mEntries[i].id)
  651. {
  652. i = (mRevNum > i) ? mRevNum - i : i;
  653. mSelIndex = i;
  654. if(mReplaceText) // DAW: Only change the displayed text if appropriate.
  655. {
  656. setText(mEntries[i].buf);
  657. }
  658. // Now perform the popup action:
  659. char idval[24];
  660. dSprintf( idval, sizeof(idval), "%d", mEntries[mSelIndex].id );
  661. if( isMethod( "onSelect" ) )
  662. Con::executef( this, 3, "onSelect", idval, mEntries[mSelIndex].buf );
  663. return;
  664. }
  665. if(mReplaceText) // DAW: Only change the displayed text if appropriate.
  666. {
  667. setText("");
  668. }
  669. mSelIndex = -1;
  670. Con::executef( this, 1, "onCancel" );
  671. if( id == -1 )
  672. return;
  673. // Execute the popup console command:
  674. if ( mConsoleCommand[0] )
  675. Con::evaluate( mConsoleCommand, false );
  676. }
  677. //------------------------------------------------------------------------------
  678. // DAW: Added to set the first item as selected.
  679. void GuiPopUpMenuCtrlEx::setFirstSelected()
  680. {
  681. if (mEntries.size() > 0)
  682. {
  683. mSelIndex = 0;
  684. if(mReplaceText) // DAW: Only change the displayed text if appropriate.
  685. {
  686. setText(mEntries[0].buf);
  687. }
  688. // Now perform the popup action:
  689. char idval[24];
  690. dSprintf( idval, sizeof(idval), "%d", mEntries[mSelIndex].id );
  691. if(isMethod("onSelect"))
  692. Con::executef( this, 3, "onSelect", idval, mEntries[mSelIndex].buf );
  693. return;
  694. }
  695. if(mReplaceText) // DAW: Only change the displayed text if appropriate.
  696. {
  697. setText("");
  698. }
  699. mSelIndex = -1;
  700. Con::executef( this, 1, "onCancel" );
  701. // Execute the popup console command:
  702. if ( mConsoleCommand[0] )
  703. Con::evaluate( mConsoleCommand, false );
  704. }
  705. //------------------------------------------------------------------------------
  706. // DAW: Added to set no items as selected.
  707. void GuiPopUpMenuCtrlEx::setNoneSelected()
  708. {
  709. if(mReplaceText) // DAW: Only change the displayed text if appropriate.
  710. {
  711. setText("");
  712. }
  713. mSelIndex = -1;
  714. }
  715. //------------------------------------------------------------------------------
  716. const char *GuiPopUpMenuCtrlEx::getScriptValue()
  717. {
  718. return getText();
  719. }
  720. //------------------------------------------------------------------------------
  721. void GuiPopUpMenuCtrlEx::onRender(Point2I offset, const RectI &updateRect)
  722. {
  723. Point2I localStart;
  724. if(mScrollDir != GuiScrollCtrl::None)
  725. autoScroll();
  726. RectI r(offset, mBounds.extent);
  727. if(mInAction)
  728. {
  729. S32 l = r.point.x, r2 = r.point.x + r.extent.x - 1;
  730. S32 t = r.point.y, b = r.point.y + r.extent.y - 1;
  731. // Do we render a bitmap border or lines?
  732. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  733. {
  734. // Render the fixed, filled in border
  735. renderFixedBitmapBordersFilled(r, 3, mProfile);
  736. } else
  737. {
  738. //renderSlightlyLoweredBox(r, mProfile);
  739. dglDrawRectFill( r, mProfile->mFillColor);
  740. }
  741. // DAW: Draw a bitmap over the background?
  742. if(mTextureDepressed)
  743. {
  744. RectI rect(offset, mBitmapBounds);
  745. dglClearBitmapModulation();
  746. dglDrawBitmapStretch(mTextureDepressed, rect);
  747. } else if(mTextureNormal)
  748. {
  749. RectI rect(offset, mBitmapBounds);
  750. dglClearBitmapModulation();
  751. dglDrawBitmapStretch(mTextureNormal, rect);
  752. }
  753. // Do we render a bitmap border or lines?
  754. if(!(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size()))
  755. {
  756. dglDrawLine(l, t, l, b, colorWhite);
  757. dglDrawLine(l, t, r2, t, colorWhite);
  758. dglDrawLine(l + 1, b, r2, b, mProfile->mBorderColor);
  759. dglDrawLine(r2, t + 1, r2, b - 1, mProfile->mBorderColor);
  760. }
  761. }
  762. else
  763. // TODO: Implement
  764. if(mMouseOver) // TODO: Add onMouseEnter() and onMouseLeave() and a definition of mMouseOver (see guiButtonBaseCtrl) for this to work.
  765. {
  766. S32 l = r.point.x, r2 = r.point.x + r.extent.x - 1;
  767. S32 t = r.point.y, b = r.point.y + r.extent.y - 1;
  768. // Do we render a bitmap border or lines?
  769. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  770. {
  771. // Render the fixed, filled in border
  772. renderFixedBitmapBordersFilled(r, 2, mProfile);
  773. } else
  774. {
  775. dglDrawRectFill( r, mProfile->mFillColorHL);
  776. }
  777. // DAW: Draw a bitmap over the background?
  778. if(mTextureNormal)
  779. {
  780. RectI rect(offset, mBitmapBounds);
  781. dglClearBitmapModulation();
  782. dglDrawBitmapStretch(mTextureNormal, rect);
  783. }
  784. // Do we render a bitmap border or lines?
  785. if(!(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size()))
  786. {
  787. dglDrawLine(l, t, l, b, colorWhite);
  788. dglDrawLine(l, t, r2, t, colorWhite);
  789. dglDrawLine(l + 1, b, r2, b, mProfile->mBorderColor);
  790. dglDrawLine(r2, t + 1, r2, b - 1, mProfile->mBorderColor);
  791. }
  792. }
  793. else
  794. {
  795. // Do we render a bitmap border or lines?
  796. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  797. {
  798. // Render the fixed, filled in border
  799. renderFixedBitmapBordersFilled(r, 1, mProfile);
  800. } else
  801. {
  802. dglDrawRectFill( r, mProfile->mFillColorNA);
  803. }
  804. // DAW: Draw a bitmap over the background?
  805. if(mTextureNormal)
  806. {
  807. RectI rect(offset, mBitmapBounds);
  808. dglClearBitmapModulation();
  809. dglDrawBitmapStretch(mTextureNormal, rect);
  810. }
  811. // Do we render a bitmap border or lines?
  812. if(!(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size()))
  813. {
  814. dglDrawRect(r, mProfile->mBorderColorNA);
  815. }
  816. }
  817. // renderSlightlyRaisedBox(r, mProfile); // DAW: Used to be the only 'else' condition to mInAction above.
  818. S32 txt_w = mFont->getStrWidth(mText);
  819. localStart.x = 0;
  820. localStart.y = (mBounds.extent.y - (mFont->getHeight())) / 2;
  821. // align the horizontal
  822. switch (mProfile->mAlignment)
  823. {
  824. case GuiControlProfile::RightJustify:
  825. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  826. {
  827. // We're making use of a bitmap border, so take into account the
  828. // right cap of the border.
  829. RectI* mBitmapBounds = mProfile->mBitmapArrayRects.address();
  830. localStart.x = mBounds.extent.x - mBitmapBounds[2].extent.x - txt_w;
  831. } else
  832. {
  833. localStart.x = mBounds.extent.x - txt_w;
  834. }
  835. break;
  836. case GuiControlProfile::CenterJustify:
  837. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  838. {
  839. // We're making use of a bitmap border, so take into account the
  840. // right cap of the border.
  841. RectI* mBitmapBounds = mProfile->mBitmapArrayRects.address();
  842. localStart.x = (mBounds.extent.x - mBitmapBounds[2].extent.x - txt_w) / 2;
  843. } else
  844. {
  845. localStart.x = (mBounds.extent.x - txt_w) / 2;
  846. }
  847. break;
  848. default:
  849. // GuiControlProfile::LeftJustify
  850. if(txt_w > mBounds.extent.x)
  851. {
  852. // DAW: The width of the text is greater than the width of the control.
  853. // In this case we will right justify the text and leave some space
  854. // for the down arrow.
  855. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  856. {
  857. // We're making use of a bitmap border, so take into account the
  858. // right cap of the border.
  859. RectI* mBitmapBounds = mProfile->mBitmapArrayRects.address();
  860. localStart.x = mBounds.extent.x - mBitmapBounds[2].extent.x - txt_w;
  861. } else
  862. {
  863. localStart.x = mBounds.extent.x - txt_w - 12;
  864. }
  865. } else
  866. {
  867. localStart.x = mProfile->mTextOffset.x; // DAW: Use mProfile->mTextOffset as a controlable margin for the control's text.
  868. }
  869. break;
  870. }
  871. // DAW: Do we first draw a coloured box beside the text?
  872. ColorI boxColor;
  873. bool drawbox = getColoredBox( boxColor, mSelIndex);
  874. if(drawbox)
  875. {
  876. Point2I coloredboxsize(15,10);
  877. RectI r(offset.x + mProfile->mTextOffset.x, offset.y + ((mBounds.extent.y - coloredboxsize.y) / 2), coloredboxsize.x, coloredboxsize.y);
  878. dglDrawRectFill( r, boxColor);
  879. dglDrawRect( r, ColorI(0,0,0));
  880. localStart.x += coloredboxsize.x + mProfile->mTextOffset.x;
  881. }
  882. // Draw the text
  883. Point2I globalStart = localToGlobalCoord(localStart);
  884. ColorI fontColor = mActive ? (mInAction ? mProfile->mFontColor : mProfile->mFontColorNA) : mProfile->mFontColorNA;
  885. dglSetBitmapModulation(fontColor); // DAW: was: (mProfile->mFontColor);
  886. // DAW: Get the number of columns in the text
  887. S32 colcount = getColumnCount(mText, "\t");
  888. // DAW: Are there two or more columns?
  889. if(colcount >= 2)
  890. {
  891. char buff[256];
  892. // Draw the first column
  893. getColumn(mText, buff, 0, "\t");
  894. dglDrawText(mFont, globalStart, buff, mProfile->mFontColors);
  895. // Draw the second column to the right
  896. getColumn(mText, buff, 1, "\t");
  897. S32 txt_w = mFont->getStrWidth(buff);
  898. if(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size())
  899. {
  900. // We're making use of a bitmap border, so take into account the
  901. // right cap of the border.
  902. RectI* mBitmapBounds = mProfile->mBitmapArrayRects.address();
  903. Point2I textpos = localToGlobalCoord(Point2I(mBounds.extent.x - txt_w - mBitmapBounds[2].extent.x,localStart.y));
  904. dglDrawText(mFont, textpos, buff, mProfile->mFontColors);
  905. } else
  906. {
  907. Point2I textpos = localToGlobalCoord(Point2I(mBounds.extent.x - txt_w - 12,localStart.y));
  908. dglDrawText(mFont, textpos, buff, mProfile->mFontColors);
  909. }
  910. } else
  911. {
  912. dglDrawText(mFont, globalStart, mText, mProfile->mFontColors);
  913. }
  914. // If we're rendering a bitmap border, then it will take care of the arrow.
  915. if(!(mProfile->mProfileForChildren && mProfile->mBitmapArrayRects.size()))
  916. {
  917. // DAW: Draw a triangle (down arrow)
  918. F32 left = (F32)(r.point.x + r.extent.x - 12);
  919. F32 right = (F32)(left + 8);
  920. F32 middle = (F32)(left + 4);
  921. F32 top = (F32)(r.extent.y / 2 + r.point.y - 4);
  922. F32 bottom = (F32)(top + 8);
  923. #ifdef TORQUE_OS_IOS
  924. // PUAP -Mat untested
  925. glColor4ub(mProfile->mFontColor.red,mProfile->mFontColor.green,mProfile->mFontColor.blue, 255);
  926. GLfloat verts[] = {
  927. left, top,
  928. right, top,
  929. middle, bottom,
  930. };
  931. glVertexPointer(2, GL_FLOAT, 0, verts);
  932. glDrawArrays(GL_TRIANGLES, 0, 4);
  933. #else
  934. glBegin(GL_TRIANGLES);
  935. glColor3i(mProfile->mFontColor.red,mProfile->mFontColor.green,mProfile->mFontColor.blue);
  936. glVertex2fv( Point3F(left,top,0) );
  937. glVertex2fv( Point3F(right,top,0) );
  938. glVertex2fv( Point3F(middle,bottom,0) );
  939. glEnd();
  940. #endif
  941. }
  942. }
  943. //------------------------------------------------------------------------------
  944. void GuiPopUpMenuCtrlEx::closePopUp()
  945. {
  946. if ( !mInAction )
  947. return;
  948. // Get the selection from the text list:
  949. mSelIndex = mTl->getSelectedCell().y;
  950. mSelIndex = (mRevNum >= mSelIndex && mSelIndex != -1) ? mRevNum - mSelIndex : mSelIndex;
  951. if ( mSelIndex != -1 )
  952. {
  953. if(mReplaceText)
  954. setText( mEntries[mSelIndex].buf );
  955. setIntVariable( mEntries[mSelIndex].id );
  956. }
  957. // Release the mouse:
  958. mInAction = false;
  959. //mTl->mouseUnlock();
  960. // DAW: Commented out below and moved to the end of the function. See the
  961. // note below for the reason why.
  962. /*
  963. // Pop the background:
  964. getRoot()->popDialogControl(mBackground);
  965. // Kill the popup:
  966. mBackground->removeObject( mSc );
  967. mTl->deleteObject();
  968. mSc->deleteObject();
  969. mBackground->deleteObject();
  970. // Set this as the first responder:
  971. setFocus();
  972. */
  973. // Now perform the popup action:
  974. if ( mSelIndex != -1 )
  975. {
  976. char idval[24];
  977. dSprintf( idval, sizeof(idval), "%d", mEntries[mSelIndex].id );
  978. if(isMethod("onSelect"))
  979. Con::executef( this, 3, "onSelect", idval, mEntries[mSelIndex].buf );
  980. }
  981. else if(isMethod("onSelect"))
  982. Con::executef( this, 1, "onCancel" );
  983. // Execute the popup console command:
  984. if ( mConsoleCommand[0] )
  985. Con::evaluate( mConsoleCommand, false );
  986. // DAW: Reordered this pop dialog to be after the script select callback. When the
  987. // background was popped it was causing all sorts of focus issues when
  988. // suddenly the first text edit control took the focus before it came back
  989. // to this popup.
  990. // Pop the background:
  991. getRoot()->popDialogControl(mBackground);
  992. // Kill the popup:
  993. mBackground->removeObject( mSc );
  994. mTl->deleteObject();
  995. mSc->deleteObject();
  996. mBackground->deleteObject();
  997. // Set this as the first responder:
  998. setFirstResponder();
  999. }
  1000. //------------------------------------------------------------------------------
  1001. bool GuiPopUpMenuCtrlEx::onKeyDown(const GuiEvent &event)
  1002. {
  1003. //if the control is a dead end, don't process the input:
  1004. if ( !mVisible || !mActive || !mAwake )
  1005. return false;
  1006. //see if the key down is a <return> or not
  1007. if ( event.keyCode == KEY_RETURN && event.modifier == 0 )
  1008. {
  1009. onAction();
  1010. return true;
  1011. }
  1012. //otherwise, pass the event to its parent
  1013. return Parent::onKeyDown( event );
  1014. }
  1015. //------------------------------------------------------------------------------
  1016. void GuiPopUpMenuCtrlEx::onAction()
  1017. {
  1018. GuiControl *canCtrl = getParent();
  1019. addChildren();
  1020. GuiCanvas *root = getRoot();
  1021. Point2I windowExt = root->mBounds.extent;
  1022. mBackground->mBounds.point.set(0,0);
  1023. mBackground->mBounds.extent = root->mBounds.extent;
  1024. S32 textWidth = 0, width = mBounds.extent.x;
  1025. //const S32 menuSpace = 5; // DAW: Removed as no longer used.
  1026. const S32 textSpace = 2;
  1027. bool setScroll = false;
  1028. for( U32 i = 0; i < (U32)mEntries.size(); ++i )
  1029. if(S32(mFont->getStrWidth(mEntries[i].buf)) > textWidth)
  1030. textWidth = mFont->getStrWidth(mEntries[i].buf);
  1031. //if(textWidth > mBounds.extent.x)
  1032. S32 sbWidth = mSc->mProfile->mBorderThickness * 2 + mSc->scrollBarThickness(); // DAW: Calculate the scroll bar width
  1033. if(textWidth > (mBounds.extent.x - sbWidth-mProfile->mTextOffset.x - mSc->getChildMargin().x * 2)) // DAW: The text draw area to test against is the width of the drop-down minus the scroll bar width, the text margin and the scroll bar child margins.
  1034. {
  1035. //textWidth +=10;
  1036. textWidth +=sbWidth + mProfile->mTextOffset.x + mSc->getChildMargin().x * 2; // DAW: The new width is the width of the text plus the scroll bar width plus the text margin size plus the scroll bar child margins.
  1037. width = textWidth;
  1038. // DAW: If a child margin is not defined for the scroll control, let's add
  1039. // some space between the text and scroll control for readability
  1040. if(mSc->getChildMargin().x == 0)
  1041. width += 2;
  1042. }
  1043. //mTl->setCellSize(Point2I(width, mFont->getHeight()+3));
  1044. mTl->setCellSize(Point2I(width, mFont->getHeight() + textSpace)); // DAW: Modified the above line to use textSpace rather than the '3' as this is what is used below.
  1045. for( U32 j=0; j < (U32)mEntries.size(); ++j )
  1046. mTl->addEntry(mEntries[j].id, mEntries[j].buf);
  1047. Point2I pointInGC = canCtrl->localToGlobalCoord(mBounds.point);
  1048. Point2I scrollPoint(pointInGC.x, pointInGC.y + mBounds.extent.y);
  1049. //Calc max Y distance, so Scroll Ctrl will fit on window
  1050. //S32 maxYdis = windowExt.y - pointInGC.y - mBounds.extent.y - menuSpace;
  1051. S32 sbBorder = mSc->mProfile->mBorderThickness * 2 + mSc->getChildMargin().y * 2; // DAW: Added to take into account the border thickness and the margin of the child controls of the scroll control when figuring out the size of the contained text list control
  1052. S32 maxYdis = windowExt.y - pointInGC.y - mBounds.extent.y - sbBorder; // - menuSpace; // DAW: Need to remove the border thickness from the contained control maximum extent and got rid of the 'menuspace' variable
  1053. //If scroll bars need to be added
  1054. mRevNum = 0; // DAW: Added here rather than within the following 'if' statements.
  1055. if(maxYdis < mTl->mBounds.extent.y + sbBorder) // DAW: Instead of adding sbBorder, it was: 'textSpace'
  1056. {
  1057. //Should we pop menu list above the button
  1058. if(maxYdis < pointInGC.y ) // DAW: removed: '- menuSpace)' from check to see if there is more space above the control than below.
  1059. {
  1060. if(mReverseTextList) // DAW: Added this check if we should reverse the text list.
  1061. reverseTextList();
  1062. maxYdis = pointInGC.y; // DAW: Was at the end: '- menuSpace;'
  1063. //Does the menu need a scroll bar
  1064. if(maxYdis < mTl->mBounds.extent.y + sbBorder) // DAW: Instead of adding sbBorder, it was: 'textSpace'
  1065. {
  1066. // DAW: Removed width recalculation for scroll bar as the scroll bar is already being taken into account.
  1067. //Calc for the width of the scroll bar
  1068. // if(textWidth >= width)
  1069. // width += 20;
  1070. // mTl->setCellSize(Point2I(width,mFont->getHeight() + textSpace));
  1071. //Pop menu list above the button
  1072. // scrollPoint.set(pointInGC.x, menuSpace - 1); // DAW: Removed as calculated outside the 'if', and was wrong to begin with
  1073. setScroll = true;
  1074. }
  1075. //No scroll bar needed
  1076. else
  1077. {
  1078. maxYdis = mTl->mBounds.extent.y + sbBorder; // DAW: Instead of adding sbBorder, it was: 'textSpace'
  1079. // scrollPoint.set(pointInGC.x, pointInGC.y - maxYdis -1); // DAW: Removed as calculated outside the 'if' and the '-1' at the end is wrong
  1080. }
  1081. // DAW: Added the next two lines
  1082. scrollPoint.set(pointInGC.x, pointInGC.y - maxYdis); // DAW: Used to have the following on the end: '-1);'
  1083. }
  1084. //Scroll bar needed but Don't pop above button
  1085. else
  1086. {
  1087. //mRevNum = 0; // DAW: Commented out as it has been added at the beginning of this function
  1088. if ( mSelIndex >= 0 )
  1089. mTl->setSelectedCell( Point2I( 0, mSelIndex ) ); // DAW: Added as we were not setting the selected cell if the list is displayed down.
  1090. // DAW: Removed width recalculation for scroll bar as the scroll bar is already being taken into account.
  1091. //Calc for the width of the scroll bar
  1092. // if(textWidth >= width)
  1093. // width += 20;
  1094. // mTl->setCellSize(Point2I(width,mFont->getHeight() + textSpace));
  1095. setScroll = true;
  1096. }
  1097. }
  1098. //No scroll bar needed
  1099. else
  1100. {
  1101. if ( mSelIndex >= 0 )
  1102. mTl->setSelectedCell( Point2I( 0, mSelIndex ) ); // DAW: Added as we were not setting the selected cell if the list is displayed down.
  1103. //maxYdis = mTl->mBounds.extent.y + textSpace;
  1104. maxYdis = mTl->mBounds.extent.y + sbBorder; // DAW: Added in the border thickness of the scroll control and removed the addition of textSpace
  1105. }
  1106. //offset it from the background so it lines up properly
  1107. mSc->mBounds.point = mBackground->globalToLocalCoord(scrollPoint);
  1108. if(mSc->mBounds.point.x + width > mBackground->mBounds.extent.x)
  1109. if(width - mBounds.extent.x > 0)
  1110. mSc->mBounds.point.x -= width - mBounds.extent.x;
  1111. //mSc->mBounds.extent.set(width-1, maxYdis);
  1112. mSc->mBounds.extent.set(width, maxYdis); // DAW: Not sure why the '-1' above.
  1113. mSc->registerObject();
  1114. mTl->registerObject();
  1115. mBackground->registerObject();
  1116. mSc->addObject( mTl );
  1117. mBackground->addObject( mSc );
  1118. mBackgroundCancel = false; // DAW: Setup check if user clicked on the background instead of the text list (ie: didn't want to change their current selection).
  1119. root->pushDialogControl(mBackground, 99);
  1120. if ( setScroll )
  1121. {
  1122. if ( mSelIndex )
  1123. mTl->scrollCellVisible( Point2I(0, mSelIndex));
  1124. else
  1125. mTl->scrollCellVisible( Point2I( 0, 0 ) );
  1126. }
  1127. mTl->setFirstResponder();
  1128. mInAction = true;
  1129. }
  1130. //------------------------------------------------------------------------------
  1131. void GuiPopUpMenuCtrlEx::addChildren()
  1132. {
  1133. mTl = new GuiPopupTextListCtrlEx(this);
  1134. AssertFatal(mTl, "Failed to create the GuiPopUpTextListCtrl for the PopUpMenu");
  1135. mTl->mProfile = mProfile->mProfileForChildren ? mProfile->mProfileForChildren : mProfile; // Use the children's profile rather than the parent's profile, if it exists.
  1136. mTl->setField("noDuplicates", "false");
  1137. mSc = new GuiScrollCtrl;
  1138. AssertFatal(mSc, "Failed to create the GuiScrollCtrl for the PopUpMenu");
  1139. mSc->mProfile = mProfile->mProfileForChildren ? mProfile->mProfileForChildren : mProfile; // Use the children's profile rather than the parent's profile, if it exists.
  1140. mSc->setField("hScrollBar","AlwaysOff");
  1141. mSc->setField("vScrollBar","dynamic");
  1142. //if(mRenderScrollInNA) // DAW: Force the scroll control to render using fillColorNA rather than fillColor
  1143. // mSc->mUseNABackground = true;
  1144. mBackground = new GuiPopUpBackgroundCtrlEx(this, mTl);
  1145. AssertFatal(mBackground, "Failed to create the GuiBackgroundCtrl for the PopUpMenu");
  1146. }
  1147. //------------------------------------------------------------------------------
  1148. void GuiPopUpMenuCtrlEx::repositionPopup()
  1149. {
  1150. if ( !mInAction || !mSc || !mTl )
  1151. return;
  1152. // I'm not concerned with this right now...
  1153. }
  1154. //------------------------------------------------------------------------------
  1155. void GuiPopUpMenuCtrlEx::reverseTextList()
  1156. {
  1157. mTl->clear();
  1158. for(S32 i=mEntries.size()-1; i >= 0; --i)
  1159. mTl->addEntry(mEntries[i].id, mEntries[i].buf);
  1160. // Don't lose the selected cell:
  1161. if ( mSelIndex >= 0 )
  1162. mTl->setSelectedCell( Point2I( 0, mEntries.size() - mSelIndex - 1 ) );
  1163. mRevNum = mEntries.size() - 1;
  1164. }
  1165. //------------------------------------------------------------------------------
  1166. bool GuiPopUpMenuCtrlEx::getFontColor( ColorI &fontColor, S32 id, bool selected, bool mouseOver )
  1167. {
  1168. U32 i;
  1169. Entry* entry = NULL;
  1170. for ( i = 0; i < (U32)mEntries.size(); i++ )
  1171. {
  1172. if ( mEntries[i].id == id )
  1173. {
  1174. entry = &mEntries[i];
  1175. break;
  1176. }
  1177. }
  1178. if ( !entry )
  1179. return( false );
  1180. if ( entry->scheme != 0 )
  1181. {
  1182. // Find the entry's color scheme:
  1183. for ( i = 0; i < (U32)mSchemes.size(); i++ )
  1184. {
  1185. if ( mSchemes[i].id == entry->scheme )
  1186. {
  1187. fontColor = selected ? mSchemes[i].fontColorSEL : mouseOver ? mSchemes[i].fontColorHL : mSchemes[i].fontColor;
  1188. return( true );
  1189. }
  1190. }
  1191. }
  1192. if(id == -1)
  1193. fontColor = mProfile->mFontColorHL;
  1194. else
  1195. // Default color scheme...
  1196. fontColor = selected ? mProfile->mFontColorSEL : mouseOver ? mProfile->mFontColorHL : mProfile->mFontColorNA; // DAW: Modified the final colour choice from mProfile->mFontColor to mProfile->mFontColorNA
  1197. return( true );
  1198. }
  1199. //------------------------------------------------------------------------------
  1200. // DAW: Added
  1201. bool GuiPopUpMenuCtrlEx::getColoredBox( ColorI &fontColor, S32 id )
  1202. {
  1203. U32 i;
  1204. Entry* entry = NULL;
  1205. for ( i = 0; i < (U32)mEntries.size(); i++ )
  1206. {
  1207. if ( mEntries[i].id == id )
  1208. {
  1209. entry = &mEntries[i];
  1210. break;
  1211. }
  1212. }
  1213. if ( !entry )
  1214. return( false );
  1215. if( entry->usesColorBox == false)
  1216. return false;
  1217. fontColor = entry->colorbox;
  1218. return true;
  1219. }
  1220. //------------------------------------------------------------------------------
  1221. void GuiPopUpMenuCtrlEx::onMouseDown(const GuiEvent &event)
  1222. {
  1223. if(!mActive)
  1224. return;
  1225. onAction();
  1226. }
  1227. //------------------------------------------------------------------------------
  1228. void GuiPopUpMenuCtrlEx::onMouseUp(const GuiEvent &event)
  1229. {
  1230. }
  1231. //------------------------------------------------------------------------------
  1232. // DAW: Added
  1233. void GuiPopUpMenuCtrlEx::onMouseEnter(const GuiEvent &event)
  1234. {
  1235. mMouseOver = true;
  1236. }
  1237. //------------------------------------------------------------------------------
  1238. // DAW: Added
  1239. void GuiPopUpMenuCtrlEx::onMouseLeave(const GuiEvent &)
  1240. {
  1241. mMouseOver = false;
  1242. }
  1243. //------------------------------------------------------------------------------
  1244. void GuiPopUpMenuCtrlEx::setupAutoScroll(const GuiEvent &event)
  1245. {
  1246. GuiControl *parent = getParent();
  1247. if (! parent) return;
  1248. Point2I mousePt = mSc->globalToLocalCoord(event.mousePoint);
  1249. mEventSave = event;
  1250. if(mLastYvalue != mousePt.y)
  1251. {
  1252. mScrollDir = GuiScrollCtrl::None;
  1253. if(mousePt.y > mSc->mBounds.extent.y || mousePt.y < 0)
  1254. {
  1255. S32 topOrBottom = (mousePt.y > mSc->mBounds.extent.y) ? 1 : 0;
  1256. mSc->scrollTo(0, topOrBottom);
  1257. return;
  1258. }
  1259. F32 percent = (F32)mousePt.y / (F32)mSc->mBounds.extent.y;
  1260. if(percent > 0.7f && mousePt.y > mLastYvalue)
  1261. {
  1262. mIncValue = percent - 0.5f;
  1263. mScrollDir = GuiScrollCtrl::DownArrow;
  1264. }
  1265. else if(percent < 0.3f && mousePt.y < mLastYvalue)
  1266. {
  1267. mIncValue = 0.5f - percent;
  1268. mScrollDir = GuiScrollCtrl::UpArrow;
  1269. }
  1270. mLastYvalue = mousePt.y;
  1271. }
  1272. }
  1273. //------------------------------------------------------------------------------
  1274. void GuiPopUpMenuCtrlEx::autoScroll()
  1275. {
  1276. mScrollCount += mIncValue;
  1277. while(mScrollCount > 1)
  1278. {
  1279. mSc->autoScroll(mScrollDir);
  1280. mScrollCount -= 1;
  1281. }
  1282. mTl->onMouseMove(mEventSave);
  1283. }
  1284. //------------------------------------------------------------------------------
  1285. void GuiPopUpMenuCtrlEx::replaceText(S32 boolVal)
  1286. {
  1287. mReplaceText = boolVal;
  1288. }