DsrWindow.cpp 9.0 KB

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