UIWidget.h 11 KB

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