guiPopUpCtrl.cpp 46 KB

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