guiAPI.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 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. #define DFPSR_INTERNAL_ACCESS
  25. #include "guiAPI.h"
  26. #include "timeAPI.h"
  27. #include "../gui/DsrWindow.h"
  28. using namespace dsr;
  29. // To be implemented outside of the core framework
  30. std::shared_ptr<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height);
  31. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.get() == nullptr) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  32. Window dsr::window_create(const String& title, int32_t width, int32_t height) {
  33. if (width < 1) { width = 1; }
  34. if (height < 1) { height = 1; }
  35. std::shared_ptr<dsr::BackendWindow> backend = createBackendWindow(title, width, height);
  36. if (backend.get() != nullptr) {
  37. return std::make_shared<DsrWindow>(backend);
  38. } else {
  39. return std::shared_ptr<DsrWindow>();
  40. }
  41. }
  42. Window dsr::window_create_fullscreen(const String& title) {
  43. return std::make_shared<DsrWindow>(createBackendWindow(title, 0, 0));
  44. }
  45. bool dsr::window_exists(const Window& window) {
  46. return window.get() != nullptr;
  47. }
  48. bool dsr::component_exists(const Component& component) {
  49. return component.get() != nullptr;
  50. }
  51. void dsr::window_loadInterfaceFromString(const Window& window, const String& content) {
  52. MUST_EXIST(window, window_loadInterfaceFromString);
  53. window->loadInterfaceFromString(content);
  54. }
  55. void dsr::window_loadInterfaceFromFile(const Window& window, const ReadableString& filename) {
  56. MUST_EXIST(window, window_loadInterfaceFromFile);
  57. window->loadInterfaceFromString(string_load(filename));
  58. }
  59. String dsr::window_saveInterfaceToString(const Window& window) {
  60. MUST_EXIST(window, window_saveInterfaceToString);
  61. return window->saveInterfaceToString();
  62. }
  63. Component dsr::window_getRoot(const Window& window) {
  64. MUST_EXIST(window, window_getRoot);
  65. return window->getRootComponent();
  66. }
  67. Component dsr::window_findComponentByName(const Window& window, const ReadableString& name, bool mustExist) {
  68. MUST_EXIST(window, window_findComponentByName);
  69. return window->findComponentByName(name);
  70. }
  71. Component dsr::window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist) {
  72. MUST_EXIST(window, window_findComponentByNameAndIndex);
  73. return window->findComponentByNameAndIndex(name, index);
  74. }
  75. bool dsr::window_executeEvents(const Window& window) {
  76. MUST_EXIST(window, window_executeEvents);
  77. return window->executeEvents();
  78. }
  79. void dsr::window_drawComponents(const Window& window) {
  80. MUST_EXIST(window, window_drawComponents);
  81. window->drawComponents();
  82. }
  83. void dsr::window_showCanvas(const Window& window) {
  84. MUST_EXIST(window, window_showCanvas);
  85. window->showCanvas();
  86. }
  87. int dsr::window_getPixelScale(const Window& window) {
  88. MUST_EXIST(window, window_getPixelScale);
  89. return window->getPixelScale();
  90. }
  91. void dsr::window_setPixelScale(const Window& window, int scale) {
  92. MUST_EXIST(window, window_setPixelScale);
  93. window->setPixelScale(scale);
  94. }
  95. void dsr::window_setFullScreen(const Window& window, bool enabled) {
  96. MUST_EXIST(window, window_setFullScreen);
  97. window->setFullScreen(enabled);
  98. }
  99. bool dsr::window_isFullScreen(const Window& window) {
  100. MUST_EXIST(window, window_isFullScreen);
  101. return window->isFullScreen();
  102. }
  103. AlignedImageRgbaU8 dsr::window_getCanvas(const Window& window) {
  104. MUST_EXIST(window, window_getCanvas);
  105. return window->getCanvas();
  106. }
  107. AlignedImageF32 dsr::window_getDepthBuffer(const Window& window) {
  108. MUST_EXIST(window, window_getDepthBuffer);
  109. return window->getDepthBuffer();
  110. }
  111. int dsr::window_getCanvasWidth(const Window& window) {
  112. MUST_EXIST(window, window_getCanvasWidth);
  113. return window->getCanvasWidth();
  114. }
  115. int dsr::window_getCanvasHeight(const Window& window) {
  116. MUST_EXIST(window, window_getCanvasHeight);
  117. return window->getCanvasHeight();
  118. }
  119. int dsr::window_getInnerWidth(const Window& window) {
  120. MUST_EXIST(window, window_getInnerWidth);
  121. return window->getInnerWidth();
  122. }
  123. int dsr::window_getInnerHeight(const Window& window) {
  124. MUST_EXIST(window, window_getInnerHeight);
  125. return window->getInnerHeight();
  126. }
  127. void dsr::window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent) {
  128. MUST_EXIST(window, window_setMouseEvent);
  129. window->windowMouseEvent() = mouseEvent;
  130. }
  131. void dsr::window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent) {
  132. MUST_EXIST(window, window_setKeyboardEvent);
  133. window->windowKeyboardEvent() = keyboardEvent;
  134. }
  135. void dsr::window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent) {
  136. MUST_EXIST(window, window_setCloseEvent);
  137. window->windowCloseEvent() = closeEvent;
  138. }
  139. void dsr::component_setPressedEvent(const Component& component, const EmptyCallback& event) {
  140. MUST_EXIST(component, component_setPressedEvent);
  141. component->pressedEvent() = event;
  142. }
  143. void dsr::component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent) {
  144. MUST_EXIST(component, component_setMouseDownEvent);
  145. component->mouseDownEvent() = mouseEvent;
  146. }
  147. void dsr::component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent) {
  148. MUST_EXIST(component, component_setMouseUpEvent);
  149. component->mouseUpEvent() = mouseEvent;
  150. }
  151. void dsr::component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent) {
  152. MUST_EXIST(component, component_setMouseMoveEvent);
  153. component->mouseMoveEvent() = mouseEvent;
  154. }
  155. void dsr::component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent) {
  156. MUST_EXIST(component, component_setMouseScrollEvent);
  157. component->mouseScrollEvent() = mouseEvent;
  158. }
  159. void dsr::component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  160. MUST_EXIST(component, component_setKeyDownEvent);
  161. component->keyDownEvent() = keyboardEvent;
  162. }
  163. void dsr::component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  164. MUST_EXIST(component, component_setKeyUpEvent);
  165. component->keyUpEvent() = keyboardEvent;
  166. }
  167. void dsr::component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  168. MUST_EXIST(component, component_setKeyTypeEvent);
  169. component->keyTypeEvent() = keyboardEvent;
  170. }
  171. void dsr::component_setSelectEvent(const Component& component, const IndexCallback& selectEvent) {
  172. MUST_EXIST(component, component_setKeyTypeEvent);
  173. component->selectEvent() = selectEvent;
  174. }
  175. bool dsr::component_hasProperty(const Component& component, const ReadableString& propertyName) {
  176. MUST_EXIST(component, component_hasProperty);
  177. Persistent* target = component->findAttribute(propertyName);
  178. return target != nullptr;
  179. }
  180. ReturnCode dsr::component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  181. MUST_EXIST(component, component_setProperty);
  182. Persistent* target = component->findAttribute(propertyName);
  183. if (target == nullptr) {
  184. if (mustAssign) {
  185. throwError("component_setProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  186. }
  187. return ReturnCode::KeyNotFound;
  188. } else {
  189. if (target->assignValue(value)) {
  190. component->changedAttribute(propertyName);
  191. return ReturnCode::Good;
  192. } else {
  193. if (mustAssign) {
  194. throwError("component_setProperty: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
  195. }
  196. return ReturnCode::ParsingFailure;
  197. }
  198. }
  199. }
  200. String dsr::component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist) {
  201. MUST_EXIST(component, component_getProperty);
  202. Persistent* target = component->findAttribute(propertyName);
  203. if (target == nullptr) {
  204. if (mustExist) {
  205. throwError("component_getProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  206. }
  207. return U"";
  208. } else {
  209. return target->toString();
  210. }
  211. }
  212. ReturnCode dsr::component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  213. MUST_EXIST(component, component_setProperty_string);
  214. Persistent* target = component->findAttribute(propertyName);
  215. PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
  216. if (target == nullptr) {
  217. if (mustAssign) {
  218. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  219. }
  220. return ReturnCode::KeyNotFound;
  221. } else if (stringTarget == nullptr) {
  222. if (mustAssign) {
  223. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of a string.\n");
  224. }
  225. return ReturnCode::KeyNotFound;
  226. } else {
  227. stringTarget->value = value;
  228. return ReturnCode::Good;
  229. }
  230. }
  231. String dsr::component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist) {
  232. MUST_EXIST(component, component_getProperty_string);
  233. Persistent* target = component->findAttribute(propertyName);
  234. PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
  235. if (target == nullptr) {
  236. if (mustExist) {
  237. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  238. }
  239. return U"";
  240. } else if (stringTarget == nullptr) {
  241. if (mustExist) {
  242. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of a string.\n");
  243. }
  244. return U"";
  245. } else {
  246. return stringTarget->value;
  247. }
  248. }
  249. ReturnCode dsr::component_setProperty_integer(const Component& component, const ReadableString& propertyName, int64_t value, bool mustAssign) {
  250. MUST_EXIST(component, component_setProperty_integer);
  251. Persistent* target = component->findAttribute(propertyName);
  252. if (target == nullptr) {
  253. if (mustAssign) {
  254. throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  255. }
  256. return ReturnCode::KeyNotFound;
  257. } else {
  258. PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
  259. PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
  260. if (integerTarget != nullptr) {
  261. integerTarget->value = value;
  262. return ReturnCode::Good;
  263. } else if (booleanTarget != nullptr) {
  264. booleanTarget->value = value ? 1 : 0;
  265. return ReturnCode::Good;
  266. } else {
  267. if (mustAssign) {
  268. throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
  269. }
  270. return ReturnCode::KeyNotFound;
  271. }
  272. }
  273. }
  274. int64_t dsr::component_getProperty_integer(const Component& component, const ReadableString& propertyName, bool mustExist, int64_t defaultValue) {
  275. MUST_EXIST(component, component_getProperty_integer);
  276. Persistent* target = component->findAttribute(propertyName);
  277. if (target == nullptr) {
  278. if (mustExist) {
  279. throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  280. }
  281. return defaultValue;
  282. } else {
  283. PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
  284. PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
  285. if (integerTarget != nullptr) {
  286. return integerTarget->value;
  287. } else if (booleanTarget != nullptr) {
  288. return booleanTarget->value;
  289. } else {
  290. if (mustExist) {
  291. throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
  292. }
  293. return defaultValue;
  294. }
  295. }
  296. }
  297. String dsr::component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments) {
  298. MUST_EXIST(component, component_call);
  299. return component->call(methodName, arguments);
  300. }
  301. String dsr::component_call(const Component& component, const ReadableString& methodName) {
  302. return component_call(component, methodName, U"");
  303. }
  304. void dsr::window_applyTheme(const Window& window, const VisualTheme& theme) {
  305. MUST_EXIST(window, window_applyTheme);
  306. MUST_EXIST(theme, window_applyTheme);
  307. window->applyTheme(theme);
  308. }