UISelectList.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. String UISelectList::GetHoverItemID()
  135. {
  136. if (!widget_)
  137. return "";
  138. if (!TBWidget::hovered_widget)
  139. return "";
  140. TBSelectList* select = (TBSelectList*) widget_;
  141. if (!select->IsAncestorOf(TBWidget::hovered_widget))
  142. {
  143. return "";
  144. }
  145. String id_;
  146. GetSubsystem<UI>()->GetTBIDString(TBWidget::hovered_widget->GetID(), id_);
  147. return id_;
  148. }
  149. void UISelectList::SetSource(UISelectItemSource* source)
  150. {
  151. if (!widget_)
  152. return;
  153. ((TBSelectList*)widget_)->SetSource(source ? source->GetTBItemSource() : NULL);
  154. }
  155. void UISelectList::ScrollToSelectedItem()
  156. {
  157. if (!widget_)
  158. return;
  159. ((TBSelectList*)widget_)->ScrollToSelectedItem();
  160. }
  161. void UISelectList::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  162. {
  163. if (!widget_)
  164. return;
  165. // if we have a drag and drop item, auto scroll if top/bottom
  166. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  167. if (dragDrop->GetDraggingObject())
  168. {
  169. TBSelectList* select = (TBSelectList*) widget_;
  170. Input* input = GetSubsystem<Input>();
  171. IntVector2 pos = input->GetMousePosition();
  172. select->ConvertFromRoot(pos.x_, pos.y_);
  173. if ((select->GetHitStatus(pos.x_, pos.y_) != WIDGET_HIT_STATUS_NO_HIT))
  174. {
  175. // Adjust speed based on pixel distance from top and bottom
  176. int value = pos.y_;
  177. if (value > 16)
  178. value = select->GetRect().h - pos.y_;
  179. if (value > 16)
  180. return;
  181. int speed = 0;
  182. if (value <= 16)
  183. speed = -2;
  184. if (value < 8)
  185. speed = -4;
  186. if (pos.y_ > 16)
  187. speed = -speed;
  188. if (speed)
  189. select->GetScrollContainer()->ScrollBy(0, speed);
  190. }
  191. }
  192. }
  193. bool UISelectList::OnEvent(const tb::TBWidgetEvent &ev)
  194. {
  195. if (ev.type == EVENT_TYPE_POINTER_DOWN)
  196. {
  197. GetTBSelectList()->SetFocus(WIDGET_FOCUS_REASON_POINTER);
  198. }
  199. if (ev.type == EVENT_TYPE_POINTER_MOVE)
  200. {
  201. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  202. //if return true, then scroll event will be controlled by that widget itself
  203. if (dragDrop->GetDraggingObject())
  204. return true;
  205. }
  206. return UIWidget::OnEvent(ev);
  207. }
  208. void UISelectList::SelectNextItem()
  209. {
  210. if (!widget_)
  211. return;
  212. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_DOWN);
  213. }
  214. void UISelectList::SelectPreviousItem()
  215. {
  216. if (!widget_)
  217. return;
  218. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_UP);
  219. }
  220. void UISelectList::SetUIListView(bool value)
  221. {
  222. if (!widget_)
  223. return;
  224. ((TBSelectList*)widget_)->SetUIListView(value);
  225. }
  226. }