DsrWindow.h 5.7 KB

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