guiListBoxCtrl.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/platform.h"
  23. #include "gui/guiListBoxCtrl.h"
  24. #include "gui/guiCanvas.h"
  25. #include <algorithm>
  26. #include "guiListBoxCtrl_ScriptBinding.h"
  27. IMPLEMENT_CONOBJECT(GuiListBoxCtrl);
  28. GuiListBoxCtrl::GuiListBoxCtrl()
  29. {
  30. mItems.clear();
  31. mSelectedItems.clear();
  32. mMultipleSelections = true;
  33. mFitParentWidth = true;
  34. mItemSize = Point2I(10,20);
  35. mLastClickItem = NULL;
  36. mIsContainer = false;
  37. mActive = true;
  38. caller = this;
  39. setField("profile", "GuiListBoxProfile");
  40. }
  41. GuiListBoxCtrl::~GuiListBoxCtrl()
  42. {
  43. clearItems();
  44. }
  45. void GuiListBoxCtrl::initPersistFields()
  46. {
  47. Parent::initPersistFields();
  48. addField( "AllowMultipleSelections", TypeBool, Offset( mMultipleSelections, GuiListBoxCtrl) );
  49. addField( "FitParentWidth", TypeBool, Offset( mFitParentWidth, GuiListBoxCtrl) );
  50. }
  51. bool GuiListBoxCtrl::onWake()
  52. {
  53. if( !Parent::onWake() )
  54. return false;
  55. updateSize();
  56. return true;
  57. }
  58. void GuiListBoxCtrl::addObject(SimObject *obj)
  59. {
  60. AssertWarn(0, "GuiListBoxCtrl::addObject: cannot add children to the GuiListBoxCtrl. It is not a container. Child was not added.");
  61. removeObject(obj);
  62. }
  63. #pragma region ItemAccessors
  64. void GuiListBoxCtrl::clearItems()
  65. {
  66. // Free item list allocated memory
  67. while( mItems.size() )
  68. deleteItem( 0 );
  69. // Free our vector lists
  70. mItems.clear();
  71. mSelectedItems.clear();
  72. }
  73. void GuiListBoxCtrl::clearSelection()
  74. {
  75. if( !mSelectedItems.size() )
  76. return;
  77. VectorPtr<LBItem*>::iterator i = mSelectedItems.begin();
  78. for( ; i != mSelectedItems.end(); i++ )
  79. (*i)->isSelected = false;
  80. mSelectedItems.clear();
  81. }
  82. void GuiListBoxCtrl::removeSelection( S32 index )
  83. {
  84. // Range Check
  85. if( index >= mItems.size() || index < 0 )
  86. {
  87. Con::warnf("GuiListBoxCtrl::removeSelection - index out of range!" );
  88. return;
  89. }
  90. removeSelection( mItems[index], index );
  91. }
  92. void GuiListBoxCtrl::removeSelection( LBItem *item, S32 index )
  93. {
  94. if( !mSelectedItems.size() )
  95. return;
  96. if( !item )
  97. return;
  98. for( S32 i = 0 ; i < mSelectedItems.size(); i++ )
  99. {
  100. if( mSelectedItems[i] == item )
  101. {
  102. mSelectedItems.erase( &mSelectedItems[i] );
  103. item->isSelected = false;
  104. Con::executef(caller, 3, "onUnSelect", Con::getIntArg( index ), item->itemText, item->ID);
  105. return;
  106. }
  107. }
  108. }
  109. void GuiListBoxCtrl::addSelection( S32 index )
  110. {
  111. // Range Check
  112. if( index >= mItems.size() || index < 0 )
  113. {
  114. Con::warnf("GuiListBoxCtrl::addSelection- index out of range!" );
  115. return;
  116. }
  117. addSelection( mItems[index], index );
  118. }
  119. void GuiListBoxCtrl::addSelection( LBItem *item, S32 index )
  120. {
  121. if( !mMultipleSelections )
  122. {
  123. if( !mSelectedItems.empty() )
  124. {
  125. LBItem* selItem = mSelectedItems.front();
  126. if( selItem != item )
  127. clearSelection();
  128. else
  129. return;
  130. }
  131. }
  132. else
  133. {
  134. if( !mSelectedItems.empty() )
  135. {
  136. for( S32 i = 0; i < mSelectedItems.size(); i++ )
  137. {
  138. if( mSelectedItems[ i ] == item )
  139. return;
  140. }
  141. }
  142. }
  143. item->isSelected = true;
  144. mSelectedItems.push_front( item );
  145. ScrollToIndex(index);
  146. if(caller->isMethod("onSelect"))
  147. Con::executef(caller, 3, "onSelect", Con::getIntArg( index ), item->itemText, item->ID);
  148. }
  149. S32 GuiListBoxCtrl::getItemIndex( LBItem *item )
  150. {
  151. if( mItems.empty() )
  152. return -1;
  153. // Lookup the index of an item in our list, by the pointer to the item
  154. for( S32 i = 0; i < mItems.size(); i++ )
  155. if( mItems[i] == item )
  156. return i;
  157. return -1;
  158. }
  159. S32 GuiListBoxCtrl::getItemCount()
  160. {
  161. return mItems.size();
  162. }
  163. S32 GuiListBoxCtrl::getSelCount()
  164. {
  165. return mSelectedItems.size();
  166. }
  167. S32 GuiListBoxCtrl::getSelectedItem()
  168. {
  169. if( mSelectedItems.empty() || mItems.empty() )
  170. return -1;
  171. for( S32 i = 0 ; i < mItems.size(); i++ )
  172. if( mItems[i]->isSelected )
  173. return i;
  174. return -1;
  175. }
  176. void GuiListBoxCtrl::getSelectedItems( Vector<S32> &Items )
  177. {
  178. // Clear our return vector
  179. Items.clear();
  180. // If there are no selected items, return an empty vector
  181. if( mSelectedItems.empty() )
  182. return;
  183. for( S32 i = 0; i < mItems.size(); i++ )
  184. if( mItems[i]->isSelected )
  185. Items.push_back( i );
  186. }
  187. S32 GuiListBoxCtrl::findItemText( StringTableEntry text, bool caseSensitive )
  188. {
  189. // Check Proper Arguments
  190. if( !text || !text[0] || text == StringTable->EmptyString )
  191. {
  192. Con::warnf("GuiListBoxCtrl::findItemText - No Text Specified!");
  193. return -1;
  194. }
  195. // Check Items Exist.
  196. if( mItems.empty() )
  197. return -1;
  198. // Lookup the index of an item in our list, by the pointer to the item
  199. for( S32 i = 0; i < mItems.size(); i++ )
  200. {
  201. // Case Sensitive Compare?
  202. if( caseSensitive && ( dStrcmp( mItems[i]->itemText, text ) == 0 ) )
  203. return i;
  204. else if (!caseSensitive && ( dStricmp( mItems[i]->itemText, text ) == 0 ))
  205. return i;
  206. }
  207. // Not Found!
  208. return -1;
  209. }
  210. void GuiListBoxCtrl::setSelectionInternal(StringTableEntry text)
  211. {
  212. S32 index = findItemText(text);
  213. if (index != -1)
  214. {
  215. mSelectedItems.clear();
  216. LBItem *item = mItems[index];
  217. item->isSelected = true;
  218. mSelectedItems.push_front(item);
  219. }
  220. }
  221. void GuiListBoxCtrl::setCurSel( S32 index )
  222. {
  223. // Range Check
  224. if( index >= mItems.size() )
  225. {
  226. Con::warnf("GuiListBoxCtrl::setCurSel - index out of range!" );
  227. return;
  228. }
  229. // If index -1 is specified, we clear the selection
  230. if( index == -1 )
  231. {
  232. mSelectedItems.clear();
  233. return;
  234. }
  235. // Add the selection
  236. addSelection( mItems[ index ], index );
  237. }
  238. void GuiListBoxCtrl::setCurSelRange( S32 start, S32 stop )
  239. {
  240. // Verify Selection Range
  241. if( start < 0 )
  242. start = 0;
  243. else if( start > mItems.size() )
  244. start = mItems.size();
  245. if( stop < 0 )
  246. stop = 0;
  247. else if( stop > mItems.size() )
  248. stop = mItems.size();
  249. S32 iterStart = ( start < stop ) ? start : stop;
  250. S32 iterStop = ( start < stop ) ? stop : start;
  251. for( ; iterStart <= iterStop; iterStart++ )
  252. addSelection( mItems[iterStart], iterStart );
  253. }
  254. S32 GuiListBoxCtrl::addItem( StringTableEntry text, void *itemData )
  255. {
  256. // This just calls insert item at the end of the list
  257. return insertItem( mItems.size(), text, itemData );
  258. }
  259. S32 GuiListBoxCtrl::addItemWithColor( StringTableEntry text, ColorF color, void *itemData )
  260. {
  261. // This just calls insert item at the end of the list
  262. return insertItemWithColor( mItems.size(), text, color, itemData );
  263. }
  264. S32 GuiListBoxCtrl::addItemWithID(StringTableEntry text, S32 ID, void *itemData)
  265. {
  266. S32 index = insertItem( mItems.size(), text, itemData);
  267. setItemID(index, ID);
  268. return index;
  269. }
  270. void GuiListBoxCtrl::setItemColor( S32 index, ColorF color )
  271. {
  272. if ((index >= mItems.size()) || index < 0)
  273. {
  274. Con::warnf("GuiListBoxCtrl::setItemColor - invalid index");
  275. return;
  276. }
  277. LBItem* item = mItems[index];
  278. item->hasColor = true;
  279. item->color = color;
  280. }
  281. void GuiListBoxCtrl::clearItemColor(S32 index)
  282. {
  283. if ((index >= mItems.size()) || index < 0)
  284. {
  285. Con::warnf("GuiListBoxCtrl::setItemColor - invalid index");
  286. return;
  287. }
  288. LBItem* item = mItems[index];
  289. item->hasColor = false;
  290. }
  291. void GuiListBoxCtrl::clearAllColors()
  292. {
  293. if (!mSelectedItems.size())
  294. return;
  295. VectorPtr<LBItem*>::iterator i = mSelectedItems.begin();
  296. for (; i != mSelectedItems.end(); i++)
  297. (*i)->hasColor = false;
  298. }
  299. ColorF GuiListBoxCtrl::getItemColor(S32 index)
  300. {
  301. if ((index >= mItems.size()) || index < 0)
  302. {
  303. Con::warnf("GuiListBoxCtrl::getItemColor - invalid index");
  304. return ColorF(0,0,0,0);
  305. }
  306. LBItem* item = mItems[index];
  307. return item->color;
  308. }
  309. bool GuiListBoxCtrl::getItemHasColor(S32 index)
  310. {
  311. if ((index >= mItems.size()) || index < 0)
  312. {
  313. Con::warnf("GuiListBoxCtrl::getItemHasColor - invalid index");
  314. return false;
  315. }
  316. LBItem* item = mItems[index];
  317. return item->hasColor;
  318. }
  319. void GuiListBoxCtrl::setItemID(S32 index, S32 ID)
  320. {
  321. if ((index >= mItems.size()) || index < 0)
  322. {
  323. Con::warnf("GuiListBoxCtrl::setItemID - invalid index");
  324. return;
  325. }
  326. LBItem* item = mItems[index];
  327. item->ID = ID;
  328. }
  329. S32 GuiListBoxCtrl::getItemID(S32 index)
  330. {
  331. if ((index >= mItems.size()) || index < 0)
  332. {
  333. Con::warnf("GuiListBoxCtrl::setItemID - invalid index");
  334. return 0;
  335. }
  336. LBItem* item = mItems[index];
  337. return item->ID;
  338. }
  339. S32 GuiListBoxCtrl::findItemID(S32 ID)
  340. {
  341. // Check Items Exist.
  342. if (mItems.empty())
  343. return -1;
  344. // Lookup the index of an item in our list, by the pointer to the item
  345. for (S32 i = 0; i < mItems.size(); i++)
  346. {
  347. if (mItems[i]->ID == ID)
  348. {
  349. return i;
  350. }
  351. }
  352. // Not Found!
  353. return -1;
  354. }
  355. void GuiListBoxCtrl::setItemActive(S32 index)
  356. {
  357. if ((index >= mItems.size()) || index < 0)
  358. {
  359. Con::warnf("GuiListBoxCtrl::setItemActive - invalid index");
  360. return;
  361. }
  362. LBItem* item = mItems[index];
  363. item->isActive = true;
  364. }
  365. void GuiListBoxCtrl::setItemInactive(S32 index)
  366. {
  367. if ((index >= mItems.size()) || index < 0)
  368. {
  369. Con::warnf("GuiListBoxCtrl::setItemInactive - invalid index");
  370. return;
  371. }
  372. LBItem* item = mItems[index];
  373. item->isActive = false;
  374. }
  375. bool GuiListBoxCtrl::getItemActive(S32 index)
  376. {
  377. if ((index >= mItems.size()) || index < 0)
  378. {
  379. Con::warnf("GuiListBoxCtrl::getItemActive - invalid index");
  380. return true;
  381. }
  382. LBItem* item = mItems[index];
  383. return item->isActive;
  384. }
  385. S32 GuiListBoxCtrl::insertItem( S32 index, StringTableEntry text, void *itemData )
  386. {
  387. // If the index is greater than our list size, insert it at the end
  388. if( index >= mItems.size() )
  389. index = mItems.size();
  390. // Sanity checking
  391. if( !text )
  392. {
  393. Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL string" );
  394. return -1;
  395. }
  396. LBItem *newItem = new LBItem;
  397. if( !newItem )
  398. {
  399. Con::warnf("GuiListBoxCtrl::insertItem - error allocating item memory!" );
  400. return -1;
  401. }
  402. // Assign item data
  403. newItem->itemText = StringTable->insert(text, true);
  404. newItem->isSelected = false;
  405. newItem->isActive = true;
  406. newItem->ID = 0;
  407. newItem->itemData = itemData;
  408. newItem->hasColor = false;
  409. // Add to list
  410. mItems.insert(index);
  411. mItems[index] = newItem;
  412. // Resize our list to fit our items
  413. updateSize();
  414. // Return our index in list (last)
  415. return index;
  416. }
  417. S32 GuiListBoxCtrl::insertItemWithColor( S32 index, StringTableEntry text, ColorF color, void *itemData )
  418. {
  419. if( color == ColorF(-1, -1, -1) )
  420. {
  421. Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL color" );
  422. return -1;
  423. }
  424. index = insertItem(index, text, itemData);
  425. if(index != -1)
  426. {
  427. setItemColor(index, color);
  428. }
  429. // Return our index in list (last)
  430. return index;
  431. }
  432. void GuiListBoxCtrl::deleteItem( S32 index )
  433. {
  434. // Range Check
  435. if( index >= mItems.size() || index < 0 )
  436. {
  437. Con::warnf("GuiListBoxCtrl::deleteItem - index out of range!" );
  438. return;
  439. }
  440. // Grab our item
  441. LBItem* item = mItems[ index ];
  442. if( !item )
  443. {
  444. Con::warnf("GuiListBoxCtrl::deleteItem - Bad Item Data!" );
  445. return;
  446. }
  447. // Remove it from the selected list.
  448. if( item->isSelected )
  449. {
  450. for( VectorPtr<LBItem*>::iterator i = mSelectedItems.begin(); i != mSelectedItems.end(); i++ )
  451. {
  452. if( item == *i )
  453. {
  454. mSelectedItems.erase_fast( i );
  455. break;
  456. }
  457. }
  458. }
  459. // Remove it from the list
  460. mItems.erase( &mItems[ index ] );
  461. // Free the memory associated with it
  462. delete item;
  463. }
  464. StringTableEntry GuiListBoxCtrl::getItemText( S32 index )
  465. {
  466. // Range Checking
  467. if( index > mItems.size() || index < 0 )
  468. {
  469. Con::warnf( "GuiListBoxCtrl::getItemText - index out of range!" );
  470. return StringTable->EmptyString;
  471. }
  472. return mItems[ index ]->itemText;
  473. }
  474. void GuiListBoxCtrl::setItemText( S32 index, StringTableEntry text )
  475. {
  476. // Sanity Checking
  477. if( !text )
  478. {
  479. Con::warnf("GuiListBoxCtrl::setItemText - Invalid Text Specified!" );
  480. return;
  481. }
  482. // Range Checking
  483. if( index > mItems.size() || index < 0 )
  484. {
  485. Con::warnf( "GuiListBoxCtrl::getItemText - index out of range!" );
  486. return;
  487. }
  488. mItems[ index ]->itemText = StringTable->insert( text );
  489. }
  490. #pragma endregion
  491. #pragma region Sizing
  492. void GuiListBoxCtrl::updateSize()
  493. {
  494. if( !mProfile || !mProfile->getFont(mFontSizeAdjust))
  495. return;
  496. GFont *font = mProfile->getFont(mFontSizeAdjust);
  497. Point2I contentSize = Point2I(10, font->getHeight() + 2);
  498. if (!mFitParentWidth)
  499. {
  500. // Find the maximum width cell:
  501. S32 maxWidth = 1;
  502. for ( U32 i = 0; i < (U32)mItems.size(); i++ )
  503. {
  504. S32 width = font->getStrWidth( mItems[i]->itemText );
  505. if( width > maxWidth )
  506. maxWidth = width;
  507. }
  508. contentSize.x = maxWidth + 6;
  509. }
  510. mItemSize = this->getOuterExtent(contentSize, NormalState, mProfile);
  511. Point2I newExtent = Point2I(mItemSize.x, mItemSize.y * mItems.size());
  512. //Don't update the extent.x if we are matching our parent's size. We will handle it during rendering.
  513. if (mFitParentWidth)
  514. {
  515. newExtent.x = mBounds.extent.x;
  516. }
  517. resize( mBounds.point, newExtent );
  518. }
  519. void GuiListBoxCtrl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
  520. {
  521. Parent::parentResized( oldParentExtent, newParentExtent );
  522. updateSize();
  523. }
  524. #pragma endregion
  525. #pragma region Rendering
  526. void GuiListBoxCtrl::onRender( Point2I offset, const RectI &updateRect )
  527. {
  528. RectI clip = dglGetClipRect();
  529. if (mFitParentWidth && ( mBounds.extent.x != clip.extent.x || mItemSize.x != clip.extent.x))
  530. {
  531. mBounds.extent.x = clip.extent.x;
  532. mItemSize.x = clip.extent.x;
  533. }
  534. for ( S32 i = 0; i < mItems.size(); i++)
  535. {
  536. // Only render visible items
  537. if ((i + 1) * mItemSize.y + offset.y < updateRect.point.y)
  538. continue;
  539. // Break out once we're no longer in visible item range
  540. if( i * mItemSize.y + offset.y >= updateRect.point.y + updateRect.extent.y)
  541. break;
  542. RectI itemRect = RectI( offset.x, offset.y + ( i * mItemSize.y ), mItemSize.x, mItemSize.y );
  543. // Render our item
  544. onRenderItem( itemRect, mItems[i] );
  545. }
  546. }
  547. void GuiListBoxCtrl::onRenderItem( RectI &itemRect, LBItem *item )
  548. {
  549. Point2I cursorPt = Point2I(0,0);
  550. GuiCanvas *root = getRoot();
  551. if (root)
  552. {
  553. cursorPt = root->getCursorPos();
  554. }
  555. GuiControlState currentState = GuiControlState::NormalState;
  556. if (!mActive || !item->isActive)
  557. currentState = GuiControlState::DisabledState;
  558. else if (item->isSelected)
  559. currentState = GuiControlState::SelectedState;
  560. else if (itemRect.pointInRect(cursorPt))
  561. currentState = GuiControlState::HighlightState;
  562. RectI ctrlRect = applyMargins(itemRect.point, itemRect.extent, currentState, mProfile);
  563. if (!ctrlRect.isValidRect())
  564. {
  565. return;
  566. }
  567. renderUniversalRect(ctrlRect, mProfile, currentState);
  568. //Render Text
  569. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  570. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  571. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  572. // Render color bullet if needed
  573. if (item->hasColor)
  574. {
  575. RectI drawArea = RectI(contentRect.point.x, contentRect.point.y, contentRect.extent.y, contentRect.extent.y);
  576. ColorI color = item->color;
  577. renderColorBullet(drawArea, color, 5);
  578. contentRect.point.x += contentRect.extent.y;
  579. contentRect.extent.x -= contentRect.extent.y;
  580. }
  581. renderText(contentRect.point, contentRect.extent, item->itemText, mProfile);
  582. }
  583. void GuiListBoxCtrl::ScrollToIndex(const S32 targetIndex)
  584. {
  585. GuiScrollCtrl* parent = dynamic_cast<GuiScrollCtrl *>(getParent());
  586. if (parent)
  587. parent->scrollRectVisible(RectI(0, mItemSize.y * targetIndex, mItemSize.x, mItemSize.y));
  588. }
  589. #pragma endregion
  590. #pragma region InputEvents
  591. void GuiListBoxCtrl::onTouchDragged(const GuiEvent &event)
  592. {
  593. if (!mActive)
  594. {
  595. return;
  596. }
  597. Parent::onTouchDragged(event);
  598. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  599. S32 itemHit = (localPoint.y < 0) ? -1 : (S32)mFloor((F32)localPoint.y / (F32)mItemSize.y);
  600. if (itemHit >= mItems.size() || itemHit == -1)
  601. return;
  602. LBItem *hitItem = mItems[itemHit];
  603. if (hitItem == NULL || !hitItem->isActive)
  604. return;
  605. if(caller->isMethod("onTouchDragged"))
  606. Con::executef(caller, 3, "onTouchDragged", Con::getIntArg(itemHit), hitItem->itemText, hitItem->ID);
  607. }
  608. void GuiListBoxCtrl::onTouchDown( const GuiEvent &event )
  609. {
  610. if (!mActive)
  611. {
  612. return;
  613. }
  614. setFirstResponder();
  615. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  616. S32 itemHit = ( localPoint.y < 0 ) ? -1 : (S32)mFloor( (F32)localPoint.y / (F32)mItemSize.y );
  617. if ( itemHit >= mItems.size() || itemHit == -1 )
  618. return;
  619. LBItem *hitItem = mItems[ itemHit ];
  620. if ( hitItem == NULL || !hitItem->isActive)
  621. return;
  622. // If we're not a multiple selection listbox, we simply select/unselect an item
  623. if( !mMultipleSelections )
  624. {
  625. // No current selection? Just select the cell and move on
  626. S32 selItem = getSelectedItem();
  627. if ( selItem != itemHit && selItem != -1 )
  628. clearSelection();
  629. // Set the current selection
  630. setCurSel( itemHit );
  631. if( itemHit == selItem && event.mouseClickCount == 2)
  632. {
  633. if(caller->isMethod("onDoubleClick") )
  634. Con::executef(caller, 3, "onDoubleClick", Con::getIntArg(itemHit), hitItem->itemText, hitItem->ID);
  635. }
  636. else if (caller->isMethod("onClick"))
  637. {
  638. Con::executef(caller, 3, "onClick", Con::getIntArg(itemHit), hitItem->itemText, hitItem->ID);
  639. }
  640. // Store the clicked item
  641. mLastClickItem = hitItem;
  642. return;
  643. }
  644. // Deal with multiple selections
  645. if( event.modifier & SI_CTRL)
  646. {
  647. // Ctrl-Click toggles selection
  648. if( hitItem->isSelected )
  649. {
  650. removeSelection( hitItem, itemHit );
  651. // We return here when we deselect an item because we don't store last clicked when we deselect
  652. return;
  653. }
  654. else
  655. addSelection( hitItem, itemHit );
  656. }
  657. else if( event.modifier & SI_SHIFT )
  658. {
  659. if( !mLastClickItem )
  660. addSelection( hitItem, itemHit );
  661. else
  662. setCurSelRange( getItemIndex( mLastClickItem ), itemHit );
  663. }
  664. else
  665. {
  666. if( getSelCount() != 0 )
  667. {
  668. S32 selItem = getSelectedItem();
  669. if( selItem != -1 && mItems[selItem] != hitItem )
  670. clearSelection();
  671. }
  672. addSelection( hitItem, itemHit );
  673. }
  674. if( hitItem == mLastClickItem && event.mouseClickCount == 2)
  675. {
  676. if(caller->isMethod("onDoubleClick") )
  677. Con::executef(caller, 3, "onDoubleClick", Con::getIntArg(itemHit), hitItem->itemText, hitItem->ID);
  678. }
  679. else if (caller->isMethod("onClick"))
  680. {
  681. Con::executef(caller, 3, "onClick", Con::getIntArg(itemHit), hitItem->itemText, hitItem->ID);
  682. }
  683. mLastClickItem = hitItem;
  684. }
  685. bool GuiListBoxCtrl::onKeyDown(const GuiEvent &event)
  686. {
  687. //if this control is a dead end, make sure the event stops here
  688. if (!mVisible || !mActive || !mAwake || mItems.size() == 0)
  689. return true;
  690. S32 index = getSelectedItem();
  691. switch (event.keyCode)
  692. {
  693. case KEY_RETURN:
  694. if (mAltConsoleCommand[0])
  695. Con::evaluate(mAltConsoleCommand, false);
  696. break;
  697. case KEY_LEFT:
  698. case KEY_UP:
  699. if (index == -1)
  700. {
  701. //Select the bottom item
  702. addSelection(mItems.size() - 1);
  703. }
  704. else if(index != 0)
  705. {
  706. addSelection(index - 1);
  707. }
  708. break;
  709. case KEY_DOWN:
  710. case KEY_RIGHT:
  711. if (index == -1)
  712. {
  713. //Select the top item
  714. addSelection(0);
  715. }
  716. else if (index != (mItems.size() - 1))
  717. {
  718. addSelection(index + 1);
  719. }
  720. break;
  721. case KEY_HOME:
  722. addSelection(0);
  723. break;
  724. case KEY_END:
  725. addSelection(mItems.size() - 1);
  726. break;
  727. case KEY_DELETE:
  728. if (index != -1 && isMethod("onDeleteKey"))
  729. Con::executef(caller, 3, "onDeleteKey", Con::getIntArg(index), getItemText(index), getItemID(index));
  730. break;
  731. default:
  732. return(Parent::onKeyDown(event));
  733. break;
  734. };
  735. }
  736. #pragma endregion
  737. #pragma region sorting
  738. bool GuiListBoxCtrl::LBItem::sIncreasing = true;
  739. void GuiListBoxCtrl::sortByText(bool increasing)
  740. {
  741. if (mItems.size() < 2)
  742. return;
  743. LBItem::sIncreasing = increasing;
  744. std::sort(mItems.begin(), mItems.end(), LBItem::compByText);
  745. }
  746. void GuiListBoxCtrl::sortByID(bool increasing)
  747. {
  748. if (mItems.size() < 2)
  749. return;
  750. LBItem::sIncreasing = increasing;
  751. std::sort(mItems.begin(), mItems.end(), LBItem::compByID);
  752. }
  753. #pragma endregion