guiAPI.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. Component result = window->findComponentByName(name);
  70. if (mustExist && result.get() == nullptr) {
  71. throwError(U"window_findComponentByName: No child component named ", name, " found!");
  72. }
  73. return result;
  74. }
  75. Component dsr::window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist) {
  76. MUST_EXIST(window, window_findComponentByNameAndIndex);
  77. Component result = window->findComponentByNameAndIndex(name, index);
  78. if (mustExist && result.get() == nullptr) {
  79. throwError(U"window_findComponentByName: No child component named ", name, " with index ", index, " found!");
  80. }
  81. return result;
  82. }
  83. int dsr::component_getChildCount(const Component& parent) {
  84. if (parent.get()) {
  85. return parent->getChildCount();
  86. } else {
  87. return -1;
  88. }
  89. }
  90. Component dsr::component_getChild(const Component& parent, int childIndex) {
  91. if (parent.get()) {
  92. return std::dynamic_pointer_cast<VisualComponent>(parent->getChild(childIndex));
  93. } else {
  94. return std::shared_ptr<VisualComponent>(); // Null handle
  95. }
  96. }
  97. static void findAllComponentsByName(const Component& component, const ReadableString& name, std::function<void(Component, int)> callback) {
  98. if (component_exists(component)) {
  99. // Check if the current component matches
  100. if (string_match(component->getName(), name)) {
  101. callback(component, component->getIndex());
  102. }
  103. // Search among child components
  104. int childCount = component_getChildCount(component);
  105. for (int childIndex = childCount - 1; childIndex >= 0; childIndex--) {
  106. findAllComponentsByName(component_getChild(component, childIndex), name, callback);
  107. }
  108. }
  109. }
  110. void dsr::window_findAllComponentsByName(const Window& window, const ReadableString& name, std::function<void(Component, int)> callback) {
  111. MUST_EXIST(window, window_findAllComponentsByName);
  112. findAllComponentsByName(window->getRootComponent(), name, callback);
  113. }
  114. bool dsr::window_executeEvents(const Window& window) {
  115. MUST_EXIST(window, window_executeEvents);
  116. return window->executeEvents();
  117. }
  118. void dsr::window_drawComponents(const Window& window) {
  119. MUST_EXIST(window, window_drawComponents);
  120. window->drawComponents();
  121. }
  122. void dsr::window_showCanvas(const Window& window) {
  123. MUST_EXIST(window, window_showCanvas);
  124. window->showCanvas();
  125. }
  126. int dsr::window_getPixelScale(const Window& window) {
  127. MUST_EXIST(window, window_getPixelScale);
  128. return window->getPixelScale();
  129. }
  130. void dsr::window_setPixelScale(const Window& window, int scale) {
  131. MUST_EXIST(window, window_setPixelScale);
  132. window->setPixelScale(scale);
  133. }
  134. void dsr::window_setFullScreen(const Window& window, bool enabled) {
  135. MUST_EXIST(window, window_setFullScreen);
  136. window->setFullScreen(enabled);
  137. }
  138. bool dsr::window_isFullScreen(const Window& window) {
  139. MUST_EXIST(window, window_isFullScreen);
  140. return window->isFullScreen();
  141. }
  142. AlignedImageRgbaU8 dsr::window_getCanvas(const Window& window) {
  143. MUST_EXIST(window, window_getCanvas);
  144. return window->getCanvas();
  145. }
  146. AlignedImageF32 dsr::window_getDepthBuffer(const Window& window) {
  147. MUST_EXIST(window, window_getDepthBuffer);
  148. return window->getDepthBuffer();
  149. }
  150. int dsr::window_getCanvasWidth(const Window& window) {
  151. MUST_EXIST(window, window_getCanvasWidth);
  152. return window->getCanvasWidth();
  153. }
  154. int dsr::window_getCanvasHeight(const Window& window) {
  155. MUST_EXIST(window, window_getCanvasHeight);
  156. return window->getCanvasHeight();
  157. }
  158. int dsr::window_getInnerWidth(const Window& window) {
  159. MUST_EXIST(window, window_getInnerWidth);
  160. return window->getInnerWidth();
  161. }
  162. int dsr::window_getInnerHeight(const Window& window) {
  163. MUST_EXIST(window, window_getInnerHeight);
  164. return window->getInnerHeight();
  165. }
  166. void dsr::window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent) {
  167. MUST_EXIST(window, window_setMouseEvent);
  168. window->windowMouseEvent() = mouseEvent;
  169. }
  170. void dsr::window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent) {
  171. MUST_EXIST(window, window_setKeyboardEvent);
  172. window->windowKeyboardEvent() = keyboardEvent;
  173. }
  174. void dsr::window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent) {
  175. MUST_EXIST(window, window_setCloseEvent);
  176. window->windowCloseEvent() = closeEvent;
  177. }
  178. void dsr::component_setPressedEvent(const Component& component, const EmptyCallback& event) {
  179. MUST_EXIST(component, component_setPressedEvent);
  180. component->pressedEvent() = event;
  181. }
  182. void dsr::component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent) {
  183. MUST_EXIST(component, component_setMouseDownEvent);
  184. component->mouseDownEvent() = mouseEvent;
  185. }
  186. void dsr::component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent) {
  187. MUST_EXIST(component, component_setMouseUpEvent);
  188. component->mouseUpEvent() = mouseEvent;
  189. }
  190. void dsr::component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent) {
  191. MUST_EXIST(component, component_setMouseMoveEvent);
  192. component->mouseMoveEvent() = mouseEvent;
  193. }
  194. void dsr::component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent) {
  195. MUST_EXIST(component, component_setMouseScrollEvent);
  196. component->mouseScrollEvent() = mouseEvent;
  197. }
  198. void dsr::component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  199. MUST_EXIST(component, component_setKeyDownEvent);
  200. component->keyDownEvent() = keyboardEvent;
  201. }
  202. void dsr::component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  203. MUST_EXIST(component, component_setKeyUpEvent);
  204. component->keyUpEvent() = keyboardEvent;
  205. }
  206. void dsr::component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  207. MUST_EXIST(component, component_setKeyTypeEvent);
  208. component->keyTypeEvent() = keyboardEvent;
  209. }
  210. void dsr::component_setSelectEvent(const Component& component, const IndexCallback& selectEvent) {
  211. MUST_EXIST(component, component_setKeyTypeEvent);
  212. component->selectEvent() = selectEvent;
  213. }
  214. bool dsr::component_hasProperty(const Component& component, const ReadableString& propertyName) {
  215. MUST_EXIST(component, component_hasProperty);
  216. Persistent* target = component->findAttribute(propertyName);
  217. return target != nullptr;
  218. }
  219. ReturnCode dsr::component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  220. MUST_EXIST(component, component_setProperty);
  221. Persistent* target = component->findAttribute(propertyName);
  222. if (target == nullptr) {
  223. if (mustAssign) {
  224. throwError("component_setProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  225. }
  226. return ReturnCode::KeyNotFound;
  227. } else {
  228. if (target->assignValue(value)) {
  229. component->changedAttribute(propertyName);
  230. return ReturnCode::Good;
  231. } else {
  232. if (mustAssign) {
  233. throwError("component_setProperty: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
  234. }
  235. return ReturnCode::ParsingFailure;
  236. }
  237. }
  238. }
  239. String dsr::component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist) {
  240. MUST_EXIST(component, component_getProperty);
  241. Persistent* target = component->findAttribute(propertyName);
  242. if (target == nullptr) {
  243. if (mustExist) {
  244. throwError("component_getProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  245. }
  246. return U"";
  247. } else {
  248. return target->toString();
  249. }
  250. }
  251. ReturnCode dsr::component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  252. MUST_EXIST(component, component_setProperty_string);
  253. Persistent* target = component->findAttribute(propertyName);
  254. PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
  255. if (target == nullptr) {
  256. if (mustAssign) {
  257. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  258. }
  259. return ReturnCode::KeyNotFound;
  260. } else if (stringTarget == nullptr) {
  261. if (mustAssign) {
  262. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of a string.\n");
  263. }
  264. return ReturnCode::KeyNotFound;
  265. } else {
  266. stringTarget->value = value;
  267. component->changedAttribute(propertyName);
  268. return ReturnCode::Good;
  269. }
  270. }
  271. String dsr::component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist) {
  272. MUST_EXIST(component, component_getProperty_string);
  273. Persistent* target = component->findAttribute(propertyName);
  274. PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
  275. if (target == nullptr) {
  276. if (mustExist) {
  277. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  278. }
  279. return U"";
  280. } else if (stringTarget == nullptr) {
  281. if (mustExist) {
  282. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of a string.\n");
  283. }
  284. return U"";
  285. } else {
  286. return stringTarget->value;
  287. }
  288. }
  289. ReturnCode dsr::component_setProperty_integer(const Component& component, const ReadableString& propertyName, int64_t value, bool mustAssign) {
  290. MUST_EXIST(component, component_setProperty_integer);
  291. Persistent* target = component->findAttribute(propertyName);
  292. if (target == nullptr) {
  293. if (mustAssign) {
  294. throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  295. }
  296. return ReturnCode::KeyNotFound;
  297. } else {
  298. PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
  299. PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
  300. if (integerTarget != nullptr) {
  301. integerTarget->value = value;
  302. component->changedAttribute(propertyName);
  303. return ReturnCode::Good;
  304. } else if (booleanTarget != nullptr) {
  305. booleanTarget->value = value ? 1 : 0;
  306. component->changedAttribute(propertyName);
  307. return ReturnCode::Good;
  308. } else {
  309. if (mustAssign) {
  310. throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
  311. }
  312. return ReturnCode::KeyNotFound;
  313. }
  314. }
  315. }
  316. int64_t dsr::component_getProperty_integer(const Component& component, const ReadableString& propertyName, bool mustExist, int64_t defaultValue) {
  317. MUST_EXIST(component, component_getProperty_integer);
  318. Persistent* target = component->findAttribute(propertyName);
  319. if (target == nullptr) {
  320. if (mustExist) {
  321. throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  322. }
  323. return defaultValue;
  324. } else {
  325. PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
  326. PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
  327. if (integerTarget != nullptr) {
  328. return integerTarget->value;
  329. } else if (booleanTarget != nullptr) {
  330. return booleanTarget->value;
  331. } else {
  332. if (mustExist) {
  333. throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
  334. }
  335. return defaultValue;
  336. }
  337. }
  338. }
  339. ReturnCode dsr::component_setProperty_image(const Component& component, const ReadableString& propertyName, const OrderedImageRgbaU8& value, bool mustAssign) {
  340. MUST_EXIST(component, component_setProperty_image);
  341. Persistent* target = component->findAttribute(propertyName);
  342. if (target == nullptr) {
  343. if (mustAssign) {
  344. throwError("component_setProperty_image: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  345. }
  346. return ReturnCode::KeyNotFound;
  347. } else {
  348. PersistentImage* imageTarget = dynamic_cast<PersistentImage*>(target);
  349. if (imageTarget != nullptr) {
  350. imageTarget->value = value;
  351. component->changedAttribute(propertyName);
  352. return ReturnCode::Good;
  353. } else {
  354. if (mustAssign) {
  355. throwError("component_setProperty_image: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an image.\n");
  356. }
  357. return ReturnCode::KeyNotFound;
  358. }
  359. }
  360. }
  361. OrderedImageRgbaU8 dsr::component_getProperty_image(const Component& component, const ReadableString& propertyName, bool mustExist) {
  362. MUST_EXIST(component, component_getProperty_image);
  363. Persistent* target = component->findAttribute(propertyName);
  364. if (target == nullptr) {
  365. if (mustExist) {
  366. throwError("component_getProperty_image: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  367. }
  368. return OrderedImageRgbaU8();
  369. } else {
  370. PersistentImage* imageTarget = dynamic_cast<PersistentImage*>(target);
  371. if (imageTarget != nullptr) {
  372. return imageTarget->value;
  373. } else {
  374. if (mustExist) {
  375. throwError("component_getProperty_image: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an image.\n");
  376. }
  377. return OrderedImageRgbaU8();
  378. }
  379. }
  380. }
  381. String dsr::component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments) {
  382. MUST_EXIST(component, component_call);
  383. return component->call(methodName, arguments);
  384. }
  385. String dsr::component_call(const Component& component, const ReadableString& methodName) {
  386. return component_call(component, methodName, U"");
  387. }
  388. void dsr::component_detachFromParent(const Component& component) {
  389. MUST_EXIST(component, component_detachFromParent);
  390. component->detachFromParent();
  391. }
  392. Component dsr::component_create(const Component& parent, const ReadableString& className, const ReadableString& identifierName, int index) {
  393. // Making sure that the default components exist before trying to create a component manually.
  394. gui_initialize();
  395. // Creating a component from the name
  396. Component child = std::dynamic_pointer_cast<VisualComponent>(createPersistentClass(className));
  397. if (child) {
  398. child->setName(identifierName);
  399. child->setIndex(index);
  400. // Attaching to a parent is optional, but convenient to do in the same call.
  401. if (parent) {
  402. parent->addChildComponent(child);
  403. }
  404. }
  405. return child;
  406. }
  407. void dsr::window_applyTheme(const Window& window, const VisualTheme& theme) {
  408. MUST_EXIST(window, window_applyTheme);
  409. MUST_EXIST(theme, window_applyTheme);
  410. window->applyTheme(theme);
  411. }