UI.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 UIBatch;
  32. class UIElement;
  33. class VertexBuffer;
  34. class XMLElement;
  35. class XMLFile;
  36. /// %UI subsystem. Manages the graphical user interface.
  37. class UI : public Object
  38. {
  39. OBJECT(UI);
  40. public:
  41. /// Construct.
  42. UI(Context* context);
  43. /// Destruct.
  44. virtual ~UI();
  45. /// Set cursor UI element.
  46. void SetCursor(Cursor* cursor);
  47. /// Set focused UI element.
  48. void SetFocusElement(UIElement* element);
  49. /// Set modal element. Until all the modal elements are dismissed, all the inputs and events are only sent to them. Return true when successful.
  50. /// Only the modal element can clear its modal status or when it is being destructed.
  51. /// 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.
  52. /// In that case, the modal element will only have its modal flag reset and reparented back to its original parent.
  53. bool SetModalElement(UIElement* modalElement, bool enable);
  54. /// Clear the UI (excluding the cursor.)
  55. void Clear();
  56. /// Update the UI logic. Called by HandlePostUpdate().
  57. void Update(float timeStep);
  58. /// Update the UI for rendering. Called by HandleRenderUpdate().
  59. void RenderUpdate();
  60. /// Render the UI.
  61. void Render();
  62. /// Debug draw a UI element.
  63. void DebugDraw(UIElement* element);
  64. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  65. SharedPtr<UIElement> LoadLayout(Deserializer& source, XMLFile* styleFile = 0);
  66. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  67. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  68. /// Save a UI layout to an XML file. Return true if successful.
  69. bool SaveLayout(Serializer& dest, UIElement* element);
  70. /// Set clipboard text.
  71. void SetClipBoardText(const String& text);
  72. /// Set mouse wheel handling flag.
  73. void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
  74. /// Return root UI element.
  75. UIElement* GetRoot() const { return rootElement_; }
  76. /// Return root modal element.
  77. UIElement* GetRootModalElement() const { return rootModalElement_; }
  78. /// Return cursor.
  79. Cursor* GetCursor() const { return cursor_; }
  80. /// Return UI element at screen coordinates.
  81. UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true);
  82. /// Return UI element at screen coordinates.
  83. UIElement* GetElementAt(int x, int y, bool enabledOnly = true);
  84. /// Return focused element.
  85. UIElement* GetFocusElement() const { return focusElement_; }
  86. /// Return topmost enabled root-level non-modal element.
  87. UIElement* GetFrontElement() const;
  88. /// Return cursor position.
  89. IntVector2 GetCursorPosition() const;
  90. /// Return clipboard text.
  91. const String& GetClipBoardText() const { return clipBoard_; }
  92. /// Return mouse wheel handling flag.
  93. bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; }
  94. /// Return true when UI has modal element(s).
  95. bool HasModalElement() const;
  96. private:
  97. /// Initialize when screen mode initially se.
  98. void Initialize();
  99. /// Update UI element logic recursively.
  100. void Update(float timeStep, UIElement* element);
  101. /// Render the batches.
  102. void Render(const PODVector<UIBatch>& batches, const PODVector<float>& vertexData, unsigned batchStart, unsigned batchSize);
  103. /// Generate batches from an UI element recursively.
  104. void GetBatches(UIElement* element, IntRect currentScissor);
  105. /// Return UI element at screen position recursively.
  106. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  107. /// Return the first element in hierarchy that can alter focus.
  108. UIElement* GetFocusableElement(UIElement* element);
  109. /// Return cursor position and visibility either from the cursor element, or the Input subsystem.
  110. void GetCursorPositionAndVisible(IntVector2& pos, bool& visible);
  111. /// Set cursor shape if it exists.
  112. void SetCursorShape(CursorShape shape);
  113. /// Handle screen mode event.
  114. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  115. /// Handle mouse button down event.
  116. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  117. /// Handle mouse button up event.
  118. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  119. /// Handle mouse move event.
  120. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  121. /// Handle mouse wheel event.
  122. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  123. /// Handle touch begin event.
  124. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  125. /// Handle touch end event.
  126. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  127. /// Handle touch move event.
  128. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  129. /// Handle keypress event.
  130. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  131. /// Handle character event.
  132. void HandleChar(StringHash eventType, VariantMap& eventData);
  133. /// Handle logic post-update event.
  134. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  135. /// Handle render update event.
  136. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  137. /// Graphics subsystem.
  138. WeakPtr<Graphics> graphics_;
  139. /// Vertex shader for no texture.
  140. SharedPtr<ShaderVariation> noTextureVS_;
  141. /// Vertex shader for diffuse texture.
  142. SharedPtr<ShaderVariation> diffTextureVS_;
  143. /// Pixel shader for no texture.
  144. SharedPtr<ShaderVariation> noTexturePS_;
  145. /// Pixel shader for diffuse texture.
  146. SharedPtr<ShaderVariation> diffTexturePS_;
  147. /// Pixel shader for diffuse texture masking.
  148. SharedPtr<ShaderVariation> diffMaskTexturePS_;
  149. /// Pixel shader for alpha texture.
  150. SharedPtr<ShaderVariation> alphaTexturePS_;
  151. /// UI root element.
  152. SharedPtr<UIElement> rootElement_;
  153. /// UI root modal element.
  154. SharedPtr<UIElement> rootModalElement_;
  155. /// Cursor.
  156. SharedPtr<Cursor> cursor_;
  157. /// UI element being dragged.
  158. WeakPtr<UIElement> dragElement_;
  159. /// Currently focused element
  160. WeakPtr<UIElement> focusElement_;
  161. /// UI rendering batches.
  162. PODVector<UIBatch> batches_;
  163. /// UI rendering vertex data.
  164. PODVector<float> vertexData_;
  165. /// UI rendering batches for debug draw.
  166. PODVector<UIBatch> debugDrawBatches_;
  167. /// UI rendering vertex data for debug draw.
  168. PODVector<float> debugDrawVertexData_;
  169. /// UI vertex buffer.
  170. SharedPtr<VertexBuffer> vertexBuffer_;
  171. /// UI element query vector.
  172. PODVector<UIElement*> tempElements_;
  173. /// Clipboard text.
  174. String clipBoard_;
  175. /// Mouse buttons held down.
  176. int mouseButtons_;
  177. /// Qualifier keys held down.
  178. int qualifiers_;
  179. /// Initialized flag.
  180. bool initialized_;
  181. /// Flag to switch mouse wheel event to be sent to non-focused element at cursor.
  182. bool nonFocusedMouseWheel_;
  183. /// Non-modal batch size (used internally for rendering).
  184. unsigned nonModalBatchSize_;
  185. };
  186. /// Register UI library objects.
  187. void RegisterUILibrary(Context* context);
  188. }