DsrWindow.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "DsrWindow.h"
  24. #include "components/Panel.h"
  25. #include "components/Button.h"
  26. #include "components/ListBox.h"
  27. #include "../math/scalar.h"
  28. #include "../math/IVector.h"
  29. #include "../api/imageAPI.h"
  30. #include "../api/drawAPI.h"
  31. using namespace dsr;
  32. static bool initialized = false;
  33. static void initializeGui() {
  34. if (!initialized) {
  35. // Register built-in components by name
  36. REGISTER_PERSISTENT_CLASS(Panel)
  37. REGISTER_PERSISTENT_CLASS(Button)
  38. REGISTER_PERSISTENT_CLASS(ListBox)
  39. initialized = true;
  40. }
  41. }
  42. DsrWindow::DsrWindow(std::shared_ptr<BackendWindow> backend)
  43. : backend(backend), innerWidth(backend->getWidth()), innerHeight(backend->getHeight()) {
  44. // Initialize the GUI system if needed
  45. initializeGui();
  46. // Listen to mouse and keyboard events from the backend window
  47. this->backend->mouseEvent() = [this](const MouseEvent& event) {
  48. this->sendMouseEvent(event);
  49. };
  50. this->backend->keyboardEvent() = [this](const KeyboardEvent& event) {
  51. this->sendKeyboardEvent(event);
  52. };
  53. this->backend->closeEvent() = [this]() {
  54. this->sendCloseEvent();
  55. };
  56. // Receiving notifications about resizing should be done in the main panel
  57. this->backend->resizeEvent() = [this](int width, int height) {
  58. BackendWindow *backend = this->backend.get();
  59. ImageRgbaU8 canvas = backend->getCanvas();
  60. this->innerWidth = width;
  61. this->innerHeight = height;
  62. if (image_getWidth(canvas) != width || image_getHeight(canvas) != height) {
  63. // Resize the image that holds everything drawn on the window
  64. backend->resizeCanvas(width, height);
  65. // Remove the old depth buffer, so that it will resize when being requested again
  66. this->removeDepthBuffer();
  67. }
  68. this->applyLayout();
  69. };
  70. this->resetInterface();
  71. }
  72. DsrWindow::~DsrWindow() {}
  73. void DsrWindow::applyLayout() {
  74. this->mainPanel->applyLayout(IVector2D(this->getCanvasWidth(), this->getCanvasHeight()));
  75. }
  76. std::shared_ptr<VisualComponent> DsrWindow::findComponentByName(ReadableString name, bool mustExist) const {
  77. if (string_match(this->mainPanel->getName(), name)) {
  78. return this->mainPanel;
  79. } else {
  80. return this->mainPanel->findChildByName(name, mustExist);
  81. }
  82. }
  83. std::shared_ptr<VisualComponent> DsrWindow::findComponentByNameAndIndex(ReadableString name, int index, bool mustExist) const {
  84. if (string_match(this->mainPanel->getName(), name) && this->mainPanel->getIndex() == index) {
  85. return this->mainPanel;
  86. } else {
  87. return this->mainPanel->findChildByNameAndIndex(name, index, mustExist);
  88. }
  89. }
  90. std::shared_ptr<VisualComponent> DsrWindow::getRootComponent() const {
  91. return this->mainPanel;
  92. }
  93. void DsrWindow::resetInterface() {
  94. // Create an empty main panel
  95. this->mainPanel = std::dynamic_pointer_cast<VisualComponent>(createPersistentClass("Panel"));
  96. if (this->mainPanel.get() == nullptr) {
  97. throwError(U"DsrWindow::resetInterface: The window's Panel could not be created!");
  98. }
  99. this->mainPanel->setName("mainPanel");
  100. this->applyLayout();
  101. }
  102. void DsrWindow::loadInterfaceFromString(String layout) {
  103. // Load a tree structure of visual components from text
  104. this->mainPanel = std::dynamic_pointer_cast<VisualComponent>(createPersistentClassFromText(layout));
  105. if (this->mainPanel.get() == nullptr) {
  106. throwError(U"DsrWindow::loadInterfaceFromString: The window's root component could not be created!\n\nLayout:\n", layout, "\n");
  107. }
  108. this->applyLayout();
  109. }
  110. String DsrWindow::saveInterfaceToString() {
  111. return this->mainPanel->toString();
  112. }
  113. bool DsrWindow::executeEvents() {
  114. return this->backend->executeEvents();
  115. }
  116. void DsrWindow::sendMouseEvent(const MouseEvent& event) {
  117. this->lastMousePosition = event.position;
  118. // Components will receive scaled mouse coordinates by being drawn to the low-resolution canvas
  119. MouseEvent scaledEvent = event / this->pixelScale;
  120. // Send the global event
  121. this->callback_windowMouseEvent(scaledEvent);
  122. // Send to the main panel and its components
  123. this->mainPanel->sendMouseEvent(scaledEvent);
  124. }
  125. void DsrWindow::sendKeyboardEvent(const KeyboardEvent& event) {
  126. // Send the global event
  127. this->callback_windowKeyboardEvent(event);
  128. // Send to the main panel and its components
  129. this->mainPanel->sendKeyboardEvent(event);
  130. }
  131. void DsrWindow::sendCloseEvent() {
  132. this->callback_windowCloseEvent();
  133. }
  134. int DsrWindow::getInnerWidth() {
  135. return this->innerWidth;
  136. }
  137. int DsrWindow::getInnerHeight() {
  138. return this->innerHeight;
  139. }
  140. int DsrWindow::getCanvasWidth() {
  141. return std::max(1, this->innerWidth / this->pixelScale);
  142. }
  143. int DsrWindow::getCanvasHeight() {
  144. return std::max(1, this->innerHeight / this->pixelScale);
  145. }
  146. AlignedImageF32 DsrWindow::getDepthBuffer() {
  147. auto fullResolutionCanvas = this->backend->getCanvas();
  148. int smallWidth = getCanvasWidth();
  149. int smallHeight = getCanvasHeight();
  150. if (!image_exists(this->depthBuffer)
  151. || image_getWidth(this->depthBuffer) != smallWidth
  152. || image_getHeight(this->depthBuffer) != smallHeight) {
  153. this->depthBuffer = image_create_F32(smallWidth, smallHeight);
  154. }
  155. return this->depthBuffer;
  156. }
  157. void DsrWindow::removeDepthBuffer() {
  158. this->depthBuffer = AlignedImageF32();
  159. }
  160. int DsrWindow::getPixelScale() const {
  161. return this->pixelScale;
  162. }
  163. void DsrWindow::setPixelScale(int scale) {
  164. if (this->pixelScale != scale) {
  165. this->pixelScale = scale;
  166. // Update layout
  167. this->applyLayout();
  168. // The mouse moves relative to the canvas when scale changes
  169. this->sendMouseEvent(MouseEvent(MouseEventType::MouseMove, MouseKeyEnum::NoKey, this->lastMousePosition));
  170. }
  171. }
  172. void DsrWindow::setFullScreen(bool enabled) {
  173. if (this->backend->isFullScreen() != enabled) {
  174. this->backend->setFullScreen(enabled);
  175. // TODO: The mouse moves relative to the canvas when the window moves, but the new mouse location was never given.
  176. // How can mouse-move events be made consistent in applications when toggling full-screen without resorting to hacks?
  177. // Return the moved pixel offset from backend's setFullScreen?
  178. }
  179. }
  180. bool DsrWindow::isFullScreen() {
  181. return this->backend->isFullScreen();
  182. }
  183. void DsrWindow::drawComponents() {
  184. auto canvas = this->getCanvas();
  185. this->mainPanel->draw(canvas, IVector2D(0, 0));
  186. }
  187. AlignedImageRgbaU8 DsrWindow::getCanvas() {
  188. // TODO: Query if the backend has an optimized upload for a smaller canvas
  189. // Useful if a window backend has GPU accelerated or native upscaling
  190. auto fullResolutionCanvas = this->backend->getCanvas();
  191. if (this->pixelScale > 1) {
  192. // Get low resolution canvas in deterministic RGBA pack order
  193. int smallWidth = getCanvasWidth();
  194. int smallHeight = getCanvasHeight();
  195. if (!image_exists(this->lowResolutionCanvas)
  196. || image_getWidth(this->lowResolutionCanvas) != smallWidth
  197. || image_getHeight(this->lowResolutionCanvas) != smallHeight) {
  198. this->lowResolutionCanvas = image_create_RgbaU8_native(smallWidth, smallHeight, image_getPackOrderIndex(fullResolutionCanvas));
  199. }
  200. return this->lowResolutionCanvas;
  201. } else {
  202. // Get full resolution canvas in arbitrary pack order
  203. return fullResolutionCanvas;
  204. }
  205. }
  206. void DsrWindow::showCanvas() {
  207. if (this->pixelScale > 1 && image_exists(this->lowResolutionCanvas)) {
  208. // Use an exact pixel size, by cutting into the last row and column when not even
  209. // This makes it easy to convert mouse coordinates using multiplication and division with pixelScale
  210. auto target = this->backend->getCanvas();
  211. auto source = this->getCanvas();
  212. filter_blockMagnify(target, source, this->pixelScale, this->pixelScale);
  213. }
  214. this->backend->showCanvas();
  215. }
  216. String DsrWindow::getTitle() {
  217. return this->backend->getTitle();
  218. }
  219. void DsrWindow::setTitle(const String &newTitle) {
  220. }
  221. void DsrWindow::applyTheme(VisualTheme theme) {
  222. this->mainPanel->applyTheme(theme);
  223. }
  224. VisualTheme DsrWindow::getTheme() {
  225. return this->mainPanel->getTheme();
  226. }