guiListBoxCtrl.cc 23 KB

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