guiListBoxCtrl.cc 20 KB

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