Basic calculator example to show the LPC4088 Experiment Base Board with a touch LCD from DisplayModule

Dependencies:   DmTftLibrary mbed

Fork of dm_calc by Display Module

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Button.cpp Source File

Button.cpp

00001 #include "Button.h"
00002 
00003 Button::Button(const char* caption, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
00004   _caption(caption), _x0(x), _y0(y), _x1(x+width), _y1(y+height), _bgCol(BLACK), 
00005   _fgCol(WHITE), _bgColPressed(CYAN), _fgColPressed(BLACK)
00006 {
00007   _enabled = true;
00008   _pressed = false;
00009   _func = NULL;
00010 }
00011 
00012 void Button::setColors(uint16_t bg, uint16_t fg, uint16_t bgPressed, uint16_t fgPressed)
00013 {
00014   _bgCol = bg;
00015   _fgCol = fg;
00016   _bgColPressed = bgPressed;
00017   _fgColPressed = fgPressed;
00018 }
00019 
00020 bool Button::handle(uint16_t x, uint16_t y, bool pressed)
00021 {
00022   bool needsRepaint = false;
00023   if (_enabled) {
00024     if (!pressed && _pressed) {
00025       // user released => click
00026       needsRepaint = true;
00027       _pressed = false;
00028       if (_func != NULL) {
00029         _func(_funcArg);
00030       }       
00031     }
00032     else if ((x >= _x0) && (y >= _y0) && (x <= _x1) && (y <= _y1)) {
00033       if (pressed && !_pressed) {
00034         // user pressing inside area
00035         needsRepaint = true;
00036         _pressed = true;
00037       } 
00038     }
00039   }
00040   return needsRepaint;
00041 }
00042 
00043 void Button::draw(DmTftBase* tft)
00044 {
00045   if (_pressed) {
00046     tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgColPressed);
00047     tft->drawRectangle(_x0, _y0, _x1, _y1, _fgColPressed);
00048     tft->setTextColor(_bgColPressed, _fgColPressed);
00049     tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
00050   } else {
00051     tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgCol);
00052     tft->drawRectangle(_x0, _y0, _x1, _y1, _fgCol);
00053     tft->setTextColor(_bgCol, _fgCol);
00054     tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
00055   }
00056 }