UI.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef UI_UI_H
  24. #define UI_UI_H
  25. #include "EventListener.h"
  26. #include "SharedPtr.h"
  27. #include "UIBatch.h"
  28. class Cursor;
  29. class Renderer;
  30. class ResourceCache;
  31. class UIBatch;
  32. class UIElement;
  33. class UIElementFactory;
  34. //! Manages the graphical user interface
  35. class UI : public RefCounted, public EventListener
  36. {
  37. public:
  38. //! Construct with renderer subsystem and resource cache pointers
  39. UI(Renderer* Renderer, ResourceCache* cache);
  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
  49. void update(float timeStep);
  50. //! Render the UI
  51. void render();
  52. //! Add a UI element factory
  53. void addElementFactory(UIElementFactory* factory);
  54. //! Create a UI element by type. Throw exception if can not create
  55. SharedPtr<UIElement> createElement(ShortStringHash type, const std::string& name = std::string());
  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(XMLFile* file, XMLFile* styleFile = 0);
  58. //! Return resource cache
  59. ResourceCache* getResourceCache() const { return mCache; }
  60. //! Return root UI elemenet
  61. UIElement* getRootElement() const { return mRootElement; }
  62. //! Return cursor
  63. Cursor* getCursor() const { return mCursor; }
  64. //! Return UI element at screen coordinates
  65. UIElement* getElementAt(const IntVector2& position, bool enabledOnly = true);
  66. //! Return UI element at screen coordinates
  67. UIElement* getElementAt(int x, int y, bool enabledOnly = true);
  68. //! Return focused element
  69. UIElement* getFocusElement();
  70. //! Return cursor position
  71. IntVector2 getCursorPosition();
  72. //! Return UI element factories
  73. const std::vector<SharedPtr<UIElementFactory> >& getElementFactories() const { return mFactories; }
  74. private:
  75. //! Update UI elements and generate batches for UI rendering
  76. void update(float timeStep, UIElement* element);
  77. //! Generate batches from an UI element recursively
  78. void getBatches(UIElement* element, IntRect currentScissor);
  79. //! Return UI element at screen position recursively
  80. void getElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  81. //! Switch focus to UI element
  82. void switchFocusTo(UIElement* element);
  83. //! Verify that an UI element belongs to the UI element hierarchy
  84. UIElement* verifyElement(UIElement* element);
  85. //! Handle window resized event
  86. void handleWindowResized(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. //! Load a UI layout from an XML file recursively
  100. void loadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFile);
  101. //! Renderer
  102. SharedPtr<Renderer> mRenderer;
  103. //! Resource cache
  104. SharedPtr<ResourceCache> mCache;
  105. //! Vertex shader for no texture
  106. SharedPtr<VertexShader> mNoTextureVS;
  107. //! Vertex shader for diffuse texture
  108. SharedPtr<VertexShader> mDiffTextureVS;
  109. //! Pixel shader for no texture
  110. SharedPtr<PixelShader> mNoTexturePS;
  111. //! Pixel shader for diffuse texture
  112. SharedPtr<PixelShader> mDiffTexturePS;
  113. //! Pixel shader for alpha texture
  114. SharedPtr<PixelShader> mAlphaTexturePS;
  115. //! UI root element
  116. SharedPtr<UIElement> mRootElement;
  117. //! Cursor
  118. SharedPtr<Cursor> mCursor;
  119. //! UI element factories
  120. std::vector<SharedPtr<UIElementFactory> > mFactories;
  121. //! UI rendering batches
  122. std::vector<UIBatch> mBatches;
  123. //! UI rendering quads
  124. std::vector<UIQuad> mQuads;
  125. //! Mouse drag flag
  126. bool mMouseDrag;
  127. //! Mouse drag UI element
  128. UIElement* mMouseDragElement;
  129. //! Element to defocus on the next update
  130. UIElement* mDefocusElement;
  131. //! Mouse buttons held down
  132. int mMouseButtons;
  133. //! Qualifier keys held down
  134. int mQualifiers;
  135. };
  136. #endif // UI_UI_H