guiAPI.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #define DFPSR_INTERNAL_ACCESS
  24. #include "guiAPI.h"
  25. #include "timeAPI.h"
  26. #include "../gui/DsrWindow.h"
  27. using namespace dsr;
  28. // To be implemented outside of the core framework
  29. std::shared_ptr<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height);
  30. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.get() == nullptr) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  31. Window dsr::window_create(const String& title, int32_t width, int32_t height) {
  32. if (width < 1) { width = 1; }
  33. if (height < 1) { height = 1; }
  34. return std::make_shared<DsrWindow>(createBackendWindow(title, width, height));
  35. }
  36. Window dsr::window_create_fullscreen(const String& title) {
  37. return std::make_shared<DsrWindow>(createBackendWindow(title, 0, 0));
  38. }
  39. bool dsr::window_exists(const Window& window) {
  40. return window.get() != nullptr;
  41. }
  42. bool dsr::component_exists(const Component& component) {
  43. return component.get() != nullptr;
  44. }
  45. void dsr::window_loadInterfaceFromString(const Window& window, const String& content) {
  46. MUST_EXIST(window, window_loadInterfaceFromString);
  47. window->loadInterfaceFromString(content);
  48. }
  49. void dsr::window_loadInterfaceFromFile(const Window& window, const ReadableString& filename) {
  50. MUST_EXIST(window, window_loadInterfaceFromFile);
  51. window->loadInterfaceFromString(string_load(filename));
  52. }
  53. String dsr::window_saveInterfaceToString(const Window& window) {
  54. MUST_EXIST(window, window_saveInterfaceToString);
  55. return window->saveInterfaceToString();
  56. }
  57. Component dsr::window_getRoot(const Window& window) {
  58. MUST_EXIST(window, window_getRoot);
  59. return window->getRootComponent();
  60. }
  61. Component dsr::window_findComponentByName(const Window& window, const ReadableString& name, bool mustExist) {
  62. MUST_EXIST(window, window_findComponentByName);
  63. return window->findComponentByName(name);
  64. }
  65. Component dsr::window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist) {
  66. MUST_EXIST(window, window_findComponentByNameAndIndex);
  67. return window->findComponentByNameAndIndex(name, index);
  68. }
  69. bool dsr::window_executeEvents(const Window& window) {
  70. MUST_EXIST(window, window_executeEvents);
  71. return window->executeEvents();
  72. }
  73. void dsr::window_drawComponents(const Window& window) {
  74. MUST_EXIST(window, window_drawComponents);
  75. window->drawComponents();
  76. }
  77. void dsr::window_showCanvas(const Window& window) {
  78. MUST_EXIST(window, window_showCanvas);
  79. window->showCanvas();
  80. }
  81. int dsr::window_getPixelScale(const Window& window) {
  82. MUST_EXIST(window, window_getPixelScale);
  83. return window->getPixelScale();
  84. }
  85. void dsr::window_setPixelScale(const Window& window, int scale) {
  86. MUST_EXIST(window, window_setPixelScale);
  87. window->setPixelScale(scale);
  88. }
  89. void dsr::window_setFullScreen(const Window& window, bool enabled) {
  90. MUST_EXIST(window, window_setFullScreen);
  91. window->setFullScreen(enabled);
  92. }
  93. bool dsr::window_isFullScreen(const Window& window) {
  94. MUST_EXIST(window, window_isFullScreen);
  95. return window->isFullScreen();
  96. }
  97. AlignedImageRgbaU8 dsr::window_getCanvas(const Window& window) {
  98. MUST_EXIST(window, window_getCanvas);
  99. return window->getCanvas();
  100. }
  101. AlignedImageF32 dsr::window_getDepthBuffer(const Window& window) {
  102. MUST_EXIST(window, window_getDepthBuffer);
  103. return window->getDepthBuffer();
  104. }
  105. int dsr::window_getCanvasWidth(const Window& window) {
  106. MUST_EXIST(window, window_getCanvasWidth);
  107. return window->getCanvasWidth();
  108. }
  109. int dsr::window_getCanvasHeight(const Window& window) {
  110. MUST_EXIST(window, window_getCanvasHeight);
  111. return window->getCanvasHeight();
  112. }
  113. int dsr::window_getInnerWidth(const Window& window) {
  114. MUST_EXIST(window, window_getInnerWidth);
  115. return window->getInnerWidth();
  116. }
  117. int dsr::window_getInnerHeight(const Window& window) {
  118. MUST_EXIST(window, window_getInnerHeight);
  119. return window->getInnerHeight();
  120. }
  121. void dsr::window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent) {
  122. MUST_EXIST(window, window_setMouseEvent);
  123. window->windowMouseEvent() = mouseEvent;
  124. }
  125. void dsr::window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent) {
  126. MUST_EXIST(window, window_setKeyboardEvent);
  127. window->windowKeyboardEvent() = keyboardEvent;
  128. }
  129. void dsr::window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent) {
  130. MUST_EXIST(window, window_setCloseEvent);
  131. window->windowCloseEvent() = closeEvent;
  132. }
  133. void dsr::component_setPressedEvent(const Component& component, const EmptyCallback& event) {
  134. MUST_EXIST(component, component_setPressedEvent);
  135. component->pressedEvent() = event;
  136. }
  137. void dsr::component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent) {
  138. MUST_EXIST(component, component_setMouseDownEvent);
  139. component->mouseDownEvent() = mouseEvent;
  140. }
  141. void dsr::component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent) {
  142. MUST_EXIST(component, component_setMouseUpEvent);
  143. component->mouseUpEvent() = mouseEvent;
  144. }
  145. void dsr::component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent) {
  146. MUST_EXIST(component, component_setMouseMoveEvent);
  147. component->mouseMoveEvent() = mouseEvent;
  148. }
  149. void dsr::component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent) {
  150. MUST_EXIST(component, component_setMouseScrollEvent);
  151. component->mouseScrollEvent() = mouseEvent;
  152. }
  153. void dsr::component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  154. MUST_EXIST(component, component_setKeyDownEvent);
  155. component->keyDownEvent() = keyboardEvent;
  156. }
  157. void dsr::component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  158. MUST_EXIST(component, component_setKeyUpEvent);
  159. component->keyUpEvent() = keyboardEvent;
  160. }
  161. void dsr::component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  162. MUST_EXIST(component, component_setKeyTypeEvent);
  163. component->keyTypeEvent() = keyboardEvent;
  164. }
  165. bool dsr::component_hasProperty(const Component& component, const ReadableString& propertyName) {
  166. MUST_EXIST(component, component_hasProperty);
  167. Persistent* target = component->findAttribute(propertyName);
  168. return target != nullptr;
  169. }
  170. ReturnCode dsr::component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  171. MUST_EXIST(component, component_setProperty_string);
  172. Persistent* target = component->findAttribute(propertyName);
  173. if (target == nullptr) {
  174. if (mustAssign) {
  175. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  176. }
  177. return ReturnCode::KeyNotFound;
  178. } else {
  179. if (target->assignValue(value)) {
  180. return ReturnCode::Good;
  181. } else {
  182. if (mustAssign) {
  183. throwError("component_setProperty_string: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
  184. }
  185. return ReturnCode::ParsingFailure;
  186. }
  187. }
  188. }
  189. String dsr::component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist) {
  190. MUST_EXIST(component, component_getProperty_string);
  191. Persistent* target = component->findAttribute(propertyName);
  192. if (target == nullptr) {
  193. if (mustExist) {
  194. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  195. }
  196. return U"";
  197. } else {
  198. return component->toString();
  199. }
  200. }
  201. void dsr::window_applyTheme(const Window& window, const VisualTheme& theme) {
  202. MUST_EXIST(window, window_applyTheme);
  203. MUST_EXIST(theme, window_applyTheme);
  204. window->applyTheme(theme);
  205. }