DsrWindow.cpp 8.6 KB

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