DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

ExampleTextWidget.hpp (3840B)


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