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@1:46c8df4608c8, 2014-12-11 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Dec 11 18:15:52 2014 +0000
- Revision:
- 1:46c8df4608c8
- Parent:
- 0:4977187e90c7
- Child:
- 2:efae611de184
Updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 1:46c8df4608c8 | 1 | /* |
embeddedartists | 1:46c8df4608c8 | 2 | * Copyright 2014 Embedded Artists AB |
embeddedartists | 1:46c8df4608c8 | 3 | * |
embeddedartists | 1:46c8df4608c8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
embeddedartists | 1:46c8df4608c8 | 5 | * you may not use this file except in compliance with the License. |
embeddedartists | 1:46c8df4608c8 | 6 | * You may obtain a copy of the License at |
embeddedartists | 1:46c8df4608c8 | 7 | * |
embeddedartists | 1:46c8df4608c8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
embeddedartists | 1:46c8df4608c8 | 9 | * |
embeddedartists | 1:46c8df4608c8 | 10 | * Unless required by applicable law or agreed to in writing, software |
embeddedartists | 1:46c8df4608c8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
embeddedartists | 1:46c8df4608c8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
embeddedartists | 1:46c8df4608c8 | 13 | * See the License for the specific language governing permissions and |
embeddedartists | 1:46c8df4608c8 | 14 | * limitations under the License. |
embeddedartists | 1:46c8df4608c8 | 15 | */ |
embeddedartists | 1:46c8df4608c8 | 16 | |
embeddedartists | 0:4977187e90c7 | 17 | #include "Button.h" |
embeddedartists | 0:4977187e90c7 | 18 | #include "mbed.h" |
embeddedartists | 0:4977187e90c7 | 19 | #include "DMBoard.h" |
embeddedartists | 0:4977187e90c7 | 20 | |
embeddedartists | 0:4977187e90c7 | 21 | #include "lpc_swim_font.h" |
embeddedartists | 0:4977187e90c7 | 22 | |
embeddedartists | 0:4977187e90c7 | 23 | Button::Button(const char* caption, COLOR_T* fb, uint16_t x, uint16_t y, uint16_t width, uint16_t height) : |
embeddedartists | 0:4977187e90c7 | 24 | _caption(caption), _capx(0), _capy(0), |
embeddedartists | 0:4977187e90c7 | 25 | _bgCol(GREEN), _fgCol(BLACK), |
embeddedartists | 0:4977187e90c7 | 26 | _bgColPressed(BLACK), _fgColPressed(GREEN) |
embeddedartists | 0:4977187e90c7 | 27 | { |
embeddedartists | 0:4977187e90c7 | 28 | _enabled = true; |
embeddedartists | 0:4977187e90c7 | 29 | _pressed = false; |
embeddedartists | 0:4977187e90c7 | 30 | _func = NULL; |
embeddedartists | 0:4977187e90c7 | 31 | |
embeddedartists | 0:4977187e90c7 | 32 | Display* disp = DMBoard::instance().display(); |
embeddedartists | 0:4977187e90c7 | 33 | |
embeddedartists | 0:4977187e90c7 | 34 | swim_window_open_noclear( |
embeddedartists | 0:4977187e90c7 | 35 | &_win, |
embeddedartists | 0:4977187e90c7 | 36 | disp->width(), disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 37 | fb, |
embeddedartists | 0:4977187e90c7 | 38 | x, y, x+width-1, y+height-1, // window position and size |
embeddedartists | 0:4977187e90c7 | 39 | 0, // border |
embeddedartists | 0:4977187e90c7 | 40 | _fgCol, _bgCol, _fgCol); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 41 | |
embeddedartists | 0:4977187e90c7 | 42 | swim_set_font_transparency(&_win, 1); |
embeddedartists | 0:4977187e90c7 | 43 | setCaption(caption); |
embeddedartists | 0:4977187e90c7 | 44 | } |
embeddedartists | 0:4977187e90c7 | 45 | |
embeddedartists | 0:4977187e90c7 | 46 | void Button::setCaption(const char* caption) |
embeddedartists | 0:4977187e90c7 | 47 | { |
embeddedartists | 0:4977187e90c7 | 48 | int w, h; |
embeddedartists | 0:4977187e90c7 | 49 | _caption = caption; |
embeddedartists | 0:4977187e90c7 | 50 | swim_get_string_bounds(&_win, _caption, &w, &h); |
embeddedartists | 0:4977187e90c7 | 51 | _capx = (_win.xpmax-_win.xpmin-w)/2; |
embeddedartists | 0:4977187e90c7 | 52 | _capy = (_win.ypmax-_win.ypmin-h)/2; |
embeddedartists | 0:4977187e90c7 | 53 | } |
embeddedartists | 0:4977187e90c7 | 54 | |
embeddedartists | 0:4977187e90c7 | 55 | void Button::setColors(COLOR_T bg, COLOR_T fg, COLOR_T bgPressed, COLOR_T fgPressed) |
embeddedartists | 0:4977187e90c7 | 56 | { |
embeddedartists | 0:4977187e90c7 | 57 | _bgCol = bg; |
embeddedartists | 0:4977187e90c7 | 58 | _fgCol = fg; |
embeddedartists | 0:4977187e90c7 | 59 | _bgColPressed = bgPressed; |
embeddedartists | 0:4977187e90c7 | 60 | _fgColPressed = fgPressed; |
embeddedartists | 0:4977187e90c7 | 61 | } |
embeddedartists | 0:4977187e90c7 | 62 | |
embeddedartists | 0:4977187e90c7 | 63 | bool Button::handle(uint16_t x, uint16_t y, bool pressed) |
embeddedartists | 0:4977187e90c7 | 64 | { |
embeddedartists | 0:4977187e90c7 | 65 | bool needsRepaint = false; |
embeddedartists | 0:4977187e90c7 | 66 | if (_enabled) { |
embeddedartists | 0:4977187e90c7 | 67 | if (!pressed && _pressed) { |
embeddedartists | 0:4977187e90c7 | 68 | // user released => click |
embeddedartists | 0:4977187e90c7 | 69 | needsRepaint = true; |
embeddedartists | 0:4977187e90c7 | 70 | _pressed = false; |
embeddedartists | 0:4977187e90c7 | 71 | if (_func != NULL) { |
embeddedartists | 0:4977187e90c7 | 72 | _func(_funcArg); |
embeddedartists | 0:4977187e90c7 | 73 | } |
embeddedartists | 0:4977187e90c7 | 74 | } |
embeddedartists | 0:4977187e90c7 | 75 | else if ((x >= _win.xpmin) && (y >= _win.ypmin) && (x <= _win.xpmax) && (y <= _win.ypmax)) { |
embeddedartists | 0:4977187e90c7 | 76 | if (pressed && !_pressed) { |
embeddedartists | 0:4977187e90c7 | 77 | // user pressing inside area |
embeddedartists | 0:4977187e90c7 | 78 | needsRepaint = true; |
embeddedartists | 0:4977187e90c7 | 79 | _pressed = true; |
embeddedartists | 0:4977187e90c7 | 80 | } |
embeddedartists | 0:4977187e90c7 | 81 | } |
embeddedartists | 0:4977187e90c7 | 82 | else if (_pressed) { |
embeddedartists | 0:4977187e90c7 | 83 | // pressed but moved outside of the button area |
embeddedartists | 0:4977187e90c7 | 84 | needsRepaint = true; |
embeddedartists | 0:4977187e90c7 | 85 | _pressed = false; |
embeddedartists | 0:4977187e90c7 | 86 | } |
embeddedartists | 0:4977187e90c7 | 87 | } |
embeddedartists | 0:4977187e90c7 | 88 | return needsRepaint; |
embeddedartists | 0:4977187e90c7 | 89 | } |
embeddedartists | 0:4977187e90c7 | 90 | |
embeddedartists | 0:4977187e90c7 | 91 | void Button::draw() |
embeddedartists | 0:4977187e90c7 | 92 | { |
embeddedartists | 0:4977187e90c7 | 93 | if (_pressed) { |
embeddedartists | 0:4977187e90c7 | 94 | swim_set_pen_color(&_win, _fgColPressed); |
embeddedartists | 0:4977187e90c7 | 95 | swim_set_bkg_color(&_win, _bgColPressed); |
embeddedartists | 0:4977187e90c7 | 96 | swim_set_fill_color(&_win, _fgColPressed); |
embeddedartists | 0:4977187e90c7 | 97 | swim_clear_screen(&_win, _bgColPressed); |
embeddedartists | 0:4977187e90c7 | 98 | } else { |
embeddedartists | 0:4977187e90c7 | 99 | swim_set_pen_color(&_win, _fgCol); |
embeddedartists | 0:4977187e90c7 | 100 | swim_set_bkg_color(&_win, _bgCol); |
embeddedartists | 0:4977187e90c7 | 101 | swim_set_fill_color(&_win, _fgCol); |
embeddedartists | 0:4977187e90c7 | 102 | swim_clear_screen(&_win, _bgCol); |
embeddedartists | 0:4977187e90c7 | 103 | } |
embeddedartists | 0:4977187e90c7 | 104 | swim_put_text_xy(&_win, _caption, _capx, _capy); |
embeddedartists | 0:4977187e90c7 | 105 | } |