DPF

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

NanoButton.cpp (2591B)


      1 /*
      2  * Copyright (C) 2018-2019 Rob van den Berg <rghvdberg at gmail dot org>
      3  * Copyright (C) 2020-2021 Filipe Coelho <falktx@falktx.com>
      4  *
      5  * This file is part of CharacterCompressor
      6  *
      7  * Nnjas2 is free software: you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation, either version 3 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * CharacterCompressor is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with CharacterCompressor.  If not, see <https://www.gnu.org/licenses/>.
     19  */
     20 
     21 #include "NanoButton.hpp"
     22 #include "Window.hpp"
     23 
     24 START_NAMESPACE_DGL
     25 
     26 Button::Button(Widget* const parent, ButtonEventHandler::Callback* const cb)
     27     : NanoWidget(parent),
     28       ButtonEventHandler(this),
     29       backgroundColor(32, 32, 32),
     30       labelColor(255, 255, 255),
     31       label("button"),
     32       fontScale(1.0f)
     33 {
     34 #ifdef DGL_NO_SHARED_RESOURCES
     35     createFontFromFile("sans", "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf");
     36 #else
     37     loadSharedResources();
     38 #endif
     39     ButtonEventHandler::setCallback(cb);
     40 }
     41 
     42 Button::~Button()
     43 {
     44 }
     45 
     46 void Button::setBackgroundColor(const Color color)
     47 {
     48     backgroundColor = color;
     49 }
     50 
     51 void Button::setFontScale(const float scale)
     52 {
     53     fontScale = scale;
     54 }
     55 
     56 void Button::setLabel(const std::string& label2)
     57 {
     58     label = label2;
     59 }
     60 
     61 void Button::setLabelColor(const Color color)
     62 {
     63     labelColor = color;
     64 }
     65 
     66 void Button::onNanoDisplay()
     67 {
     68     const uint w = getWidth();
     69     const uint h = getHeight();
     70     const float margin = 1.0f;
     71 
     72     // Background
     73     beginPath();
     74     fillColor(backgroundColor);
     75     strokeColor(labelColor);
     76     rect(margin, margin, w - 2 * margin, h - 2 * margin);
     77     fill();
     78     stroke();
     79     closePath();
     80 
     81     // Label
     82     beginPath();
     83     fontSize(14 * fontScale);
     84     fillColor(labelColor);
     85     Rectangle<float> bounds;
     86     textBounds(0, 0, label.c_str(), NULL, bounds);
     87     float tx = w / 2.0f ;
     88     float ty = h / 2.0f;
     89     textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
     90 
     91     fillColor(255, 255, 255, 255);
     92     text(tx, ty, label.c_str(), NULL);
     93     closePath();
     94 }
     95 
     96 bool Button::onMouse(const MouseEvent& ev)
     97 {
     98     return ButtonEventHandler::mouseEvent(ev);
     99 }
    100 
    101 bool Button::onMotion(const MotionEvent& ev)
    102 {
    103     return ButtonEventHandler::motionEvent(ev);
    104 }
    105 
    106 END_NAMESPACE_DGL