main.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. DSR_MAIN_CALLER(dsrMain)
  12. void dsrMain(List<String> args) {
  13. // Set current path to the application folder, so that it's safe to use relative paths for loading GUI resources.
  14. // 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.
  15. file_setCurrentPath(file_getApplicationFolder());
  16. // Create a window
  17. window = window_create(U"GUI example", 1000, 700);
  18. // Register your custom components here
  19. //REGISTER_PERSISTENT_CLASS(className);
  20. // Load an interface to the window
  21. window_loadInterfaceFromFile(window, U"media/interface.lof");
  22. // Bind methods to events
  23. window_setCloseEvent(window, []() {
  24. running = false;
  25. });
  26. // Look up components by name
  27. buttonClear = window_findComponentByName(window, U"buttonClear");
  28. buttonAdd = window_findComponentByName(window, U"buttonAdd");
  29. myListBox = window_findComponentByName(window, U"myListBox");
  30. textElement = window_findComponentByName(window, U"textElement");
  31. // Connect components with actions
  32. component_setPressedEvent(buttonClear, []() {
  33. // Clear list
  34. component_call(myListBox, U"ClearAll");
  35. });
  36. component_setPressedEvent(buttonAdd, []() {
  37. // Add to list
  38. component_call(myListBox, U"PushElement", component_getProperty_string(textElement, U"Text", false));
  39. });
  40. component_setKeyDownEvent(myListBox, [](const KeyboardEvent& event) {
  41. if (event.dsrKey == DsrKey_Delete) {
  42. // Delete from list
  43. int64_t index = component_getProperty_integer(myListBox, U"SelectedIndex", false, 0);
  44. //int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex")); // There is also a getter for the index
  45. if (index > -1) {
  46. component_call(myListBox, U"RemoveElement", string_combine(index));
  47. }
  48. }
  49. });
  50. // Called when the selected index has changed, when indices have changed their meaning
  51. // Triggered by mouse, keyboard, list changes and initialization
  52. component_setSelectEvent(myListBox, [](int64_t index) {
  53. String content = component_call(myListBox, U"GetSelectedText");
  54. printText("Select event: content is (", content, ") at index ", index, "\n");
  55. });
  56. // Only triggered by mouse presses like any other component
  57. component_setPressedEvent(myListBox, []() {
  58. int64_t index = component_getProperty_integer(myListBox, U"SelectedIndex", false, 0);
  59. //int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex")); // There is also a getter for the index
  60. String content = component_call(myListBox, U"GetSelectedText");
  61. printText("Pressed event: content is (", content, ") at index ", index, "\n");
  62. });
  63. // Execute
  64. while(running) {
  65. // Wait for actions
  66. while (!window_executeEvents(window)) {
  67. time_sleepSeconds(0.01);
  68. }
  69. // Busy loop instead of waiting
  70. //window_executeEvents(window);
  71. // Draw interface
  72. window_drawComponents(window);
  73. // Show the final image
  74. window_showCanvas(window);
  75. }
  76. }