Same as original

button.cpp

Committer:
MikamiUitOpen
Date:
2016-02-18
Revision:
12:710078d50d9b
Parent:
10:5ca60e724a76
Child:
13:af578b53ff0e

File content as of revision 12:710078d50d9b:

//-----------------------------------------------------------
//  Button class coping with multi-touch
//
//  2016/02/18, Copyright (c) 2016 MIKAMI, Naoki
//-----------------------------------------------------------

#include "button.hpp"

namespace Mikami
{
    // Draw button
    void Button::Draw(uint32_t color, uint32_t textColor)
    {
        lcd_.SetTextColor(color);
        lcd_.FillRect(X_, Y_, W_, H_);

        if (STR_.length() != 0)
        {
            lcd_.SetFont(FONTS_);
            lcd_.SetBackColor(color);
            lcd_.SetTextColor(textColor);

            uint16_t x0 = X_ + (W_ - FONT_WIDTH_*(STR_.length()))/2;
            uint16_t y0 = Y_ + (H_ - FONT_HEIGHT_)/2 + 1;
            lcd_.DisplayStringAt(x0, y0, (uint8_t *)STR_.c_str(),
                                 LEFT_MODE);
            lcd_.SetBackColor(BACK_COLOR_); // recover back color
        }            
    }
        
    // Check touch detected
    bool Button::Touched()
    {
        bool rtn = false;
        TS_StateTypeDef state;
        ts_.GetState(&state);

        for (int n=0; n<state.touchDetected; n++)
        {
            uint16_t x = state.touchX[n];
            uint16_t y = state.touchY[n];

            if ( (X_ <= x) && (x <= X_+W_) &&
                 (Y_ <= y) && (y <= Y_+H_) ) rtn = true;
            if (rtn) break;
        }
        return rtn;
    }
        
    // Check touch detected and redraw button
    bool Button::Touched(uint32_t color, uint32_t textColor)
    {
        bool rtn = Touched();
        if (rtn) Draw(color, textColor);
        return rtn;
    }
}