DsrWindow.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Called to register the default components before trying to construct visual components from text or names.
  35. void gui_initialize();
  36. class DsrWindow {
  37. public:
  38. // TODO: Should there be a separate interface context object to reduce the number of variables placed in each component?
  39. // If they all store a handle to the backend window, they could instead have a generic interface object storing pointers to the window, root, active components, et cetera...
  40. // Window backend, which the API is allowed to call directly to bypass DsrWindow for trivial operations.
  41. std::shared_ptr<BackendWindow> backend;
  42. private:
  43. // The root component
  44. std::shared_ptr<VisualComponent> mainPanel;
  45. AlignedImageF32 depthBuffer;
  46. // The inner window dimensions that are synchronized with the canvas.
  47. // The backend on the contrary may have its size changed before the resize event has been fetched.
  48. // Getting the asynchronous window dimensions directly wouldn't be synchronized with the canvas.
  49. int innerWidth, innerHeight;
  50. // The last mouse position is used to create new mouse-move events when pixelScale changes.
  51. IVector2D lastMousePosition;
  52. public:
  53. // Constructor
  54. explicit DsrWindow(std::shared_ptr<BackendWindow> backend);
  55. // Destructor
  56. virtual ~DsrWindow();
  57. public:
  58. // GUI layout
  59. void applyLayout();
  60. // Component getters
  61. std::shared_ptr<VisualComponent> findComponentByName(ReadableString name) const;
  62. template <typename T>
  63. std::shared_ptr<T> findComponentByName(ReadableString name) const {
  64. return std::dynamic_pointer_cast<T>(this->findComponentByName(name));
  65. }
  66. std::shared_ptr<VisualComponent> findComponentByNameAndIndex(ReadableString name, int index) const;
  67. template <typename T>
  68. std::shared_ptr<T> findComponentByNameAndIndex(ReadableString name, int index) const {
  69. return std::dynamic_pointer_cast<T>(this->findComponentByNameAndIndex(name, index));
  70. }
  71. // Get the root component that contains all other components in the window
  72. std::shared_ptr<VisualComponent> getRootComponent() const;
  73. void resetInterface();
  74. void loadInterfaceFromString(String layout, const ReadableString &fromPath);
  75. String saveInterfaceToString();
  76. public:
  77. // Events
  78. // Call to listen for all events given to the window
  79. // This will interact with components and call registered events
  80. // Returns true if any event was processed
  81. bool executeEvents();
  82. // Callback for any mouse event given to the window, before components receive the event
  83. DECLARE_CALLBACK(windowMouseEvent, mouseCallback);
  84. // Send a mouse event directly to the visual components
  85. // Can be called manually for automatic testing
  86. void sendMouseEvent(const MouseEvent& event);
  87. // Callback for any keyboard event given to the window, before components receive the event
  88. DECLARE_CALLBACK(windowKeyboardEvent, keyboardCallback);
  89. // Send a keyboard event directly to the visual components
  90. // Can be called manually for automatic testing
  91. void sendKeyboardEvent(const KeyboardEvent& event);
  92. // Callback for when the user tries to close the window
  93. DECLARE_CALLBACK(windowCloseEvent, emptyCallback);
  94. // Send a close event directly
  95. // Can be called manually for automatic testing
  96. void sendCloseEvent();
  97. private:
  98. // Upscaling information
  99. int pixelScale = 1;
  100. AlignedImageRgbaU8 lowResolutionCanvas;
  101. public:
  102. // Upscaling interface
  103. int getPixelScale() const;
  104. void setPixelScale(int scale);
  105. public:
  106. // Graphics
  107. // Get the color buffer for drawing or 3D rendering
  108. // The resulting color buffer may be outdated after resizing the window and calling executeEvents()
  109. AlignedImageRgbaU8 getCanvas();
  110. // Get the depth buffer for 3D rendering
  111. // The resulting depth buffer may be outdated after resizing the window and calling executeEvents()
  112. AlignedImageF32 getDepthBuffer();
  113. // Detach the depth buffer so that it can be freed
  114. // Called automatically when the canvas resizes
  115. void removeDepthBuffer();
  116. // Draw components directly to the canvas in full resolution
  117. void drawComponents();
  118. // Show the canvas when an image is ready
  119. void showCanvas();
  120. // Canvas width in the pre-upscale resolution
  121. int getCanvasWidth();
  122. // Canvas height in the pre-upscale resolution
  123. int getCanvasHeight();
  124. public:
  125. // Full-screen
  126. void setFullScreen(bool enabled);
  127. bool isFullScreen();
  128. public:
  129. // Theme
  130. void applyTheme(VisualTheme theme);
  131. VisualTheme getTheme();
  132. public:
  133. // Access to backend window
  134. // Full width after upscaling
  135. int getInnerWidth();
  136. // Full height after upscaling
  137. int getInnerHeight();
  138. String getTitle();
  139. void setTitle(const String &newTitle);
  140. };
  141. }
  142. #endif