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.

152 lines
4.1KB

  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. public:
  26. NanoFilePicker(Application& app)
  27. : NanoStandaloneWindow(app)
  28. {
  29. #ifndef DGL_NO_SHARED_RESOURCES
  30. loadSharedResources();
  31. #endif
  32. }
  33. protected:
  34. void onNanoDisplay() override
  35. {
  36. Color labelColor(255, 255, 255);
  37. Color backgroundColor(32,
  38. buttonClick ? 128 : 32,
  39. buttonHover ? 128 : 32);
  40. Color borderColor;
  41. // Button background
  42. beginPath();
  43. fillColor(backgroundColor);
  44. strokeColor(borderColor);
  45. rect(buttonBounds.getX(), buttonBounds.getY(), buttonBounds.getWidth(), buttonBounds.getHeight());
  46. fill();
  47. stroke();
  48. closePath();
  49. // Label
  50. beginPath();
  51. fontSize(14);
  52. fillColor(labelColor);
  53. Rectangle<float> buttonTextBounds;
  54. textBounds(0, 0, "Press me", NULL, buttonTextBounds);
  55. textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
  56. fillColor(255, 255, 255, 255);
  57. text(buttonBounds.getX() + buttonBounds.getWidth()/2,
  58. buttonBounds.getY() + buttonBounds.getHeight()/2,
  59. "Press me", NULL);
  60. closePath();
  61. }
  62. bool onMotion(const MotionEvent& ev) override
  63. {
  64. const bool newButtonHover = buttonBounds.contains(ev.pos);
  65. if (newButtonHover != buttonHover)
  66. {
  67. buttonHover = newButtonHover;
  68. repaint();
  69. return true;
  70. }
  71. return newButtonHover;
  72. }
  73. bool onMouse(const MouseEvent& ev) override
  74. {
  75. if (ev.button != 1)
  76. return false;
  77. if (! buttonBounds.contains(ev.pos))
  78. {
  79. if (buttonClick)
  80. {
  81. buttonClick = false;
  82. repaint();
  83. return true;
  84. }
  85. return false;
  86. }
  87. const bool newButtonClick = ev.press;
  88. if (newButtonClick != buttonClick)
  89. {
  90. buttonClick = newButtonClick;
  91. repaint();
  92. if (newButtonClick)
  93. {
  94. FileBrowserOptions opts;
  95. opts.title = "Look at me";
  96. openFileBrowser(opts);
  97. }
  98. return true;
  99. }
  100. return newButtonClick;
  101. }
  102. void onResize(const ResizeEvent& ev) override
  103. {
  104. const uint width = ev.size.getWidth();
  105. const uint height = ev.size.getHeight();
  106. buttonBounds = Rectangle<uint>(width - 120, height/2 - 20, 100, 40);
  107. }
  108. };
  109. // --------------------------------------------------------------------------------------------------------------------
  110. END_NAMESPACE_DGL
  111. int main()
  112. {
  113. USE_NAMESPACE_DGL;
  114. Application app(true);
  115. NanoFilePicker win(app);
  116. win.setSize(500, 200);
  117. win.setTitle("FileBrowserDialog");
  118. win.show();
  119. app.exec();
  120. return 0;
  121. }
  122. // --------------------------------------------------------------------------------------------------------------------