Basic swim GUI for LPC4088

Fork of DMBasicGUI by Embedded Artists

Application/Button.cpp

Committer:
embeddedartists
Date:
2014-12-11
Revision:
1:46c8df4608c8
Parent:
0:4977187e90c7
Child:
2:efae611de184

File content as of revision 1:46c8df4608c8:

/*
 *  Copyright 2014 Embedded Artists AB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include "Button.h"
#include "mbed.h"
#include "DMBoard.h"

#include "lpc_swim_font.h"

Button::Button(const char* caption, COLOR_T* fb, uint16_t x, uint16_t y, uint16_t width, uint16_t height) :
  _caption(caption), _capx(0), _capy(0), 
  _bgCol(GREEN), _fgCol(BLACK), 
  _bgColPressed(BLACK), _fgColPressed(GREEN)
{
  _enabled = true;
  _pressed = false;
  _func = NULL;
  
  Display* disp = DMBoard::instance().display();
    
  swim_window_open_noclear(
    &_win, 
    disp->width(), disp->height(),   // full size
    fb,
    x, y, x+width-1, y+height-1,     // window position and size
    0,                               // border
    _fgCol, _bgCol, _fgCol);         // colors: pen, backgr, forgr
    
  swim_set_font_transparency(&_win, 1);
  setCaption(caption);
}

void Button::setCaption(const char* caption)
{
  int w, h;
  _caption = caption;
  swim_get_string_bounds(&_win, _caption, &w, &h);
  _capx = (_win.xpmax-_win.xpmin-w)/2;
  _capy = (_win.ypmax-_win.ypmin-h)/2;
}

void Button::setColors(COLOR_T bg, COLOR_T fg, COLOR_T bgPressed, COLOR_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 (!pressed && _pressed) {
      // user released => click
      needsRepaint = true;
      _pressed = false;
      if (_func != NULL) {
        _func(_funcArg);
      }       
    }
    else if ((x >= _win.xpmin) && (y >= _win.ypmin) && (x <= _win.xpmax) && (y <= _win.ypmax)) {
      if (pressed && !_pressed) {
        // user pressing inside area
        needsRepaint = true;
        _pressed = true;
      } 
    }
    else if (_pressed) {
      // pressed but moved outside of the button area
      needsRepaint = true;
      _pressed = false;
    }
  }
  return needsRepaint;
}

void Button::draw()
{
  if (_pressed) {
    swim_set_pen_color(&_win, _fgColPressed);
    swim_set_bkg_color(&_win, _bgColPressed);
    swim_set_fill_color(&_win, _fgColPressed);
    swim_clear_screen(&_win, _bgColPressed);
  } else {
    swim_set_pen_color(&_win, _fgCol);
    swim_set_bkg_color(&_win, _bgCol);
    swim_set_fill_color(&_win, _fgCol);
    swim_clear_screen(&_win, _bgCol);
  }
  swim_put_text_xy(&_win, _caption, _capx, _capy);
}