DsrWindow.cpp 9.4 KB

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