guiAPI.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. std::shared_ptr<dsr::BackendWindow> backend = createBackendWindow(title, width, height);
  35. if (backend.get() != nullptr) {
  36. return std::make_shared<DsrWindow>(backend);
  37. } else {
  38. return std::shared_ptr<DsrWindow>();
  39. }
  40. }
  41. Window dsr::window_create_fullscreen(const String& title) {
  42. return std::make_shared<DsrWindow>(createBackendWindow(title, 0, 0));
  43. }
  44. bool dsr::window_exists(const Window& window) {
  45. return window.get() != nullptr;
  46. }
  47. bool dsr::component_exists(const Component& component) {
  48. return component.get() != nullptr;
  49. }
  50. void dsr::window_loadInterfaceFromString(const Window& window, const String& content) {
  51. MUST_EXIST(window, window_loadInterfaceFromString);
  52. window->loadInterfaceFromString(content);
  53. }
  54. void dsr::window_loadInterfaceFromFile(const Window& window, const ReadableString& filename) {
  55. MUST_EXIST(window, window_loadInterfaceFromFile);
  56. window->loadInterfaceFromString(string_load(filename));
  57. }
  58. String dsr::window_saveInterfaceToString(const Window& window) {
  59. MUST_EXIST(window, window_saveInterfaceToString);
  60. return window->saveInterfaceToString();
  61. }
  62. Component dsr::window_getRoot(const Window& window) {
  63. MUST_EXIST(window, window_getRoot);
  64. return window->getRootComponent();
  65. }
  66. Component dsr::window_findComponentByName(const Window& window, const ReadableString& name, bool mustExist) {
  67. MUST_EXIST(window, window_findComponentByName);
  68. return window->findComponentByName(name);
  69. }
  70. Component dsr::window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist) {
  71. MUST_EXIST(window, window_findComponentByNameAndIndex);
  72. return window->findComponentByNameAndIndex(name, index);
  73. }
  74. bool dsr::window_executeEvents(const Window& window) {
  75. MUST_EXIST(window, window_executeEvents);
  76. return window->executeEvents();
  77. }
  78. void dsr::window_drawComponents(const Window& window) {
  79. MUST_EXIST(window, window_drawComponents);
  80. window->drawComponents();
  81. }
  82. void dsr::window_showCanvas(const Window& window) {
  83. MUST_EXIST(window, window_showCanvas);
  84. window->showCanvas();
  85. }
  86. int dsr::window_getPixelScale(const Window& window) {
  87. MUST_EXIST(window, window_getPixelScale);
  88. return window->getPixelScale();
  89. }
  90. void dsr::window_setPixelScale(const Window& window, int scale) {
  91. MUST_EXIST(window, window_setPixelScale);
  92. window->setPixelScale(scale);
  93. }
  94. void dsr::window_setFullScreen(const Window& window, bool enabled) {
  95. MUST_EXIST(window, window_setFullScreen);
  96. window->setFullScreen(enabled);
  97. }
  98. bool dsr::window_isFullScreen(const Window& window) {
  99. MUST_EXIST(window, window_isFullScreen);
  100. return window->isFullScreen();
  101. }
  102. AlignedImageRgbaU8 dsr::window_getCanvas(const Window& window) {
  103. MUST_EXIST(window, window_getCanvas);
  104. return window->getCanvas();
  105. }
  106. AlignedImageF32 dsr::window_getDepthBuffer(const Window& window) {
  107. MUST_EXIST(window, window_getDepthBuffer);
  108. return window->getDepthBuffer();
  109. }
  110. int dsr::window_getCanvasWidth(const Window& window) {
  111. MUST_EXIST(window, window_getCanvasWidth);
  112. return window->getCanvasWidth();
  113. }
  114. int dsr::window_getCanvasHeight(const Window& window) {
  115. MUST_EXIST(window, window_getCanvasHeight);
  116. return window->getCanvasHeight();
  117. }
  118. int dsr::window_getInnerWidth(const Window& window) {
  119. MUST_EXIST(window, window_getInnerWidth);
  120. return window->getInnerWidth();
  121. }
  122. int dsr::window_getInnerHeight(const Window& window) {
  123. MUST_EXIST(window, window_getInnerHeight);
  124. return window->getInnerHeight();
  125. }
  126. void dsr::window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent) {
  127. MUST_EXIST(window, window_setMouseEvent);
  128. window->windowMouseEvent() = mouseEvent;
  129. }
  130. void dsr::window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent) {
  131. MUST_EXIST(window, window_setKeyboardEvent);
  132. window->windowKeyboardEvent() = keyboardEvent;
  133. }
  134. void dsr::window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent) {
  135. MUST_EXIST(window, window_setCloseEvent);
  136. window->windowCloseEvent() = closeEvent;
  137. }
  138. void dsr::component_setPressedEvent(const Component& component, const EmptyCallback& event) {
  139. MUST_EXIST(component, component_setPressedEvent);
  140. component->pressedEvent() = event;
  141. }
  142. void dsr::component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent) {
  143. MUST_EXIST(component, component_setMouseDownEvent);
  144. component->mouseDownEvent() = mouseEvent;
  145. }
  146. void dsr::component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent) {
  147. MUST_EXIST(component, component_setMouseUpEvent);
  148. component->mouseUpEvent() = mouseEvent;
  149. }
  150. void dsr::component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent) {
  151. MUST_EXIST(component, component_setMouseMoveEvent);
  152. component->mouseMoveEvent() = mouseEvent;
  153. }
  154. void dsr::component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent) {
  155. MUST_EXIST(component, component_setMouseScrollEvent);
  156. component->mouseScrollEvent() = mouseEvent;
  157. }
  158. void dsr::component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  159. MUST_EXIST(component, component_setKeyDownEvent);
  160. component->keyDownEvent() = keyboardEvent;
  161. }
  162. void dsr::component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  163. MUST_EXIST(component, component_setKeyUpEvent);
  164. component->keyUpEvent() = keyboardEvent;
  165. }
  166. void dsr::component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  167. MUST_EXIST(component, component_setKeyTypeEvent);
  168. component->keyTypeEvent() = keyboardEvent;
  169. }
  170. bool dsr::component_hasProperty(const Component& component, const ReadableString& propertyName) {
  171. MUST_EXIST(component, component_hasProperty);
  172. Persistent* target = component->findAttribute(propertyName);
  173. return target != nullptr;
  174. }
  175. ReturnCode dsr::component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  176. MUST_EXIST(component, component_setProperty_string);
  177. Persistent* target = component->findAttribute(propertyName);
  178. if (target == nullptr) {
  179. if (mustAssign) {
  180. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  181. }
  182. return ReturnCode::KeyNotFound;
  183. } else {
  184. if (target->assignValue(value)) {
  185. component->changedAttribute(propertyName);
  186. return ReturnCode::Good;
  187. } else {
  188. if (mustAssign) {
  189. throwError("component_setProperty_string: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
  190. }
  191. return ReturnCode::ParsingFailure;
  192. }
  193. }
  194. }
  195. String dsr::component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist) {
  196. MUST_EXIST(component, component_getProperty_string);
  197. Persistent* target = component->findAttribute(propertyName);
  198. if (target == nullptr) {
  199. if (mustExist) {
  200. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  201. }
  202. return U"";
  203. } else {
  204. return target->toString();
  205. }
  206. }
  207. String dsr::component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments) {
  208. MUST_EXIST(component, component_call);
  209. return component->call(methodName, arguments);
  210. }
  211. String dsr::component_call(const Component& component, const ReadableString& methodName) {
  212. return component_call(component, methodName, U"");
  213. }
  214. void dsr::window_applyTheme(const Window& window, const VisualTheme& theme) {
  215. MUST_EXIST(window, window_applyTheme);
  216. MUST_EXIST(theme, window_applyTheme);
  217. window->applyTheme(theme);
  218. }