main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 
  2. #include "../../DFPSR/includeFramework.h"
  3. using namespace dsr;
  4. bool running = true;
  5. // GUI handles
  6. Window window;
  7. Component buttonClear;
  8. Component buttonAdd;
  9. Component myListBox;
  10. Component textElement;
  11. // Custom message handling
  12. List<String> messages;
  13. void showMessages() {
  14. if (messages.length() > 0) {
  15. // Summarizing all messages from the last action, which can also be used to display them in the same pop-up message.
  16. String content;
  17. string_append(content, U"Messages:\n");
  18. for (int m = 0; m < messages.length(); m++) {
  19. string_append(content, U" * ", messages[m]);
  20. }
  21. string_append(content, U"\n");
  22. string_sendMessage_default(content, MessageType::StandardPrinting);
  23. messages.clear();
  24. }
  25. }
  26. DSR_MAIN_CALLER(dsrMain)
  27. void dsrMain(List<String> args) {
  28. // Assign custom message handling to get control over errors, warnings and any other text being printed to the terminal.
  29. string_assignMessageHandler([](const ReadableString &message, MessageType type) {
  30. // Deferring messages can be useful for showing them at a later time.
  31. messages.push(message);
  32. // A custom message handler still have to throw exceptions or terminate the program when errors are thrown.
  33. if (type == MessageType::Error) {
  34. string_sendMessage_default(message, MessageType::Error);
  35. }
  36. });
  37. // Set current path to the application folder, so that it's safe to use relative paths for loading GUI resources.
  38. // Loading and saving files will automatically convert / and \ to the local format using file_optimizePath, so that you can use them directly in relative paths.
  39. file_setCurrentPath(file_getApplicationFolder());
  40. // Create a window
  41. window = window_create(U"GUI example", 1000, 700);
  42. // Register your custom components here
  43. //REGISTER_PERSISTENT_CLASS(className);
  44. // Load an interface to the window
  45. window_loadInterfaceFromFile(window, U"media/interface.lof");
  46. // Bind methods to events
  47. window_setCloseEvent(window, []() {
  48. sendWarning(U"Ahhh, you killed me! But closing a window directly is okay, because the program can run logic for saving things before terminating.");
  49. running = false;
  50. });
  51. // Look up components by name
  52. buttonClear = window_findComponentByName(window, U"buttonClear");
  53. buttonAdd = window_findComponentByName(window, U"buttonAdd");
  54. myListBox = window_findComponentByName(window, U"myListBox");
  55. textElement = window_findComponentByName(window, U"textElement");
  56. // Connect components with actions
  57. component_setPressedEvent(buttonClear, []() {
  58. // Clear list
  59. component_call(myListBox, U"ClearAll");
  60. });
  61. component_setPressedEvent(buttonAdd, []() {
  62. // Add to list
  63. component_call(myListBox, U"PushElement", component_getProperty_string(textElement, U"Text", false));
  64. });
  65. component_setKeyDownEvent(myListBox, [](const KeyboardEvent& event) {
  66. if (event.dsrKey == DsrKey_Delete) {
  67. // Delete from list
  68. int64_t index = component_getProperty_integer(myListBox, U"SelectedIndex", false, 0);
  69. //int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex")); // There is also a getter for the index
  70. if (index > -1) {
  71. component_call(myListBox, U"RemoveElement", string_combine(index));
  72. }
  73. }
  74. });
  75. // Connect actions to components without saving their handles
  76. component_setPressedEvent(window_findComponentByName(window, U"menuExit"), []() {
  77. sendWarning(U"You forgot to save your project and now I'm throwing it away because you forgot to save!");
  78. running = false;
  79. });
  80. // Called when the selected index has changed, when indices have changed their meaning
  81. // Triggered by mouse, keyboard, list changes and initialization
  82. component_setSelectEvent(myListBox, [](int64_t index) {
  83. String content = component_call(myListBox, U"GetSelectedText");
  84. printText("Select event: content is (", content, ") at index ", index, "\n");
  85. });
  86. // Only triggered by mouse presses like any other component
  87. component_setPressedEvent(myListBox, []() {
  88. int64_t index = component_getProperty_integer(myListBox, U"SelectedIndex", false, 0);
  89. //int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex")); // There is also a getter for the index
  90. String content = component_call(myListBox, U"GetSelectedText");
  91. printText("Pressed event: content is (", content, ") at index ", index, "\n");
  92. });
  93. // Execute
  94. while(running) {
  95. // Wait for actions
  96. while (!window_executeEvents(window)) {
  97. time_sleepSeconds(0.01);
  98. }
  99. // Busy loop instead of waiting
  100. //window_executeEvents(window);
  101. // Custom message handling
  102. showMessages();
  103. // Draw interface
  104. window_drawComponents(window);
  105. // Show the final image
  106. window_showCanvas(window);
  107. }
  108. // Empty the messages and switch back to the default message handler so that errors from deallocating global resources can be displayed
  109. showMessages();
  110. string_unassignMessageHandler();
  111. printText(U"Printing text using the default message handler again.\n");
  112. }