guiListBoxCtrl.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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/controls/guiListBoxCtrl.h"
  23. #include "gfx/gfxDrawUtil.h"
  24. #include "console/engineAPI.h"
  25. IMPLEMENT_CONOBJECT(GuiListBoxCtrl);
  26. ConsoleDocClass( GuiListBoxCtrl,
  27. "@brief A list of text items.\n\n"
  28. "A list of text items where each individual entry can have its own text value, text color and associated SimObject.\n\n"
  29. "@tsexample\n"
  30. "new GuiListBoxCtrl(GuiMusicPlayerMusicList)\n"
  31. "{\n"
  32. " allowMultipleSelections = \"true\";\n"
  33. " fitParentWidth = \"true\";\n"
  34. " mirrorSet = \"AnotherGuiListBoxCtrl\";\n"
  35. " makeNameCallback = \"\";\n"
  36. " colorBullet = \"1\";\n"
  37. " //Properties not specific to this control have been omitted from this example.\n"
  38. "};\n"
  39. "@endtsexample\n\n"
  40. "@see GuiControl\n\n"
  41. "@ingroup GuiCore\n"
  42. );
  43. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onMouseDragged, void, (),(),
  44. "@brief Called whenever the mouse is dragged across the control.\n\n"
  45. "@tsexample\n"
  46. "// Mouse is dragged across the control, causing the callback to occur.\n"
  47. "GuiListBoxCtrl::onMouseDragged(%this)\n"
  48. " {\n"
  49. " // Code to run whenever the mouse is dragged across the control\n"
  50. " }\n"
  51. "@endtsexample\n\n"
  52. "@see GuiControl\n\n"
  53. );
  54. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onClearSelection, void, (),(),
  55. "@brief Called whenever a selected item in the list is cleared.\n\n"
  56. "@tsexample\n"
  57. "// A selected item is cleared, causing the callback to occur.\n"
  58. "GuiListBoxCtrl::onClearSelection(%this)\n"
  59. " {\n"
  60. " // Code to run whenever a selected item is cleared\n"
  61. " }\n"
  62. "@endtsexample\n\n"
  63. "@see GuiControl\n\n"
  64. );
  65. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onUnSelect, void, ( S32 index, const char* itemText),( index, itemText ),
  66. "@brief Called whenever a selected item in the list has been unselected.\n\n"
  67. "@param index Index id of the item that was unselected\n"
  68. "@param itemText Text for the list entry at the index id that was unselected\n\n"
  69. "@tsexample\n"
  70. "// A selected item is unselected, causing the callback to occur\n"
  71. "GuiListBoxCtrl::onUnSelect(%this, %indexId, %itemText)\n"
  72. " {\n"
  73. " // Code to run whenever a selected list item is unselected\n"
  74. " }\n"
  75. "@endtsexample\n\n"
  76. "@see GuiControl\n\n"
  77. );
  78. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onSelect, void, ( S32 index , const char* itemText ),( index, itemText ),
  79. "@brief Called whenever an item in the list is selected.\n\n"
  80. "@param index Index id for the item in the list that was selected.\n"
  81. "@param itemText Text for the list item at the index that was selected.\n\n"
  82. "@tsexample\n"
  83. "// An item in the list is selected, causing the callback to occur\n"
  84. "GuiListBoxCtrl::onSelect(%this, %index, %itemText)\n"
  85. " {\n"
  86. " // Code to run whenever an item in the list is selected\n"
  87. " }\n"
  88. "@endtsexample\n\n"
  89. "@see GuiControl\n\n"
  90. );
  91. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onDoubleClick, void, (),(),
  92. "@brief Called whenever an item in the list has been double clicked.\n\n"
  93. "@tsexample\n"
  94. "// An item in the list is double clicked, causing the callback to occur.\n"
  95. "GuiListBoxCtrl::onDoubleClick(%this)\n"
  96. " {\n"
  97. " // Code to run whenever an item in the control has been double clicked\n"
  98. " }\n"
  99. "@endtsexample\n\n"
  100. "@see GuiControl\n\n"
  101. );
  102. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onMouseUp, void, ( S32 itemHit, S32 mouseClickCount ),( itemHit,mouseClickCount ),
  103. "@brief Called whenever the mouse has previously been clicked down (onMouseDown) and has now been raised on the control.\n"
  104. "If an item in the list was hit during the click cycle, then the index id of the clicked object along with how many clicks occured are passed\n"
  105. "into the callback.\n\n"
  106. "Detailed description\n\n"
  107. "@param itemHit Index id for the list item that was hit\n"
  108. "@param mouseClickCount How many mouse clicks occured on this list item\n\n"
  109. "@tsexample\n"
  110. "// Mouse was previously clicked down, and now has been released, causing the callback to occur.\n"
  111. "GuiListBoxCtrl::onMouseUp(%this, %itemHit, %mouseClickCount)\n"
  112. " {\n"
  113. " // Code to call whenever the mouse has been clicked and released on the control\n"
  114. " }\n"
  115. "@endtsexample\n\n"
  116. "@see GuiControl\n\n"
  117. );
  118. IMPLEMENT_CALLBACK( GuiListBoxCtrl, onDeleteKey, void, (),(),
  119. "@brief Called whenever the Delete key on the keyboard has been pressed while in this control.\n\n"
  120. "@tsexample\n"
  121. "// The delete key on the keyboard has been pressed while this control is in focus, causing the callback to occur.\n"
  122. "GuiListBoxCtrl::onDeleteKey(%this)\n"
  123. " {\n"
  124. " // Code to call whenever the delete key is pressed\n"
  125. " }\n"
  126. "@endtsexample\n\n"
  127. "@see GuiControl\n\n"
  128. );
  129. IMPLEMENT_CALLBACK( GuiListBoxCtrl, isObjectMirrored, bool, ( const char* indexIdString ),( indexIdString ),
  130. "@brief Checks if a list item at a defined index id is mirrored, and returns the result.\n\n"
  131. "@param indexIdString Index id of the list to check.\n"
  132. "@tsexample\n"
  133. "// Engine has requested of the script level to determine if a list entry is mirrored or not.\n"
  134. "GuiListBoxCtrl::isObjectMirrored(%this, %indexIdString)\n"
  135. " {\n"
  136. " // Perform code required to check and see if the list item at the index id is mirrored or not.\n"
  137. " return %isMirrored;\n"
  138. " }\n"
  139. "@endtsexample\n\n"
  140. "@return A boolean value on if the list item is mirrored or not.\n\n"
  141. "@see GuiControl\n\n"
  142. );
  143. GuiListBoxCtrl::GuiListBoxCtrl()
  144. {
  145. mItems.clear();
  146. mSelectedItems.clear();
  147. mMultipleSelections = true;
  148. mFitParentWidth = true;
  149. mColorBullet = true;
  150. mItemSize = Point2I(10,20);
  151. mLastClickItem = NULL;
  152. mRenderTooltipDelegate.bind( this, &GuiListBoxCtrl::renderTooltip );
  153. }
  154. GuiListBoxCtrl::~GuiListBoxCtrl()
  155. {
  156. clearItems();
  157. }
  158. void GuiListBoxCtrl::initPersistFields()
  159. {
  160. addField( "allowMultipleSelections", TypeBool, Offset( mMultipleSelections, GuiListBoxCtrl), "If true, will allow the selection of multiple items in the listbox.\n");
  161. addField( "fitParentWidth", TypeBool, Offset( mFitParentWidth, GuiListBoxCtrl), "If true, the width of the listbox will match the width of its parent control.\n");
  162. addField( "colorBullet", TypeBool, Offset( mColorBullet, GuiListBoxCtrl), "If true, colored items will render a colored rectangular bullet next to the item text.\n");
  163. addField( "mirrorSet", TypeRealString, Offset( mMirrorSetName, GuiListBoxCtrl ), "If populated with the name of another GuiListBoxCtrl, then this list box will mirror the contents of the mirrorSet listbox.\n");
  164. addField( "makeNameCallback", TypeRealString, Offset( mMakeNameCallback, GuiListBoxCtrl ), "A script snippet to control what is displayed in the list for a SimObject. Within this snippet, $ThisControl is bound to the guiListBoxCtrl and $ThisObject to the contained object in question.\n");
  165. Parent::initPersistFields();
  166. }
  167. bool GuiListBoxCtrl::onWake()
  168. {
  169. if( !Parent::onWake() )
  170. return false;
  171. updateSize();
  172. return true;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Item Accessors
  176. //-----------------------------------------------------------------------------
  177. DefineEngineMethod( GuiListBoxCtrl, setMultipleSelection, void, (bool allowMultSelections),,
  178. "@brief Enable or disable multiple selections for this GuiListBoxCtrl object.\n\n"
  179. "@param allowMultSelections Boolean variable to set the use of multiple selections or not.\n"
  180. "@tsexample\n"
  181. "// Define the multiple selection use state.\n"
  182. "%allowMultSelections = \"true\";\n\n"
  183. "// Set the allow multiple selection state on the GuiListBoxCtrl object.\n"
  184. "%thisGuiListBoxCtrl.setMultipleSelection(%allowMultSelections);\n"
  185. "@endtsexample\n\n"
  186. "@see GuiControl\n")
  187. {
  188. object->setMultipleSelection( allowMultSelections );
  189. }
  190. DefineEngineMethod( GuiListBoxCtrl, clearItems, void, (),,
  191. "@brief Clears all the items in the listbox.\n\n"
  192. "@tsexample\n"
  193. "// Inform the GuiListBoxCtrl object to clear all items from its list.\n"
  194. "%thisGuiListBoxCtrl.clearItems();\n"
  195. "@endtsexample\n\n"
  196. "@see GuiControl")
  197. {
  198. object->clearItems();
  199. }
  200. void GuiListBoxCtrl::clearItems()
  201. {
  202. // Free item list allocated memory
  203. while( mItems.size() )
  204. deleteItem( 0 );
  205. // Free our vector lists
  206. mItems.clear();
  207. mSelectedItems.clear();
  208. mFilteredItems.clear();
  209. }
  210. DefineEngineMethod( GuiListBoxCtrl, clearSelection, void, (),,
  211. "@brief Sets all currently selected items to unselected.\n\n"
  212. "Detailed description\n\n"
  213. "@tsexample\n"
  214. "// Inform the GuiListBoxCtrl object to set all of its items to unselected./n"
  215. "%thisGuiListBoxCtrl.clearSelection();\n"
  216. "@endtsexample\n\n"
  217. "@see GuiControl")
  218. {
  219. object->clearSelection();
  220. }
  221. void GuiListBoxCtrl::clearSelection()
  222. {
  223. if( !mSelectedItems.size() )
  224. return;
  225. VectorPtr<LBItem*>::iterator i = mSelectedItems.begin();
  226. for( ; i != mSelectedItems.end(); i++ )
  227. (*i)->isSelected = false;
  228. mSelectedItems.clear();
  229. onClearSelection_callback();
  230. }
  231. DefineEngineMethod( GuiListBoxCtrl, setSelected, void, (S32 index, bool setSelected), (true),
  232. "@brief Sets the item at the index specified to selected or not.\n\n"
  233. "Detailed description\n\n"
  234. "@param index Item index to set selected or unselected.\n"
  235. "@param setSelected Boolean selection state to set the requested item index.\n"
  236. "@tsexample\n"
  237. "// Define the index\n"
  238. "%index = \"5\";\n\n"
  239. "// Define the selection state\n"
  240. "%selected = \"true\"\n\n"
  241. "// Inform the GuiListBoxCtrl object of the new selection state for the requested index entry.\n"
  242. "%thisGuiListBoxCtrl.setSelected(%index,%selected);\n"
  243. "@endtsexample\n\n"
  244. "@see GuiControl")
  245. {
  246. if( setSelected == true )
  247. object->addSelection( index );
  248. else
  249. object->removeSelection( index );
  250. }
  251. void GuiListBoxCtrl::removeSelection( S32 index )
  252. {
  253. // Range Check
  254. if( index >= mItems.size() || index < 0 )
  255. {
  256. Con::warnf("GuiListBoxCtrl::removeSelection - index out of range!" );
  257. return;
  258. }
  259. removeSelection( mItems[index], index );
  260. }
  261. void GuiListBoxCtrl::removeSelection( LBItem *item, S32 index )
  262. {
  263. if( !mSelectedItems.size() )
  264. return;
  265. if( !item )
  266. return;
  267. for( S32 i = 0 ; i < mSelectedItems.size(); i++ )
  268. {
  269. if( mSelectedItems[i] == item )
  270. {
  271. mSelectedItems.erase( &mSelectedItems[i] );
  272. item->isSelected = false;
  273. onUnSelect_callback(index, item->itemText);
  274. return;
  275. }
  276. }
  277. }
  278. void GuiListBoxCtrl::addSelection( S32 index )
  279. {
  280. // Range Check
  281. if( index >= mItems.size() || index < 0 )
  282. {
  283. Con::warnf("GuiListBoxCtrl::addSelection- index out of range!" );
  284. return;
  285. }
  286. addSelection( mItems[index], index );
  287. }
  288. void GuiListBoxCtrl::addSelection( LBItem *item, S32 index )
  289. {
  290. if( !mMultipleSelections )
  291. {
  292. if( !mSelectedItems.empty() )
  293. {
  294. LBItem* selItem = mSelectedItems.front();
  295. if( selItem != item )
  296. clearSelection();
  297. else
  298. return;
  299. }
  300. }
  301. else
  302. {
  303. if( !mSelectedItems.empty() )
  304. {
  305. for( S32 i = 0; i < mSelectedItems.size(); i++ )
  306. {
  307. if( mSelectedItems[ i ] == item )
  308. return;
  309. }
  310. }
  311. }
  312. item->isSelected = true;
  313. mSelectedItems.push_front( item );
  314. onSelect_callback(index, item->itemText);
  315. }
  316. S32 GuiListBoxCtrl::getItemIndex( LBItem *item )
  317. {
  318. if( mItems.empty() )
  319. return -1;
  320. // Lookup the index of an item in our list, by the pointer to the item
  321. for( S32 i = 0; i < mItems.size(); i++ )
  322. if( mItems[i] == item )
  323. return i;
  324. return -1;
  325. }
  326. DefineEngineMethod( GuiListBoxCtrl, getItemCount, S32, (),,
  327. "@brief Returns the number of items in the list.\n\n"
  328. "@tsexample\n"
  329. "// Request the number of items in the list of the GuiListBoxCtrl object.\n"
  330. "%listItemCount = %thisGuiListBoxCtrl.getItemCount();\n"
  331. "@endtsexample\n\n"
  332. "@return The number of items in the list.\n\n"
  333. "@see GuiControl")
  334. {
  335. return object->getItemCount();
  336. }
  337. S32 GuiListBoxCtrl::getItemCount()
  338. {
  339. return mItems.size();
  340. }
  341. DefineEngineMethod( GuiListBoxCtrl, getSelCount, S32, (),,
  342. "@brief Returns the number of items currently selected.\n\n"
  343. "@tsexample\n"
  344. "// Request the number of currently selected items\n"
  345. "%selectedItemCount = %thisGuiListBoxCtrl.getSelCount();\n"
  346. "@endtsexample\n\n"
  347. "@return Number of currently selected items.\n\n"
  348. "@see GuiControl")
  349. {
  350. return object->getSelCount();
  351. }
  352. S32 GuiListBoxCtrl::getSelCount()
  353. {
  354. return mSelectedItems.size();
  355. }
  356. DefineEngineMethod( GuiListBoxCtrl, getSelectedItem, S32, (),,
  357. "@brief Returns the selected items index or -1 if none selected. If multiple selections exist it returns the first selected item. \n\n"
  358. "@tsexample\n"
  359. "// Request the index id of the currently selected item\n"
  360. "%selectedItemId = %thisGuiListBoxCtrl.getSelectedItem();\n"
  361. "@endtsexample\n\n"
  362. "@return The selected items index or -1 if none selected.\n\n"
  363. "@see GuiControl")
  364. {
  365. return object->getSelectedItem();
  366. }
  367. S32 GuiListBoxCtrl::getSelectedItem()
  368. {
  369. if( mSelectedItems.empty() || mItems.empty() )
  370. return -1;
  371. for( S32 i = 0 ; i < mItems.size(); i++ )
  372. if( mItems[i]->isSelected )
  373. return i;
  374. return -1;
  375. }
  376. DefineEngineMethod( GuiListBoxCtrl, getSelectedItems, const char*, (),,
  377. "@brief Returns a space delimited list of the selected items indexes in the list.\n\n"
  378. "@tsexample\n"
  379. "// Request a space delimited list of the items in the GuiListBoxCtrl object.\n"
  380. "%selectionList = %thisGuiListBoxCtrl.getSelectedItems();\n"
  381. "@endtsexample\n\n"
  382. "@return Space delimited list of the selected items indexes in the list\n\n"
  383. "@see GuiControl")
  384. {
  385. S32 selCount = object->getSelCount();
  386. if( selCount == -1 || selCount == 0 )
  387. return StringTable->lookup("-1");
  388. else if( selCount == 1 )
  389. return Con::getIntArg(object->getSelectedItem());
  390. Vector<S32> selItems;
  391. object->getSelectedItems( selItems );
  392. if( selItems.empty() )
  393. return StringTable->lookup("-1");
  394. static const U32 bufSize = selItems.size() * 4;
  395. UTF8 *retBuffer = Con::getReturnBuffer( bufSize );
  396. dMemset( retBuffer, 0, bufSize );
  397. Vector<S32>::iterator i = selItems.begin();
  398. for( ; i != selItems.end(); i++ )
  399. {
  400. UTF8 retFormat[12];
  401. dSprintf( retFormat, 12, "%d ", (*i) );
  402. dStrcat( retBuffer, retFormat, 12 );
  403. }
  404. return retBuffer;
  405. }
  406. void GuiListBoxCtrl::getSelectedItems( Vector<S32> &Items )
  407. {
  408. // Clear our return vector
  409. Items.clear();
  410. // If there are no selected items, return an empty vector
  411. if( mSelectedItems.empty() )
  412. return;
  413. for( S32 i = 0; i < mItems.size(); i++ )
  414. if( mItems[i]->isSelected )
  415. Items.push_back( i );
  416. }
  417. DefineEngineMethod( GuiListBoxCtrl, findItemText, S32, (const char* findText, bool bCaseSensitive), (false),
  418. "@brief Returns index of item with matching text or -1 if none found.\n\n"
  419. "@param findText Text in the list to find.\n"
  420. "@param isCaseSensitive If true, the search will be case sensitive.\n"
  421. "@tsexample\n"
  422. "// Define the text we wish to find in the list.\n"
  423. "%findText = \"Hickory Smoked Gideon\"/n/n"
  424. "// Define if this is a case sensitive search or not.\n"
  425. "%isCaseSensitive = \"false\";\n\n"
  426. "// Ask the GuiListBoxCtrl object what item id in the list matches the requested text.\n"
  427. "%matchingId = %thisGuiListBoxCtrl.findItemText(%findText,%isCaseSensitive);\n"
  428. "@endtsexample\n\n"
  429. "@return Index id of item with matching text or -1 if none found.\n\n"
  430. "@see GuiControl")
  431. {
  432. return object->findItemText( findText, bCaseSensitive );
  433. }
  434. S32 GuiListBoxCtrl::findItemText( StringTableEntry text, bool caseSensitive )
  435. {
  436. // Check Proper Arguments
  437. if( !text || !text[0] || text == StringTable->lookup("") )
  438. {
  439. Con::warnf("GuiListBoxCtrl::findItemText - No Text Specified!");
  440. return -1;
  441. }
  442. // Check Items Exist.
  443. if( mItems.empty() )
  444. return -1;
  445. // Lookup the index of an item in our list, by the pointer to the item
  446. for( S32 i = 0; i < mItems.size(); i++ )
  447. {
  448. // Case Sensitive Compare?
  449. if( caseSensitive && ( String::compare( mItems[i]->itemText, text ) == 0 ) )
  450. return i;
  451. else if (!caseSensitive && ( dStricmp( mItems[i]->itemText, text ) == 0 ))
  452. return i;
  453. }
  454. // Not Found!
  455. return -1;
  456. }
  457. DefineEngineMethod( GuiListBoxCtrl, setCurSel, void, (S32 indexId),,
  458. "@brief Sets the currently selected item at the specified index.\n\n"
  459. "@param indexId Index Id to set selected.\n"
  460. "@tsexample\n"
  461. "// Define the index id that we wish to select.\n"
  462. "%selectId = \"4\";\n\n"
  463. "// Inform the GuiListBoxCtrl object to set the requested index as selected.\n"
  464. "%thisGuiListBoxCtrl.setCurSel(%selectId);\n"
  465. "@endtsexample\n\n"
  466. "@see GuiControl")
  467. {
  468. object->setCurSel( indexId );
  469. }
  470. void GuiListBoxCtrl::setCurSel( S32 index )
  471. {
  472. // Range Check
  473. if( index >= mItems.size() )
  474. {
  475. Con::warnf("GuiListBoxCtrl::setCurSel - index out of range!" );
  476. return;
  477. }
  478. // If index -1 is specified, we clear the selection
  479. if( index == -1 )
  480. {
  481. mSelectedItems.clear();
  482. return;
  483. }
  484. // Add the selection
  485. addSelection( mItems[ index ], index );
  486. }
  487. DefineEngineMethod( GuiListBoxCtrl, setCurSelRange, void, (S32 indexStart, S32 indexStop), (999999),
  488. "@brief Sets the current selection range from index start to stop. If no stop is specified it sets from start index to the end of the list\n\n"
  489. "@param indexStart Index Id to start selection.\n"
  490. "@param indexStop Index Id to end selection.\n"
  491. "@tsexample\n"
  492. "// Set start id\n"
  493. "%indexStart = \"3\";\n\n"
  494. "// Set end id\n"
  495. "%indexEnd = \"6\";\n\n"
  496. "// Request the GuiListBoxCtrl object to select the defined range.\n"
  497. "%thisGuiListBoxCtrl.setCurSelRange(%indexStart,%indexEnd);\n"
  498. "@endtsexample\n\n"
  499. "@see GuiControl")
  500. {
  501. object->setCurSelRange( indexStart , indexStop );
  502. }
  503. void GuiListBoxCtrl::setCurSelRange( S32 start, S32 stop )
  504. {
  505. // Verify Selection Range
  506. if( start < 0 )
  507. start = 0;
  508. else if( start > mItems.size() )
  509. start = mItems.size();
  510. if( stop < 0 )
  511. stop = 0;
  512. else if( stop > mItems.size() )
  513. stop = mItems.size();
  514. S32 iterStart = ( start < stop ) ? start : stop;
  515. S32 iterStop = ( start < stop ) ? stop : start;
  516. for( ; iterStart <= iterStop; iterStart++ )
  517. addSelection( mItems[iterStart], iterStart );
  518. }
  519. DefineEngineMethod( GuiListBoxCtrl, addItem, S32, (const char* newItem, const char* color), ( "" ),
  520. "@brief Adds an item to the end of the list with an optional color.\n\n"
  521. "@param newItem New item to add to the list.\n"
  522. "@param color Optional color parameter to add to the new item.\n"
  523. "@tsexample\n"
  524. "// Define the item to add to the list.\n"
  525. "%newItem = \"Gideon's Blue Coat\";\n\n"
  526. "// Define the optional color for the new list item.\n"
  527. "%color = \"0.0 0.0 1.0\";\n\n"
  528. "// Inform the GuiListBoxCtrl object to add the item to the end of the list with the defined color.\n"
  529. "%thisGuiListBoxCtrl.addItem(%newItem,%color);\n"
  530. "@endtsexample\n\n"
  531. "@return If not void, return value and description\n\n"
  532. "@see GuiControl\n"
  533. "@hide")
  534. {
  535. if(dStricmp(color,"") == 0)
  536. {
  537. return object->addItem( newItem );
  538. }
  539. else
  540. {
  541. U32 elementCount = GuiListBoxCtrl::getStringElementCount(color);
  542. if(elementCount == 3)
  543. {
  544. F32 red, green, blue;
  545. red = dAtof(GuiListBoxCtrl::getStringElement( color, 0 ));
  546. green = dAtof(GuiListBoxCtrl::getStringElement( color, 1 ));
  547. blue = dAtof(GuiListBoxCtrl::getStringElement( color, 2 ));
  548. return object->addItemWithColor( newItem, LinearColorF(red, green, blue) );
  549. }
  550. else if(elementCount == 1)
  551. {
  552. U32 objId = dAtoi( color );
  553. return object->addItem( newItem, (void*)(uintptr_t)objId );
  554. }
  555. else
  556. {
  557. Con::warnf("GuiListBoxCtrl::addItem() - Invalid number of parameters for the color!");
  558. return -1;
  559. }
  560. }
  561. }
  562. static ConsoleDocFragment sGuiControlSetExtent1(
  563. "@brief Adds an item to the control with the specific text.\n\n"
  564. "@param text Text item to add to the list.\n"
  565. "GuiListBoxCtrl", // The class to place the method in; use NULL for functions.
  566. "void addItem( const char* text );" );
  567. S32 GuiListBoxCtrl::addItem( StringTableEntry text, void *itemData )
  568. {
  569. // This just calls insert item at the end of the list
  570. return insertItem( mItems.size(), text, itemData );
  571. }
  572. S32 GuiListBoxCtrl::addItemWithColor( StringTableEntry text, LinearColorF color, void *itemData )
  573. {
  574. // This just calls insert item at the end of the list
  575. return insertItemWithColor( mItems.size(), text, color, itemData );
  576. }
  577. DefineEngineMethod( GuiListBoxCtrl, setItemColor, void, (S32 index, LinearColorF color),,
  578. "@brief Sets the color of a single list entry at the specified index id.\n\n"
  579. "@param index Index id to modify the color of in the list.\n"
  580. "@param color Color value to set the list entry to.\n"
  581. "@tsexample\n"
  582. "// Define the index id value\n"
  583. "%index = \"5\";\n\n"
  584. "// Define the color value\n"
  585. "%color = \"1.0 0.0 0.0\";\n\n"
  586. "// Inform the GuiListBoxCtrl object to change the color of the requested index\n"
  587. "%thisGuiListBoxCtrl.setItemColor(%index,%color);\n"
  588. "@endtsexample\n\n"
  589. "@see GuiControl")
  590. {
  591. object->setItemColor( index, color );
  592. }
  593. void GuiListBoxCtrl::setItemColor(S32 index, const LinearColorF& color)
  594. {
  595. if ((index >= mItems.size()) || index < 0)
  596. {
  597. Con::warnf("GuiListBoxCtrl::setItemColor - invalid index");
  598. return;
  599. }
  600. LBItem* item = mItems[index];
  601. item->hasColor = true;
  602. item->color = color;
  603. }
  604. DefineEngineMethod( GuiListBoxCtrl, clearItemColor, void, (S32 index),,
  605. "@brief Removes any custom coloring from an item at the defined index id in the list.\n\n"
  606. "@param index Index id for the item to clear any custom color from.\n"
  607. "@tsexample\n"
  608. "// Define the index id\n"
  609. "%index = \"4\";\n\n"
  610. "// Request the GuiListBoxCtrl object to remove any custom coloring from the defined index entry\n"
  611. "%thisGuiListBoxCtrl.clearItemColor(%index);\n"
  612. "@endtsexample\n\n"
  613. "@see GuiControl")
  614. {
  615. object->clearItemColor(index);
  616. }
  617. void GuiListBoxCtrl::clearItemColor( S32 index )
  618. {
  619. if ((index >= mItems.size()) || index < 0)
  620. {
  621. Con::warnf("GuiListBoxCtrl::setItemColor - invalid index");
  622. return;
  623. }
  624. LBItem* item = mItems[index];
  625. item->hasColor = false;
  626. }
  627. DefineEngineMethod( GuiListBoxCtrl, insertItem, void, (const char* text, S32 index),,
  628. "@brief Inserts an item into the list at the specified index and returns the index assigned or -1 on error.\n\n"
  629. "@param text Text item to add.\n"
  630. "@param index Index id to insert the list item text at.\n"
  631. "@tsexample\n"
  632. "// Define the text to insert\n"
  633. "%text = \"Secret Agent Gideon\";\n\n"
  634. "// Define the index entry to insert the text at\n"
  635. "%index = \"14\";\n\n"
  636. "// In form the GuiListBoxCtrl object to insert the text at the defined index.\n"
  637. "%assignedId = %thisGuiListBoxCtrl.insertItem(%text,%index);\n"
  638. "@endtsexample\n\n"
  639. "@return If successful will return the index id assigned. If unsuccessful, will return -1.\n\n"
  640. "@see GuiControl")
  641. {
  642. object->insertItem( index, text );
  643. }
  644. S32 GuiListBoxCtrl::insertItem( S32 index, StringTableEntry text, void *itemData )
  645. {
  646. // If the index is greater than our list size, insert it at the end
  647. if( index >= mItems.size() )
  648. index = mItems.size();
  649. // Sanity checking
  650. if( !text )
  651. {
  652. Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL string" );
  653. return -1;
  654. }
  655. LBItem *newItem = new LBItem;
  656. // Assign item data
  657. newItem->itemText = StringTable->insert(text, true);
  658. newItem->itemData = itemData;
  659. newItem->isSelected = false;
  660. newItem->hasColor = false;
  661. // Add to list
  662. mItems.insert(index);
  663. mItems[index] = newItem;
  664. // Resize our list to fit our items
  665. updateSize();
  666. // Return our index in list (last)
  667. return index;
  668. }
  669. S32 GuiListBoxCtrl::insertItemWithColor( S32 index, StringTableEntry text, LinearColorF color, void *itemData )
  670. {
  671. // If the index is greater than our list size, insert it at the end
  672. if( index >= mItems.size() )
  673. index = mItems.size();
  674. // Sanity checking
  675. if( !text )
  676. {
  677. Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL string" );
  678. return -1;
  679. }
  680. if( color == LinearColorF(-1, -1, -1) )
  681. {
  682. Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL color" );
  683. return -1;
  684. }
  685. LBItem *newItem = new LBItem;
  686. // Assign item data
  687. newItem->itemText = StringTable->insert(text, true);
  688. newItem->itemData = itemData;
  689. newItem->isSelected = false;
  690. newItem->hasColor = true;
  691. newItem->color = color;
  692. // Add to list
  693. mItems.insert(index);
  694. mItems[index] = newItem;
  695. // Resize our list to fit our items
  696. updateSize();
  697. // Return our index in list (last)
  698. return index;
  699. }
  700. DefineEngineMethod( GuiListBoxCtrl, deleteItem, void, (S32 itemIndex),,
  701. "@brief Removes the list entry at the requested index id from the control and clears the memory associated with it.\n\n"
  702. "@param itemIndex Index id location to remove the item from.\n"
  703. "@tsexample\n"
  704. "// Define the index id we want to remove from the list\n"
  705. "%itemIndex = \"8\";\n\n"
  706. "// Inform the GuiListBoxCtrl object to remove the item at the defined index id.\n"
  707. "%thisGuiListBoxCtrl.deleteItem(%itemIndex);\n"
  708. "@endtsexample\n\n"
  709. "@see References")
  710. {
  711. object->deleteItem( itemIndex );
  712. }
  713. void GuiListBoxCtrl::deleteItem( S32 index )
  714. {
  715. // Range Check
  716. if( index >= mItems.size() || index < 0 )
  717. {
  718. Con::warnf("GuiListBoxCtrl::deleteItem - index out of range!" );
  719. return;
  720. }
  721. // Grab our item
  722. LBItem* item = mItems[ index ];
  723. if( !item )
  724. {
  725. Con::warnf("GuiListBoxCtrl::deleteItem - Bad Item Data!" );
  726. return;
  727. }
  728. // Remove it from the selected list.
  729. if( item->isSelected )
  730. {
  731. for( VectorPtr<LBItem*>::iterator i = mSelectedItems.begin(); i != mSelectedItems.end(); i++ )
  732. {
  733. if( item == *i )
  734. {
  735. mSelectedItems.erase_fast( i );
  736. break;
  737. }
  738. }
  739. }
  740. // Remove it from the list
  741. mItems.erase( &mItems[ index ] );
  742. // Free the memory associated with it
  743. delete item;
  744. }
  745. DefineEngineMethod( GuiListBoxCtrl, getItemText, const char*, (S32 index),,
  746. "@brief Returns the text of the item at the specified index.\n\n"
  747. "@param index Index id to return the item text from.\n"
  748. "@tsexample\n"
  749. "// Define the index id entry to request the text from\n"
  750. "%index = \"12\";\n\n"
  751. "// Request the item id text from the GuiListBoxCtrl object.\n"
  752. "%text = %thisGuiListBoxCtrl.getItemText(%index);\n"
  753. "@endtsexample\n\n"
  754. "@return The text of the requested index id.\n\n"
  755. "@see GuiControl")
  756. {
  757. return object->getItemText( index );
  758. }
  759. StringTableEntry GuiListBoxCtrl::getItemText( S32 index )
  760. {
  761. // Range Checking
  762. if( index > mItems.size() || index < 0 )
  763. {
  764. Con::warnf( "GuiListBoxCtrl::getItemText - index out of range!" );
  765. return StringTable->lookup("");
  766. }
  767. return mItems[ index ]->itemText;
  768. }
  769. DefineEngineMethod( GuiListBoxCtrl, getItemObject, const char*, (S32 index),,
  770. "@brief Returns the object associated with an item. This only makes sense if you are mirroring a simset.\n\n"
  771. "@param index Index id to request the associated item from.\n"
  772. "@tsexample\n"
  773. "// Define the index id\n"
  774. "%index = \"12\";\n\n"
  775. "// Request the item from the GuiListBoxCtrl object\n"
  776. "%object = %thisGuiListBoxCtrl.getItemObject(%index);\n"
  777. "@endtsexample\n\n"
  778. "@return The object associated with the item in the list.\n\n"
  779. "@see References")
  780. {
  781. SimObject *outObj = object->getItemObject( index );
  782. if ( !outObj )
  783. return NULL;
  784. return outObj->getIdString();
  785. }
  786. SimObject* GuiListBoxCtrl::getItemObject( S32 index )
  787. {
  788. // Range Checking
  789. if( index > mItems.size() || index < 0 )
  790. {
  791. Con::warnf( "GuiListBoxCtrl::getItemObject - index out of range!" );
  792. return NULL;
  793. }
  794. SimObject *outObj;
  795. Sim::findObject( (SimObjectId)(uintptr_t)(mItems[ index ]->itemData), outObj );
  796. return outObj;
  797. }
  798. DefineEngineMethod( GuiListBoxCtrl, setItemText, void, (S32 index, const char* newtext),,
  799. "@brief Sets the items text at the specified index.\n\n"
  800. "@param index Index id to set the item text at.\n"
  801. "@param newtext Text to change the list item at index id to.\n"
  802. "@tsexample\n"
  803. "// Define the index id/n"
  804. "%index = \"12\";\n\n"
  805. "// Define the text to set the list item to\n"
  806. "%newtext = \"Gideon's Fancy Goggles\";\n\n"
  807. "// Inform the GuiListBoxCtrl object to change the text at the requested index\n"
  808. "%thisGuiListBoxCtrl.setItemText(%index,%newText);\n"
  809. "@endtsexample\n\n"
  810. "@see GuiControl")
  811. {
  812. object->setItemText(index, newtext );
  813. }
  814. void GuiListBoxCtrl::setItemText( S32 index, StringTableEntry text )
  815. {
  816. // Sanity Checking
  817. if( !text )
  818. {
  819. Con::warnf("GuiListBoxCtrl::setItemText - Invalid Text Specified!" );
  820. return;
  821. }
  822. // Range Checking
  823. if( index > mItems.size() || index < 0 )
  824. {
  825. Con::warnf( "GuiListBoxCtrl::getItemText - index out of range!" );
  826. return;
  827. }
  828. mItems[ index ]->itemText = StringTable->insert( text, true );
  829. }
  830. DefineEngineMethod( GuiListBoxCtrl, setItemTooltip, void, (S32 index, const char* text),,
  831. "@brief Set the tooltip text to display for the given list item.\n\n"
  832. "@param index Index id to change the tooltip text\n"
  833. "@param text Text for the tooltip.\n"
  834. "@tsexample\n"
  835. "// Define the index id\n"
  836. "%index = \"12\";\n\n"
  837. "// Define the tooltip text\n"
  838. "%tooltip = \"Gideon's goggles can see through space and time.\"\n\n"
  839. "// Inform the GuiListBoxCtrl object to set the tooltop for the item at the defined index id\n"
  840. "%thisGuiListBoxCtrl.setItemToolTip(%index,%tooltip);\n"
  841. "@endtsexample\n\n"
  842. "@see GuiControl")
  843. {
  844. if( index > object->mItems.size() || index < 0 )
  845. {
  846. Con::errorf( "GuiListBoxCtrl::setItemTooltip - index '%i' out of range", index );
  847. return;
  848. }
  849. object->mItems[ index ]->itemTooltip = text;
  850. }
  851. DefineEngineMethod( GuiListBoxCtrl, getLastClickItem, S32, (),,
  852. "@brief Request the item index for the item that was last clicked.\n\n"
  853. "@tsexample\n"
  854. "// Request the item index for the last clicked item in the list\n"
  855. "%lastClickedIndex = %thisGuiListBoxCtrl.getLastClickItem();\n"
  856. "@endtsexample\n\n"
  857. "@return Index id for the last clicked item in the list.\n\n"
  858. "@see GuiControl")
  859. {
  860. GuiListBoxCtrl::LBItem *lastItem = object->mLastClickItem;
  861. if ( !lastItem )
  862. return -1;
  863. return object->getItemIndex( lastItem );
  864. }
  865. //-----------------------------------------------------------------------------
  866. // Sizing Functions
  867. //-----------------------------------------------------------------------------
  868. void GuiListBoxCtrl::updateSize()
  869. {
  870. if( !mProfile || !mProfile->mFont )
  871. return;
  872. GFont *font = mProfile->mFont;
  873. GuiScrollCtrl* parent = dynamic_cast<GuiScrollCtrl *>(getParent());
  874. if ( mFitParentWidth && parent )
  875. mItemSize.x = parent->getContentExtent().x;
  876. else
  877. {
  878. // Find the maximum width cell:
  879. S32 maxWidth = 1;
  880. for ( U32 i = 0; i < mItems.size(); i++ )
  881. {
  882. S32 width = font->getStrWidth( mItems[i]->itemText );
  883. if( width > maxWidth )
  884. maxWidth = width;
  885. }
  886. mItemSize.x = maxWidth + 6;
  887. }
  888. mItemSize.y = font->getHeight() + 2;
  889. Point2I newExtent( mItemSize.x, mItemSize.y * mItems.size() );
  890. setExtent( newExtent );
  891. }
  892. void GuiListBoxCtrl::parentResized(const RectI &oldParentRect, const RectI &newParentRect)
  893. {
  894. Parent::parentResized( oldParentRect, newParentRect );
  895. updateSize();
  896. }
  897. //-----------------------------------------------------------------------------
  898. // Overrides
  899. //-----------------------------------------------------------------------------
  900. void GuiListBoxCtrl::onRender( Point2I offset, const RectI &updateRect )
  901. {
  902. RectI clipRect(updateRect.point, updateRect.extent);
  903. if( !mProfile )
  904. return;
  905. _mirror();
  906. // Save our original clip rect
  907. RectI oldClipRect = clipRect;
  908. for ( S32 i = 0; i < mItems.size(); i++)
  909. {
  910. S32 colorBoxSize = 0;
  911. ColorI boxColor = ColorI(0, 0, 0);
  912. // Only render visible items
  913. if ((i + 1) * mItemSize.y + offset.y < updateRect.point.y)
  914. continue;
  915. // Break our once we're no longer in visible item range
  916. if( i * mItemSize.y + offset.y >= updateRect.point.y + updateRect.extent.y)
  917. break;
  918. // Render color box if needed
  919. if(mColorBullet && mItems[i]->hasColor)
  920. {
  921. // Set the size of the color box to be drawn next to the item text
  922. colorBoxSize = 3;
  923. boxColor = ColorI(mItems[i]->color.toColorI());
  924. // Draw the box first
  925. ColorI black = ColorI(0, 0, 0);
  926. drawBox( Point2I(offset.x + mProfile->mTextOffset.x + colorBoxSize, offset.y + ( i * mItemSize.y ) + 8), colorBoxSize, black, boxColor );
  927. }
  928. RectI itemRect = RectI( offset.x + mProfile->mTextOffset.x + (colorBoxSize * 3), offset.y + ( i * mItemSize.y ), mItemSize.x, mItemSize.y );
  929. // Render our item
  930. onRenderItem( itemRect, mItems[i] );
  931. }
  932. GFX->setClipRect( oldClipRect );
  933. }
  934. void GuiListBoxCtrl::onRenderItem(const RectI& itemRect, LBItem *item)
  935. {
  936. if( item->isSelected )
  937. GFX->getDrawUtil()->drawRectFill( itemRect, mProfile->mFillColorSEL );
  938. GFX->getDrawUtil()->setBitmapModulation( item->hasColor ? item->color.toColorI() : mProfile->mFontColor);
  939. renderJustifiedText(itemRect.point + Point2I( 2, 0 ), itemRect.extent, item->itemText);
  940. }
  941. void GuiListBoxCtrl::drawBox(const Point2I &box, S32 size, ColorI &outlineColor, ColorI &boxColor)
  942. {
  943. RectI r(box.x - size, box.y - size, 2 * size + 1, 2 * size + 1);
  944. r.inset(1, 1);
  945. GFX->getDrawUtil()->drawRectFill(r, boxColor);
  946. r.inset(-1, -1);
  947. GFX->getDrawUtil()->drawRect(r, outlineColor);
  948. }
  949. bool GuiListBoxCtrl::renderTooltip( const Point2I &hoverPos, const Point2I& cursorPos, const char* tipText )
  950. {
  951. S32 hitItemIndex;
  952. if( hitTest( hoverPos, hitItemIndex ) )
  953. tipText = mItems[ hitItemIndex ]->itemTooltip;
  954. return defaultTooltipRender( hoverPos, cursorPos, tipText );
  955. }
  956. //-----------------------------------------------------------------------------
  957. // Hit Detection
  958. //-----------------------------------------------------------------------------
  959. bool GuiListBoxCtrl::hitTest( const Point2I& point, S32& outItem )
  960. {
  961. Point2I localPoint = globalToLocalCoord( point );
  962. S32 itemHit = ( localPoint.y < 0 ) ? -1 : (S32)mFloor( (F32)localPoint.y / (F32)mItemSize.y );
  963. if ( itemHit >= mItems.size() || itemHit == -1 )
  964. return false;
  965. LBItem *hitItem = mItems[ itemHit ];
  966. if ( hitItem == NULL )
  967. return false;
  968. outItem = itemHit;
  969. return true;
  970. }
  971. //-----------------------------------------------------------------------------
  972. // Mouse Events
  973. //-----------------------------------------------------------------------------
  974. void GuiListBoxCtrl::onMouseDragged(const GuiEvent &event)
  975. {
  976. Parent::onMouseDragged(event);
  977. onMouseDragged_callback();
  978. }
  979. void GuiListBoxCtrl::onMouseDown( const GuiEvent &event )
  980. {
  981. S32 itemHit;
  982. if( !hitTest( event.mousePoint, itemHit ) )
  983. return;
  984. LBItem* hitItem = mItems[ itemHit ];
  985. // If we're not a multiple selection listbox, we simply select/unselect an item
  986. if( !mMultipleSelections )
  987. {
  988. // No current selection? Just select the cell and move on
  989. S32 selItem = getSelectedItem();
  990. if ( selItem != itemHit && selItem != -1 )
  991. clearSelection();
  992. // Set the current selection
  993. setCurSel( itemHit );
  994. if( itemHit == selItem && event.mouseClickCount == 2 )
  995. onDoubleClick_callback();
  996. // Store the clicked item
  997. mLastClickItem = hitItem;
  998. // Evaluate the console command if we clicked the same item twice
  999. if( selItem == itemHit && event.mouseClickCount > 1 )
  1000. execAltConsoleCallback();
  1001. return;
  1002. }
  1003. // Deal with multiple selections
  1004. if( event.modifier & SI_MULTISELECT)
  1005. {
  1006. // Ctrl-Click toggles selection
  1007. if( hitItem->isSelected )
  1008. {
  1009. removeSelection( hitItem, itemHit );
  1010. // We return here when we deselect an item because we don't store last clicked when we deselect
  1011. return;
  1012. }
  1013. else
  1014. addSelection( hitItem, itemHit );
  1015. }
  1016. else if( event.modifier & SI_RANGESELECT )
  1017. {
  1018. if( !mLastClickItem )
  1019. addSelection( hitItem, itemHit );
  1020. else
  1021. setCurSelRange( getItemIndex( mLastClickItem ), itemHit );
  1022. }
  1023. else
  1024. {
  1025. if( getSelCount() != 0 )
  1026. {
  1027. S32 selItem = getSelectedItem();
  1028. if( selItem != -1 && mItems[selItem] != hitItem )
  1029. clearSelection();
  1030. }
  1031. addSelection( hitItem, itemHit );
  1032. }
  1033. if( hitItem == mLastClickItem && event.mouseClickCount == 2 )
  1034. onDoubleClick_callback();
  1035. mLastClickItem = hitItem;
  1036. }
  1037. void GuiListBoxCtrl::onMouseUp( const GuiEvent& event )
  1038. {
  1039. S32 itemHit = -1;
  1040. if( hitTest( event.mousePoint, itemHit ) )
  1041. onMouseUp_callback( itemHit, event.mouseClickCount );
  1042. // Execute console command
  1043. execConsoleCallback();
  1044. Parent::onMouseUp( event );
  1045. }
  1046. bool GuiListBoxCtrl::onKeyDown( const GuiEvent &event )
  1047. {
  1048. if ( event.keyCode == KEY_DELETE )
  1049. {
  1050. onDeleteKey_callback();
  1051. return true;
  1052. }
  1053. return Parent::onKeyDown( event );
  1054. }
  1055. U32 GuiListBoxCtrl::getStringElementCount( const char* inString )
  1056. {
  1057. // Non-whitespace chars.
  1058. static const char* set = " \t\n";
  1059. // End of string.
  1060. if ( *inString == 0 )
  1061. return 0;
  1062. U32 wordCount = 0;
  1063. U8 search = 0;
  1064. // Search String.
  1065. while( *inString )
  1066. {
  1067. // Get string element.
  1068. search = *inString;
  1069. // End of string?
  1070. if ( search == 0 )
  1071. break;
  1072. // Move to next element.
  1073. inString++;
  1074. // Search for seperators.
  1075. for( U32 i = 0; set[i]; i++ )
  1076. {
  1077. // Found one?
  1078. if( search == set[i] )
  1079. {
  1080. // Yes...
  1081. search = 0;
  1082. break;
  1083. }
  1084. }
  1085. // Found a seperator?
  1086. if ( search == 0 )
  1087. continue;
  1088. // We've found a non-seperator.
  1089. wordCount++;
  1090. // Search for end of non-seperator.
  1091. while( 1 )
  1092. {
  1093. // Get string element.
  1094. search = *inString;
  1095. // End of string?
  1096. if ( search == 0 )
  1097. break;
  1098. // Move to next element.
  1099. inString++;
  1100. // Search for seperators.
  1101. for( U32 i = 0; set[i]; i++ )
  1102. {
  1103. // Found one?
  1104. if( search == set[i] )
  1105. {
  1106. // Yes...
  1107. search = 0;
  1108. break;
  1109. }
  1110. }
  1111. // Found Seperator?
  1112. if ( search == 0 )
  1113. break;
  1114. }
  1115. // End of string?
  1116. if ( *inString == 0 )
  1117. {
  1118. // Bah!
  1119. break;
  1120. }
  1121. }
  1122. // We've finished.
  1123. return wordCount;
  1124. }
  1125. //------------------------------------------------------------------------------
  1126. // Get String Element.
  1127. //------------------------------------------------------------------------------
  1128. const char* GuiListBoxCtrl::getStringElement( const char* inString, const U32 index )
  1129. {
  1130. // Non-whitespace chars.
  1131. static const char* set = " \t\n";
  1132. U32 wordCount = 0;
  1133. U8 search = 0;
  1134. const char* pWordStart = NULL;
  1135. // End of string?
  1136. if ( *inString != 0 )
  1137. {
  1138. // No, so search string.
  1139. while( *inString )
  1140. {
  1141. // Get string element.
  1142. search = *inString;
  1143. // End of string?
  1144. if ( search == 0 )
  1145. break;
  1146. // Move to next element.
  1147. inString++;
  1148. // Search for seperators.
  1149. for( U32 i = 0; set[i]; i++ )
  1150. {
  1151. // Found one?
  1152. if( search == set[i] )
  1153. {
  1154. // Yes...
  1155. search = 0;
  1156. break;
  1157. }
  1158. }
  1159. // Found a seperator?
  1160. if ( search == 0 )
  1161. continue;
  1162. // Found are word?
  1163. if ( wordCount == index )
  1164. {
  1165. // Yes, so mark it.
  1166. pWordStart = inString-1;
  1167. }
  1168. // We've found a non-seperator.
  1169. wordCount++;
  1170. // Search for end of non-seperator.
  1171. while( 1 )
  1172. {
  1173. // Get string element.
  1174. search = *inString;
  1175. // End of string?
  1176. if ( search == 0 )
  1177. break;
  1178. // Move to next element.
  1179. inString++;
  1180. // Search for seperators.
  1181. for( U32 i = 0; set[i]; i++ )
  1182. {
  1183. // Found one?
  1184. if( search == set[i] )
  1185. {
  1186. // Yes...
  1187. search = 0;
  1188. break;
  1189. }
  1190. }
  1191. // Found Seperator?
  1192. if ( search == 0 )
  1193. break;
  1194. }
  1195. // Have we found our word?
  1196. if ( pWordStart )
  1197. {
  1198. // Yes, so we've got our word...
  1199. // Result Buffer.
  1200. static char buffer[4096];
  1201. // Calculate word length.
  1202. const U32 length = inString - pWordStart - ((*inString)?1:0);
  1203. // Copy Word.
  1204. dStrncpy( buffer, pWordStart, length);
  1205. buffer[length] = '\0';
  1206. // Return Word.
  1207. return buffer;
  1208. }
  1209. // End of string?
  1210. if ( *inString == 0 )
  1211. {
  1212. // Bah!
  1213. break;
  1214. }
  1215. }
  1216. }
  1217. // Sanity!
  1218. AssertFatal( false, "t2dSceneObject::getStringElement() - Couldn't find specified string element!" );
  1219. // Didn't find it
  1220. return " ";
  1221. }
  1222. void GuiListBoxCtrl::_mirror()
  1223. {
  1224. SimSet *mirrorSet;
  1225. if ( !Sim::findObject( mMirrorSetName, mirrorSet ) )
  1226. return;
  1227. // Allow script to filter out objects if desired.
  1228. Vector<SimObjectId> workingSet;
  1229. // If the method is not defined we assume user wants us to add
  1230. // all objects.
  1231. bool isObjMirroredDefined = isMethod( "isObjectMirrored" );
  1232. for ( S32 i = 0; i < mirrorSet->size(); i++ )
  1233. {
  1234. bool addObj = true;
  1235. if ( isObjMirroredDefined )
  1236. addObj = isObjectMirrored_callback(mirrorSet->at(i)->getIdString());
  1237. if ( addObj )
  1238. workingSet.push_back( mirrorSet->at(i)->getId() );
  1239. }
  1240. // Remove existing items that are no longer in the SimSet.
  1241. // Or are no longer valid objects.
  1242. SimObjectId curId;
  1243. SimObject *curObj;
  1244. for ( U32 i = 0; i < mItems.size(); i++ )
  1245. {
  1246. curId = (SimObjectId)(uintptr_t)mItems[i]->itemData;
  1247. Sim::findObject( curId, curObj );
  1248. bool keep = false;
  1249. if ( curObj )
  1250. {
  1251. if ( workingSet.contains( curId ) )
  1252. {
  1253. mItems[i]->itemText = _makeMirrorItemName( curObj );
  1254. keep = true;
  1255. }
  1256. }
  1257. if ( !keep )
  1258. {
  1259. deleteItem( i );
  1260. i--;
  1261. }
  1262. }
  1263. // Add items that are in the SimSet but not yet in the list.
  1264. for ( U32 i = 0; i < workingSet.size(); i++ )
  1265. {
  1266. curId = workingSet[i];
  1267. Sim::findObject( curId, curObj );
  1268. bool found = false;
  1269. for ( U32 j = 0; j < mItems.size(); j++ )
  1270. {
  1271. if ( (SimObjectId)(uintptr_t)(mItems[j]->itemData) == curId )
  1272. {
  1273. found = true;
  1274. break;
  1275. }
  1276. }
  1277. for ( U32 j = 0; j < mFilteredItems.size(); j++ )
  1278. {
  1279. if ( (SimObjectId)(uintptr_t)(mFilteredItems[j]->itemData) == curId )
  1280. {
  1281. found = true;
  1282. break;
  1283. }
  1284. }
  1285. if ( !found )
  1286. {
  1287. addItem( _makeMirrorItemName( curObj ), (void*)(uintptr_t)curId );
  1288. }
  1289. }
  1290. }
  1291. StringTableEntry GuiListBoxCtrl::_makeMirrorItemName( SimObject *inObj )
  1292. {
  1293. StringTableEntry outName = StringTable->EmptyString();
  1294. if ( mMakeNameCallback.isNotEmpty() )
  1295. {
  1296. Con::setIntVariable( "$ThisControl", getId() );
  1297. Con::setIntVariable( "$ThisObject", inObj->getId() );
  1298. outName = StringTable->insert( Con::evaluate( mMakeNameCallback ), true );
  1299. }
  1300. else if ( inObj->getName() )
  1301. outName = StringTable->insert( inObj->getName() );
  1302. if ( !outName || !outName[0] )
  1303. outName = StringTable->insert( "(no name)" );
  1304. return outName;
  1305. }
  1306. DefineEngineMethod( GuiListBoxCtrl, doMirror, void, (),,
  1307. "@brief Informs the GuiListBoxCtrl object to mirror the contents of the GuiListBoxCtrl stored in the mirrorSet field.\n\n"
  1308. "@tsexample\n"
  1309. "\\ Inform the object to mirror the object located at %thisGuiListBox.mirrorSet\n"
  1310. "%thisGuiListBox.doMirror();\n"
  1311. "@endtsexample\n\n"
  1312. "@see GuiCore")
  1313. {
  1314. object->_mirror();
  1315. }
  1316. DefineEngineMethod( GuiListBoxCtrl, addFilteredItem, void, (const char* newItem),,
  1317. "@brief Checks if there is an item with the exact text of what is passed in, and if so\n"
  1318. "the item is removed from the list and adds that item's data to the filtered list.\n\n"
  1319. "@param itemName Name of the item that we wish to add to the filtered item list of the GuiListBoxCtrl.\n"
  1320. "@tsexample\n"
  1321. "// Define the itemName that we wish to add to the filtered item list.\n"
  1322. "%itemName = \"This Item Name\";\n\n"
  1323. "// Add the item name to the filtered item list.\n"
  1324. "%thisGuiListBoxCtrl.addFilteredItem(%filteredItemName);\n"
  1325. "@endtsexample\n\n"
  1326. "@see GuiControl")
  1327. {
  1328. String item(newItem);
  1329. if( item == String::EmptyString )
  1330. return;
  1331. object->addFilteredItem( item );
  1332. }
  1333. void GuiListBoxCtrl::addFilteredItem( String item )
  1334. {
  1335. // Delete from selected items list
  1336. for ( S32 i = 0; i < mSelectedItems.size(); i++ )
  1337. {
  1338. String itemText = mSelectedItems[i]->itemText;
  1339. if ( String::compare( itemText.c_str(), item.c_str() ) == 0 )
  1340. {
  1341. mSelectedItems.erase_fast( i );
  1342. break;
  1343. }
  1344. }
  1345. for ( S32 i = 0; i < mItems.size(); i++ )
  1346. {
  1347. String itemText = mItems[i]->itemText;
  1348. if( String::compare( itemText.c_str(), item.c_str() ) == 0 )
  1349. {
  1350. mItems[i]->isSelected = false;
  1351. mFilteredItems.push_front( mItems[i] );
  1352. mItems.erase( &mItems[i] );
  1353. break;
  1354. }
  1355. }
  1356. }
  1357. DefineEngineMethod( GuiListBoxCtrl, removeFilteredItem, void, ( const char* itemName ),,
  1358. "@brief Removes an item of the entered name from the filtered items list.\n\n"
  1359. "@param itemName Name of the item to remove from the filtered list.\n"
  1360. "@tsexample\n"
  1361. "// Define the itemName that you wish to remove.\n"
  1362. "%itemName = \"This Item Name\";\n\n"
  1363. "// Remove the itemName from the GuiListBoxCtrl\n"
  1364. "%thisGuiListBoxCtrl.removeFilteredItem(%itemName);\n"
  1365. "@endtsexample\n\n"
  1366. "@see GuiControl")
  1367. {
  1368. String item(itemName);
  1369. if( item == String::EmptyString )
  1370. return;
  1371. object->removeFilteredItem( item );
  1372. }
  1373. void GuiListBoxCtrl::removeFilteredItem( String item )
  1374. {
  1375. for ( S32 i = 0; i < mFilteredItems.size(); i++ )
  1376. {
  1377. String itemText = mFilteredItems[i]->itemText;
  1378. if( String::compare( itemText.c_str(), item.c_str() ) == 0 )
  1379. {
  1380. mItems.push_front( mFilteredItems[i] );
  1381. mFilteredItems.erase( &mFilteredItems[i] );
  1382. break;
  1383. }
  1384. }
  1385. }