main.cpp 3.0 KB

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