DsrWindow.h 6.0 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 "VisualComponent.h"
  27. #include "BackendWindow.h"
  28. #include "../../api/stringAPI.h"
  29. // The DSR window is responsible for connecting visual interfaces with the backend window.
  30. // An optional depth buffer is allocated on demand when requested, and kept until the window resizes.
  31. namespace dsr {
  32. // Called to register the default components before trying to construct visual components from text or names.
  33. void gui_initialize();
  34. class DsrWindow {
  35. public:
  36. // TODO: Should there be a separate interface context object to reduce the number of variables placed in each component?
  37. // 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...
  38. // Window backend, which the API is allowed to call directly to bypass DsrWindow for trivial operations.
  39. Handle<BackendWindow> backend;
  40. private:
  41. // The root component
  42. Handle<VisualComponent> mainPanel;
  43. AlignedImageF32 depthBuffer;
  44. // The inner window dimensions that are synchronized with the canvas.
  45. // The backend on the contrary may have its size changed before the resize event has been fetched.
  46. // Getting the asynchronous window dimensions directly wouldn't be synchronized with the canvas.
  47. int innerWidth, innerHeight;
  48. // The last mouse position is used to create new mouse-move events when pixelScale changes.
  49. IVector2D lastMousePosition;
  50. public:
  51. // Constructor
  52. explicit DsrWindow(Handle<BackendWindow> backend);
  53. // Destructor
  54. virtual ~DsrWindow();
  55. public:
  56. // GUI layout
  57. void applyLayout();
  58. // Component getters
  59. Handle<VisualComponent> findComponentByName(ReadableString name) const;
  60. template <typename T>
  61. Handle<T> findComponentByName(ReadableString name) const {
  62. return handle_dynamicCast<T>(this->findComponentByName(name));
  63. }
  64. Handle<VisualComponent> findComponentByNameAndIndex(ReadableString name, int index) const;
  65. template <typename T>
  66. Handle<T> findComponentByNameAndIndex(ReadableString name, int index) const {
  67. return handle_dynamicCast<T>(this->findComponentByNameAndIndex(name, index));
  68. }
  69. // Get the root component that contains all other components in the window
  70. Handle<VisualComponent> getRootComponent() const;
  71. void resetInterface();
  72. void loadInterfaceFromString(String layout, const ReadableString &fromPath);
  73. String saveInterfaceToString();
  74. public:
  75. // Events
  76. // Call to listen for all events given to the window
  77. // This will interact with components and call registered events
  78. // Returns true if any event was processed
  79. bool executeEvents();
  80. // Callback for any mouse event given to the window, before components receive the event
  81. DECLARE_CALLBACK(windowMouseEvent, mouseCallback);
  82. // Send a mouse event directly to the visual components
  83. // Can be called manually for automatic testing
  84. void sendMouseEvent(const MouseEvent& event);
  85. // Callback for any keyboard event given to the window, before components receive the event
  86. DECLARE_CALLBACK(windowKeyboardEvent, keyboardCallback);
  87. // Send a keyboard event directly to the visual components
  88. // Can be called manually for automatic testing
  89. void sendKeyboardEvent(const KeyboardEvent& event);
  90. // Callback for when the user tries to close the window
  91. DECLARE_CALLBACK(windowCloseEvent, emptyCallback);
  92. // Send a close event directly
  93. // Can be called manually for automatic testing
  94. void sendCloseEvent();
  95. private:
  96. // Upscaling information
  97. int pixelScale = 1;
  98. AlignedImageRgbaU8 lowResolutionCanvas;
  99. public:
  100. // Upscaling interface
  101. int getPixelScale() const;
  102. void setPixelScale(int scale);
  103. public:
  104. // Graphics
  105. // Get the color buffer for drawing or 3D rendering
  106. // The resulting color buffer may be outdated after resizing the window and calling executeEvents()
  107. AlignedImageRgbaU8 getCanvas();
  108. // Get the depth buffer for 3D rendering
  109. // The resulting depth buffer may be outdated after resizing the window and calling executeEvents()
  110. AlignedImageF32 getDepthBuffer();
  111. // Detach the depth buffer so that it can be freed
  112. // Called automatically when the canvas resizes
  113. void removeDepthBuffer();
  114. // Draw components directly to the canvas in full resolution
  115. void drawComponents();
  116. // Execute deferred actions once it is safe to trigger callbacks from affected components.
  117. void flushDeferredActions();
  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