R G / Interfacage

Dependencies:   Array_Matrix TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Button.cpp Source File

Button.cpp

00001 //-----------------------------------------------------------
00002 //  Button class handling multi-touch
00003 //      Multi-touch: Enabled (default)
00004 //
00005 //  2016/04/08, Copyright (c) 2016 MIKAMI, Naoki
00006 //-----------------------------------------------------------
00007 
00008 #include "Button.hpp"
00009 
00010 namespace Mikami
00011 {
00012     // Draw button
00013     void Button::Draw(uint32_t color, uint32_t textColor)
00014     {
00015         if (color == BACK_COLOR_) active_ = true;
00016         if (!active_) return;
00017         lcd_.SetTextColor(color);
00018         lcd_.FillRect(X_, Y_, W_, H_);
00019 
00020         if (STR_.length() != 0)
00021         {
00022             lcd_.SetFont(FONTS_);
00023             lcd_.SetBackColor(color);
00024             lcd_.SetTextColor(textColor);
00025             uint16_t x0 = X_ + (W_ - FONTS_->Width*(STR_.length()))/2;
00026             uint16_t y0 = Y_ + (H_ - FONTS_->Height)/2 + 1;
00027             DrawString(x0, y0, STR_);
00028             lcd_.SetBackColor(BACK_COLOR_);  // recover back color
00029         }            
00030     }
00031 
00032     // Check touch detected
00033     bool Button::Touched()
00034     {
00035         if (!active_) return false;
00036         if (!PanelTouched()) return false;
00037         if (!IsOnButton()) return false;
00038         Draw(TOUCHED_COLOR_, TEXT_COLOR_);
00039         return true;
00040     }
00041 
00042     // If touched position is on the button, return true
00043     bool Button::IsOnButton()
00044     {
00045         int nTouch = multiTouch_ ? state_.touchDetected : 1;
00046         for (int n=0; n<nTouch; n++)
00047         {
00048             uint16_t x = state_.touchX[n];
00049             uint16_t y = state_.touchY[n];
00050             if ( (X_ <= x) && (x <= X_+W_) &&
00051                  (Y_ <= y) && (y <= Y_+H_) ) return true;
00052         }
00053         return false;
00054     }
00055 
00056     void Button::Activate()
00057     {
00058         active_ = true;
00059         Draw();
00060     }
00061 
00062     void Button::Inactivate()
00063     {
00064         Draw(0xFF003538, 0xFF003538); //Draw(INACTIVE_COLOR_, INACTIVE_TEXT_COLOR_);
00065         active_ = false;
00066     }
00067 }