UISelectList.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. void UISelectList::SetValue(int value)
  56. {
  57. if (!widget_)
  58. return;
  59. ((TBSelectList*)widget_)->SetValue(value);
  60. }
  61. double UISelectList::GetValue()
  62. {
  63. if (!widget_)
  64. return 0;
  65. return (double) ((TBSelectList*)widget_)->GetValue();
  66. }
  67. void UISelectList::InvalidateList()
  68. {
  69. if (!widget_)
  70. return;
  71. return ((TBSelectList*)widget_)->InvalidateList();
  72. }
  73. String UISelectList::GetSelectedItemID()
  74. {
  75. if (!widget_)
  76. return "";
  77. String id_;
  78. TBID id = ((TBSelectList*)widget_)->GetSelectedItemID();
  79. GetSubsystem<UI>()->GetTBIDString(id, id_);
  80. return id_;
  81. }
  82. String UISelectList::GetHoverItemID()
  83. {
  84. if (!widget_)
  85. return "";
  86. if (!TBWidget::hovered_widget)
  87. return "";
  88. TBSelectList* select = (TBSelectList*) widget_;
  89. if (!select->IsAncestorOf(TBWidget::hovered_widget))
  90. {
  91. return "";
  92. }
  93. String id_;
  94. GetSubsystem<UI>()->GetTBIDString(TBWidget::hovered_widget->GetID(), id_);
  95. return id_;
  96. }
  97. void UISelectList::SetSource(UISelectItemSource* source)
  98. {
  99. if (!widget_)
  100. return;
  101. ((TBSelectList*)widget_)->SetSource(source ? source->GetTBItemSource() : NULL);
  102. }
  103. void UISelectList::ScrollToSelectedItem()
  104. {
  105. if (!widget_)
  106. return;
  107. ((TBSelectList*)widget_)->ScrollToSelectedItem();
  108. }
  109. void UISelectList::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  110. {
  111. if (!widget_)
  112. return;
  113. // if we have a drag and drop item, auto scroll if top/bottom
  114. UIDragDrop* dragDrop = GetSubsystem<UIDragDrop>();
  115. if (dragDrop->GetDraggingObject())
  116. {
  117. TBSelectList* select = (TBSelectList*) widget_;
  118. Input* input = GetSubsystem<Input>();
  119. IntVector2 pos = input->GetMousePosition();
  120. select->ConvertFromRoot(pos.x_, pos.y_);
  121. if ((select->GetHitStatus(pos.x_, pos.y_) != WIDGET_HIT_STATUS_NO_HIT))
  122. {
  123. // Adjust speed based on pixel distance from top and bottom
  124. int value = pos.y_;
  125. if (value > 16)
  126. value = select->GetRect().h - pos.y_;
  127. if (value > 16)
  128. return;
  129. int speed = 0;
  130. if (value <= 16)
  131. speed = -2;
  132. if (value < 8)
  133. speed = -4;
  134. if (pos.y_ > 16)
  135. speed = -speed;
  136. if (speed)
  137. select->GetScrollContainer()->ScrollBy(0, speed);
  138. }
  139. }
  140. }
  141. bool UISelectList::OnEvent(const tb::TBWidgetEvent &ev)
  142. {
  143. if (ev.type == EVENT_TYPE_POINTER_DOWN)
  144. {
  145. GetTBSelectList()->SetFocus(WIDGET_FOCUS_REASON_POINTER);
  146. }
  147. return UIWidget::OnEvent(ev);
  148. }
  149. void UISelectList::SelectNextItem()
  150. {
  151. if (!widget_)
  152. return;
  153. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_DOWN);
  154. }
  155. void UISelectList::SelectPreviousItem()
  156. {
  157. if (!widget_)
  158. return;
  159. ((TBSelectList*)widget_)->ChangeValue(TB_KEY_UP);
  160. }
  161. }