guiPopUpCtrl.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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, (), , "Gets the selected index")
  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 or -1 if not found.")
  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. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  714. RectI r( offset, getExtent() );
  715. if ( mInAction )
  716. {
  717. S32 l = r.point.x, r2 = r.point.x + r.extent.x - 1;
  718. S32 t = r.point.y, b = r.point.y + r.extent.y - 1;
  719. // Do we render a bitmap border or lines?
  720. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  721. {
  722. // Render the fixed, filled in border
  723. renderFixedBitmapBordersFilled(r, 3, mProfile );
  724. }
  725. else
  726. {
  727. //renderSlightlyLoweredBox(r, mProfile);
  728. drawUtil->drawRectFill( r, mProfile->mFillColor );
  729. }
  730. // Draw a bitmap over the background?
  731. if ( mTextureDepressed )
  732. {
  733. RectI rect(offset, mBitmapBounds);
  734. drawUtil->clearBitmapModulation();
  735. drawUtil->drawBitmapStretch( mTextureDepressed, rect );
  736. }
  737. else if ( mTextureNormal )
  738. {
  739. RectI rect(offset, mBitmapBounds);
  740. drawUtil->clearBitmapModulation();
  741. drawUtil->drawBitmapStretch( mTextureNormal, rect );
  742. }
  743. // Do we render a bitmap border or lines?
  744. if ( !( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() ) )
  745. {
  746. drawUtil->drawLine( l, t, l, b, colorWhite );
  747. drawUtil->drawLine( l, t, r2, t, colorWhite );
  748. drawUtil->drawLine( l + 1, b, r2, b, mProfile->mBorderColor );
  749. drawUtil->drawLine( r2, t + 1, r2, b - 1, mProfile->mBorderColor );
  750. }
  751. }
  752. else
  753. // TODO: Implement
  754. // TODO: Add onMouseEnter() and onMouseLeave() and a definition of mMouseOver (see guiButtonBaseCtrl) for this to work.
  755. if ( mMouseOver )
  756. {
  757. S32 l = r.point.x, r2 = r.point.x + r.extent.x - 1;
  758. S32 t = r.point.y, b = r.point.y + r.extent.y - 1;
  759. // Do we render a bitmap border or lines?
  760. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  761. {
  762. // Render the fixed, filled in border
  763. renderFixedBitmapBordersFilled( r, 2, mProfile );
  764. }
  765. else
  766. {
  767. drawUtil->drawRectFill( r, mProfile->mFillColorHL );
  768. }
  769. // Draw a bitmap over the background?
  770. if ( mTextureNormal )
  771. {
  772. RectI rect( offset, mBitmapBounds );
  773. drawUtil->clearBitmapModulation();
  774. drawUtil->drawBitmapStretch( mTextureNormal, rect );
  775. }
  776. // Do we render a bitmap border or lines?
  777. if ( !( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() ) )
  778. {
  779. drawUtil->drawLine( l, t, l, b, colorWhite );
  780. drawUtil->drawLine( l, t, r2, t, colorWhite );
  781. drawUtil->drawLine( l + 1, b, r2, b, mProfile->mBorderColor );
  782. drawUtil->drawLine( r2, t + 1, r2, b - 1, mProfile->mBorderColor );
  783. }
  784. }
  785. else
  786. {
  787. // Do we render a bitmap border or lines?
  788. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  789. {
  790. // Render the fixed, filled in border
  791. renderFixedBitmapBordersFilled( r, 1, mProfile );
  792. }
  793. else
  794. {
  795. drawUtil->drawRectFill( r, mProfile->mFillColorNA );
  796. }
  797. // Draw a bitmap over the background?
  798. if ( mTextureNormal )
  799. {
  800. RectI rect(offset, mBitmapBounds);
  801. drawUtil->clearBitmapModulation();
  802. drawUtil->drawBitmapStretch( mTextureNormal, rect );
  803. }
  804. // Do we render a bitmap border or lines?
  805. if ( !( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() ) )
  806. {
  807. drawUtil->drawRect( r, mProfile->mBorderColorNA );
  808. }
  809. }
  810. // renderSlightlyRaisedBox(r, mProfile); // Used to be the only 'else' condition to mInAction above.
  811. S32 txt_w = mProfile->mFont->getStrWidth(mText);
  812. localStart.x = 0;
  813. localStart.y = (getHeight() - (mProfile->mFont->getHeight())) / 2;
  814. // align the horizontal
  815. switch (mProfile->mAlignment)
  816. {
  817. case GuiControlProfile::RightJustify:
  818. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  819. {
  820. // We're making use of a bitmap border, so take into account the
  821. // right cap of the border.
  822. RectI* bitmapBounds = mProfile->mBitmapArrayRects.address();
  823. localStart.x = getWidth() - bitmapBounds[2].extent.x - txt_w;
  824. }
  825. else
  826. {
  827. localStart.x = getWidth() - txt_w;
  828. }
  829. break;
  830. case GuiControlProfile::CenterJustify:
  831. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  832. {
  833. // We're making use of a bitmap border, so take into account the
  834. // right cap of the border.
  835. RectI* bitmapBounds = mProfile->mBitmapArrayRects.address();
  836. localStart.x = (getWidth() - bitmapBounds[2].extent.x - txt_w) / 2;
  837. } else
  838. {
  839. localStart.x = (getWidth() - txt_w) / 2;
  840. }
  841. break;
  842. default:
  843. // GuiControlProfile::LeftJustify
  844. if ( txt_w > getWidth() )
  845. {
  846. // The width of the text is greater than the width of the control.
  847. // In this case we will right justify the text and leave some space
  848. // for the down arrow.
  849. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  850. {
  851. // We're making use of a bitmap border, so take into account the
  852. // right cap of the border.
  853. RectI* bitmapBounds = mProfile->mBitmapArrayRects.address();
  854. localStart.x = getWidth() - bitmapBounds[2].extent.x - txt_w;
  855. }
  856. else
  857. {
  858. localStart.x = getWidth() - txt_w - 12;
  859. }
  860. }
  861. else
  862. {
  863. localStart.x = mProfile->mTextOffset.x; // Use mProfile->mTextOffset as a controlable margin for the control's text.
  864. }
  865. break;
  866. }
  867. // Do we first draw a coloured box beside the text?
  868. ColorI boxColor;
  869. bool drawbox = getColoredBox( boxColor, mSelIndex);
  870. if ( drawbox )
  871. {
  872. Point2I coloredboxsize( 15, 10 );
  873. RectI r( offset.x + mProfile->mTextOffset.x, offset.y + ( (getHeight() - coloredboxsize.y ) / 2 ), coloredboxsize.x, coloredboxsize.y );
  874. drawUtil->drawRectFill( r, boxColor);
  875. drawUtil->drawRect( r, ColorI(0,0,0));
  876. localStart.x += coloredboxsize.x + mProfile->mTextOffset.x;
  877. }
  878. // Draw the text
  879. Point2I globalStart = localToGlobalCoord( localStart );
  880. ColorI fontColor = mActive ? ( mInAction ? mProfile->mFontColor : mProfile->mFontColorNA ) : mProfile->mFontColorNA;
  881. drawUtil->setBitmapModulation( fontColor ); // was: (mProfile->mFontColor);
  882. // Get the number of columns in the text
  883. S32 colcount = getColumnCount( mText, "\t" );
  884. // Are there two or more columns?
  885. if ( colcount >= 2 )
  886. {
  887. char buff[256];
  888. // Draw the first column
  889. getColumn( mText, buff, 0, "\t" );
  890. drawUtil->drawText( mProfile->mFont, globalStart, buff, mProfile->mFontColors );
  891. // Draw the second column to the right
  892. getColumn( mText, buff, 1, "\t" );
  893. S32 txt_w = mProfile->mFont->getStrWidth( buff );
  894. if ( mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size() )
  895. {
  896. // We're making use of a bitmap border, so take into account the
  897. // right cap of the border.
  898. RectI* bitmapBounds = mProfile->mBitmapArrayRects.address();
  899. Point2I textpos = localToGlobalCoord( Point2I( getWidth() - txt_w - bitmapBounds[2].extent.x, localStart.y ) );
  900. drawUtil->drawText( mProfile->mFont, textpos, buff, mProfile->mFontColors );
  901. } else
  902. {
  903. Point2I textpos = localToGlobalCoord( Point2I( getWidth() - txt_w - 12, localStart.y ) );
  904. drawUtil->drawText( mProfile->mFont, textpos, buff, mProfile->mFontColors );
  905. }
  906. } else
  907. {
  908. drawUtil->drawText( mProfile->mFont, globalStart, mText, mProfile->mFontColors );
  909. }
  910. // If we're rendering a bitmap border, then it will take care of the arrow.
  911. if ( !(mProfile->getChildrenProfile() && mProfile->mBitmapArrayRects.size()) )
  912. {
  913. // Draw a triangle (down arrow)
  914. S32 left = r.point.x + r.extent.x - 12;
  915. S32 right = left + 8;
  916. S32 middle = left + 4;
  917. S32 top = r.extent.y / 2 + r.point.y - 4;
  918. S32 bottom = top + 8;
  919. PrimBuild::color( mProfile->mFontColor );
  920. PrimBuild::begin( GFXTriangleList, 3 );
  921. PrimBuild::vertex2fv( Point3F( (F32)left, (F32)top, 0.0f ) );
  922. PrimBuild::vertex2fv( Point3F( (F32)right, (F32)top, 0.0f ) );
  923. PrimBuild::vertex2fv( Point3F( (F32)middle, (F32)bottom, 0.0f ) );
  924. PrimBuild::end();
  925. }
  926. }
  927. //------------------------------------------------------------------------------
  928. void GuiPopUpMenuCtrl::closePopUp()
  929. {
  930. if ( !mInAction )
  931. return;
  932. // Get the selection from the text list:
  933. if( !mBackgroundCancel )
  934. {
  935. mSelIndex = mTl->getSelectedCell().y;
  936. mSelIndex = ( mRevNum >= mSelIndex && mSelIndex != -1 ) ? mRevNum - mSelIndex : mSelIndex;
  937. if ( mSelIndex != -1 )
  938. {
  939. if ( mReplaceText )
  940. setText( mEntries[mSelIndex].buf );
  941. setIntVariable( mEntries[mSelIndex].id );
  942. }
  943. }
  944. // Release the mouse:
  945. mInAction = false;
  946. mTl->mouseUnlock();
  947. // Now perform the popup action:
  948. if( mSelIndex != -1 && !mBackgroundCancel )
  949. {
  950. if ( isMethod( "onSelect" ) )
  951. Con::executef( this, "onSelect", Con::getIntArg( mEntries[ mSelIndex ].id ), mEntries[mSelIndex].buf );
  952. // Execute the popup console command:
  953. execConsoleCallback();
  954. }
  955. else if ( isMethod( "onCancel" ) )
  956. Con::executef( this, "onCancel" );
  957. // Pop the background:
  958. GuiCanvas *root = getRoot();
  959. if ( root )
  960. root->popDialogControl(mBackground);
  961. // Kill the popup:
  962. mBackground->removeObject( mSc );
  963. mTl->deleteObject();
  964. mSc->deleteObject();
  965. mBackground->deleteObject();
  966. mBackground = NULL;
  967. mTl = NULL;
  968. mSc = NULL;
  969. // Set this as the first responder:
  970. setFirstResponder();
  971. }
  972. //------------------------------------------------------------------------------
  973. bool GuiPopUpMenuCtrl::onKeyDown(const GuiEvent &event)
  974. {
  975. //if the control is a dead end, don't process the input:
  976. if ( !mVisible || !mActive || !mAwake )
  977. return false;
  978. //see if the key down is a <return> or not
  979. if ( event.keyCode == KEY_RETURN && event.modifier == 0 )
  980. {
  981. onAction();
  982. return true;
  983. }
  984. //otherwise, pass the event to its parent
  985. return Parent::onKeyDown( event );
  986. }
  987. //------------------------------------------------------------------------------
  988. void GuiPopUpMenuCtrl::onAction()
  989. {
  990. GuiControl *canCtrl = getParent();
  991. addChildren();
  992. GuiCanvas *root = getRoot();
  993. Point2I windowExt = root->getExtent();
  994. mBackground->resize( Point2I(0,0), root->getExtent() );
  995. S32 textWidth = 0, width = getWidth();
  996. const S32 textSpace = 2;
  997. bool setScroll = false;
  998. for ( U32 i = 0; i < mEntries.size(); ++i )
  999. if ( S32(mProfile->mFont->getStrWidth( mEntries[i].buf )) > textWidth )
  1000. textWidth = mProfile->mFont->getStrWidth( mEntries[i].buf );
  1001. S32 sbWidth = mSc->getControlProfile()->mBorderThickness * 2 + mSc->scrollBarThickness(); // Calculate the scroll bar width
  1002. 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.
  1003. {
  1004. 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.
  1005. width = textWidth;
  1006. // If a child margin is not defined for the scroll control, let's add
  1007. // some space between the text and scroll control for readability
  1008. if(mSc->getChildMargin().x == 0)
  1009. width += textSpace;
  1010. }
  1011. mTl->setCellSize(Point2I(width, mProfile->mFont->getHeight() + textSpace));
  1012. for ( U32 j = 0; j < mEntries.size(); ++j )
  1013. mTl->addEntry( mEntries[j].id, mEntries[j].buf );
  1014. if ( mSelIndex >= 0 )
  1015. mTl->setSelectedCell( Point2I( 0, mSelIndex ) );
  1016. Point2I pointInGC = canCtrl->localToGlobalCoord( getPosition() );
  1017. Point2I scrollPoint( pointInGC.x, pointInGC.y + getHeight() );
  1018. //Calc max Y distance, so Scroll Ctrl will fit on window
  1019. S32 sbBorder = mSc->getControlProfile()->mBorderThickness * 2 + mSc->getChildMargin().y * 2;
  1020. S32 maxYdis = windowExt.y - pointInGC.y - getHeight() - sbBorder;
  1021. //If scroll bars need to be added
  1022. mRevNum = 0;
  1023. if ( maxYdis < mTl->getHeight() + sbBorder )
  1024. {
  1025. //Should we pop menu list above the button
  1026. if ( maxYdis < pointInGC.y )
  1027. {
  1028. if(mReverseTextList)
  1029. reverseTextList();
  1030. maxYdis = pointInGC.y;
  1031. //Does the menu need a scroll bar
  1032. if ( maxYdis < mTl->getHeight() + sbBorder )
  1033. {
  1034. setScroll = true;
  1035. }
  1036. //No scroll bar needed
  1037. else
  1038. {
  1039. maxYdis = mTl->getHeight() + sbBorder;
  1040. }
  1041. // Added the next two lines
  1042. scrollPoint.set(pointInGC.x, pointInGC.y - maxYdis); // Used to have the following on the end: '-1);'
  1043. }
  1044. //Scroll bar needed but Don't pop above button
  1045. else
  1046. {
  1047. setScroll = true;
  1048. }
  1049. }
  1050. //No scroll bar needed
  1051. else
  1052. {
  1053. maxYdis = mTl->getHeight() + sbBorder;
  1054. }
  1055. RectI newBounds = mSc->getBounds();
  1056. //offset it from the background so it lines up properly
  1057. newBounds.point = mBackground->globalToLocalCoord( scrollPoint );
  1058. if ( newBounds.point.x + width > mBackground->getWidth() )
  1059. if ( width - getWidth() > 0 )
  1060. newBounds.point.x -= width - getWidth();
  1061. newBounds.extent.set( width, maxYdis );
  1062. mSc->setBounds( newBounds );
  1063. mSc->registerObject();
  1064. mTl->registerObject();
  1065. mBackground->registerObject();
  1066. mSc->addObject( mTl );
  1067. mBackground->addObject( mSc );
  1068. mBackgroundCancel = false; // Setup check if user clicked on the background instead of the text list (ie: didn't want to change their current selection).
  1069. root->pushDialogControl( mBackground, 99 );
  1070. if ( setScroll )
  1071. {
  1072. // Resize the text list
  1073. Point2I cellSize;
  1074. mTl->getCellSize( cellSize );
  1075. cellSize.x = width - mSc->scrollBarThickness() - sbBorder;
  1076. mTl->setCellSize( cellSize );
  1077. mTl->setWidth( cellSize.x );
  1078. if ( mSelIndex )
  1079. mTl->scrollCellVisible( Point2I( 0, mSelIndex ) );
  1080. else
  1081. mTl->scrollCellVisible( Point2I( 0, 0 ) );
  1082. }
  1083. mTl->setFirstResponder();
  1084. mInAction = true;
  1085. }
  1086. //------------------------------------------------------------------------------
  1087. void GuiPopUpMenuCtrl::addChildren()
  1088. {
  1089. // Create Text List.
  1090. mTl = new GuiPopupTextListCtrl( this );
  1091. AssertFatal( mTl, "Failed to create the GuiPopUpTextListCtrl for the PopUpMenu" );
  1092. // Use the children's profile rather than the parent's profile, if it exists.
  1093. mTl->setControlProfile( mProfile->getChildrenProfile() ? mProfile->getChildrenProfile() : mProfile );
  1094. mTl->setField("noDuplicates", "false");
  1095. mSc = new GuiScrollCtrl;
  1096. AssertFatal( mSc, "Failed to create the GuiScrollCtrl for the PopUpMenu" );
  1097. GuiControlProfile *prof;
  1098. if ( Sim::findObject( "GuiScrollProfile", prof ) )
  1099. {
  1100. mSc->setControlProfile( prof );
  1101. }
  1102. else
  1103. {
  1104. // Use the children's profile rather than the parent's profile, if it exists.
  1105. mSc->setControlProfile( mProfile->getChildrenProfile() ? mProfile->getChildrenProfile() : mProfile );
  1106. }
  1107. mSc->setField( "hScrollBar", "AlwaysOff" );
  1108. mSc->setField( "vScrollBar", "dynamic" );
  1109. //if(mRenderScrollInNA) // Force the scroll control to render using fillColorNA rather than fillColor
  1110. // mSc->mUseNABackground = true;
  1111. mBackground = new GuiPopUpBackgroundCtrl( this, mTl );
  1112. AssertFatal( mBackground, "Failed to create the GuiBackgroundCtrl for the PopUpMenu" );
  1113. }
  1114. //------------------------------------------------------------------------------
  1115. void GuiPopUpMenuCtrl::repositionPopup()
  1116. {
  1117. if ( !mInAction || !mSc || !mTl )
  1118. return;
  1119. // I'm not concerned with this right now...
  1120. }
  1121. //------------------------------------------------------------------------------
  1122. void GuiPopUpMenuCtrl::reverseTextList()
  1123. {
  1124. mTl->clear();
  1125. for ( S32 i = mEntries.size()-1; i >= 0; --i )
  1126. mTl->addEntry( mEntries[i].id, mEntries[i].buf );
  1127. // Don't lose the selected cell:
  1128. if ( mSelIndex >= 0 )
  1129. mTl->setSelectedCell( Point2I( 0, mEntries.size() - mSelIndex - 1 ) );
  1130. mRevNum = mEntries.size() - 1;
  1131. }
  1132. //------------------------------------------------------------------------------
  1133. bool GuiPopUpMenuCtrl::getFontColor( ColorI &fontColor, S32 id, bool selected, bool mouseOver )
  1134. {
  1135. U32 i;
  1136. Entry* entry = NULL;
  1137. for ( i = 0; i < mEntries.size(); i++ )
  1138. {
  1139. if ( mEntries[i].id == id )
  1140. {
  1141. entry = &mEntries[i];
  1142. break;
  1143. }
  1144. }
  1145. if ( !entry )
  1146. return( false );
  1147. if ( entry->scheme != 0 )
  1148. {
  1149. // Find the entry's color scheme:
  1150. for ( i = 0; i < mSchemes.size(); i++ )
  1151. {
  1152. if ( mSchemes[i].id == entry->scheme )
  1153. {
  1154. fontColor = selected ? mSchemes[i].fontColorSEL : mouseOver ? mSchemes[i].fontColorHL : mSchemes[i].fontColor;
  1155. return( true );
  1156. }
  1157. }
  1158. }
  1159. // Default color scheme...
  1160. fontColor = selected ? mProfile->mFontColorSEL : mouseOver ? mProfile->mFontColorHL : mProfile->mFontColorNA; // Modified the final color choice from mProfile->mFontColor to mProfile->mFontColorNA
  1161. return( true );
  1162. }
  1163. //------------------------------------------------------------------------------
  1164. // Added
  1165. bool GuiPopUpMenuCtrl::getColoredBox( ColorI &fontColor, S32 id )
  1166. {
  1167. U32 i;
  1168. Entry* entry = NULL;
  1169. for ( i = 0; i < mEntries.size(); i++ )
  1170. {
  1171. if ( mEntries[i].id == id )
  1172. {
  1173. entry = &mEntries[i];
  1174. break;
  1175. }
  1176. }
  1177. if ( !entry )
  1178. return false;
  1179. if ( entry->usesColorBox == false )
  1180. return false;
  1181. fontColor = entry->colorbox;
  1182. return true;
  1183. }
  1184. //------------------------------------------------------------------------------
  1185. void GuiPopUpMenuCtrl::onMouseDown( const GuiEvent &event )
  1186. {
  1187. TORQUE_UNUSED(event);
  1188. if( !mVisible || !mActive || !mAwake )
  1189. return;
  1190. onAction();
  1191. }
  1192. //------------------------------------------------------------------------------
  1193. void GuiPopUpMenuCtrl::onMouseUp( const GuiEvent &event )
  1194. {
  1195. TORQUE_UNUSED(event);
  1196. }
  1197. //------------------------------------------------------------------------------
  1198. // Added
  1199. void GuiPopUpMenuCtrl::onMouseEnter( const GuiEvent &event )
  1200. {
  1201. mMouseOver = true;
  1202. }
  1203. //------------------------------------------------------------------------------
  1204. // Added
  1205. void GuiPopUpMenuCtrl::onMouseLeave( const GuiEvent &event )
  1206. {
  1207. mMouseOver = false;
  1208. }
  1209. //------------------------------------------------------------------------------
  1210. void GuiPopUpMenuCtrl::setupAutoScroll( const GuiEvent &event )
  1211. {
  1212. GuiControl *parent = getParent();
  1213. if ( !parent )
  1214. return;
  1215. Point2I mousePt = mSc->globalToLocalCoord( event.mousePoint );
  1216. mEventSave = event;
  1217. if ( mLastYvalue != mousePt.y )
  1218. {
  1219. mScrollDir = GuiScrollCtrl::None;
  1220. if ( mousePt.y > mSc->getHeight() || mousePt.y < 0 )
  1221. {
  1222. S32 topOrBottom = ( mousePt.y > mSc->getHeight() ) ? 1 : 0;
  1223. mSc->scrollTo( 0, topOrBottom );
  1224. return;
  1225. }
  1226. F32 percent = (F32)mousePt.y / (F32)mSc->getHeight();
  1227. if ( percent > 0.7f && mousePt.y > mLastYvalue )
  1228. {
  1229. mIncValue = percent - 0.5f;
  1230. mScrollDir = GuiScrollCtrl::DownArrow;
  1231. }
  1232. else if ( percent < 0.3f && mousePt.y < mLastYvalue )
  1233. {
  1234. mIncValue = 0.5f - percent;
  1235. mScrollDir = GuiScrollCtrl::UpArrow;
  1236. }
  1237. mLastYvalue = mousePt.y;
  1238. }
  1239. }
  1240. //------------------------------------------------------------------------------
  1241. void GuiPopUpMenuCtrl::autoScroll()
  1242. {
  1243. mScrollCount += mIncValue;
  1244. while ( mScrollCount > 1 )
  1245. {
  1246. mSc->autoScroll( mScrollDir );
  1247. mScrollCount -= 1;
  1248. }
  1249. mTl->onMouseMove( mEventSave );
  1250. }
  1251. //------------------------------------------------------------------------------
  1252. void GuiPopUpMenuCtrl::replaceText(S32 boolVal)
  1253. {
  1254. mReplaceText = boolVal;
  1255. }