main.cpp 2.5 KB

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