UI.h 8.4 KB

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