main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "../../DFPSR/includeFramework.h"
  2. using namespace dsr;
  3. // Global
  4. const String mediaPath = string_combine(U"media", file_separator());
  5. bool running = true;
  6. // GUI handles
  7. Window window;
  8. Component buttonClear;
  9. Component buttonAdd;
  10. Component myListBox;
  11. int main(int argn, char **argv) {
  12. // Create a window
  13. window = window_create(U"GUI example", 1000, 700);
  14. // Register your custom components here
  15. //REGISTER_PERSISTENT_CLASS(className);
  16. // Load an interface to the window
  17. window_loadInterfaceFromFile(window, mediaPath + U"interface.lof");
  18. // Bind methods to events
  19. window_setCloseEvent(window, []() {
  20. running = false;
  21. });
  22. // Look up components by name
  23. buttonClear = window_findComponentByName(window, U"buttonClear");
  24. buttonAdd = window_findComponentByName(window, U"buttonAdd");
  25. myListBox = window_findComponentByName(window, U"myListBox");
  26. // Connect components with actions
  27. component_setPressedEvent(buttonClear, []() {
  28. // Clear list
  29. component_call(myListBox, U"ClearAll");
  30. });
  31. component_setPressedEvent(buttonAdd, []() {
  32. // Add to list
  33. component_call(myListBox, U"PushElement", U"New item");
  34. });
  35. component_setKeyDownEvent(myListBox, [](const KeyboardEvent& event) {
  36. if (event.dsrKey == DsrKey_Delete) {
  37. // Delete from list
  38. int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex"));
  39. if (index > -1) {
  40. component_call(myListBox, U"RemoveElement", string_combine(index));
  41. }
  42. }
  43. });
  44. // Called when the selected index has changed, when indices have changed their meaning
  45. // Triggered by mouse, keyboard, list changes and initialization
  46. component_setSelectEvent(myListBox, [](int64_t index) {
  47. String content = component_call(myListBox, U"GetSelectedText");
  48. printText("Select event: content is (", content, ") at index ", index, "\n");
  49. });
  50. // Only triggered by mouse presses like any other component
  51. component_setPressedEvent(myListBox, []() {
  52. int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex"));
  53. String content = component_call(myListBox, U"GetSelectedText");
  54. printText("Pressed event: content is (", content, ") at index ", index, "\n");
  55. });
  56. // Execute
  57. while(running) {
  58. // Wait for actions
  59. while (!window_executeEvents(window)) {
  60. time_sleepSeconds(0.01);
  61. }
  62. // Busy loop instead of waiting
  63. //window_executeEvents(window);
  64. // Draw interface
  65. window_drawComponents(window);
  66. // Show the final image
  67. window_showCanvas(window);
  68. }
  69. }