UI.h 7.1 KB

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