UI.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 XMLElement;
  32. class XMLFile;
  33. /// UI subsystem. Manages the graphical user interface
  34. class UI : public Object
  35. {
  36. OBJECT(UI);
  37. public:
  38. /// Construct
  39. UI(Context* context);
  40. /// Destruct
  41. virtual ~UI();
  42. /// Set cursor UI element
  43. void SetCursor(Cursor* cursor);
  44. /// Set focused UI element
  45. void SetFocusElement(UIElement* element);
  46. /// Clear the UI (excluding the cursor)
  47. void Clear();
  48. /// Update the UI logic. Called by HandlePostUpdate()
  49. void Update(float timeStep);
  50. /// Update the UI for rendering. Called by HandleRenderUpdate()
  51. void RenderUpdate();
  52. /// Render the UI
  53. void Render();
  54. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element
  55. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  56. /// Set clipboard text
  57. void SetClipBoardText(const std::string& text);
  58. /// Return root UI elemenet
  59. UIElement* GetRootElement() const { return rootElement_; }
  60. /// Return cursor
  61. Cursor* GetCursor() const { return cursor_; }
  62. /// Return UI element at screen coordinates
  63. UIElement* GetElementAt(const IntVector2& position, bool activeOnly = true);
  64. /// Return UI element at screen coordinates
  65. UIElement* GetElementAt(int x, int y, bool activeOnly = true);
  66. /// Return focused element
  67. UIElement* GetFocusElement() const;
  68. /// Return topmost enabled root-level element
  69. UIElement* GetFrontElement() const;
  70. /// Return cursor position
  71. IntVector2 GetCursorPosition();
  72. /// Return clipboard text
  73. const std::string& GetClipBoardText() const { return clipBoard_; }
  74. private:
  75. /// Initialize when screen mode initially set
  76. void Initialize();
  77. /// Update UI element logic recursively
  78. void Update(float timeStep, UIElement* element);
  79. /// Generate batches from an UI element recursively
  80. void GetBatches(UIElement* element, IntRect currentScissor);
  81. /// Return UI element at screen position recursively
  82. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool activeOnly);
  83. /// Load a UI layout from an XML file recursively
  84. void LoadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFile);
  85. /// Handle screen mode event
  86. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  87. /// Handle mouse move event
  88. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  89. /// Handle mouse button down event
  90. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  91. /// Handle mouse button up event
  92. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  93. /// Handle mouse wheel event
  94. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  95. /// Handle keypress event
  96. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  97. /// Handle character event
  98. void HandleChar(StringHash eventType, VariantMap& eventData);
  99. /// Handle logic post-update event
  100. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  101. /// Handle render update event
  102. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  103. /// Graphics
  104. WeakPtr<Graphics> graphics_;
  105. /// Resource cache
  106. WeakPtr<ResourceCache> cache_;
  107. /// Vertex shader for no texture
  108. SharedPtr<VertexShader> noTextureVS_;
  109. /// Vertex shader for diffuse texture
  110. SharedPtr<VertexShader> diffTextureVS_;
  111. /// Pixel shader for no texture
  112. SharedPtr<PixelShader> noTexturePS_;
  113. /// Pixel shader for diffuse texture
  114. SharedPtr<PixelShader> diffTexturePS_;
  115. /// Pixel shader for alpha texture
  116. SharedPtr<PixelShader> alphaTexturePS_;
  117. /// UI root element
  118. SharedPtr<UIElement> rootElement_;
  119. /// Cursor
  120. SharedPtr<Cursor> cursor_;
  121. /// UI element being dragged
  122. WeakPtr<UIElement> dragElement_;
  123. /// Element to defocus on the next update
  124. WeakPtr<UIElement> defocusElement_;
  125. /// UI rendering batches
  126. PODVector<UIBatch> batches_;
  127. /// UI rendering quads
  128. PODVector<UIQuad> quads_;
  129. /// Clipboard text
  130. std::string clipBoard_;
  131. /// Mouse buttons held down
  132. int mouseButtons_;
  133. /// Qualifier keys held down
  134. int qualifiers_;
  135. /// Initialized flag
  136. bool initialized_;
  137. };
  138. /// Register UI library objects
  139. void RegisterUILibrary(Context* context);