DsrWindow.cpp 8.9 KB

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