UI.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 UIElement;
  32. class UIBatch;
  33. //! Manages the graphical user interface
  34. class UI : public RefCounted, public EventListener
  35. {
  36. public:
  37. //! Construct with renderer subsystem and resource cache pointers
  38. UI(Renderer* Renderer, ResourceCache* cache);
  39. //! Destruct
  40. virtual ~UI();
  41. //! Set cursor UI element
  42. void setCursor(Cursor* cursor);
  43. //! Set focused UI element
  44. void setFocusElement(UIElement* element);
  45. //! Bring an UI element to front
  46. void bringToFront(UIElement* element);
  47. //! Update the UI
  48. void update(float timeStep);
  49. //! Render the UI
  50. void render();
  51. //! Return root UI elemenet
  52. UIElement* getRootElement() const { return mRootElement; }
  53. //! Return cursor
  54. Cursor* getCursor() const { return mCursor; }
  55. //! Return UI element at screen coordinates
  56. UIElement* getElementAt(const IntVector2& position, bool enabledOnly = true);
  57. //! Return UI element at screen coordinates
  58. UIElement* getElementAt(int x, int y, bool enabledOnly = true);
  59. //! Return focused element
  60. UIElement* getFocusElement();
  61. //! Return cursor position
  62. IntVector2 getCursorPosition();
  63. private:
  64. //! Update UI elements and generate batches for UI rendering
  65. void update(float timeStep, UIElement* element);
  66. //! Generate batches from an UI element recursively
  67. void getBatches(UIElement* element, IntRect currentScissor);
  68. //! Return UI element at screen position recursively
  69. void getElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  70. //! Switch focus to UI element
  71. void switchFocusTo(UIElement* element);
  72. //! Verify that an UI element belongs to the UI element hierarchy
  73. UIElement* verifyElement(UIElement* element);
  74. //! Handle window resized event
  75. void handleWindowResized(StringHash eventType, VariantMap& eventData);
  76. //! Handle mouse move event
  77. void handleMouseMove(StringHash eventType, VariantMap& eventData);
  78. //! Handle mouse button down event
  79. void handleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  80. //! Handle mouse button up event
  81. void handleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  82. //! Handle character event
  83. void handleChar(StringHash eventType, VariantMap& eventData);
  84. //! Renderer
  85. SharedPtr<Renderer> mRenderer;
  86. //! Resource cache
  87. SharedPtr<ResourceCache> mCache;
  88. //! Vertex shader for no texture
  89. SharedPtr<VertexShader> mNoTextureVS;
  90. //! Vertex shader for diffuse texture
  91. SharedPtr<VertexShader> mDiffTextureVS;
  92. //! Pixel shader for no texture
  93. SharedPtr<PixelShader> mNoTexturePS;
  94. //! Pixel shader for diffuse texture
  95. SharedPtr<PixelShader> mDiffTexturePS;
  96. //! Pixel shader for alpha texture
  97. SharedPtr<PixelShader> mAlphaTexturePS;
  98. //! UI root element
  99. SharedPtr<UIElement> mRootElement;
  100. //! Cursor
  101. SharedPtr<Cursor> mCursor;
  102. //! UI rendering batches
  103. std::vector<UIBatch> mBatches;
  104. //! UI rendering quads
  105. std::vector<UIQuad> mQuads;
  106. //! Mouse drag flag
  107. bool mMouseDrag;
  108. //! Mouse drag UI element
  109. UIElement* mMouseDragElement;
  110. //! Mouse buttons held down
  111. unsigned mMouseButtons;
  112. };
  113. //! Return UI instance
  114. UI* getUI();
  115. #endif // UI_UI_H