UIWidget.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. class UISelectItemSource;
  138. /// Wraps a TurboBadger widget in our Object model
  139. class ATOMIC_API UIWidget : public Object, public tb::TBWidgetDelegate
  140. {
  141. friend class UI;
  142. ATOMIC_OBJECT(UIWidget, Object)
  143. public:
  144. UIWidget(Context* context, bool createWidget = true);
  145. virtual ~UIWidget();
  146. bool Load(const String& filename);
  147. const String& GetId();
  148. UIWidget* GetParent();
  149. UIWidget* GetContentRoot();
  150. IntRect GetRect();
  151. UIPreferredSize* GetPreferredSize();
  152. String GetText();
  153. void SetRect(IntRect r);
  154. virtual bool SetSize(int width, int height);
  155. void SetPosition(int x, int y);
  156. void SetText(const String& text);
  157. void SetSkinBg(const String& id);
  158. void SetLayoutParams(UILayoutParams* params);
  159. void SetFontDescription(UIFontDescription* fd);
  160. virtual void Remove();
  161. virtual void RemoveChild(UIWidget* child, bool cleanup = true);
  162. virtual void DeleteAllChildren();
  163. /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
  164. virtual UIWidget *FindWidget ( const String& searchid );
  165. /// return all of the widgets of the specified classname and id that is not 0 from the current widget
  166. virtual void SearchWidgetClass ( const String& className, PODVector<UIWidget*> &results );
  167. /// return all of the widgets of the specified id and id that is not 0 from the current widget
  168. virtual void SearchWidgetId ( const String& searchid, PODVector<UIWidget*> &results );
  169. /// return all of the widgets with the specified text and id that is not 0 from the current widget
  170. virtual void SearchWidgetText ( const String& searchText, PODVector<UIWidget*> &results );
  171. /// print out the widget tree to stdout from the current widget
  172. virtual void PrintPrettyTree();
  173. // String ID
  174. virtual void SetId(const String& id);
  175. void Center();
  176. void SetGravity(UI_GRAVITY gravity);
  177. void SetAxis(UI_AXIS axis);
  178. void SetValue(double value);
  179. virtual double GetValue();
  180. virtual void SetFocus();
  181. virtual bool GetFocus() const;
  182. /// Set focus to first widget which accepts it
  183. void SetFocusRecursive();
  184. void OnFocusChanged(bool focused);
  185. void SetState(UI_WIDGET_STATE state, bool on);
  186. bool GetState(UI_WIDGET_STATE state);
  187. void SetVisibility(UI_WIDGET_VISIBILITY visibility);
  188. UI_WIDGET_VISIBILITY GetVisibility();
  189. void SetStateRaw(UI_WIDGET_STATE state);
  190. UI_WIDGET_STATE GetStateRaw();
  191. void Invalidate();
  192. void Die();
  193. void SetDragObject(UIDragObject* object) { dragObject_ = object; }
  194. UIDragObject* GetDragObject() { return dragObject_; }
  195. virtual UIWidget* GetFirstChild();
  196. UIWidget* GetNext();
  197. bool IsAncestorOf(UIWidget* widget);
  198. void SetIsFocusable(bool value);
  199. // get this or child widget with id
  200. virtual UIWidget* GetWidget(const String& id);
  201. UIView* GetView();
  202. virtual void AddChild(UIWidget* child);
  203. virtual void AddChildAfter(UIWidget* child, UIWidget* otherChild);
  204. virtual void AddChildBefore(UIWidget* child, UIWidget* otherChild);
  205. /// Add the child to this widget. See AddChild for adding a child to the top or bottom.
  206. /// This takes a relative Z and insert the child before or after the given reference widget.
  207. virtual void AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference);
  208. void InvalidateLayout();
  209. tb::TBWidget* GetInternalWidget() { return widget_; }
  210. void SetDelegate(UIWidget* widget) { widget_->SetDelegate(widget); }
  211. void SetMultiTouch(bool multiTouch) { multiTouch_ = multiTouch; }
  212. bool IsMultiTouch() { return multiTouch_; }
  213. bool GetCaptured();
  214. void SetCapturing(bool capturing);
  215. bool GetCapturing();
  216. void InvokeShortcut(const String& shortcut);
  217. bool GetShortened();
  218. void SetShortened(bool shortened);
  219. String GetTooltip();
  220. void SetTooltip(const String& text);
  221. void Enable();
  222. void Disable();
  223. // Font Description
  224. void SetFontId(const String& fontId);
  225. String GetFontId();
  226. void SetFontSize(int size);
  227. int GetFontSize();
  228. // Rect
  229. void SetX(int x) { IntRect r(GetRect()); r.right_ = x + r.Width(); r.left_ = x; SetRect(r); }
  230. int GetX() { return GetRect().left_; }
  231. void SetY(int y) { IntRect r(GetRect()); r.bottom_ = y + r.Height(); r.top_ = y; SetRect(r); }
  232. int GetY() { return GetRect().top_; }
  233. void SetWidth(int width) { IntRect r(GetRect()); r.right_ = r.left_ + width; SetRect(r); }
  234. int GetWidth() { return GetRect().Width(); }
  235. void SetHeight(int height) { IntRect r(GetRect()); r.bottom_ = r.top_ + height; SetRect(r); }
  236. int GetHeight() { return GetRect().Height(); }
  237. // Layout Params
  238. void SetLayoutWidth(int width);
  239. int GetLayoutWidth();
  240. void SetLayoutHeight(int height);
  241. int GetLayoutHeight();
  242. void SetLayoutPrefWidth(int width);
  243. int GetLayoutPrefWidth();
  244. void SetLayoutPrefHeight(int height);
  245. int GetLayoutPrefHeight();
  246. void SetLayoutMinWidth(int width);
  247. int GetLayoutMinWidth();
  248. void SetLayoutMinHeight(int height);
  249. int GetLayoutMinHeight();
  250. void SetLayoutMaxWidth(int width);
  251. int GetLayoutMaxWidth();
  252. void SetLayoutMaxHeight(int height);
  253. int GetLayoutMaxHeight();
  254. // Get x and y (relative to this widget) relative to the upper left corner of the root widget
  255. IntVector2 ConvertToRoot(const IntVector2 position ) const;
  256. // Get x and y (relative to the upper left corner of the root widget) relative to this widget
  257. IntVector2 ConvertFromRoot(const IntVector2 position) const;
  258. // Opacity and AutoOpacity (AutoOpacity sets visibility as well based on opacity being 0.0 or non-0.0).
  259. void SetOpacity(float opacity);
  260. float GetOpacity();
  261. void SetAutoOpacity(float autoOpacity);
  262. float GetAutoOpacity();
  263. protected:
  264. void ConvertEvent(UIWidget* handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data);
  265. void SetWidget(tb::TBWidget* widget);
  266. virtual bool OnEvent(const tb::TBWidgetEvent &ev);
  267. virtual void OnDelete();
  268. virtual void OnResized(int old_w, int old_h);
  269. String id_;
  270. tb::TBWidget* widget_;
  271. SharedPtr<UIPreferredSize> preferredSize_;
  272. SharedPtr<UIDragObject> dragObject_;
  273. bool multiTouch_;
  274. };
  275. }