DsrWindow.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // To pass the event to the main panel, the panel has to be visible with the cursor inside of it,
  149. // unless it is a drag move or button release in which the event should never be blocked.
  150. if ((this->mainPanel->getVisible() && this->mainPanel->pointIsInside(scaledEvent.position)) || event.mouseEventType == MouseEventType::MouseUp || this->mainPanel->holdCount > 0) {
  151. // In case of the root panel not covering the entire window, adjust input coordinates to the panel's local system.
  152. scaledEvent.position -= this->mainPanel->location.upperLeft();
  153. // Send to the main panel and its components
  154. this->mainPanel->sendMouseEvent(scaledEvent);
  155. }
  156. }
  157. void DsrWindow::sendKeyboardEvent(const KeyboardEvent& event) {
  158. // Send the global event
  159. this->callback_windowKeyboardEvent(event);
  160. // Send to the main panel and its components
  161. this->mainPanel->sendKeyboardEvent(event);
  162. }
  163. void DsrWindow::sendCloseEvent() {
  164. this->callback_windowCloseEvent();
  165. }
  166. int DsrWindow::getInnerWidth() {
  167. return this->innerWidth;
  168. }
  169. int DsrWindow::getInnerHeight() {
  170. return this->innerHeight;
  171. }
  172. int DsrWindow::getCanvasWidth() {
  173. return max(1, this->innerWidth / this->pixelScale);
  174. }
  175. int DsrWindow::getCanvasHeight() {
  176. return max(1, this->innerHeight / this->pixelScale);
  177. }
  178. AlignedImageF32 DsrWindow::getDepthBuffer() {
  179. this->backend->getCanvas();
  180. int smallWidth = getCanvasWidth();
  181. int smallHeight = getCanvasHeight();
  182. if (!image_exists(this->depthBuffer)
  183. || image_getWidth(this->depthBuffer) != smallWidth
  184. || image_getHeight(this->depthBuffer) != smallHeight) {
  185. this->depthBuffer = image_create_F32(smallWidth, smallHeight);
  186. }
  187. return this->depthBuffer;
  188. }
  189. void DsrWindow::removeDepthBuffer() {
  190. this->depthBuffer = AlignedImageF32();
  191. }
  192. int DsrWindow::getPixelScale() const {
  193. return this->pixelScale;
  194. }
  195. void DsrWindow::setPixelScale(int scale) {
  196. if (this->pixelScale != scale) {
  197. this->pixelScale = scale;
  198. // Update layout
  199. this->applyLayout();
  200. // The mouse moves relative to the canvas when scale changes
  201. this->sendMouseEvent(MouseEvent(MouseEventType::MouseMove, MouseKeyEnum::NoKey, this->lastMousePosition));
  202. }
  203. }
  204. void DsrWindow::setFullScreen(bool enabled) {
  205. if (this->backend->isFullScreen() != enabled) {
  206. this->backend->setFullScreen(enabled);
  207. // TODO: The mouse moves relative to the canvas when the window moves, but the new mouse location was never given.
  208. // How can mouse-move events be made consistent in applications when toggling full-screen without resorting to hacks?
  209. // Return the moved pixel offset from backend's setFullScreen?
  210. }
  211. }
  212. bool DsrWindow::isFullScreen() {
  213. return this->backend->isFullScreen();
  214. }
  215. void DsrWindow::drawComponents() {
  216. auto canvas = this->getCanvas();
  217. this->mainPanel->draw(canvas, IVector2D(0, 0));
  218. }
  219. void DsrWindow::flushDeferredActions() {
  220. this->mainPanel->flushDeferredActions();
  221. }
  222. AlignedImageRgbaU8 DsrWindow::getCanvas() {
  223. auto fullResolutionCanvas = this->backend->getCanvas();
  224. if (this->pixelScale > 1) {
  225. // Get low resolution canvas in deterministic RGBA pack order
  226. int smallWidth = getCanvasWidth();
  227. int smallHeight = getCanvasHeight();
  228. if (!image_exists(this->lowResolutionCanvas)
  229. || image_getWidth(this->lowResolutionCanvas) != smallWidth
  230. || image_getHeight(this->lowResolutionCanvas) != smallHeight) {
  231. this->lowResolutionCanvas = image_create_RgbaU8_native(smallWidth, smallHeight, image_getPackOrderIndex(fullResolutionCanvas));
  232. }
  233. return this->lowResolutionCanvas;
  234. } else {
  235. // Get full resolution canvas in arbitrary pack order
  236. return fullResolutionCanvas;
  237. }
  238. }
  239. void DsrWindow::showCanvas() {
  240. if (this->pixelScale > 1 && image_exists(this->lowResolutionCanvas)) {
  241. // Use an exact pixel size, by cutting into the last row and column when not even
  242. // This makes it easy to convert mouse coordinates using multiplication and division with pixelScale
  243. auto target = this->backend->getCanvas();
  244. auto source = this->getCanvas();
  245. filter_blockMagnify(target, source, this->pixelScale, this->pixelScale);
  246. }
  247. this->backend->showCanvas();
  248. }
  249. String DsrWindow::getTitle() {
  250. return this->backend->getTitle();
  251. }
  252. void DsrWindow::setTitle(const String &newTitle) {
  253. return this->backend->setTitle(newTitle);
  254. }
  255. void DsrWindow::applyTheme(VisualTheme theme) {
  256. this->mainPanel->applyTheme(theme);
  257. }
  258. VisualTheme DsrWindow::getTheme() {
  259. return this->mainPanel->getTheme();
  260. }