DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "tests.hpp"
  17. #include "dgl/NanoVG.hpp"
  18. START_NAMESPACE_DGL
  19. // --------------------------------------------------------------------------------------------------------------------
  20. class NanoFilePicker : public NanoStandaloneWindow
  21. {
  22. Rectangle<uint> buttonBounds;
  23. bool buttonClick = false;
  24. bool buttonHover = false;
  25. String selectedFile;
  26. public:
  27. NanoFilePicker(Application& app)
  28. : NanoStandaloneWindow(app),
  29. selectedFile("No file selected yet")
  30. {
  31. #ifndef DGL_NO_SHARED_RESOURCES
  32. loadSharedResources();
  33. #endif
  34. }
  35. protected:
  36. void onNanoDisplay() override
  37. {
  38. // Selected file
  39. beginPath();
  40. fontSize(14);
  41. textAlign(ALIGN_LEFT | ALIGN_MIDDLE);
  42. fillColor(255, 255, 255, 255);
  43. text(20, getHeight()/2, selectedFile, NULL);
  44. closePath();
  45. // Button background
  46. beginPath();
  47. fillColor(Color(32, buttonClick ? 128 : 32, buttonHover ? 128 : 32));
  48. strokeColor(Color());
  49. rect(buttonBounds.getX(), buttonBounds.getY(), buttonBounds.getWidth(), buttonBounds.getHeight());
  50. fill();
  51. stroke();
  52. closePath();
  53. // Button label
  54. beginPath();
  55. fontSize(14);
  56. Rectangle<float> buttonTextBounds;
  57. textBounds(0, 0, "Press me", NULL, buttonTextBounds);
  58. textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
  59. fillColor(255, 255, 255, 255);
  60. text(buttonBounds.getX() + buttonBounds.getWidth()/2,
  61. buttonBounds.getY() + buttonBounds.getHeight()/2,
  62. "Press me", NULL);
  63. closePath();
  64. }
  65. bool onMotion(const MotionEvent& ev) override
  66. {
  67. const bool newButtonHover = buttonBounds.contains(ev.pos);
  68. if (newButtonHover != buttonHover)
  69. {
  70. buttonHover = newButtonHover;
  71. repaint();
  72. return true;
  73. }
  74. return newButtonHover;
  75. }
  76. bool onMouse(const MouseEvent& ev) override
  77. {
  78. if (ev.button != 1)
  79. return false;
  80. if (! buttonBounds.contains(ev.pos))
  81. {
  82. if (buttonClick)
  83. {
  84. buttonClick = false;
  85. repaint();
  86. return true;
  87. }
  88. return false;
  89. }
  90. const bool newButtonClick = ev.press;
  91. if (newButtonClick != buttonClick)
  92. {
  93. buttonClick = newButtonClick;
  94. repaint();
  95. if (newButtonClick)
  96. {
  97. selectedFile = "(in progress)";
  98. repaint();
  99. FileBrowserOptions opts;
  100. opts.title = "Look at me";
  101. openFileBrowser(opts);
  102. }
  103. return true;
  104. }
  105. return newButtonClick;
  106. }
  107. void onResize(const ResizeEvent& ev) override
  108. {
  109. const uint width = ev.size.getWidth();
  110. const uint height = ev.size.getHeight();
  111. buttonBounds = Rectangle<uint>(width - 120, height/2 - 20, 100, 40);
  112. }
  113. void onFileSelected(const char* filename) override
  114. {
  115. if (filename == nullptr)
  116. filename = "Cancelled";
  117. if (selectedFile == filename)
  118. return;
  119. selectedFile = filename;
  120. repaint();
  121. }
  122. };
  123. // --------------------------------------------------------------------------------------------------------------------
  124. END_NAMESPACE_DGL
  125. int main()
  126. {
  127. USE_NAMESPACE_DGL;
  128. Application app(true);
  129. NanoFilePicker win(app);
  130. win.setSize(500, 200);
  131. win.setTitle("FileBrowserDialog");
  132. win.show();
  133. app.exec();
  134. return 0;
  135. }
  136. // --------------------------------------------------------------------------------------------------------------------