DsrWindow.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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(Handle<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.getUnsafe();
  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. static void setBackendWindowHandle(Handle<VisualComponent> component, Handle<BackendWindow> windowHandle) {
  86. component->window = windowHandle;
  87. for (int c = 0; c < component->children.length(); c++) {
  88. setBackendWindowHandle(component->children[c], windowHandle);
  89. }
  90. }
  91. DsrWindow::~DsrWindow() {
  92. // Disconnect the backend window from all components, so that handles to components without a DsrWindow will not prevent the BackendWindow from being freed.
  93. setBackendWindowHandle(this->mainPanel, Handle<BackendWindow>());
  94. }
  95. void DsrWindow::applyLayout() {
  96. this->mainPanel->applyLayout(IRect(0, 0, this->getCanvasWidth(), this->getCanvasHeight()));
  97. }
  98. Handle<VisualComponent> DsrWindow::findComponentByName(ReadableString name) const {
  99. if (string_match(this->mainPanel->getName(), name)) {
  100. return this->mainPanel;
  101. } else {
  102. return this->mainPanel->findChildByName(name);
  103. }
  104. }
  105. Handle<VisualComponent> DsrWindow::findComponentByNameAndIndex(ReadableString name, int index) const {
  106. if (string_match(this->mainPanel->getName(), name) && this->mainPanel->getIndex() == index) {
  107. return this->mainPanel;
  108. } else {
  109. return this->mainPanel->findChildByNameAndIndex(name, index);
  110. }
  111. }
  112. Handle<VisualComponent> DsrWindow::getRootComponent() const {
  113. return this->mainPanel;
  114. }
  115. void DsrWindow::resetInterface() {
  116. // Create an empty main panel
  117. this->mainPanel = handle_dynamicCast<VisualComponent>(createPersistentClass("Panel"));
  118. if (this->mainPanel.isNull()) {
  119. throwError(U"DsrWindow::resetInterface: The window's Panel could not be created!");
  120. }
  121. this->mainPanel->setName("mainPanel");
  122. // Inherit handle to backend window to access the clipboard.
  123. this->mainPanel->window = this->backend;
  124. this->applyLayout();
  125. }
  126. void DsrWindow::loadInterfaceFromString(String layout, const ReadableString &fromPath) {
  127. // Load a tree structure of visual components from text
  128. this->mainPanel = handle_dynamicCast<VisualComponent>(createPersistentClassFromText(layout, fromPath));
  129. // Re-assign the backend window handle
  130. setBackendWindowHandle(this->mainPanel, this->backend);
  131. if (this->mainPanel.isNull()) {
  132. throwError(U"DsrWindow::loadInterfaceFromString: The window's root component could not be created!\n\nLayout:\n", layout, "\n");
  133. }
  134. this->applyLayout();
  135. }
  136. String DsrWindow::saveInterfaceToString() {
  137. return this->mainPanel->toString();
  138. }
  139. bool DsrWindow::executeEvents() {
  140. return this->backend->executeEvents();
  141. }
  142. void DsrWindow::sendMouseEvent(const MouseEvent& event) {
  143. this->lastMousePosition = event.position;
  144. // Components will receive scaled mouse coordinates by being drawn to the low-resolution canvas
  145. MouseEvent scaledEvent = event / this->pixelScale;
  146. // Send the global event
  147. this->callback_windowMouseEvent(scaledEvent);
  148. if (this->mainPanel->getVisible() && this->mainPanel->pointIsInside(scaledEvent.position)) {
  149. // In case of the root panel not covering the entire window, adjust input coordinates to the panel's local system.
  150. scaledEvent.position -= this->mainPanel->location.upperLeft();
  151. // Send to the main panel and its components
  152. this->mainPanel->sendMouseEvent(scaledEvent);
  153. }
  154. }
  155. void DsrWindow::sendKeyboardEvent(const KeyboardEvent& event) {
  156. // Send the global event
  157. this->callback_windowKeyboardEvent(event);
  158. // Send to the main panel and its components
  159. this->mainPanel->sendKeyboardEvent(event);
  160. }
  161. void DsrWindow::sendCloseEvent() {
  162. this->callback_windowCloseEvent();
  163. }
  164. int DsrWindow::getInnerWidth() {
  165. return this->innerWidth;
  166. }
  167. int DsrWindow::getInnerHeight() {
  168. return this->innerHeight;
  169. }
  170. int DsrWindow::getCanvasWidth() {
  171. return max(1, this->innerWidth / this->pixelScale);
  172. }
  173. int DsrWindow::getCanvasHeight() {
  174. return max(1, this->innerHeight / this->pixelScale);
  175. }
  176. AlignedImageF32 DsrWindow::getDepthBuffer() {
  177. this->backend->getCanvas();
  178. int smallWidth = getCanvasWidth();
  179. int smallHeight = getCanvasHeight();
  180. if (!image_exists(this->depthBuffer)
  181. || image_getWidth(this->depthBuffer) != smallWidth
  182. || image_getHeight(this->depthBuffer) != smallHeight) {
  183. this->depthBuffer = image_create_F32(smallWidth, smallHeight);
  184. }
  185. return this->depthBuffer;
  186. }
  187. void DsrWindow::removeDepthBuffer() {
  188. this->depthBuffer = AlignedImageF32();
  189. }
  190. int DsrWindow::getPixelScale() const {
  191. return this->pixelScale;
  192. }
  193. void DsrWindow::setPixelScale(int scale) {
  194. if (this->pixelScale != scale) {
  195. this->pixelScale = scale;
  196. // Update layout
  197. this->applyLayout();
  198. // The mouse moves relative to the canvas when scale changes
  199. this->sendMouseEvent(MouseEvent(MouseEventType::MouseMove, MouseKeyEnum::NoKey, this->lastMousePosition));
  200. }
  201. }
  202. void DsrWindow::setFullScreen(bool enabled) {
  203. if (this->backend->isFullScreen() != enabled) {
  204. this->backend->setFullScreen(enabled);
  205. // TODO: The mouse moves relative to the canvas when the window moves, but the new mouse location was never given.
  206. // How can mouse-move events be made consistent in applications when toggling full-screen without resorting to hacks?
  207. // Return the moved pixel offset from backend's setFullScreen?
  208. }
  209. }
  210. bool DsrWindow::isFullScreen() {
  211. return this->backend->isFullScreen();
  212. }
  213. void DsrWindow::drawComponents() {
  214. auto canvas = this->getCanvas();
  215. this->mainPanel->draw(canvas, IVector2D(0, 0));
  216. }
  217. AlignedImageRgbaU8 DsrWindow::getCanvas() {
  218. auto fullResolutionCanvas = this->backend->getCanvas();
  219. if (this->pixelScale > 1) {
  220. // Get low resolution canvas in deterministic RGBA pack order
  221. int smallWidth = getCanvasWidth();
  222. int smallHeight = getCanvasHeight();
  223. if (!image_exists(this->lowResolutionCanvas)
  224. || image_getWidth(this->lowResolutionCanvas) != smallWidth
  225. || image_getHeight(this->lowResolutionCanvas) != smallHeight) {
  226. this->lowResolutionCanvas = image_create_RgbaU8_native(smallWidth, smallHeight, image_getPackOrderIndex(fullResolutionCanvas));
  227. }
  228. return this->lowResolutionCanvas;
  229. } else {
  230. // Get full resolution canvas in arbitrary pack order
  231. return fullResolutionCanvas;
  232. }
  233. }
  234. void DsrWindow::showCanvas() {
  235. if (this->pixelScale > 1 && image_exists(this->lowResolutionCanvas)) {
  236. // Use an exact pixel size, by cutting into the last row and column when not even
  237. // This makes it easy to convert mouse coordinates using multiplication and division with pixelScale
  238. auto target = this->backend->getCanvas();
  239. auto source = this->getCanvas();
  240. filter_blockMagnify(target, source, this->pixelScale, this->pixelScale);
  241. }
  242. this->backend->showCanvas();
  243. }
  244. String DsrWindow::getTitle() {
  245. return this->backend->getTitle();
  246. }
  247. void DsrWindow::setTitle(const String &newTitle) {
  248. return this->backend->setTitle(newTitle);
  249. }
  250. void DsrWindow::applyTheme(VisualTheme theme) {
  251. this->mainPanel->applyTheme(theme);
  252. }
  253. VisualTheme DsrWindow::getTheme() {
  254. return this->mainPanel->getTheme();
  255. }