UISelectList.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_select.h>
  23. #include "../Atomic/IO/Log.h"
  24. #include "../Atomic/Input/Input.h"
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "UIDragDrop.h"
  28. #include "UISelectList.h"
  29. using namespace tb;
  30. namespace Atomic
  31. {
  32. UISelectList::UISelectList(Context* context, bool createWidget) : UIWidget(context, false)
  33. {
  34. if (createWidget)
  35. {
  36. widget_ = new TBSelectList();
  37. widget_->SetDelegate(this);
  38. GetSubsystem<UI>()->WrapWidget(this, widget_);
  39. }
  40. SubscribeToEvent(E_UIUPDATE, ATOMIC_HANDLER(UISelectList, HandleUIUpdate));
  41. }
  42. UISelectList::~UISelectList()
  43. {
  44. }
  45. tb::TBSelectList* UISelectList::GetTBSelectList()
  46. {
  47. return (TBSelectList*) widget_;
  48. }
  49. void UISelectList::SetFilter(const String& filter)
  50. {
  51. if (!widget_)
  52. return;
  53. ((TBSelectList*)widget_)->SetFilter(filter.CString());
  54. }
  55. int UISelectList::GetNumItems() const
  56. {
  57. if (!widget_)
  58. return 0;
  59. return ((TBSelectList*)widget_)->GetNumItems();
  60. }
  61. void UISelectList::SelectItem(int index, bool selected)
  62. {
  63. if (!widget_)
  64. return;
  65. ((TBSelectList*)widget_)->SelectItem(index, selected);
  66. }
  67. void UISelectList::SetValue(int value)
  68. {
  69. if (!widget_)
  70. return;
  71. ((TBSelectList*)widget_)->SetValue(value);
  72. }
  73. double UISelectList::GetValue()
  74. {
  75. if (!widget_)
  76. return 0;
  77. return (double) ((TBSelectList*)widget_)->GetValue();
  78. }
  79. void UISelectList::InvalidateList()
  80. {
  81. if (!widget_)
  82. return;
  83. return ((TBSelectList*)widget_)->InvalidateList();
  84. }
  85. String UISelectList::GetSelectedItemID()
  86. {
  87. if (!widget_)
  88. return "";
  89. String id_;
  90. TBID id = ((TBSelectList*)widget_)->GetSelectedItemID();
  91. GetSubsystem<UI>()->GetTBIDString(id, id_);
  92. return id_;
  93. }
  94. /// Returns the string of the selected item from the list widget
  95. String UISelectList::GetSelectedItemString()
  96. {
  97. int selected = ((TBSelectList*)widget_)->GetValue();
  98. TBSelectItemSource *tbsource = (TBSelectItemSource*)((TBSelectList*)widget_)->GetSource();
  99. if ( tbsource && selected >= 0 && selected < tbsource->GetNumItems() )
  100. {
  101. const char *strx = tbsource->GetItemString(selected);
  102. if (strx )
  103. return ( tbsource->GetItemString(selected) );
  104. }
  105. return String::EMPTY;
  106. }
  107. bool UISelectList::GetItemSelected(int index)
  108. {
  109. if (!widget_)
  110. return false;
  111. return ((TBSelectList*)widget_)->GetItemSelected(index);
  112. }
  113. String UISelectList::GetItemID(int index)
  114. {
  115. if (!widget_)
  116. return "";
  117. String _id;
  118. TBID id = ((TBSelectList*)widget_)->GetItemID(index);
  119. GetSubsystem<UI>()->GetTBIDString(id, _id);
  120. return _id;
  121. }
  122. /// Returns the string of item at the requested index from the list widget
  123. String UISelectList::GetItemString(int index)
  124. {
  125. TBSelectItemSource *tbsource = (TBSelectItemSource*)((TBSelectList*)widget_)->GetSource();
  126. if ( tbsource && index >= 0 && index < tbsource->GetNumItems() )
  127. {
  128. const char *strx = tbsource->GetItemString(index);
  129. if (strx != NULL)
  130. return ( tbsource->GetItemString(index) );
  131. }
  132. return String::EMPTY;
  133. }
  134. /// Add a new item at the given index.
  135. bool UISelectList::AddItem(int index, const String& str, const String& id )
  136. {
  137. if ( index < 0 ) return false; // dont let the UI crash.
  138. TBSelectItemSourceList<TBGenericStringItem> *tbsource = (TBSelectItemSourceList<TBGenericStringItem> *)((TBSelectList*)widget_)->GetSource();
  139. if ( tbsource )
  140. {
  141. return tbsource->AddItem ( new TBGenericStringItem(str.CString(), TBID(id.CString())), index);
  142. }
  143. return false;
  144. }
  145. /// Delete the item at the given index.
  146. void UISelectList::DeleteItem(int index)
  147. {
  148. TBSelectItemSourceList<TBGenericStringItem> *tbsource = (TBSelectItemSourceList<TBGenericStringItem> *)((TBSelectList*)widget_)->GetSource();
  149. if ( tbsource && index >= 0 && index < tbsource->GetNumItems() )
  150. {
  151. tbsource->DeleteItem(index);
  152. }
  153. }
  154. /// Delete all items.
  155. void UISelectList::DeleteAllItems()
  156. {
  157. TBSelectItemSourceList<TBGenericStringItem> *tbsource = (TBSelectItemSourceList<TBGenericStringItem> *)((TBSelectList*)widget_)->GetSource();
  158. if ( tbsource )
  159. {
  160. tbsource->DeleteAllItems();
  161. }
  162. }
  163. /// Searches the items for the id as a number, returns index, -1 if not found
  164. int UISelectList::FindId ( int idnum )
  165. {
  166. uint32 myid = (uint32)idnum;
  167. TBSelectItemSource *tbsource = (TBSelectItemSource*)((TBSelectList*)widget_)->GetSource();
  168. int nn = 0;
  169. for( nn=0; nn < tbsource->GetNumItems(); nn++ )
  170. {
  171. if ( tbsource->GetItemID(nn) == myid )
  172. return nn;
  173. }
  174. return -1;
  175. }
  176. String UISelectList::GetHoverItemID()
  177. {
  178. if (!widget_)
  179. return "";
  180. if (!TBWidget::hovered_widget)
  181. return "";
  182. TBSelectList* select = (TBSelectList*) widget_;
  183. if (!select->IsAncestorOf(TBWidget::hovered_widget))
  184. {
  185. return "";
  186. }
  187. String id_;
  188. GetSubsystem<UI>()->GetTBIDString(TBWidget::hovered_widget->GetID(), id_);
  189. return id_;
  190. }
  191. void UISelectList::SetSource(UISelectItemSource* source)
  192. {
  193. if (!widget_)
  194. return;
  195. ((TBSelectList*)widget_)->SetSource(source ? source->GetTBItemSource() : NULL);
  196. }
  197. void UISelectList::ScrollToSelectedItem()
  198. {
  199. if (!widget_)
  200. return;
  201. ((TBSelectList*)widget_)->ScrollToSelectedItem();
  202. }
  203. void UISelectList::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  204. {
  205. if (!widget_)
  206. return;
  207. // if we have a drag and drop item, auto scroll if top/bottom
  208. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  209. if (dragDrop->GetDraggingObject())
  210. {
  211. TBSelectList* select = (TBSelectList*) widget_;
  212. Input* input = GetSubsystem<Input>();
  213. IntVector2 pos = input->GetMousePosition();
  214. select->ConvertFromRoot(pos.x_, pos.y_);
  215. if ((select->GetHitStatus(pos.x_, pos.y_) != WIDGET_HIT_STATUS_NO_HIT))
  216. {
  217. // Adjust speed based on pixel distance from top and bottom
  218. int value = pos.y_;
  219. if (value > 16)
  220. value = select->GetRect().h - pos.y_;
  221. if (value > 16)
  222. return;
  223. int speed = 0;
  224. if (value <= 16)
  225. speed = -2;
  226. if (value < 8)
  227. speed = -4;
  228. if (pos.y_ > 16)
  229. speed = -speed;
  230. if (speed)
  231. select->GetScrollContainer()->ScrollBy(0, speed);
  232. }
  233. }
  234. }
  235. bool UISelectList::OnEvent(const tb::TBWidgetEvent &ev)
  236. {
  237. if (ev.type == EVENT_TYPE_POINTER_DOWN)
  238. {
  239. GetTBSelectList()->SetFocus(WIDGET_FOCUS_REASON_POINTER);
  240. }
  241. if (ev.type == EVENT_TYPE_POINTER_MOVE)
  242. {
  243. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  244. //if return true, then scroll event will be controlled by that widget itself
  245. if (dragDrop->GetDraggingObject())
  246. return true;
  247. }
  248. return UIWidget::OnEvent(ev);
  249. }
  250. void UISelectList::SelectNextItem()
  251. {
  252. if (!widget_)
  253. return;
  254. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_DOWN);
  255. }
  256. void UISelectList::SelectPreviousItem()
  257. {
  258. if (!widget_)
  259. return;
  260. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_UP);
  261. }
  262. void UISelectList::SetUIListView(bool value)
  263. {
  264. if (!widget_)
  265. return;
  266. ((TBSelectList*)widget_)->SetUIListView(value);
  267. }
  268. }