UI.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (c) 2008-2013 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);
  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. /// UI subystem auto-removes modal element when an ESC key is pressed, however if this is not desirable, setting a user-defined variable "NoAutoRemove" in the modal element would prevent this.
  53. /// In that case, the modal element will only have its modal flag reset and reparented back to its original parent.
  54. bool SetModalElement(UIElement* modalElement, bool enable);
  55. /// Clear the UI (excluding the cursor.)
  56. void Clear();
  57. /// Update the UI logic. Called by HandlePostUpdate().
  58. void Update(float timeStep);
  59. /// Update the UI for rendering. Called by HandleRenderUpdate().
  60. void RenderUpdate();
  61. /// Render the UI.
  62. void Render();
  63. /// Debug draw a UI element.
  64. void DebugDraw(UIElement* element);
  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(Deserializer& source, XMLFile* styleFile = 0);
  67. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  68. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  69. /// Save a UI layout to an XML file. Return true if successful.
  70. bool SaveLayout(Serializer& dest, UIElement* element);
  71. /// Set clipboard text.
  72. void SetClipBoardText(const String& text);
  73. /// Set UI element double click interval in seconds.
  74. void SetDoubleClickInterval(float interval);
  75. /// Set maximum font face texture size. Must be a power of two. Default is 2048.
  76. void SetMaxFontTextureSize(int size);
  77. /// Set whether mouse wheel can control also a non-focused element.
  78. void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
  79. /// Set whether to use system clipboard. Default false.
  80. void SetUseSystemClipBoard(bool enable);
  81. /// Set whether to use mutable (eraseable) glyphs to ensure a font face never expands to more than one texture. Default false.
  82. void SetUseMutableGlyphs(bool enable);
  83. /// Set whether to force font autohinting instead of using FreeType's TTF bytecode interpreter.
  84. void SetForceAutoHint(bool enable);
  85. /// Return root UI element.
  86. UIElement* GetRoot() const { return rootElement_; }
  87. /// Return root modal element.
  88. UIElement* GetRootModalElement() const { return rootModalElement_; }
  89. /// Return cursor.
  90. Cursor* GetCursor() const { return cursor_; }
  91. /// Return cursor position.
  92. IntVector2 GetCursorPosition() const;
  93. /// Return UI element at screen coordinates.
  94. UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true);
  95. /// Return UI element at screen coordinates.
  96. UIElement* GetElementAt(int x, int y, bool enabledOnly = true);
  97. /// Return focused element.
  98. UIElement* GetFocusElement() const { return focusElement_; }
  99. /// Return topmost enabled root-level non-modal element.
  100. UIElement* GetFrontElement() const;
  101. /// Return clipboard text.
  102. const String& GetClipBoardText() const;
  103. /// Return UI element double click interval in seconds.
  104. float GetDoubleClickInterval() const { return doubleClickInterval_; }
  105. /// Return font texture maximum size.
  106. int GetMaxFontTextureSize() const { return maxFontTextureSize_; }
  107. /// Return whether mouse wheel can control also a non-focused element.
  108. bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; }
  109. /// Return whether is using the system clipboard.
  110. bool GetUseSystemClipBoard() const { return useSystemClipBoard_; }
  111. /// Return whether is using mutable (eraseable) glyphs for fonts.
  112. bool GetUseMutableGlyphs() const { return useMutableGlyphs_; }
  113. /// Return whether is using forced autohinting.
  114. bool GetForceAutoHint() const { return forceAutoHint_; }
  115. /// Return true when UI has modal element(s).
  116. bool HasModalElement() const;
  117. private:
  118. /// Initialize when screen mode initially set.
  119. void Initialize();
  120. /// Update UI element logic recursively.
  121. void Update(float timeStep, UIElement* element);
  122. /// Upload UI geometry into a vertex buffer.
  123. void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
  124. /// Render UI batches. Geometry must have been uploaded first.
  125. void Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd);
  126. /// Generate batches from an UI element recursively. Skip the cursor element.
  127. void GetBatches(UIElement* element, IntRect currentScissor);
  128. /// Return UI element at screen position recursively.
  129. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  130. /// Return the first element in hierarchy that can alter focus.
  131. UIElement* GetFocusableElement(UIElement* element);
  132. /// Return cursor position and visibility either from the cursor element, or the Input subsystem.
  133. void GetCursorPositionAndVisible(IntVector2& pos, bool& visible);
  134. /// Set active cursor's shape.
  135. void SetCursorShape(CursorShape shape);
  136. /// Force release of font faces when global font properties change.
  137. void ReleaseFontFaces();
  138. /// Handle button or touch begin.
  139. void ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  140. /// Handle button or touch end.
  141. void ProcessClickEnd(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  142. /// Handle mouse or touch move.
  143. void ProcessMove(const IntVector2& cursorPos, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  144. /// Send a UI element drag event.
  145. void SendDragEvent(StringHash eventType, UIElement* element, const IntVector2& screenPos);
  146. /// Send a UI click or double click event.
  147. void SendClickEvent(StringHash eventType, UIElement* element, const IntVector2& pos, int button, int buttons, int qualifiers);
  148. /// Handle screen mode event.
  149. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  150. /// Handle mouse button down event.
  151. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  152. /// Handle mouse button up event.
  153. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  154. /// Handle mouse move event.
  155. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  156. /// Handle mouse wheel event.
  157. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  158. /// Handle touch begin event.
  159. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  160. /// Handle touch end event.
  161. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  162. /// Handle touch move event.
  163. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  164. /// Handle keypress event.
  165. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  166. /// Handle character event.
  167. void HandleChar(StringHash eventType, VariantMap& eventData);
  168. /// Handle logic post-update event.
  169. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  170. /// Handle render update event.
  171. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  172. /// Handle a file being drag-dropped into the application window.
  173. void HandleDropFile(StringHash eventType, VariantMap& eventData);
  174. /// Graphics subsystem.
  175. WeakPtr<Graphics> graphics_;
  176. /// Vertex shader for no texture.
  177. SharedPtr<ShaderVariation> noTextureVS_;
  178. /// Vertex shader for diffuse texture.
  179. SharedPtr<ShaderVariation> diffTextureVS_;
  180. /// Pixel shader for no texture.
  181. SharedPtr<ShaderVariation> noTexturePS_;
  182. /// Pixel shader for diffuse texture.
  183. SharedPtr<ShaderVariation> diffTexturePS_;
  184. /// Pixel shader for diffuse texture masking.
  185. SharedPtr<ShaderVariation> diffMaskTexturePS_;
  186. /// Pixel shader for alpha texture.
  187. SharedPtr<ShaderVariation> alphaTexturePS_;
  188. /// UI root element.
  189. SharedPtr<UIElement> rootElement_;
  190. /// UI root modal element.
  191. SharedPtr<UIElement> rootModalElement_;
  192. /// Cursor.
  193. SharedPtr<Cursor> cursor_;
  194. /// UI element being dragged.
  195. WeakPtr<UIElement> dragElement_;
  196. /// Currently focused element
  197. WeakPtr<UIElement> focusElement_;
  198. /// UI rendering batches.
  199. PODVector<UIBatch> batches_;
  200. /// UI rendering vertex data.
  201. PODVector<float> vertexData_;
  202. /// UI rendering batches for debug draw.
  203. PODVector<UIBatch> debugDrawBatches_;
  204. /// UI rendering vertex data for debug draw.
  205. PODVector<float> debugVertexData_;
  206. /// UI vertex buffer.
  207. SharedPtr<VertexBuffer> vertexBuffer_;
  208. /// UI debug geometry vertex buffer.
  209. SharedPtr<VertexBuffer> debugVertexBuffer_;
  210. /// UI element query vector.
  211. PODVector<UIElement*> tempElements_;
  212. /// Clipboard text.
  213. mutable String clipBoard_;
  214. /// Mouse buttons held down.
  215. int mouseButtons_;
  216. /// Qualifier keys held down.
  217. int qualifiers_;
  218. /// Font texture maximum size.
  219. int maxFontTextureSize_;
  220. /// Initialized flag.
  221. bool initialized_;
  222. /// Touch used flag.
  223. bool usingTouchInput_;
  224. /// Flag to switch mouse wheel event to be sent to non-focused element at cursor.
  225. bool nonFocusedMouseWheel_;
  226. /// Flag for using operating system clipboard instead of internal.
  227. bool useSystemClipBoard_;
  228. /// Flag for using mutable (eraseable) font glyphs.
  229. bool useMutableGlyphs_;
  230. /// Flag for forcing FreeType autohinting.
  231. bool forceAutoHint_;
  232. /// Non-modal batch size (used internally for rendering).
  233. unsigned nonModalBatchSize_;
  234. /// Timer used to trigger double click.
  235. Timer* clickTimer_;
  236. /// UI element last clicked for tracking click end.
  237. WeakPtr<UIElement> clickElement_;
  238. /// UI element last clicked for tracking double clicks.
  239. WeakPtr<UIElement> doubleClickElement_;
  240. /// Last mouse button pressed.
  241. int lastMouseButtons_;
  242. /// Seconds between clicks to register a double click.
  243. float doubleClickInterval_;
  244. };
  245. /// Register UI library objects.
  246. void URHO3D_API RegisterUILibrary(Context* context);
  247. }