UIWidget.h 9.4 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. #pragma once
  23. #include <ThirdParty/TurboBadger/tb_widgets.h>
  24. #include <ThirdParty/TurboBadger/tb_widgets_common.h>
  25. #include "../Core/Object.h"
  26. #include "UIPreferredSize.h"
  27. #include "UIDragObject.h"
  28. namespace Atomic
  29. {
  30. /// Defines widget visibility, used with UIWidget::SetVisibility.
  31. enum UI_WIDGET_VISIBILITY
  32. {
  33. /// Visible (default)
  34. UI_WIDGET_VISIBILITY_VISIBLE = tb:: WIDGET_VISIBILITY_VISIBLE,
  35. /// Invisible, but layouted. Interaction disabled.
  36. UI_WIDGET_VISIBILITY_INVISIBLE = tb::WIDGET_VISIBILITY_INVISIBLE,
  37. /// Invisible and no layout. Interaction disabled.
  38. UI_WIDGET_VISIBILITY_GONE = tb::WIDGET_VISIBILITY_GONE
  39. };
  40. /// TBWidget gravity (may be combined).
  41. /// Gravity gives hints about positioning and sizing preferences.
  42. enum UI_GRAVITY {
  43. UI_GRAVITY_NONE = tb::WIDGET_GRAVITY_NONE,
  44. UI_GRAVITY_LEFT = tb::WIDGET_GRAVITY_LEFT,
  45. UI_GRAVITY_RIGHT = tb::WIDGET_GRAVITY_RIGHT,
  46. UI_GRAVITY_TOP = tb::WIDGET_GRAVITY_TOP,
  47. UI_GRAVITY_BOTTOM = tb::WIDGET_GRAVITY_BOTTOM,
  48. UI_GRAVITY_LEFT_RIGHT = tb::WIDGET_GRAVITY_LEFT_RIGHT,
  49. UI_GRAVITY_TOP_BOTTOM = tb::WIDGET_GRAVITY_TOP_BOTTOM,
  50. UI_GRAVITY_ALL = tb::WIDGET_GRAVITY_ALL,
  51. UI_GRAVITY_DEFAULT = tb::WIDGET_GRAVITY_DEFAULT
  52. };
  53. enum UI_EVENT_TYPE {
  54. /** Click event is what should be used to trig actions in almost all cases.
  55. It is invoked on a widget after POINTER_UP if the pointer is still inside
  56. its hit area. It can also be invoked by keyboard on some clickable widgets
  57. (see TBWidget::SetClickByKey).
  58. If panning of scrollable widgets start while the pointer is down, CLICK
  59. won't be invoked when releasing the pointer (since that should stop panning). */
  60. UI_EVENT_TYPE_CLICK = tb::EVENT_TYPE_CLICK,
  61. /** Long click event is sent when the pointer has been down for some time
  62. without moving much.
  63. It is invoked on a widget that has enabled it (TBWidget::SetWantLongClick
  64. If this event isn't handled, the widget will invoke a CONTEXT_MENU event.
  65. If any of those are handled, the CLICK event that would normally be
  66. invoked after the pending POINTER_UP will be suppressed. */
  67. UI_EVENT_TYPE_LONG_CLICK = tb::EVENT_TYPE_LONG_CLICK,
  68. UI_EVENT_TYPE_POINTER_DOWN = tb::EVENT_TYPE_POINTER_DOWN,
  69. UI_EVENT_TYPE_POINTER_UP = tb::EVENT_TYPE_POINTER_UP,
  70. UI_EVENT_TYPE_POINTER_MOVE = tb::EVENT_TYPE_POINTER_MOVE,
  71. UI_EVENT_TYPE_RIGHT_POINTER_DOWN = tb::EVENT_TYPE_RIGHT_POINTER_DOWN,
  72. UI_EVENT_TYPE_RIGHT_POINTER_UP = tb::EVENT_TYPE_RIGHT_POINTER_UP,
  73. UI_EVENT_TYPE_WHEEL = tb::EVENT_TYPE_WHEEL,
  74. /** Invoked after changing text in a TBTextField, changing selected item
  75. in a TBSelectList etc. Invoking this event trigs synchronization with
  76. connected TBWidgetValue and other widgets connected to it. */
  77. UI_EVENT_TYPE_CHANGED = tb::EVENT_TYPE_CHANGED,
  78. UI_EVENT_TYPE_KEY_DOWN = tb::EVENT_TYPE_KEY_DOWN,
  79. UI_EVENT_TYPE_KEY_UP = tb::EVENT_TYPE_KEY_UP,
  80. /** Invoked by the platform when a standard keyboard shortcut is pressed.
  81. It's called before InvokeKeyDown (EVENT_TYPE_KEY_DOWN) and if the event
  82. is handled (returns true), the KeyDown is canceled.
  83. The ref_id will be set to one of the following:
  84. "cut", "copy", "paste", "selectall", "undo", "redo", "new", "open", "save". */
  85. UI_EVENT_TYPE_SHORTCUT = tb::EVENT_TYPE_SHORTCUT,
  86. /** Invoked when a context menu should be opened at the event x and y coordinates.
  87. It may be invoked automatically for a widget on long click, if nothing handles
  88. the long click event. */
  89. UI_EVENT_TYPE_CONTEXT_MENU = tb::EVENT_TYPE_CONTEXT_MENU,
  90. /** Invoked by the platform when one or multiple files has been dropped on
  91. the widget. The event is guaranteed to be a TBWidgetEventFileDrop. */
  92. UI_EVENT_TYPE_FILE_DROP = tb::EVENT_TYPE_FILE_DROP,
  93. /** Invoked by the platform when a tab container's tab changed */
  94. UI_EVENT_TYPE_TAB_CHANGED = tb::EVENT_TYPE_TAB_CHANGED,
  95. /** Custom event. Not used internally. ref_id may be used for additional type info. */
  96. UI_EVENT_TYPE_CUSTOM = tb::EVENT_TYPE_CUSTOM
  97. };
  98. /** Defines widget z level relative to another widget, used with TBWidget::AddChildRelative. */
  99. enum UI_WIDGET_Z_REL {
  100. UI_WIDGET_Z_REL_BEFORE = tb::WIDGET_Z_REL_BEFORE, ///< Before the reference widget (visually behind reference).
  101. UI_WIDGET_Z_REL_AFTER = tb::WIDGET_Z_REL_AFTER ///< After the reference widget (visually above reference).
  102. };
  103. /// TB_TEXT_ALIGN specifies horizontal text alignment
  104. enum UI_TEXT_ALIGN
  105. {
  106. UI_TEXT_ALIGN_LEFT = tb::TB_TEXT_ALIGN_LEFT,
  107. UI_TEXT_ALIGN_RIGHT = tb::TB_TEXT_ALIGN_RIGHT,
  108. UI_TEXT_ALIGN_CENTER = tb::TB_TEXT_ALIGN_CENTER
  109. };
  110. enum UI_WIDGET_STATE {
  111. UI_WIDGET_STATE_NONE = tb::WIDGET_STATE_NONE,
  112. UI_WIDGET_STATE_DISABLED = tb::WIDGET_STATE_DISABLED,
  113. UI_WIDGET_STATE_FOCUSED = tb::WIDGET_STATE_FOCUSED,
  114. UI_WIDGET_STATE_PRESSED = tb::WIDGET_STATE_PRESSED,
  115. UI_WIDGET_STATE_SELECTED = tb::WIDGET_STATE_SELECTED,
  116. UI_WIDGET_STATE_HOVERED = tb::WIDGET_STATE_HOVERED,
  117. UI_WIDGET_STATE_ALL = tb::WIDGET_STATE_ALL
  118. };
  119. class UIView;
  120. class UILayoutParams;
  121. class UIFontDescription;
  122. /// Wraps a TurboBadger widget in our Object model
  123. class UIWidget : public Object, public tb::TBWidgetDelegate
  124. {
  125. friend class UI;
  126. OBJECT(UIWidget)
  127. public:
  128. UIWidget(Context* context, bool createWidget = true);
  129. virtual ~UIWidget();
  130. bool Load(const String& filename);
  131. const String& GetId();
  132. UIWidget* GetParent();
  133. UIWidget* GetContentRoot();
  134. IntRect GetRect();
  135. UIPreferredSize* GetPreferredSize();
  136. String GetText();
  137. void SetRect(IntRect r);
  138. void SetSize(int width, int height);
  139. void SetPosition(int x, int y);
  140. void SetText(const String& text);
  141. void SetSkinBg(const String& id);
  142. void SetLayoutParams(UILayoutParams* params);
  143. void SetFontDescription(UIFontDescription* fd);
  144. void Remove();
  145. void RemoveChild(UIWidget* child, bool cleanup = true);
  146. void DeleteAllChildren();
  147. // String ID
  148. virtual void SetId(const String& id);
  149. void Center();
  150. void SetGravity(UI_GRAVITY gravity);
  151. void SetValue(double value);
  152. virtual double GetValue();
  153. void SetFocus();
  154. bool GetFocus();
  155. /// Set focus to first widget which accepts it
  156. void SetFocusRecursive();
  157. void OnFocusChanged(bool focused);
  158. void SetState(UI_WIDGET_STATE state, bool on);
  159. bool GetState(UI_WIDGET_STATE state);
  160. void SetVisibility(UI_WIDGET_VISIBILITY visibility);
  161. UI_WIDGET_VISIBILITY GetVisibility();
  162. void SetStateRaw(UI_WIDGET_STATE state);
  163. UI_WIDGET_STATE GetStateRaw();
  164. void Invalidate();
  165. void Die();
  166. void SetDragObject(UIDragObject* object) { dragObject_ = object; }
  167. UIDragObject* GetDragObject() { return dragObject_; }
  168. UIWidget* GetFirstChild();
  169. UIWidget* GetNext();
  170. bool IsAncestorOf(UIWidget* widget);
  171. void SetIsFocusable(bool value);
  172. // get this or child widget with id
  173. UIWidget* GetWidget(const String& id);
  174. UIView* GetView();
  175. virtual void AddChild(UIWidget* child);
  176. void AddChildAfter(UIWidget* child, UIWidget* otherChild);
  177. void AddChildBefore(UIWidget* child, UIWidget* otherChild);
  178. /// Add the child to this widget. See AddChild for adding a child to the top or bottom.
  179. /// This takes a relative Z and insert the child before or after the given reference widget.
  180. void AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference);
  181. void InvalidateLayout();
  182. tb::TBWidget* GetInternalWidget() { return widget_; }
  183. void SetDelegate(UIWidget* widget) { widget_->SetDelegate(widget); }
  184. void SetMultiTouch(bool multiTouch) { multiTouch_ = multiTouch; }
  185. bool IsMultiTouch() { return multiTouch_; }
  186. bool GetCaptured();
  187. void SetCapturing(bool capturing);
  188. bool GetCapturing() { return widget_->GetCapturing(); }
  189. void InvokeShortcut(const String& shortcut);
  190. protected:
  191. void ConvertEvent(UIWidget* handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data);
  192. void SetWidget(tb::TBWidget* widget);
  193. virtual bool OnEvent(const tb::TBWidgetEvent &ev);
  194. virtual void OnDelete();
  195. String id_;
  196. tb::TBWidget* widget_;
  197. SharedPtr<UIPreferredSize> preferredSize_;
  198. SharedPtr<UIDragObject> dragObject_;
  199. bool multiTouch_;
  200. };
  201. }