main.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. component_setPressedEvent(myListBox, []() {
  45. int64_t index = string_toInteger(component_call(myListBox, U"GetSelectedIndex"));
  46. String content = component_call(myListBox, U"GetSelectedText");
  47. printText("content is (", content, ") at index ", index, "\n");
  48. });
  49. // Execute
  50. while(running) {
  51. // Wait for actions
  52. while (!window_executeEvents(window)) {
  53. time_sleepSeconds(0.01);
  54. }
  55. // Busy loop instead of waiting
  56. //window_executeEvents(window);
  57. // Draw interface
  58. window_drawComponents(window);
  59. // Show the final image
  60. window_showCanvas(window);
  61. }
  62. }