DsrWindow.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_GUI_DSRWINDOW
  25. #define DFPSR_GUI_DSRWINDOW
  26. #include <memory>
  27. #include "../gui/VisualComponent.h"
  28. #include "../gui/BackendWindow.h"
  29. #include "../api/stringAPI.h"
  30. #include "../api/types.h"
  31. // The DSR window is responsible for connecting visual interfaces with the backend window.
  32. // An optional depth buffer is allocated on demand when requested, and kept until the window resizes.
  33. namespace dsr {
  34. class DsrWindow {
  35. private:
  36. // Window backend
  37. std::shared_ptr<BackendWindow> backend;
  38. // The root component
  39. std::shared_ptr<VisualComponent> mainPanel;
  40. AlignedImageF32 depthBuffer;
  41. // The inner window dimensions that are synchronized with the canvas.
  42. // The backend on the contrary may have its size changed before the resize event has been fetched.
  43. // Getting the asynchronous window dimensions directly wouldn't be synchronized with the canvas.
  44. int innerWidth, innerHeight;
  45. // The last mouse position is used to create new mouse-move events when pixelScale changes.
  46. IVector2D lastMousePosition;
  47. public:
  48. // Constructor
  49. explicit DsrWindow(std::shared_ptr<BackendWindow> backend);
  50. // Destructor
  51. virtual ~DsrWindow();
  52. public:
  53. // GUI layout
  54. void applyLayout();
  55. // Component getters
  56. std::shared_ptr<VisualComponent> findComponentByName(ReadableString name, bool mustExist = true) const;
  57. template <typename T>
  58. std::shared_ptr<T> findComponentByName(ReadableString name, bool mustExist = true) const {
  59. return std::dynamic_pointer_cast<T>(this->findComponentByName(name, mustExist));
  60. }
  61. std::shared_ptr<VisualComponent> findComponentByNameAndIndex(ReadableString name, int index, bool mustExist = true) const;
  62. template <typename T>
  63. std::shared_ptr<T> findComponentByNameAndIndex(ReadableString name, int index, bool mustExist = true) const {
  64. return std::dynamic_pointer_cast<T>(this->findComponentByNameAndIndex(name, index, mustExist));
  65. }
  66. // Get the root component that contains all other components in the window
  67. std::shared_ptr<VisualComponent> getRootComponent() const;
  68. void resetInterface();
  69. void loadInterfaceFromString(String layout);
  70. String saveInterfaceToString();
  71. public:
  72. // Events
  73. // Call to listen for all events given to the window
  74. // This will interact with components and call registered events
  75. // Returns true if any event was processed
  76. bool executeEvents();
  77. // Callback for any mouse event given to the window, before components receive the event
  78. DECLARE_CALLBACK(windowMouseEvent, mouseCallback);
  79. // Send a mouse event directly to the visual components
  80. // Can be called manually for automatic testing
  81. void sendMouseEvent(const MouseEvent& event);
  82. // Callback for any keyboard event given to the window, before components receive the event
  83. DECLARE_CALLBACK(windowKeyboardEvent, keyboardCallback);
  84. // Send a keyboard event directly to the visual components
  85. // Can be called manually for automatic testing
  86. void sendKeyboardEvent(const KeyboardEvent& event);
  87. // Callback for when the user tries to close the window
  88. DECLARE_CALLBACK(windowCloseEvent, emptyCallback);
  89. // Send a close event directly
  90. // Can be called manually for automatic testing
  91. void sendCloseEvent();
  92. private:
  93. // Upscaling information
  94. int pixelScale = 1;
  95. AlignedImageRgbaU8 lowResolutionCanvas;
  96. public:
  97. // Upscaling interface
  98. int getPixelScale() const;
  99. void setPixelScale(int scale);
  100. public:
  101. // Graphics
  102. // Get the color buffer for drawing or 3D rendering
  103. // The resulting color buffer may be outdated after resizing the window and calling executeEvents()
  104. AlignedImageRgbaU8 getCanvas();
  105. // Get the depth buffer for 3D rendering
  106. // The resulting depth buffer may be outdated after resizing the window and calling executeEvents()
  107. AlignedImageF32 getDepthBuffer();
  108. // Detach the depth buffer so that it can be freed
  109. // Called automatically when the canvas resizes
  110. void removeDepthBuffer();
  111. // Draw components directly to the canvas in full resolution
  112. void drawComponents();
  113. // Show the canvas when an image is ready
  114. void showCanvas();
  115. // Canvas width in the pre-upscale resolution
  116. int getCanvasWidth();
  117. // Canvas height in the pre-upscale resolution
  118. int getCanvasHeight();
  119. public:
  120. // Full-screen
  121. void setFullScreen(bool enabled);
  122. bool isFullScreen();
  123. public:
  124. // Theme
  125. void applyTheme(VisualTheme theme);
  126. VisualTheme getTheme();
  127. public:
  128. // Access to backend window
  129. // Full width after upscaling
  130. int getInnerWidth();
  131. // Full height after upscaling
  132. int getInnerHeight();
  133. String getTitle();
  134. void setTitle(const String &newTitle);
  135. };
  136. }
  137. #endif