UISelectList.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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), transferStr()
  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. const 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. {
  104. transferStr = (String)strx;
  105. return (transferStr);
  106. }
  107. }
  108. return String::EMPTY;
  109. }
  110. bool UISelectList::GetItemSelected(int index)
  111. {
  112. if (!widget_)
  113. return false;
  114. return ((TBSelectList*)widget_)->GetItemSelected(index);
  115. }
  116. String UISelectList::GetItemID(int index)
  117. {
  118. if (!widget_)
  119. return "";
  120. String _id;
  121. TBID id = ((TBSelectList*)widget_)->GetItemID(index);
  122. GetSubsystem<UI>()->GetTBIDString(id, _id);
  123. return _id;
  124. }
  125. /// Returns the string of item at the requested index from the list widget
  126. const String& UISelectList::GetItemString(int index)
  127. {
  128. TBSelectItemSource *tbsource = (TBSelectItemSource*)((TBSelectList*)widget_)->GetSource();
  129. if ( tbsource && index >= 0 && index < tbsource->GetNumItems() )
  130. {
  131. const char *strx = tbsource->GetItemString(index);
  132. if (strx != NULL)
  133. {
  134. transferStr = (String)strx;
  135. return (transferStr);
  136. }
  137. }
  138. return String::EMPTY;
  139. }
  140. String UISelectList::GetHoverItemID()
  141. {
  142. if (!widget_)
  143. return "";
  144. if (!TBWidget::hovered_widget)
  145. return "";
  146. TBSelectList* select = (TBSelectList*) widget_;
  147. if (!select->IsAncestorOf(TBWidget::hovered_widget))
  148. {
  149. return "";
  150. }
  151. String id_;
  152. GetSubsystem<UI>()->GetTBIDString(TBWidget::hovered_widget->GetID(), id_);
  153. return id_;
  154. }
  155. void UISelectList::SetSource(UISelectItemSource* source)
  156. {
  157. if (!widget_)
  158. return;
  159. ((TBSelectList*)widget_)->SetSource(source ? source->GetTBItemSource() : NULL);
  160. }
  161. void UISelectList::ScrollToSelectedItem()
  162. {
  163. if (!widget_)
  164. return;
  165. ((TBSelectList*)widget_)->ScrollToSelectedItem();
  166. }
  167. void UISelectList::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  168. {
  169. if (!widget_)
  170. return;
  171. // if we have a drag and drop item, auto scroll if top/bottom
  172. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  173. if (dragDrop->GetDraggingObject())
  174. {
  175. TBSelectList* select = (TBSelectList*) widget_;
  176. Input* input = GetSubsystem<Input>();
  177. IntVector2 pos = input->GetMousePosition();
  178. select->ConvertFromRoot(pos.x_, pos.y_);
  179. if ((select->GetHitStatus(pos.x_, pos.y_) != WIDGET_HIT_STATUS_NO_HIT))
  180. {
  181. // Adjust speed based on pixel distance from top and bottom
  182. int value = pos.y_;
  183. if (value > 16)
  184. value = select->GetRect().h - pos.y_;
  185. if (value > 16)
  186. return;
  187. int speed = 0;
  188. if (value <= 16)
  189. speed = -2;
  190. if (value < 8)
  191. speed = -4;
  192. if (pos.y_ > 16)
  193. speed = -speed;
  194. if (speed)
  195. select->GetScrollContainer()->ScrollBy(0, speed);
  196. }
  197. }
  198. }
  199. bool UISelectList::OnEvent(const tb::TBWidgetEvent &ev)
  200. {
  201. if (ev.type == EVENT_TYPE_POINTER_DOWN)
  202. {
  203. GetTBSelectList()->SetFocus(WIDGET_FOCUS_REASON_POINTER);
  204. }
  205. if (ev.type == EVENT_TYPE_POINTER_MOVE)
  206. {
  207. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  208. //if return true, then scroll event will be controlled by that widget itself
  209. if (dragDrop->GetDraggingObject())
  210. return true;
  211. }
  212. return UIWidget::OnEvent(ev);
  213. }
  214. void UISelectList::SelectNextItem()
  215. {
  216. if (!widget_)
  217. return;
  218. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_DOWN);
  219. }
  220. void UISelectList::SelectPreviousItem()
  221. {
  222. if (!widget_)
  223. return;
  224. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_UP);
  225. }
  226. void UISelectList::SetUIListView(bool value)
  227. {
  228. if (!widget_)
  229. return;
  230. ((TBSelectList*)widget_)->SetUIListView(value);
  231. }
  232. }