main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Create a virtual machine with reusable image generating functions.
  47. // The same Media Machine Code (*.mmc) can be used for multiple themes.
  48. MediaMachine machine = machine_create(string_load(U"media/Drawing.mmc"));
  49. // Use the virtual machine with a specific style referring to the functions in machine.
  50. window_applyTheme(window, theme_createFromFile(machine, U"media/Theme.ini"));
  51. // Bind methods to events
  52. window_setCloseEvent(window, []() {
  53. sendWarning(U"Ahhh, you killed me! But closing a window directly is okay, because the program can run logic for saving things before terminating.");
  54. running = false;
  55. });
  56. // Look up components by name
  57. buttonClear = window_findComponentByName(window, U"buttonClear");
  58. buttonAdd = window_findComponentByName(window, U"buttonAdd");
  59. myListBox = window_findComponentByName(window, U"myListBox");
  60. textElement = window_findComponentByName(window, U"textElement");
  61. // Connect components with actions
  62. component_setPressedEvent(buttonClear, []() {
  63. // Clear list
  64. component_call(myListBox, U"ClearAll");
  65. });
  66. component_setPressedEvent(buttonAdd, []() {
  67. // Add to list
  68. component_call(myListBox, U"PushElement", component_getProperty_string(textElement, U"Text", false));
  69. });
  70. component_setKeyDownEvent(myListBox, [](const KeyboardEvent& event) {
  71. if (event.dsrKey == DsrKey_Delete) {
  72. // Delete from list
  73. int64_t index = component_getProperty_integer(myListBox, U"SelectedIndex", false, 0);
  74. //int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex")); // There is also a getter for the index
  75. if (index > -1) {
  76. component_call(myListBox, U"RemoveElement", string_combine(index));
  77. }
  78. }
  79. });
  80. // Connect actions to components without saving their handles
  81. component_setPressedEvent(window_findComponentByName(window, U"menuExit"), []() {
  82. sendWarning(U"You forgot to save your project and now I'm throwing it away because you forgot to save!");
  83. running = false;
  84. });
  85. // Called when the selected index has changed, when indices have changed their meaning
  86. // Triggered by mouse, keyboard, list changes and initialization
  87. component_setSelectEvent(myListBox, [](int64_t index) {
  88. String content = component_call(myListBox, U"GetSelectedText");
  89. printText("Select event: content is (", content, ") at index ", index, "\n");
  90. });
  91. // Only triggered by mouse presses like any other component
  92. component_setPressedEvent(myListBox, []() {
  93. int64_t index = component_getProperty_integer(myListBox, U"SelectedIndex", false, 0);
  94. //int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex")); // There is also a getter for the index
  95. String content = component_call(myListBox, U"GetSelectedText");
  96. printText("Pressed event: content is (", content, ") at index ", index, "\n");
  97. });
  98. // Execute
  99. while(running) {
  100. // Wait for actions
  101. while (!window_executeEvents(window)) {
  102. time_sleepSeconds(0.01);
  103. }
  104. // Busy loop instead of waiting
  105. //window_executeEvents(window);
  106. // Custom message handling
  107. showMessages();
  108. // Draw interface
  109. window_drawComponents(window);
  110. // Show the final image
  111. window_showCanvas(window);
  112. }
  113. // Empty the messages and switch back to the default message handler so that errors from deallocating global resources can be displayed
  114. showMessages();
  115. string_unassignMessageHandler();
  116. printText(U"Printing text using the default message handler again.\n");
  117. }