UIView.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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 "UIBatch.h"
  24. #include "UIWidget.h"
  25. namespace Atomic
  26. {
  27. class Texture;
  28. class Texture2D;
  29. class VertexBuffer;
  30. class UI;
  31. class UIRenderer;
  32. class UIComponent;
  33. static int const UIVIEW_DEFAULT_TEXTURE_SIZE = 512;
  34. static int const UIVIEW_MIN_TEXTURE_SIZE = 64;
  35. static int const UIVIEW_MAX_TEXTURE_SIZE = 4096;
  36. /// Top level UIView management
  37. class ATOMIC_API UIView : public UIWidget
  38. {
  39. friend class UI;
  40. ATOMIC_OBJECT(UIView, UIWidget)
  41. public:
  42. UIView(Context* context);
  43. virtual ~UIView();
  44. /// Set the view size
  45. bool SetSize(int width, int height);
  46. /// Remove the UIView from the UI subsystem, readding removed views is not advised
  47. void Remove();
  48. /// Focuses the UIView for input events
  49. virtual void SetFocus();
  50. /// Gets whether this UIView is focused
  51. virtual bool GetFocus() const;
  52. /// Resigned this views focus, the bottom UIView with autofocus on will become focused
  53. void ResignFocus();
  54. /// Sets whether the view will autofocus when at the bottom of the view stack
  55. void SetAutoFocus(bool value) { autoFocus_ = value; }
  56. /// Gets whether the view will autofocus when at the bottom of the view stack
  57. bool GetAutoFocus() const { return autoFocus_; }
  58. /// Gets whether both mouse and keyboard input are enabled
  59. bool GetInputEnabled() const { return mouseEnabled_ || keyboardEnabled_; }
  60. /// Sets whether both mouse and keyboard input are enabled
  61. void SetInputEnabled(bool value) { mouseEnabled_ = keyboardEnabled_ = value; }
  62. /// Gets whether mouse input is enabled
  63. bool GetMouseEnabled() const { return mouseEnabled_; }
  64. /// Sets whether mouse input is enabled
  65. void SetMouseEnabled(bool value) { mouseEnabled_ = keyboardEnabled_ = value; }
  66. /// Gets whether keyboard input is enabled
  67. bool GetKeyboardEnabled() const { return keyboardEnabled_; }
  68. /// Sets whether keyboard input is enabled
  69. void SetKeyboardEnabled(bool value) { keyboardEnabled_ = value; }
  70. /// Set whether the UIView renders to texture, useful for 3D UI's, etc
  71. bool SetRenderToTexture(bool value, const int width = UIVIEW_DEFAULT_TEXTURE_SIZE, const int height = UIVIEW_DEFAULT_TEXTURE_SIZE);
  72. /// Gets the UIViews render texture, if any
  73. Texture2D* GetRenderTexture() { return renderTexture_; }
  74. /// Returns whether this UIView has a valid render texture
  75. bool HasRenderTexture() const { return renderTexture_.NotNull(); }
  76. /// Render the UI
  77. void Render(bool resetRenderTargets = true);
  78. /// Low level vertex data submission
  79. void SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData);
  80. protected:
  81. private:
  82. bool FilterDefaultInput(bool keyEvent = false) const ;
  83. void BecomeFocused();
  84. void UpdateUIBatches();
  85. void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  86. void Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd);
  87. void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
  88. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  89. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  90. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  91. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  92. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  93. void HandleKeyUp(StringHash eventType, VariantMap& eventData);
  94. void HandleKey(bool keydown, int keycode, int scancode);
  95. void HandleTextInput(StringHash eventType, VariantMap& eventData);
  96. //Touch Input
  97. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  98. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  99. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  100. /// UI rendering batches.
  101. PODVector<UIBatch> batches_;
  102. /// UI rendering vertex data.
  103. PODVector<float> vertexData_;
  104. SharedPtr<VertexBuffer> vertexBuffer_;
  105. WeakPtr<Graphics> graphics_;
  106. UIRenderer* renderer_;
  107. WeakPtr<UI> ui_;
  108. WeakPtr<UIComponent> uiComponent_;
  109. SharedPtr<Texture2D> renderTexture_;
  110. bool autoFocus_;
  111. bool mouseEnabled_;
  112. bool keyboardEnabled_;
  113. };
  114. }