UISelectList.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, 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. bool UISelectList::GetItemSelected(int index)
  95. {
  96. if (!widget_)
  97. return false;
  98. return ((TBSelectList*)widget_)->GetItemSelected(index);
  99. }
  100. String UISelectList::GetItemID(int index)
  101. {
  102. if (!widget_)
  103. return "";
  104. String _id;
  105. TBID id = ((TBSelectList*)widget_)->GetItemID(index);
  106. GetSubsystem<UI>()->GetTBIDString(id, _id);
  107. return _id;
  108. }
  109. String UISelectList::GetHoverItemID()
  110. {
  111. if (!widget_)
  112. return "";
  113. if (!TBWidget::hovered_widget)
  114. return "";
  115. TBSelectList* select = (TBSelectList*) widget_;
  116. if (!select->IsAncestorOf(TBWidget::hovered_widget))
  117. {
  118. return "";
  119. }
  120. String id_;
  121. GetSubsystem<UI>()->GetTBIDString(TBWidget::hovered_widget->GetID(), id_);
  122. return id_;
  123. }
  124. void UISelectList::SetSource(UISelectItemSource* source)
  125. {
  126. if (!widget_)
  127. return;
  128. ((TBSelectList*)widget_)->SetSource(source ? source->GetTBItemSource() : NULL);
  129. }
  130. void UISelectList::ScrollToSelectedItem()
  131. {
  132. if (!widget_)
  133. return;
  134. ((TBSelectList*)widget_)->ScrollToSelectedItem();
  135. }
  136. void UISelectList::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  137. {
  138. if (!widget_)
  139. return;
  140. // if we have a drag and drop item, auto scroll if top/bottom
  141. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  142. if (dragDrop->GetDraggingObject())
  143. {
  144. TBSelectList* select = (TBSelectList*) widget_;
  145. Input* input = GetSubsystem<Input>();
  146. IntVector2 pos = input->GetMousePosition();
  147. select->ConvertFromRoot(pos.x_, pos.y_);
  148. if ((select->GetHitStatus(pos.x_, pos.y_) != WIDGET_HIT_STATUS_NO_HIT))
  149. {
  150. // Adjust speed based on pixel distance from top and bottom
  151. int value = pos.y_;
  152. if (value > 16)
  153. value = select->GetRect().h - pos.y_;
  154. if (value > 16)
  155. return;
  156. int speed = 0;
  157. if (value <= 16)
  158. speed = -2;
  159. if (value < 8)
  160. speed = -4;
  161. if (pos.y_ > 16)
  162. speed = -speed;
  163. if (speed)
  164. select->GetScrollContainer()->ScrollBy(0, speed);
  165. }
  166. }
  167. }
  168. bool UISelectList::OnEvent(const tb::TBWidgetEvent &ev)
  169. {
  170. if (ev.type == EVENT_TYPE_POINTER_DOWN)
  171. {
  172. GetTBSelectList()->SetFocus(WIDGET_FOCUS_REASON_POINTER);
  173. }
  174. if (ev.type == EVENT_TYPE_POINTER_MOVE)
  175. {
  176. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  177. //if return true, then scroll event will be controlled by that widget itself
  178. if (dragDrop->GetDraggingObject())
  179. return true;
  180. }
  181. return UIWidget::OnEvent(ev);
  182. }
  183. void UISelectList::SelectNextItem()
  184. {
  185. if (!widget_)
  186. return;
  187. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_DOWN);
  188. }
  189. void UISelectList::SelectPreviousItem()
  190. {
  191. if (!widget_)
  192. return;
  193. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_UP);
  194. }
  195. void UISelectList::SetUIListView(bool value)
  196. {
  197. if (!widget_)
  198. return;
  199. ((TBSelectList*)widget_)->SetUIListView(value);
  200. }
  201. }