main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = string_toInteger(component_call(myListBox, U"GetSelectedIndex"));
  42. if (index > -1) {
  43. component_call(myListBox, U"RemoveElement", string_combine(index));
  44. }
  45. }
  46. });
  47. // Called when the selected index has changed, when indices have changed their meaning
  48. // Triggered by mouse, keyboard, list changes and initialization
  49. component_setSelectEvent(myListBox, [](int64_t index) {
  50. String content = component_call(myListBox, U"GetSelectedText");
  51. printText("Select event: content is (", content, ") at index ", index, "\n");
  52. });
  53. // Only triggered by mouse presses like any other component
  54. component_setPressedEvent(myListBox, []() {
  55. int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex"));
  56. String content = component_call(myListBox, U"GetSelectedText");
  57. printText("Pressed event: content is (", content, ") at index ", index, "\n");
  58. });
  59. // Execute
  60. while(running) {
  61. // Wait for actions
  62. while (!window_executeEvents(window)) {
  63. time_sleepSeconds(0.01);
  64. }
  65. // Busy loop instead of waiting
  66. //window_executeEvents(window);
  67. // Draw interface
  68. window_drawComponents(window);
  69. // Show the final image
  70. window_showCanvas(window);
  71. }
  72. }