DsrWindow.cpp 8.9 KB

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