UIWidget.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <ThirdParty/TurboBadger/tb_font_renderer.h>
  26. #include "../Core/Object.h"
  27. #include "UIPreferredSize.h"
  28. #include "UIDragObject.h"
  29. namespace Atomic
  30. {
  31. // UI_WIDGET_* enums must match TurboBadger internal enum values, we assign directly to TB enum values
  32. // as C# script bindings need the values
  33. // Note, this could be automated with a dumper util that compiles values in, however that is quite complicated
  34. /// Defines widget visibility, used with UIWidget::SetVisibility.
  35. enum UI_WIDGET_VISIBILITY
  36. {
  37. /// Visible (default)
  38. UI_WIDGET_VISIBILITY_VISIBLE = 0, //tb::WIDGET_VISIBILITY_VISIBLE,
  39. /// Invisible, but layouted. Interaction disabled.
  40. UI_WIDGET_VISIBILITY_INVISIBLE = 1, //tb::WIDGET_VISIBILITY_INVISIBLE,
  41. /// Invisible and no layout. Interaction disabled.
  42. UI_WIDGET_VISIBILITY_GONE = 2 //tb::WIDGET_VISIBILITY_GONE
  43. };
  44. /// TBWidget gravity (may be combined).
  45. /// Gravity gives hints about positioning and sizing preferences.
  46. enum UI_GRAVITY {
  47. UI_GRAVITY_NONE = 0, // tb::WIDGET_GRAVITY_NONE,
  48. UI_GRAVITY_LEFT = 1, // tb::WIDGET_GRAVITY_LEFT,
  49. UI_GRAVITY_RIGHT = 2, // tb::WIDGET_GRAVITY_RIGHT,
  50. UI_GRAVITY_TOP = 4, // tb::WIDGET_GRAVITY_TOP,
  51. UI_GRAVITY_BOTTOM = 8, // tb::WIDGET_GRAVITY_BOTTOM,
  52. UI_GRAVITY_LEFT_RIGHT = 3 , // tb::WIDGET_GRAVITY_LEFT_RIGHT,
  53. UI_GRAVITY_TOP_BOTTOM = 12, // tb::WIDGET_GRAVITY_TOP_BOTTOM,
  54. UI_GRAVITY_ALL = 15, // tb::WIDGET_GRAVITY_ALL,
  55. UI_GRAVITY_DEFAULT = 5 // tb::WIDGET_GRAVITY_DEFAULT
  56. };
  57. enum UI_EVENT_TYPE {
  58. /** Click event is what should be used to trig actions in almost all cases.
  59. It is invoked on a widget after POINTER_UP if the pointer is still inside
  60. its hit area. It can also be invoked by keyboard on some clickable widgets
  61. (see TBWidget::SetClickByKey).
  62. If panning of scrollable widgets start while the pointer is down, CLICK
  63. won't be invoked when releasing the pointer (since that should stop panning). */
  64. UI_EVENT_TYPE_CLICK = 0, // tb::EVENT_TYPE_CLICK,
  65. /** Long click event is sent when the pointer has been down for some time
  66. without moving much.
  67. It is invoked on a widget that has enabled it (TBWidget::SetWantLongClick
  68. If this event isn't handled, the widget will invoke a CONTEXT_MENU event.
  69. If any of those are handled, the CLICK event that would normally be
  70. invoked after the pending POINTER_UP will be suppressed. */
  71. UI_EVENT_TYPE_LONG_CLICK = 1, // tb::EVENT_TYPE_LONG_CLICK,
  72. UI_EVENT_TYPE_POINTER_DOWN = 2, // tb::EVENT_TYPE_POINTER_DOWN,
  73. UI_EVENT_TYPE_POINTER_UP = 3, // tb::EVENT_TYPE_POINTER_UP,
  74. UI_EVENT_TYPE_POINTER_MOVE = 4, // tb::EVENT_TYPE_POINTER_MOVE,
  75. UI_EVENT_TYPE_RIGHT_POINTER_DOWN = 5, // tb::EVENT_TYPE_RIGHT_POINTER_DOWN,
  76. UI_EVENT_TYPE_RIGHT_POINTER_UP = 6, // tb::EVENT_TYPE_RIGHT_POINTER_UP,
  77. UI_EVENT_TYPE_WHEEL = 7, // tb::EVENT_TYPE_WHEEL,
  78. /** Invoked after changing text in a TBTextField, changing selected item
  79. in a TBSelectList etc. Invoking this event trigs synchronization with
  80. connected TBWidgetValue and other widgets connected to it. */
  81. UI_EVENT_TYPE_CHANGED = 8, // tb::EVENT_TYPE_CHANGED,
  82. UI_EVENT_TYPE_KEY_DOWN = 9, // tb::EVENT_TYPE_KEY_DOWN,
  83. UI_EVENT_TYPE_KEY_UP = 10, // tb::EVENT_TYPE_KEY_UP,
  84. /** Invoked by the platform when a standard keyboard shortcut is pressed.
  85. It's called before InvokeKeyDown (EVENT_TYPE_KEY_DOWN) and if the event
  86. is handled (returns true), the KeyDown is canceled.
  87. The ref_id will be set to one of the following:
  88. "cut", "copy", "paste", "selectall", "undo", "redo", "new", "open", "save". */
  89. UI_EVENT_TYPE_SHORTCUT = 11, // tb::EVENT_TYPE_SHORTCUT,
  90. /** Invoked when a context menu should be opened at the event x and y coordinates.
  91. It may be invoked automatically for a widget on long click, if nothing handles
  92. the long click event. */
  93. UI_EVENT_TYPE_CONTEXT_MENU = 12, // tb::EVENT_TYPE_CONTEXT_MENU,
  94. /** Invoked by the platform when one or multiple files has been dropped on
  95. the widget. The event is guaranteed to be a TBWidgetEventFileDrop. */
  96. UI_EVENT_TYPE_FILE_DROP = 13, // tb::EVENT_TYPE_FILE_DROP,
  97. /** Invoked by the platform when a tab container's tab changed */
  98. UI_EVENT_TYPE_TAB_CHANGED = 14, // tb::EVENT_TYPE_TAB_CHANGED,
  99. /** Custom event. Not used internally. ref_id may be used for additional type info. */
  100. UI_EVENT_TYPE_CUSTOM = 15, // tb::EVENT_TYPE_CUSTOM
  101. /** Local Turbobadger touch events */
  102. UI_EVENT_TYPE_TOUCH_DOWN,
  103. UI_EVENT_TYPE_TOUCH_UP,
  104. UI_EVENT_TYPE_TOUCH_MOVE,
  105. UI_EVENT_TYPE_TOUCH_CANCEL
  106. };
  107. /** Defines widget z level relative to another widget, used with TBWidget::AddChildRelative. */
  108. enum UI_WIDGET_Z_REL {
  109. UI_WIDGET_Z_REL_BEFORE = 0, // tb::WIDGET_Z_REL_BEFORE, ///< Before the reference widget (visually behind reference).
  110. UI_WIDGET_Z_REL_AFTER = 1 // tb::WIDGET_Z_REL_AFTER ///< After the reference widget (visually above reference).
  111. };
  112. /// TB_TEXT_ALIGN specifies horizontal text alignment
  113. enum UI_TEXT_ALIGN
  114. {
  115. UI_TEXT_ALIGN_LEFT = 0, // tb::TB_TEXT_ALIGN_LEFT,
  116. UI_TEXT_ALIGN_RIGHT = 1, // tb::TB_TEXT_ALIGN_RIGHT,
  117. UI_TEXT_ALIGN_CENTER = 2 // tb::TB_TEXT_ALIGN_CENTER
  118. };
  119. enum UI_WIDGET_STATE {
  120. UI_WIDGET_STATE_NONE = 0, // tb::WIDGET_STATE_NONE,
  121. UI_WIDGET_STATE_DISABLED = 1, // tb::WIDGET_STATE_DISABLED,
  122. UI_WIDGET_STATE_FOCUSED = 2, // tb::WIDGET_STATE_FOCUSED,
  123. UI_WIDGET_STATE_PRESSED = 4, // tb::WIDGET_STATE_PRESSED,
  124. UI_WIDGET_STATE_SELECTED = 8, // tb::WIDGET_STATE_SELECTED,
  125. UI_WIDGET_STATE_HOVERED = 16, // tb::WIDGET_STATE_HOVERED,
  126. UI_WIDGET_STATE_ALL = 31 // tb::WIDGET_STATE_ALL
  127. };
  128. enum UI_AXIS {
  129. ///< Horizontal layout
  130. UI_AXIS_X = 0, // tb::AXIS_X,
  131. ///< Vertical layout
  132. UI_AXIS_Y = 1 // tb::AXIS_Y,
  133. };
  134. class UIView;
  135. class UILayoutParams;
  136. class UIFontDescription;
  137. /// Wraps a TurboBadger widget in our Object model
  138. class ATOMIC_API UIWidget : public Object, public tb::TBWidgetDelegate
  139. {
  140. friend class UI;
  141. ATOMIC_OBJECT(UIWidget, Object)
  142. public:
  143. UIWidget(Context* context, bool createWidget = true);
  144. virtual ~UIWidget();
  145. bool Load(const String& filename);
  146. const String& GetId();
  147. UIWidget* GetParent();
  148. UIWidget* GetContentRoot();
  149. IntRect GetRect();
  150. UIPreferredSize* GetPreferredSize();
  151. String GetText();
  152. void SetRect(IntRect r);
  153. virtual bool SetSize(int width, int height);
  154. void SetPosition(int x, int y);
  155. void SetText(const String& text);
  156. void SetSkinBg(const String& id);
  157. void SetLayoutParams(UILayoutParams* params);
  158. void SetFontDescription(UIFontDescription* fd);
  159. virtual void Remove();
  160. void RemoveChild(UIWidget* child, bool cleanup = true);
  161. void DeleteAllChildren();
  162. // String ID
  163. virtual void SetId(const String& id);
  164. void Center();
  165. void SetGravity(UI_GRAVITY gravity);
  166. void SetAxis(UI_AXIS axis);
  167. void SetValue(double value);
  168. virtual double GetValue();
  169. virtual void SetFocus();
  170. virtual bool GetFocus() const;
  171. /// Set focus to first widget which accepts it
  172. void SetFocusRecursive();
  173. void OnFocusChanged(bool focused);
  174. void SetState(UI_WIDGET_STATE state, bool on);
  175. bool GetState(UI_WIDGET_STATE state);
  176. void SetVisibility(UI_WIDGET_VISIBILITY visibility);
  177. UI_WIDGET_VISIBILITY GetVisibility();
  178. void SetStateRaw(UI_WIDGET_STATE state);
  179. UI_WIDGET_STATE GetStateRaw();
  180. void Invalidate();
  181. void Die();
  182. void SetDragObject(UIDragObject* object) { dragObject_ = object; }
  183. UIDragObject* GetDragObject() { return dragObject_; }
  184. UIWidget* GetFirstChild();
  185. UIWidget* GetNext();
  186. bool IsAncestorOf(UIWidget* widget);
  187. void SetIsFocusable(bool value);
  188. // get this or child widget with id
  189. UIWidget* GetWidget(const String& id);
  190. UIView* GetView();
  191. virtual void AddChild(UIWidget* child);
  192. void AddChildAfter(UIWidget* child, UIWidget* otherChild);
  193. void AddChildBefore(UIWidget* child, UIWidget* otherChild);
  194. /// Add the child to this widget. See AddChild for adding a child to the top or bottom.
  195. /// This takes a relative Z and insert the child before or after the given reference widget.
  196. void AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference);
  197. void InvalidateLayout();
  198. tb::TBWidget* GetInternalWidget() { return widget_; }
  199. void SetDelegate(UIWidget* widget) { widget_->SetDelegate(widget); }
  200. void SetMultiTouch(bool multiTouch) { multiTouch_ = multiTouch; }
  201. bool IsMultiTouch() { return multiTouch_; }
  202. bool GetCaptured();
  203. void SetCapturing(bool capturing);
  204. bool GetCapturing();
  205. void InvokeShortcut(const String& shortcut);
  206. bool GetShortened();
  207. void SetShortened(bool shortened);
  208. String GetTooltip();
  209. void SetTooltip(const String& text);
  210. void Enable();
  211. void Disable();
  212. // Font Description
  213. void SetFontId(const String& fontId);
  214. String GetFontId();
  215. void SetFontSize(int size);
  216. int GetFontSize();
  217. // Rect
  218. void SetX(int x) { IntRect r(GetRect()); r.right_ = x + r.Width(); r.left_ = x; SetRect(r); }
  219. int GetX() { return GetRect().left_; }
  220. void SetY(int y) { IntRect r(GetRect()); r.bottom_ = y + r.Height(); r.top_ = y; SetRect(r); }
  221. int GetY() { return GetRect().top_; }
  222. void SetWidth(int width) { IntRect r(GetRect()); r.right_ = r.left_ + width; SetRect(r); }
  223. int GetWidth() { return GetRect().Width(); }
  224. void SetHeight(int height) { IntRect r(GetRect()); r.bottom_ = r.top_ + height; SetRect(r); }
  225. int GetHeight() { return GetRect().Height(); }
  226. // Layout Params
  227. void SetLayoutWidth(int width);
  228. int GetLayoutWidth();
  229. void SetLayoutHeight(int height);
  230. int GetLayoutHeight();
  231. void SetLayoutPrefWidth(int width);
  232. int GetLayoutPrefWidth();
  233. void SetLayoutPrefHeight(int height);
  234. int GetLayoutPrefHeight();
  235. void SetLayoutMinWidth(int width);
  236. int GetLayoutMinWidth();
  237. void SetLayoutMinHeight(int height);
  238. int GetLayoutMinHeight();
  239. void SetLayoutMaxWidth(int width);
  240. int GetLayoutMaxWidth();
  241. void SetLayoutMaxHeight(int height);
  242. int GetLayoutMaxHeight();
  243. // Get x and y (relative to this widget) relative to the upper left corner of the root widget
  244. IntVector2 ConvertToRoot(const IntVector2 position ) const;
  245. // Get x and y (relative to the upper left corner of the root widget) relative to this widget
  246. IntVector2 ConvertFromRoot(const IntVector2 position) const;
  247. // Opacity and AutoOpacity (AutoOpacity sets visibility as well based on opacity being 0.0 or non-0.0).
  248. void SetOpacity(float opacity);
  249. float GetOpacity();
  250. void SetAutoOpacity(float autoOpacity);
  251. float GetAutoOpacity();
  252. protected:
  253. void ConvertEvent(UIWidget* handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data);
  254. void SetWidget(tb::TBWidget* widget);
  255. virtual bool OnEvent(const tb::TBWidgetEvent &ev);
  256. virtual void OnDelete();
  257. String id_;
  258. tb::TBWidget* widget_;
  259. SharedPtr<UIPreferredSize> preferredSize_;
  260. SharedPtr<UIDragObject> dragObject_;
  261. bool multiTouch_;
  262. };
  263. }