guiAPI.cpp 21 KB

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