A basic graphics package for the LPC4088 Display Module.
Dependents: lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI lpc4088_displaymodule_fs_aid ... more
Fork of DMBasicGUI by
Application/Button.cpp@0:4977187e90c7, 2014-12-11 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Dec 11 11:03:57 2014 +0000
- Revision:
- 0:4977187e90c7
- Child:
- 1:46c8df4608c8
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:4977187e90c7 | 1 | #include "Button.h" |
embeddedartists | 0:4977187e90c7 | 2 | #include "mbed.h" |
embeddedartists | 0:4977187e90c7 | 3 | #include "DMBoard.h" |
embeddedartists | 0:4977187e90c7 | 4 | |
embeddedartists | 0:4977187e90c7 | 5 | #include "lpc_swim_font.h" |
embeddedartists | 0:4977187e90c7 | 6 | |
embeddedartists | 0:4977187e90c7 | 7 | Button::Button(const char* caption, COLOR_T* fb, uint16_t x, uint16_t y, uint16_t width, uint16_t height) : |
embeddedartists | 0:4977187e90c7 | 8 | _caption(caption), _capx(0), _capy(0), |
embeddedartists | 0:4977187e90c7 | 9 | _bgCol(GREEN), _fgCol(BLACK), |
embeddedartists | 0:4977187e90c7 | 10 | _bgColPressed(BLACK), _fgColPressed(GREEN) |
embeddedartists | 0:4977187e90c7 | 11 | { |
embeddedartists | 0:4977187e90c7 | 12 | _enabled = true; |
embeddedartists | 0:4977187e90c7 | 13 | _pressed = false; |
embeddedartists | 0:4977187e90c7 | 14 | _func = NULL; |
embeddedartists | 0:4977187e90c7 | 15 | |
embeddedartists | 0:4977187e90c7 | 16 | Display* disp = DMBoard::instance().display(); |
embeddedartists | 0:4977187e90c7 | 17 | |
embeddedartists | 0:4977187e90c7 | 18 | swim_window_open_noclear( |
embeddedartists | 0:4977187e90c7 | 19 | &_win, |
embeddedartists | 0:4977187e90c7 | 20 | disp->width(), disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 21 | fb, |
embeddedartists | 0:4977187e90c7 | 22 | x, y, x+width-1, y+height-1, // window position and size |
embeddedartists | 0:4977187e90c7 | 23 | 0, // border |
embeddedartists | 0:4977187e90c7 | 24 | _fgCol, _bgCol, _fgCol); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 25 | |
embeddedartists | 0:4977187e90c7 | 26 | swim_set_font_transparency(&_win, 1); |
embeddedartists | 0:4977187e90c7 | 27 | setCaption(caption); |
embeddedartists | 0:4977187e90c7 | 28 | } |
embeddedartists | 0:4977187e90c7 | 29 | |
embeddedartists | 0:4977187e90c7 | 30 | void Button::setCaption(const char* caption) |
embeddedartists | 0:4977187e90c7 | 31 | { |
embeddedartists | 0:4977187e90c7 | 32 | int w, h; |
embeddedartists | 0:4977187e90c7 | 33 | _caption = caption; |
embeddedartists | 0:4977187e90c7 | 34 | swim_get_string_bounds(&_win, _caption, &w, &h); |
embeddedartists | 0:4977187e90c7 | 35 | _capx = (_win.xpmax-_win.xpmin-w)/2; |
embeddedartists | 0:4977187e90c7 | 36 | _capy = (_win.ypmax-_win.ypmin-h)/2; |
embeddedartists | 0:4977187e90c7 | 37 | } |
embeddedartists | 0:4977187e90c7 | 38 | |
embeddedartists | 0:4977187e90c7 | 39 | void Button::setColors(COLOR_T bg, COLOR_T fg, COLOR_T bgPressed, COLOR_T fgPressed) |
embeddedartists | 0:4977187e90c7 | 40 | { |
embeddedartists | 0:4977187e90c7 | 41 | _bgCol = bg; |
embeddedartists | 0:4977187e90c7 | 42 | _fgCol = fg; |
embeddedartists | 0:4977187e90c7 | 43 | _bgColPressed = bgPressed; |
embeddedartists | 0:4977187e90c7 | 44 | _fgColPressed = fgPressed; |
embeddedartists | 0:4977187e90c7 | 45 | } |
embeddedartists | 0:4977187e90c7 | 46 | |
embeddedartists | 0:4977187e90c7 | 47 | bool Button::handle(uint16_t x, uint16_t y, bool pressed) |
embeddedartists | 0:4977187e90c7 | 48 | { |
embeddedartists | 0:4977187e90c7 | 49 | bool needsRepaint = false; |
embeddedartists | 0:4977187e90c7 | 50 | if (_enabled) { |
embeddedartists | 0:4977187e90c7 | 51 | if (!pressed && _pressed) { |
embeddedartists | 0:4977187e90c7 | 52 | // user released => click |
embeddedartists | 0:4977187e90c7 | 53 | needsRepaint = true; |
embeddedartists | 0:4977187e90c7 | 54 | _pressed = false; |
embeddedartists | 0:4977187e90c7 | 55 | if (_func != NULL) { |
embeddedartists | 0:4977187e90c7 | 56 | _func(_funcArg); |
embeddedartists | 0:4977187e90c7 | 57 | } |
embeddedartists | 0:4977187e90c7 | 58 | } |
embeddedartists | 0:4977187e90c7 | 59 | else if ((x >= _win.xpmin) && (y >= _win.ypmin) && (x <= _win.xpmax) && (y <= _win.ypmax)) { |
embeddedartists | 0:4977187e90c7 | 60 | if (pressed && !_pressed) { |
embeddedartists | 0:4977187e90c7 | 61 | // user pressing inside area |
embeddedartists | 0:4977187e90c7 | 62 | needsRepaint = true; |
embeddedartists | 0:4977187e90c7 | 63 | _pressed = true; |
embeddedartists | 0:4977187e90c7 | 64 | } |
embeddedartists | 0:4977187e90c7 | 65 | } |
embeddedartists | 0:4977187e90c7 | 66 | else if (_pressed) { |
embeddedartists | 0:4977187e90c7 | 67 | // pressed but moved outside of the button area |
embeddedartists | 0:4977187e90c7 | 68 | needsRepaint = true; |
embeddedartists | 0:4977187e90c7 | 69 | _pressed = false; |
embeddedartists | 0:4977187e90c7 | 70 | } |
embeddedartists | 0:4977187e90c7 | 71 | } |
embeddedartists | 0:4977187e90c7 | 72 | return needsRepaint; |
embeddedartists | 0:4977187e90c7 | 73 | } |
embeddedartists | 0:4977187e90c7 | 74 | |
embeddedartists | 0:4977187e90c7 | 75 | void Button::draw() |
embeddedartists | 0:4977187e90c7 | 76 | { |
embeddedartists | 0:4977187e90c7 | 77 | if (_pressed) { |
embeddedartists | 0:4977187e90c7 | 78 | swim_set_pen_color(&_win, _fgColPressed); |
embeddedartists | 0:4977187e90c7 | 79 | swim_set_bkg_color(&_win, _bgColPressed); |
embeddedartists | 0:4977187e90c7 | 80 | swim_set_fill_color(&_win, _fgColPressed); |
embeddedartists | 0:4977187e90c7 | 81 | swim_clear_screen(&_win, _bgColPressed); |
embeddedartists | 0:4977187e90c7 | 82 | } else { |
embeddedartists | 0:4977187e90c7 | 83 | swim_set_pen_color(&_win, _fgCol); |
embeddedartists | 0:4977187e90c7 | 84 | swim_set_bkg_color(&_win, _bgCol); |
embeddedartists | 0:4977187e90c7 | 85 | swim_set_fill_color(&_win, _fgCol); |
embeddedartists | 0:4977187e90c7 | 86 | swim_clear_screen(&_win, _bgCol); |
embeddedartists | 0:4977187e90c7 | 87 | } |
embeddedartists | 0:4977187e90c7 | 88 | swim_put_text_xy(&_win, _caption, _capx, _capy); |
embeddedartists | 0:4977187e90c7 | 89 | } |
embeddedartists | 0:4977187e90c7 | 90 |