UI.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 it is dismissed, all the inputs and events are only sent to this modal element. Return true when successful. Only the current modal element can clear its modal status.
  49. bool SetModalElement(UIElement* modalElement, bool enable);
  50. /// Clear the UI (excluding the cursor.)
  51. void Clear();
  52. /// Update the UI logic. Called by HandlePostUpdate().
  53. void Update(float timeStep);
  54. /// Update the UI for rendering. Called by HandleRenderUpdate().
  55. void RenderUpdate();
  56. /// Render the UI.
  57. void Render();
  58. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  59. SharedPtr<UIElement> LoadLayout(Deserializer& source, XMLFile* styleFile = 0);
  60. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  61. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  62. /// Save a UI layout to an XML file. Return true if successful.
  63. bool SaveLayout(Serializer& dest, UIElement* element);
  64. /// Set clipboard text.
  65. void SetClipBoardText(const String& text);
  66. /// Set mouse wheel handling flag.
  67. void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
  68. /// Return root UI element.
  69. UIElement* GetRoot() const { return rootElement_; }
  70. /// Return cursor.
  71. Cursor* GetCursor() const { return cursor_; }
  72. /// Return UI element at screen coordinates.
  73. UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true);
  74. /// Return UI element at screen coordinates.
  75. UIElement* GetElementAt(int x, int y, bool enabledOnly = true);
  76. /// Return focused element.
  77. UIElement* GetFocusElement() const { return focusElement_; }
  78. /// Return modal element.
  79. UIElement* GetModalElement() const { return modalElement_; }
  80. /// Return topmost enabled root-level element.
  81. UIElement* GetFrontElement() const;
  82. /// Return cursor position.
  83. IntVector2 GetCursorPosition();
  84. /// Return clipboard text.
  85. const String& GetClipBoardText() const { return clipBoard_; }
  86. /// Return mouse wheel handling flag.
  87. bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; }
  88. private:
  89. /// Initialize when screen mode initially se.
  90. void Initialize();
  91. /// Update UI element logic recursively.
  92. void Update(float timeStep, UIElement* element);
  93. /// Generate batches from an UI element recursively.
  94. void GetBatches(UIElement* element, IntRect currentScissor);
  95. /// Return UI element at screen position recursively.
  96. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  97. /// Return the first element in hierarchy that can alter focus.
  98. UIElement* GetFocusableElement(UIElement* element);
  99. /// Handle screen mode event.
  100. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  101. /// Handle mouse button down event.
  102. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  103. /// Handle mouse button up event.
  104. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  105. /// Handle mouse move event.
  106. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  107. /// Handle mouse wheel event.
  108. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  109. /// Handle touch begin event.
  110. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  111. /// Handle touch end event.
  112. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  113. /// Handle touch move event.
  114. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  115. /// Handle keypress event.
  116. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  117. /// Handle character event.
  118. void HandleChar(StringHash eventType, VariantMap& eventData);
  119. /// Handle logic post-update event.
  120. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  121. /// Handle render update event.
  122. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  123. /// Graphics subsystem.
  124. WeakPtr<Graphics> graphics_;
  125. /// Vertex shader for no texture.
  126. SharedPtr<ShaderVariation> noTextureVS_;
  127. /// Vertex shader for diffuse texture.
  128. SharedPtr<ShaderVariation> diffTextureVS_;
  129. /// Pixel shader for no texture.
  130. SharedPtr<ShaderVariation> noTexturePS_;
  131. /// Pixel shader for diffuse texture.
  132. SharedPtr<ShaderVariation> diffTexturePS_;
  133. /// Pixel shader for diffuse texture masking.
  134. SharedPtr<ShaderVariation> diffMaskTexturePS_;
  135. /// Pixel shader for alpha texture.
  136. SharedPtr<ShaderVariation> alphaTexturePS_;
  137. /// UI root element.
  138. SharedPtr<UIElement> rootElement_;
  139. /// Cursor.
  140. SharedPtr<Cursor> cursor_;
  141. /// UI element being dragged.
  142. WeakPtr<UIElement> dragElement_;
  143. /// Currently focused element
  144. WeakPtr<UIElement> focusElement_;
  145. /// Modal element.
  146. WeakPtr<UIElement> modalElement_;
  147. /// UI rendering batches.
  148. PODVector<UIBatch> batches_;
  149. /// UI rendering vertex data.
  150. PODVector<float> vertexData_;
  151. /// UI vertex buffer.
  152. SharedPtr<VertexBuffer> vertexBuffer_;
  153. /// UI element query vector.
  154. PODVector<UIElement*> tempElements_;
  155. /// Clipboard text.
  156. String clipBoard_;
  157. /// Mouse buttons held down.
  158. int mouseButtons_;
  159. /// Qualifier keys held down.
  160. int qualifiers_;
  161. /// Initialized flag.
  162. bool initialized_;
  163. /// Flag to switch mouse wheel event to be sent to non-focused element at cursor.
  164. bool nonFocusedMouseWheel_;
  165. };
  166. /// Register UI library objects.
  167. void RegisterUILibrary(Context* context);
  168. }