DPF

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

CairoExampleUI.cpp (5421B)


      1 /*
      2  * DISTRHO Plugin Framework (DPF)
      3  * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
      4  * Copyright (C) 2019-2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
      5  *
      6  * Permission to use, copy, modify, and/or distribute this software for any purpose with
      7  * or without fee is hereby granted, provided that the above copyright notice and this
      8  * permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
     11  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
     12  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     14  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16  */
     17 
     18 #include "DistrhoUI.hpp"
     19 
     20 #include "Artwork.hpp"
     21 #include "DemoWidgetBanner.hpp"
     22 #include "DemoWidgetClickable.hpp"
     23 
     24 START_NAMESPACE_DISTRHO
     25 
     26 // We need a few classes from DGL.
     27 using DGL_NAMESPACE::CairoGraphicsContext;
     28 using DGL_NAMESPACE::CairoImage;
     29 using DGL_NAMESPACE::CairoImageKnob;
     30 using DGL_NAMESPACE::CairoImageSwitch;
     31 
     32 // And from ourselves
     33 using DGL_NAMESPACE::DemoWidgetBanner;
     34 
     35 class CairoExampleUI : public UI,
     36                        public CairoImageKnob::Callback,
     37                        public CairoImageSwitch::Callback,
     38                        public DemoWidgetClickable::Callback
     39 {
     40     ScopedPointer<CairoImageKnob> fKnob;
     41     ScopedPointer<CairoImageSwitch> fButton;
     42     ScopedPointer<DemoWidgetBanner> fWidgetBanner;
     43     ScopedPointer<DemoWidgetClickable> fWidgetClickable;
     44 
     45 public:
     46     CairoExampleUI()
     47     {
     48         CairoImage knobSkin;
     49         knobSkin.loadFromPNG(Artwork::knobData, Artwork::knobDataSize);
     50 
     51         fWidgetBanner = new DemoWidgetBanner(this);
     52         fWidgetBanner->setAbsolutePos(10, 10);
     53         fWidgetBanner->setSize(180, 80);
     54 
     55         fWidgetClickable = new DemoWidgetClickable(this);
     56         fWidgetClickable->setAbsolutePos(100, 100);
     57         fWidgetClickable->setSize(50, 50);
     58         fWidgetClickable->setCallback(this);
     59         fWidgetClickable->setId(kParameterTriState);
     60 
     61         fKnob = new CairoImageKnob(this, knobSkin);
     62         fKnob->setAbsolutePos(10, 100);
     63         fKnob->setSize(80, 80);
     64         fKnob->setCallback(this);
     65         fKnob->setId(kParameterKnob);
     66 
     67         CairoImage buttonOn, buttonOff;
     68         buttonOn.loadFromPNG(Artwork::buttonOnData, Artwork::buttonOnDataSize);
     69         buttonOff.loadFromPNG(Artwork::buttonOffData, Artwork::buttonOffDataSize);
     70 
     71         fButton = new CairoImageSwitch(this, buttonOff, buttonOn);
     72         fButton->setAbsolutePos(100, 160);
     73         fButton->setSize(60, 35);
     74         fButton->setCallback(this);
     75         fButton->setId(kParameterButton);
     76 
     77 #if 0
     78         // we can use this if/when our resources are scalable, for now they are PNGs
     79         const double scaleFactor = getScaleFactor();
     80         if (scaleFactor != 1.0)
     81             setSize(200 * scaleFactor, 200 * scaleFactor);
     82 #else
     83         // without scalable resources, let DPF handle the scaling internally
     84         setGeometryConstraints(DISTRHO_UI_DEFAULT_WIDTH, DISTRHO_UI_DEFAULT_HEIGHT, true, true);
     85 #endif
     86     }
     87 
     88 protected:
     89     void onCairoDisplay(const CairoGraphicsContext& context)
     90     {
     91         cairo_t* const cr = context.handle;
     92         cairo_set_source_rgb(cr, 1.0, 0.8, 0.5);
     93         cairo_paint(cr);
     94     }
     95 
     96 #if 0
     97     // we can use this if/when our resources are scalable, for now they are PNGs
     98     void onResize(const ResizeEvent& ev) override
     99     {
    100         UI::onResize(ev);
    101 
    102         const double scaleFactor = getScaleFactor();
    103 
    104         fWidgetClickable->setSize(50*scaleFactor, 50*scaleFactor);
    105         fWidgetClickable->setAbsolutePos(100*scaleFactor, 100*scaleFactor);
    106 
    107         fWidgetBanner->setSize(180*scaleFactor, 80*scaleFactor);
    108         fWidgetBanner->setAbsolutePos(10*scaleFactor, 10*scaleFactor);
    109 
    110         fKnob->setSize(80*scaleFactor, 80*scaleFactor);
    111         fKnob->setAbsolutePos(10*scaleFactor, 100*scaleFactor);
    112 
    113         fButton->setSize(60*scaleFactor, 35*scaleFactor);
    114         fButton->setAbsolutePos(100*scaleFactor, 160*scaleFactor);
    115     }
    116 #endif
    117 
    118     void parameterChanged(const uint32_t index, const float value) override
    119     {
    120         switch (index)
    121         {
    122         case kParameterKnob:
    123             fKnob->setValue(value);
    124             break;
    125         case kParameterTriState:
    126             fWidgetClickable->setColorId(static_cast<int>(value + 0.5f));
    127             break;
    128         case kParameterButton:
    129             fButton->setDown(value > 0.5f);
    130             break;
    131         }
    132     }
    133 
    134     void demoWidgetClicked(DemoWidgetClickable*, const uint8_t colorId) override
    135     {
    136         setParameterValue(kParameterTriState, colorId);
    137     }
    138 
    139     void imageKnobDragStarted(CairoImageKnob*) override
    140     {
    141         editParameter(kParameterKnob, true);
    142     }
    143 
    144     void imageKnobDragFinished(CairoImageKnob*) override
    145     {
    146         editParameter(kParameterKnob, false);
    147     }
    148 
    149     void imageKnobValueChanged(CairoImageKnob*, const float value) override
    150     {
    151         setParameterValue(kParameterKnob, value);
    152     }
    153 
    154     void imageSwitchClicked(CairoImageSwitch*, bool down) override
    155     {
    156         setParameterValue(kParameterButton, down ? 1.f : 0.f);
    157     }
    158 };
    159 
    160 UI* createUI()
    161 {
    162     return new CairoExampleUI;
    163 }
    164 
    165 END_NAMESPACE_DISTRHO