UI.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  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 "Object.h"
  24. #include "Cursor.h"
  25. #include "UIBatch.h"
  26. namespace Urho3D
  27. {
  28. class Cursor;
  29. class Graphics;
  30. class ResourceCache;
  31. class Timer;
  32. class UIBatch;
  33. class UIElement;
  34. class VertexBuffer;
  35. class XMLElement;
  36. class XMLFile;
  37. /// %UI subsystem. Manages the graphical user interface.
  38. class URHO3D_API UI : public Object
  39. {
  40. OBJECT(UI);
  41. public:
  42. /// Construct.
  43. UI(Context* context);
  44. /// Destruct.
  45. virtual ~UI();
  46. /// Set cursor UI element.
  47. void SetCursor(Cursor* cursor);
  48. /// Set focused UI element.
  49. void SetFocusElement(UIElement* element, bool byKey = false);
  50. /// Set modal element. Until all the modal elements are dismissed, all the inputs and events are only sent to them. Return true when successful.
  51. /// Only the modal element can clear its modal status or when it is being destructed.
  52. bool SetModalElement(UIElement* modalElement, bool enable);
  53. /// Clear the UI (excluding the cursor.)
  54. void Clear();
  55. /// Update the UI logic. Called by HandlePostUpdate().
  56. void Update(float timeStep);
  57. /// Update the UI for rendering. Called by HandleRenderUpdate().
  58. void RenderUpdate();
  59. /// Render the UI.
  60. void Render();
  61. /// Debug draw a UI element.
  62. void DebugDraw(UIElement* element);
  63. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  64. SharedPtr<UIElement> LoadLayout(Deserializer& source, XMLFile* styleFile = 0);
  65. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  66. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  67. /// Save a UI layout to an XML file. Return true if successful.
  68. bool SaveLayout(Serializer& dest, UIElement* element);
  69. /// Set clipboard text.
  70. void SetClipboardText(const String& text);
  71. /// Set UI element double click interval in seconds.
  72. void SetDoubleClickInterval(float interval);
  73. /// Set UI drag event start interval in seconds.
  74. void SetDragBeginInterval(float interval);
  75. /// Set UI drag event start distance threshold in pixels.
  76. void SetDragBeginDistance(int pixels);
  77. /// Set tooltip default display delay in seconds.
  78. void SetDefaultToolTipDelay(float delay);
  79. /// Set maximum font face texture size. Must be a power of two. Default is 2048.
  80. void SetMaxFontTextureSize(int size);
  81. /// Set whether mouse wheel can control also a non-focused element.
  82. void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
  83. /// Set whether to use system clipboard. Default false.
  84. void SetUseSystemClipboard(bool enable);
  85. /// Set whether to show the on-screen keyboard (if supported) when a %LineEdit is focused. Default true on mobile devices.
  86. void SetUseScreenKeyboard(bool enable);
  87. /// Set whether to use mutable (eraseable) glyphs to ensure a font face never expands to more than one texture. Default false.
  88. void SetUseMutableGlyphs(bool enable);
  89. /// Set whether to force font autohinting instead of using FreeType's TTF bytecode interpreter.
  90. void SetForceAutoHint(bool enable);
  91. /// Return root UI element.
  92. UIElement* GetRoot() const { return rootElement_; }
  93. /// Return root modal element.
  94. UIElement* GetRootModalElement() const { return rootModalElement_; }
  95. /// Return cursor.
  96. Cursor* GetCursor() const { return cursor_; }
  97. /// Return cursor position.
  98. IntVector2 GetCursorPosition() const;
  99. /// Return UI element at screen coordinates. By default returns only input-enabled elements.
  100. UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true);
  101. /// Return UI element at screen coordinates. By default returns only input-enabled elements.
  102. UIElement* GetElementAt(int x, int y, bool enabledOnly = true);
  103. /// Return focused element.
  104. UIElement* GetFocusElement() const { return focusElement_; }
  105. /// Return topmost enabled root-level non-modal element.
  106. UIElement* GetFrontElement() const;
  107. /// Return currently dragged element.
  108. UIElement* GetDragElement() const;
  109. /// Return clipboard text.
  110. const String& GetClipboardText() const;
  111. /// Return UI element double click interval in seconds.
  112. float GetDoubleClickInterval() const { return doubleClickInterval_; }
  113. /// Return UI drag start event interval in seconds.
  114. float GetDragBeginInterval() const { return dragBeginInterval_; }
  115. /// Return UI drag start event distance threshold in pixels.
  116. int GetDragBeginDistance() const { return dragBeginDistance_; }
  117. /// Return tooltip default display delay in seconds.
  118. float GetDefaultToolTipDelay() const { return defaultToolTipDelay_; }
  119. /// Return font texture maximum size.
  120. int GetMaxFontTextureSize() const { return maxFontTextureSize_; }
  121. /// Return whether mouse wheel can control also a non-focused element.
  122. bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; }
  123. /// Return whether is using the system clipboard.
  124. bool GetUseSystemClipboard() const { return useSystemClipboard_; }
  125. /// Return whether focusing a %LineEdit will show the on-screen keyboard.
  126. bool GetUseScreenKeyboard() const { return useScreenKeyboard_; }
  127. /// Return whether is using mutable (eraseable) glyphs for fonts.
  128. bool GetUseMutableGlyphs() const { return useMutableGlyphs_; }
  129. /// Return whether is using forced autohinting.
  130. bool GetForceAutoHint() const { return forceAutoHint_; }
  131. /// Return true when UI has modal element(s).
  132. bool HasModalElement() const;
  133. private:
  134. /// Initialize when screen mode initially set.
  135. void Initialize();
  136. /// Update UI element logic recursively.
  137. void Update(float timeStep, UIElement* element);
  138. /// Upload UI geometry into a vertex buffer.
  139. void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
  140. /// Render UI batches. Geometry must have been uploaded first.
  141. void Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd);
  142. /// Generate batches from an UI element recursively. Skip the cursor element.
  143. void GetBatches(UIElement* element, IntRect currentScissor);
  144. /// Return UI element at screen position recursively.
  145. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  146. /// Return the first element in hierarchy that can alter focus.
  147. UIElement* GetFocusableElement(UIElement* element);
  148. /// Return cursor position and visibility either from the cursor element, or the Input subsystem.
  149. void GetCursorPositionAndVisible(IntVector2& pos, bool& visible);
  150. /// Set active cursor's shape.
  151. void SetCursorShape(CursorShape shape);
  152. /// Force release of font faces when global font properties change.
  153. void ReleaseFontFaces();
  154. /// Handle button or touch hover
  155. void ProcessHover(const IntVector2& cursorPos, int buttons, int qualifiers, Cursor* cursor);
  156. /// Handle button or touch begin.
  157. void ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  158. /// Handle button or touch end.
  159. void ProcessClickEnd(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  160. /// Handle mouse or touch move.
  161. void ProcessMove(const IntVector2& cursorPos, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  162. /// Send a UI element drag or hover begin event.
  163. void SendDragOrHoverEvent(StringHash eventType, UIElement* element, const IntVector2& screenPos);
  164. /// Send a UI click or double click event.
  165. void SendClickEvent(StringHash eventType, UIElement* element, const IntVector2& pos, int button, int buttons, int qualifiers);
  166. /// Handle screen mode event.
  167. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  168. /// Handle mouse button down event.
  169. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  170. /// Handle mouse button up event.
  171. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  172. /// Handle mouse move event.
  173. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  174. /// Handle mouse wheel event.
  175. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  176. /// Handle touch begin event.
  177. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  178. /// Handle touch end event.
  179. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  180. /// Handle touch move event.
  181. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  182. /// Handle keypress event.
  183. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  184. /// Handle text input event.
  185. void HandleTextInput(StringHash eventType, VariantMap& eventData);
  186. /// Handle frame begin event.
  187. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  188. /// Handle logic post-update event.
  189. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  190. /// Handle render update event.
  191. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  192. /// Handle a file being drag-dropped into the application window.
  193. void HandleDropFile(StringHash eventType, VariantMap& eventData);
  194. /// Graphics subsystem.
  195. WeakPtr<Graphics> graphics_;
  196. /// UI root element.
  197. SharedPtr<UIElement> rootElement_;
  198. /// UI root modal element.
  199. SharedPtr<UIElement> rootModalElement_;
  200. /// Cursor.
  201. SharedPtr<Cursor> cursor_;
  202. /// UI element being dragged.
  203. WeakPtr<UIElement> dragElement_;
  204. /// Currently focused element
  205. WeakPtr<UIElement> focusElement_;
  206. /// UI rendering batches.
  207. PODVector<UIBatch> batches_;
  208. /// UI rendering vertex data.
  209. PODVector<float> vertexData_;
  210. /// UI rendering batches for debug draw.
  211. PODVector<UIBatch> debugDrawBatches_;
  212. /// UI rendering vertex data for debug draw.
  213. PODVector<float> debugVertexData_;
  214. /// UI vertex buffer.
  215. SharedPtr<VertexBuffer> vertexBuffer_;
  216. /// UI debug geometry vertex buffer.
  217. SharedPtr<VertexBuffer> debugVertexBuffer_;
  218. /// UI element query vector.
  219. PODVector<UIElement*> tempElements_;
  220. /// Clipboard text.
  221. mutable String clipBoard_;
  222. /// Seconds between clicks to register a double click.
  223. float doubleClickInterval_;
  224. /// Seconds from mouse button down to begin a drag if there has been no movement exceeding pixel threshold.
  225. float dragBeginInterval_;
  226. /// Tooltip default display delay in seconds.
  227. float defaultToolTipDelay_;
  228. /// Drag begin event distance threshold in pixels.
  229. int dragBeginDistance_;
  230. /// Mouse buttons held down.
  231. int mouseButtons_;
  232. /// Last mouse button pressed.
  233. int lastMouseButtons_;
  234. /// Qualifier keys held down.
  235. int qualifiers_;
  236. /// Font texture maximum size.
  237. int maxFontTextureSize_;
  238. /// Initialized flag.
  239. bool initialized_;
  240. /// Touch used flag.
  241. bool usingTouchInput_;
  242. /// Flag to switch mouse wheel event to be sent to non-focused element at cursor.
  243. bool nonFocusedMouseWheel_;
  244. /// Flag for using operating system clipboard instead of internal.
  245. bool useSystemClipboard_;
  246. /// Flag for showing the on-screen keyboard on focusing a %LineEdit.
  247. bool useScreenKeyboard_;
  248. /// Flag for using mutable (eraseable) font glyphs.
  249. bool useMutableGlyphs_;
  250. /// Flag for forcing FreeType autohinting.
  251. bool forceAutoHint_;
  252. /// Flag for a drag start event pending.
  253. bool dragBeginPending_;
  254. /// Non-modal batch size (used internally for rendering).
  255. unsigned nonModalBatchSize_;
  256. /// Timer used to trigger double click.
  257. Timer clickTimer_;
  258. /// Timer used to trigger drag begin event.
  259. Timer dragBeginTimer_;
  260. /// Drag start position.
  261. IntVector2 dragBeginPos_;
  262. /// Timer used for drag begin.
  263. /// UI element last clicked for tracking click end.
  264. WeakPtr<UIElement> clickElement_;
  265. /// UI element last clicked for tracking double clicks.
  266. WeakPtr<UIElement> doubleClickElement_;
  267. /// Currently hovered elements.
  268. HashMap<WeakPtr<UIElement>, bool> hoveredElements_;
  269. };
  270. /// Register UI library objects.
  271. void URHO3D_API RegisterUILibrary(Context* context);
  272. }