guiAPI.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. #include "fileAPI.h"
  29. using namespace dsr;
  30. // To be implemented outside of the core framework
  31. std::shared_ptr<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height);
  32. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.get() == nullptr) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  33. Window dsr::window_create(const String& title, int32_t width, int32_t height) {
  34. if (width < 1) { width = 1; }
  35. if (height < 1) { height = 1; }
  36. std::shared_ptr<dsr::BackendWindow> backend = createBackendWindow(title, width, height);
  37. if (backend.get() != nullptr) {
  38. return std::make_shared<DsrWindow>(backend);
  39. } else {
  40. return std::shared_ptr<DsrWindow>();
  41. }
  42. }
  43. Window dsr::window_create_fullscreen(const String& title) {
  44. return std::make_shared<DsrWindow>(createBackendWindow(title, 0, 0));
  45. }
  46. bool dsr::window_exists(const Window& window) {
  47. return window.get() != nullptr;
  48. }
  49. bool dsr::component_exists(const Component& component) {
  50. return component.get() != nullptr;
  51. }
  52. void dsr::window_loadInterfaceFromString(const Window& window, const String& content, const ReadableString &fromPath) {
  53. MUST_EXIST(window, window_loadInterfaceFromString);
  54. window->loadInterfaceFromString(content, fromPath);
  55. }
  56. void dsr::window_loadInterfaceFromString(const Window& window, const String& content) {
  57. MUST_EXIST(window, window_loadInterfaceFromString);
  58. window->loadInterfaceFromString(content, file_getCurrentPath());
  59. }
  60. void dsr::window_loadInterfaceFromFile(const Window& window, const ReadableString& filename) {
  61. MUST_EXIST(window, window_loadInterfaceFromFile);
  62. window->loadInterfaceFromString(string_load(filename), file_getRelativeParentFolder(filename));
  63. }
  64. String dsr::window_saveInterfaceToString(const Window& window) {
  65. MUST_EXIST(window, window_saveInterfaceToString);
  66. return window->saveInterfaceToString();
  67. }
  68. Component dsr::window_getRoot(const Window& window) {
  69. MUST_EXIST(window, window_getRoot);
  70. return window->getRootComponent();
  71. }
  72. Component dsr::component_createWithInterfaceFromString(Component& parent, const String& content, const ReadableString &fromPath) {
  73. MUST_EXIST(parent, component_createWithInterfaceFromString);
  74. Component result = std::dynamic_pointer_cast<VisualComponent>(createPersistentClassFromText(content, fromPath));
  75. if (result.get() == nullptr) {
  76. throwError(U"component_createWithInterfaceFromString: The component could not be created!\n\nLayout:\n", content, "\n");
  77. }
  78. parent->addChildComponent(result);
  79. result->applyLayout(parent->getSize());
  80. return result;
  81. }
  82. Component dsr::component_createWithInterfaceFromString(Component& parent, const String& content) {
  83. return component_createWithInterfaceFromString(parent, content, file_getCurrentPath());
  84. }
  85. Component dsr::component_createWithInterfaceFromFile(Component& parent, const String& filename) {
  86. return component_createWithInterfaceFromString(parent, string_load(filename), file_getRelativeParentFolder(filename));
  87. }
  88. Component dsr::component_findChildByName(const Component& parent, const ReadableString& name, bool mustExist) {
  89. MUST_EXIST(parent, component_findChildByName);
  90. return parent->findChildByName(name);
  91. }
  92. Component dsr::component_findChildByNameAndIndex(const Component& parent, const ReadableString& name, int index, bool mustExist) {
  93. MUST_EXIST(parent, component_findChildByNameAndIndex);
  94. return parent->findChildByNameAndIndex(name, index);
  95. }
  96. Component dsr::window_findComponentByName(const Window& window, const ReadableString& name, bool mustExist) {
  97. MUST_EXIST(window, window_findComponentByName);
  98. Component result = window->findComponentByName(name);
  99. if (mustExist && result.get() == nullptr) {
  100. throwError(U"window_findComponentByName: No child component named ", name, " found!");
  101. }
  102. return result;
  103. }
  104. Component dsr::window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist) {
  105. MUST_EXIST(window, window_findComponentByNameAndIndex);
  106. Component result = window->findComponentByNameAndIndex(name, index);
  107. if (mustExist && result.get() == nullptr) {
  108. throwError(U"window_findComponentByName: No child component named ", name, " with index ", index, " found!");
  109. }
  110. return result;
  111. }
  112. int dsr::component_getChildCount(const Component& parent) {
  113. if (parent.get()) {
  114. return parent->getChildCount();
  115. } else {
  116. return -1;
  117. }
  118. }
  119. Component dsr::component_getChild(const Component& parent, int childIndex) {
  120. if (parent.get()) {
  121. return std::dynamic_pointer_cast<VisualComponent>(parent->getChild(childIndex));
  122. } else {
  123. return std::shared_ptr<VisualComponent>(); // Null handle
  124. }
  125. }
  126. static void findAllComponentsByName(const Component& component, const ReadableString& name, std::function<void(Component, int)> callback) {
  127. if (component_exists(component)) {
  128. // Check if the current component matches
  129. if (string_match(component->getName(), name)) {
  130. callback(component, component->getIndex());
  131. }
  132. // Search among child components
  133. int childCount = component_getChildCount(component);
  134. for (int childIndex = childCount - 1; childIndex >= 0; childIndex--) {
  135. findAllComponentsByName(component_getChild(component, childIndex), name, callback);
  136. }
  137. }
  138. }
  139. void dsr::window_findAllComponentsByName(const Window& window, const ReadableString& name, std::function<void(Component, int)> callback) {
  140. MUST_EXIST(window, window_findAllComponentsByName);
  141. findAllComponentsByName(window->getRootComponent(), name, callback);
  142. }
  143. bool dsr::window_executeEvents(const Window& window) {
  144. MUST_EXIST(window, window_executeEvents);
  145. return window->executeEvents();
  146. }
  147. void dsr::window_drawComponents(const Window& window) {
  148. MUST_EXIST(window, window_drawComponents);
  149. window->drawComponents();
  150. }
  151. void dsr::window_showCanvas(const Window& window) {
  152. MUST_EXIST(window, window_showCanvas);
  153. window->showCanvas();
  154. }
  155. int dsr::window_getPixelScale(const Window& window) {
  156. MUST_EXIST(window, window_getPixelScale);
  157. return window->getPixelScale();
  158. }
  159. void dsr::window_setPixelScale(const Window& window, int scale) {
  160. MUST_EXIST(window, window_setPixelScale);
  161. window->setPixelScale(scale);
  162. }
  163. bool dsr::window_setCursorVisibility(const Window& window, bool visible) {
  164. MUST_EXIST(window, window_setCursorVisibility);
  165. return window->backend->setCursorVisibility(visible);
  166. }
  167. bool dsr::window_getCursorVisibility(const Window& window) {
  168. MUST_EXIST(window, window_getCursorVisibility);
  169. return window->backend->visibleCursor;
  170. }
  171. void dsr::window_setCursorPosition(const Window& window, int x, int y) {
  172. MUST_EXIST(window, window_setCursorPosition);
  173. window->backend->setCursorPosition(x, y);
  174. }
  175. void dsr::window_setFullScreen(const Window& window, bool enabled) {
  176. MUST_EXIST(window, window_setFullScreen);
  177. window->setFullScreen(enabled);
  178. }
  179. bool dsr::window_isFullScreen(const Window& window) {
  180. MUST_EXIST(window, window_isFullScreen);
  181. return window->isFullScreen();
  182. }
  183. AlignedImageRgbaU8 dsr::window_getCanvas(const Window& window) {
  184. MUST_EXIST(window, window_getCanvas);
  185. return window->getCanvas();
  186. }
  187. AlignedImageF32 dsr::window_getDepthBuffer(const Window& window) {
  188. MUST_EXIST(window, window_getDepthBuffer);
  189. return window->getDepthBuffer();
  190. }
  191. int dsr::window_getCanvasWidth(const Window& window) {
  192. MUST_EXIST(window, window_getCanvasWidth);
  193. return window->getCanvasWidth();
  194. }
  195. int dsr::window_getCanvasHeight(const Window& window) {
  196. MUST_EXIST(window, window_getCanvasHeight);
  197. return window->getCanvasHeight();
  198. }
  199. int dsr::window_getInnerWidth(const Window& window) {
  200. MUST_EXIST(window, window_getInnerWidth);
  201. return window->getInnerWidth();
  202. }
  203. int dsr::window_getInnerHeight(const Window& window) {
  204. MUST_EXIST(window, window_getInnerHeight);
  205. return window->getInnerHeight();
  206. }
  207. void dsr::window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent) {
  208. MUST_EXIST(window, window_setMouseEvent);
  209. window->windowMouseEvent() = mouseEvent;
  210. }
  211. void dsr::window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent) {
  212. MUST_EXIST(window, window_setKeyboardEvent);
  213. window->windowKeyboardEvent() = keyboardEvent;
  214. }
  215. void dsr::window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent) {
  216. MUST_EXIST(window, window_setCloseEvent);
  217. window->windowCloseEvent() = closeEvent;
  218. }
  219. void dsr::component_setPressedEvent(const Component& component, const EmptyCallback& event) {
  220. MUST_EXIST(component, component_setPressedEvent);
  221. component->pressedEvent() = event;
  222. }
  223. void dsr::component_setDestroyEvent(const Component& component, const EmptyCallback& event) {
  224. MUST_EXIST(component, component_setDestroyEvent);
  225. component->destroyEvent() = event;
  226. }
  227. void dsr::component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent) {
  228. MUST_EXIST(component, component_setMouseDownEvent);
  229. component->mouseDownEvent() = mouseEvent;
  230. }
  231. void dsr::component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent) {
  232. MUST_EXIST(component, component_setMouseUpEvent);
  233. component->mouseUpEvent() = mouseEvent;
  234. }
  235. void dsr::component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent) {
  236. MUST_EXIST(component, component_setMouseMoveEvent);
  237. component->mouseMoveEvent() = mouseEvent;
  238. }
  239. void dsr::component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent) {
  240. MUST_EXIST(component, component_setMouseScrollEvent);
  241. component->mouseScrollEvent() = mouseEvent;
  242. }
  243. void dsr::component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  244. MUST_EXIST(component, component_setKeyDownEvent);
  245. component->keyDownEvent() = keyboardEvent;
  246. }
  247. void dsr::component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  248. MUST_EXIST(component, component_setKeyUpEvent);
  249. component->keyUpEvent() = keyboardEvent;
  250. }
  251. void dsr::component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent) {
  252. MUST_EXIST(component, component_setKeyTypeEvent);
  253. component->keyTypeEvent() = keyboardEvent;
  254. }
  255. void dsr::component_setSelectEvent(const Component& component, const IndexCallback& selectEvent) {
  256. MUST_EXIST(component, component_setSelectEvent);
  257. component->selectEvent() = selectEvent;
  258. }
  259. bool dsr::component_hasProperty(const Component& component, const ReadableString& propertyName) {
  260. MUST_EXIST(component, component_hasProperty);
  261. Persistent* target = component->findAttribute(propertyName);
  262. return target != nullptr;
  263. }
  264. ReturnCode dsr::component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, const ReadableString& fromPath, bool mustAssign) {
  265. MUST_EXIST(component, component_setProperty);
  266. Persistent* target = component->findAttribute(propertyName);
  267. if (target == nullptr) {
  268. if (mustAssign) {
  269. throwError("component_setProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  270. }
  271. return ReturnCode::KeyNotFound;
  272. } else {
  273. if (target->assignValue(value, fromPath)) {
  274. component->changedAttribute(propertyName);
  275. return ReturnCode::Good;
  276. } else {
  277. if (mustAssign) {
  278. throwError("component_setProperty: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
  279. }
  280. return ReturnCode::ParsingFailure;
  281. }
  282. }
  283. }
  284. ReturnCode dsr::component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  285. return component_setProperty(component, propertyName, value, file_getCurrentPath(), mustAssign);
  286. }
  287. String dsr::component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist) {
  288. MUST_EXIST(component, component_getProperty);
  289. Persistent* target = component->findAttribute(propertyName);
  290. if (target == nullptr) {
  291. if (mustExist) {
  292. throwError("component_getProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  293. }
  294. return U"";
  295. } else {
  296. return target->toString();
  297. }
  298. }
  299. ReturnCode dsr::component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
  300. MUST_EXIST(component, component_setProperty_string);
  301. Persistent* target = component->findAttribute(propertyName);
  302. PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
  303. if (target == nullptr) {
  304. if (mustAssign) {
  305. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  306. }
  307. return ReturnCode::KeyNotFound;
  308. } else if (stringTarget == nullptr) {
  309. if (mustAssign) {
  310. throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of a string.\n");
  311. }
  312. return ReturnCode::KeyNotFound;
  313. } else {
  314. stringTarget->value = value;
  315. component->changedAttribute(propertyName);
  316. return ReturnCode::Good;
  317. }
  318. }
  319. String dsr::component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist) {
  320. MUST_EXIST(component, component_getProperty_string);
  321. Persistent* target = component->findAttribute(propertyName);
  322. PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
  323. if (target == nullptr) {
  324. if (mustExist) {
  325. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  326. }
  327. return U"";
  328. } else if (stringTarget == nullptr) {
  329. if (mustExist) {
  330. throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of a string.\n");
  331. }
  332. return U"";
  333. } else {
  334. return stringTarget->value;
  335. }
  336. }
  337. ReturnCode dsr::component_setProperty_integer(const Component& component, const ReadableString& propertyName, int64_t value, bool mustAssign) {
  338. MUST_EXIST(component, component_setProperty_integer);
  339. Persistent* target = component->findAttribute(propertyName);
  340. if (target == nullptr) {
  341. if (mustAssign) {
  342. throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  343. }
  344. return ReturnCode::KeyNotFound;
  345. } else {
  346. PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
  347. PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
  348. if (integerTarget != nullptr) {
  349. integerTarget->value = value;
  350. component->changedAttribute(propertyName);
  351. return ReturnCode::Good;
  352. } else if (booleanTarget != nullptr) {
  353. booleanTarget->value = value ? 1 : 0;
  354. component->changedAttribute(propertyName);
  355. return ReturnCode::Good;
  356. } else {
  357. if (mustAssign) {
  358. throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
  359. }
  360. return ReturnCode::KeyNotFound;
  361. }
  362. }
  363. }
  364. int64_t dsr::component_getProperty_integer(const Component& component, const ReadableString& propertyName, bool mustExist, int64_t defaultValue) {
  365. MUST_EXIST(component, component_getProperty_integer);
  366. Persistent* target = component->findAttribute(propertyName);
  367. if (target == nullptr) {
  368. if (mustExist) {
  369. throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  370. }
  371. return defaultValue;
  372. } else {
  373. PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
  374. PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
  375. if (integerTarget != nullptr) {
  376. return integerTarget->value;
  377. } else if (booleanTarget != nullptr) {
  378. return booleanTarget->value;
  379. } else {
  380. if (mustExist) {
  381. throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
  382. }
  383. return defaultValue;
  384. }
  385. }
  386. }
  387. ReturnCode dsr::component_setProperty_image(const Component& component, const ReadableString& propertyName, const OrderedImageRgbaU8& value, bool mustAssign) {
  388. MUST_EXIST(component, component_setProperty_image);
  389. Persistent* target = component->findAttribute(propertyName);
  390. if (target == nullptr) {
  391. if (mustAssign) {
  392. throwError("component_setProperty_image: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  393. }
  394. return ReturnCode::KeyNotFound;
  395. } else {
  396. PersistentImage* imageTarget = dynamic_cast<PersistentImage*>(target);
  397. if (imageTarget != nullptr) {
  398. imageTarget->value = value;
  399. component->changedAttribute(propertyName);
  400. return ReturnCode::Good;
  401. } else {
  402. if (mustAssign) {
  403. throwError("component_setProperty_image: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an image.\n");
  404. }
  405. return ReturnCode::KeyNotFound;
  406. }
  407. }
  408. }
  409. OrderedImageRgbaU8 dsr::component_getProperty_image(const Component& component, const ReadableString& propertyName, bool mustExist) {
  410. MUST_EXIST(component, component_getProperty_image);
  411. Persistent* target = component->findAttribute(propertyName);
  412. if (target == nullptr) {
  413. if (mustExist) {
  414. throwError("component_getProperty_image: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
  415. }
  416. return OrderedImageRgbaU8();
  417. } else {
  418. PersistentImage* imageTarget = dynamic_cast<PersistentImage*>(target);
  419. if (imageTarget != nullptr) {
  420. return imageTarget->value;
  421. } else {
  422. if (mustExist) {
  423. throwError("component_getProperty_image: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an image.\n");
  424. }
  425. return OrderedImageRgbaU8();
  426. }
  427. }
  428. }
  429. String dsr::component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments) {
  430. MUST_EXIST(component, component_call);
  431. return component->call(methodName, arguments);
  432. }
  433. String dsr::component_call(const Component& component, const ReadableString& methodName) {
  434. return component_call(component, methodName, U"");
  435. }
  436. void dsr::component_detachFromParent(const Component& component) {
  437. MUST_EXIST(component, component_detachFromParent);
  438. component->detachFromParent();
  439. }
  440. Component dsr::component_create(const Component& parent, const ReadableString& className, const ReadableString& identifierName, int index) {
  441. // Making sure that the default components exist before trying to create a component manually.
  442. gui_initialize();
  443. // Creating a component from the name
  444. Component child = std::dynamic_pointer_cast<VisualComponent>(createPersistentClass(className));
  445. if (child) {
  446. child->setName(identifierName);
  447. child->setIndex(index);
  448. // Attaching to a parent is optional, but convenient to do in the same call.
  449. if (parent) {
  450. parent->addChildComponent(child);
  451. }
  452. }
  453. return child;
  454. }
  455. void dsr::window_applyTheme(const Window& window, const VisualTheme& theme) {
  456. MUST_EXIST(window, window_applyTheme);
  457. MUST_EXIST(theme, window_applyTheme);
  458. window->applyTheme(theme);
  459. }