guiPopUpCtrl.cpp 45 KB

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