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

Revision:
0:3713b01e72fe
Child:
2:b17b19212e5a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.cpp	Tue May 20 15:36:25 2014 +0000
@@ -0,0 +1,56 @@
+#include "Button.h"
+
+Button::Button(const char* caption, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
+  _caption(caption), _x0(x), _y0(y), _x1(x+width), _y1(y+height), _bgCol(BLACK), 
+  _fgCol(WHITE), _bgColPressed(CYAN), _fgColPressed(BLACK)
+{
+  _enabled = true;
+  _pressed = false;
+  _func = NULL;
+}
+
+void Button::setColors(uint16_t bg, uint16_t fg, uint16_t bgPressed, uint16_t fgPressed)
+{
+  _bgCol = bg;
+  _fgCol = fg;
+  _bgColPressed = bgPressed;
+  _fgColPressed = fgPressed;
+}
+
+bool Button::handle(uint16_t x, uint16_t y, bool pressed)
+{
+  bool needsRepaint = false;
+  if (_enabled) {
+    if ((x >= _x0) && (y >= _y0) && (x <= _x1) && (y <= _y1)) {
+      if (pressed && !_pressed) {
+        // user pressing inside area
+        needsRepaint = true;
+        _pressed = true;
+      } else if (!pressed && _pressed) {
+        // user released inside area => click
+        needsRepaint = true;
+        _pressed = false;
+        if (_func != NULL) {
+          _func(_funcArg);
+        }       
+      }
+    }
+  }
+  return needsRepaint;
+}
+
+void Button::draw(DmTftBase* tft)
+{
+  if (_pressed) {
+    tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgColPressed);
+    tft->drawRectangle(_x0, _y0, _x1, _y1, _fgColPressed);
+    tft->setTextColor(_bgColPressed, _fgColPressed);
+    tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
+  } else {
+    tft->fillRectangle(_x0+1, _y0+1, _x1-1, _y1-1, _bgCol);
+    tft->drawRectangle(_x0, _y0, _x1, _y1, _fgCol);
+    tft->setTextColor(_bgCol, _fgCol);
+    tft->drawStringCentered(_x0, _y0, _x1-_x0, _y1-_y0, _caption);
+  }
+}
+