UI.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Object.h"
  25. #include "UIBatch.h"
  26. class Cursor;
  27. class Graphics;
  28. class ResourceCache;
  29. class UIBatch;
  30. class UIElement;
  31. class VertexBuffer;
  32. class XMLElement;
  33. class XMLFile;
  34. /// %UI subsystem. Manages the graphical user interface.
  35. class UI : public Object
  36. {
  37. OBJECT(UI);
  38. public:
  39. /// Construct.
  40. UI(Context* context);
  41. /// Destruct.
  42. virtual ~UI();
  43. /// %Set cursor UI element.
  44. void SetCursor(Cursor* cursor);
  45. /// %Set focused UI element.
  46. void SetFocusElement(UIElement* element);
  47. /// Clear the UI (excluding the cursor.)
  48. void Clear();
  49. /// Update the UI logic. Called by HandlePostUpdate().
  50. void Update(float timeStep);
  51. /// Update the UI for rendering. Called by HandleRenderUpdate().
  52. void RenderUpdate();
  53. /// Render the UI.
  54. void Render();
  55. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  56. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  57. /// %Set clipboard text.
  58. void SetClipBoardText(const String& text);
  59. /// Return root UI element.
  60. UIElement* GetRoot() const { return rootElement_; }
  61. /// Return cursor.
  62. Cursor* GetCursor() const { return cursor_; }
  63. /// Return UI element at screen coordinates.
  64. UIElement* GetElementAt(const IntVector2& position, bool activeOnly = true);
  65. /// Return UI element at screen coordinates.
  66. UIElement* GetElementAt(int x, int y, bool activeOnly = true);
  67. /// Return focused element.
  68. UIElement* GetFocusElement() const;
  69. /// Return topmost enabled root-level element.
  70. UIElement* GetFrontElement() const;
  71. /// Return cursor position.
  72. IntVector2 GetCursorPosition();
  73. /// Return clipboard text.
  74. const String& GetClipBoardText() const { return clipBoard_; }
  75. private:
  76. /// Initialize when screen mode initially se.
  77. void Initialize();
  78. /// Update UI element logic recursively.
  79. void Update(float timeStep, UIElement* element);
  80. /// Generate batches from an UI element recursively.
  81. void GetBatches(UIElement* element, IntRect currentScissor);
  82. /// Return whether element is visible and within the scissor rectangle
  83. bool IsVisible(UIElement* element, const IntRect& currentScissor);
  84. /// Return UI element at screen position recursively.
  85. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool activeOnly);
  86. /// Return the first element in hierarchy that can alter focus.
  87. UIElement* GetFocusableElement(UIElement* element);
  88. /// Load a UI layout from an XML file recursively.
  89. void LoadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFile);
  90. /// Handle screen mode event.
  91. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  92. /// Handle mouse move event.
  93. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  94. /// Handle mouse button down event.
  95. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  96. /// Handle mouse button up event.
  97. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  98. /// Handle mouse wheel event.
  99. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  100. /// Handle keypress event.
  101. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  102. /// Handle character event.
  103. void HandleChar(StringHash eventType, VariantMap& eventData);
  104. /// Handle logic post-update event.
  105. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  106. /// Handle render update event.
  107. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  108. /// Graphics subsystem.
  109. WeakPtr<Graphics> graphics_;
  110. /// Resource cache subsystem.
  111. WeakPtr<ResourceCache> cache_;
  112. /// Vertex shader for no texture.
  113. SharedPtr<ShaderVariation> noTextureVS_;
  114. /// Vertex shader for diffuse texture.
  115. SharedPtr<ShaderVariation> diffTextureVS_;
  116. /// Pixel shader for no texture.
  117. SharedPtr<ShaderVariation> noTexturePS_;
  118. /// Pixel shader for diffuse texture.
  119. SharedPtr<ShaderVariation> diffTexturePS_;
  120. /// Pixel shader for alpha texture.
  121. SharedPtr<ShaderVariation> alphaTexturePS_;
  122. /// UI root element.
  123. SharedPtr<UIElement> rootElement_;
  124. /// Cursor.
  125. SharedPtr<Cursor> cursor_;
  126. /// UI element being dragged.
  127. WeakPtr<UIElement> dragElement_;
  128. /// Currently focused element
  129. WeakPtr<UIElement> focusElement_;
  130. /// UI rendering batches.
  131. PODVector<UIBatch> batches_;
  132. /// UI rendering quads.
  133. PODVector<UIQuad> quads_;
  134. /// UI vertex buffer.
  135. SharedPtr<VertexBuffer> vertexBuffer_;
  136. /// UI element query vector.
  137. PODVector<UIElement*> tempElements_;
  138. /// Clipboard text.
  139. String clipBoard_;
  140. /// Mouse buttons held down.
  141. int mouseButtons_;
  142. /// Qualifier keys held down.
  143. int qualifiers_;
  144. /// Initialized flag.
  145. bool initialized_;
  146. };
  147. /// Register UI library objects.
  148. void RegisterUILibrary(Context* context);