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.

129 lines
3.8KB

  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. #ifndef EXAMPLE_TEXT_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_TEXT_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "../../dgl/NanoVG.hpp"
  21. START_NAMESPACE_DGL
  22. // ------------------------------------------------------
  23. // our widget
  24. template <class BaseWidget>
  25. class ExampleTextWidget : public BaseWidget
  26. {
  27. public:
  28. static constexpr const char* kExampleWidgetName = "Text";
  29. // SubWidget
  30. explicit ExampleTextWidget(Widget* const parent);
  31. // TopLevelWidget
  32. explicit ExampleTextWidget(Window& windowToMapTo);
  33. // StandaloneWindow
  34. explicit ExampleTextWidget(Application& app);
  35. // helper
  36. double getScaleFactor();
  37. protected:
  38. void onNanoDisplay() override
  39. {
  40. const int width = BaseWidget::getWidth();
  41. const int height = BaseWidget::getHeight();
  42. const double scaleFactor = getScaleFactor();
  43. NanoVG::fontSize(40.0f * scaleFactor);
  44. NanoVG::textAlign(NanoVG::Align(NanoVG::ALIGN_CENTER|NanoVG::ALIGN_MIDDLE));
  45. NanoVG::textLineHeight(20.0f * scaleFactor);
  46. NanoVG::beginPath();
  47. NanoVG::fillColor(220,220,220,255);
  48. NanoVG::roundedRect(10 * scaleFactor, height/4 + 10 * scaleFactor,
  49. width - 20 * scaleFactor, height/2 - 20 * scaleFactor, 3 * scaleFactor);
  50. NanoVG::fill();
  51. NanoVG::fillColor(0,150,0,220);
  52. NanoVG::textBox(10 * scaleFactor, height/2, width - 20 * scaleFactor, "Hello World!", nullptr);
  53. }
  54. };
  55. // SubWidget
  56. template<> inline
  57. ExampleTextWidget<NanoSubWidget>::ExampleTextWidget(Widget* const parent)
  58. : NanoSubWidget(parent)
  59. {
  60. loadSharedResources();
  61. setSize(500, 300);
  62. }
  63. template<> inline
  64. double ExampleTextWidget<NanoSubWidget>::getScaleFactor()
  65. {
  66. return getWindow().getScaleFactor();
  67. }
  68. // TopLevelWidget
  69. template<> inline
  70. ExampleTextWidget<NanoTopLevelWidget>::ExampleTextWidget(Window& windowToMapTo)
  71. : NanoTopLevelWidget(windowToMapTo)
  72. {
  73. loadSharedResources();
  74. setSize(500, 300);
  75. }
  76. template<> inline
  77. double ExampleTextWidget<NanoTopLevelWidget>::getScaleFactor()
  78. {
  79. return NanoTopLevelWidget::getScaleFactor();
  80. }
  81. // StandaloneWindow
  82. template<> inline
  83. ExampleTextWidget<NanoStandaloneWindow>::ExampleTextWidget(Application& app)
  84. : NanoStandaloneWindow(app)
  85. {
  86. loadSharedResources();
  87. setSize(500, 300);
  88. done();
  89. }
  90. template<> inline
  91. double ExampleTextWidget<NanoStandaloneWindow>::getScaleFactor()
  92. {
  93. return Window::getScaleFactor();
  94. }
  95. template class ExampleTextWidget<NanoSubWidget>;
  96. template class ExampleTextWidget<NanoTopLevelWidget>;
  97. template class ExampleTextWidget<NanoStandaloneWindow>;
  98. typedef ExampleTextWidget<NanoSubWidget> ExampleTextSubWidget;
  99. typedef ExampleTextWidget<NanoTopLevelWidget> ExampleTextTopLevelWidget;
  100. typedef ExampleTextWidget<NanoStandaloneWindow> ExampleTextStandaloneWindow;
  101. // ------------------------------------------------------
  102. END_NAMESPACE_DGL
  103. #endif // EXAMPLE_TEXT_WIDGET_HPP_INCLUDED