guiPopUpCtrl.cpp 45 KB

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