2
0

guiPopUpCtrl.cpp 47 KB

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