Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DMBasicGUI by
Application/Button.h@14:647b1896ed84, 2015-03-09 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Mar 09 10:50:17 2015 +0100
- Revision:
- 14:647b1896ed84
- Parent:
- 10:651861441108
Updated code 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 | 1:46c8df4608c8 | 17 | #ifndef BUTTON_H |
| embeddedartists | 1:46c8df4608c8 | 18 | #define BUTTON_H |
| embeddedartists | 0:4977187e90c7 | 19 | |
| embeddedartists | 2:efae611de184 | 20 | #include "Clickable.h" |
| embeddedartists | 0:4977187e90c7 | 21 | |
| embeddedartists | 1:46c8df4608c8 | 22 | /** |
| embeddedartists | 14:647b1896ed84 | 23 | * A button with a caption |
| embeddedartists | 1:46c8df4608c8 | 24 | * |
| embeddedartists | 1:46c8df4608c8 | 25 | * @code |
| embeddedartists | 1:46c8df4608c8 | 26 | * #include "mbed.h" |
| embeddedartists | 1:46c8df4608c8 | 27 | * #include "Button.h" |
| embeddedartists | 1:46c8df4608c8 | 28 | * |
| embeddedartists | 1:46c8df4608c8 | 29 | * SWIM_WINDOW_T win; |
| embeddedartists | 1:46c8df4608c8 | 30 | * |
| embeddedartists | 1:46c8df4608c8 | 31 | * static void buttonClicked(uint32_t x) |
| embeddedartists | 1:46c8df4608c8 | 32 | * { |
| embeddedartists | 1:46c8df4608c8 | 33 | * bool* done = (bool*)x; |
| embeddedartists | 1:46c8df4608c8 | 34 | * *done = true; |
| embeddedartists | 1:46c8df4608c8 | 35 | * } |
| embeddedartists | 1:46c8df4608c8 | 36 | * |
| embeddedartists | 1:46c8df4608c8 | 37 | * int main(void) { |
| embeddedartists | 1:46c8df4608c8 | 38 | * // initialize the display and touch |
| embeddedartists | 1:46c8df4608c8 | 39 | * DMBoard::instance().init(); |
| embeddedartists | 1:46c8df4608c8 | 40 | * |
| embeddedartists | 1:46c8df4608c8 | 41 | * // setup the SWIM window to use |
| embeddedartists | 1:46c8df4608c8 | 42 | * swim_window_open(&win, ...); |
| embeddedartists | 1:46c8df4608c8 | 43 | * |
| embeddedartists | 1:46c8df4608c8 | 44 | * // create a 60x30 pixels button labeled "Done" at 100,100 |
| embeddedartists | 1:46c8df4608c8 | 45 | * Button btn("Done", win.fb, 100, 100, 60, 30); |
| embeddedartists | 1:46c8df4608c8 | 46 | * btn.draw(); |
| embeddedartists | 1:46c8df4608c8 | 47 | * |
| embeddedartists | 1:46c8df4608c8 | 48 | * // register a callback for when the button is pressed and pass the |
| embeddedartists | 1:46c8df4608c8 | 49 | * // done flag for the callback to modify |
| embeddedartists | 1:46c8df4608c8 | 50 | * bool done = false; |
| embeddedartists | 1:46c8df4608c8 | 51 | * btn.setAction(buttonClicked, (uint32_t)&done); |
| embeddedartists | 1:46c8df4608c8 | 52 | * |
| embeddedartists | 1:46c8df4608c8 | 53 | * // keep processing touch events until the button is clicked |
| embeddedartists | 1:46c8df4608c8 | 54 | * TouchPanel* touch = DMBoard::instance().touchPanel(); |
| embeddedartists | 1:46c8df4608c8 | 55 | * TouchPanel::touchCoordinate_t coord; |
| embeddedartists | 1:46c8df4608c8 | 56 | * while(!done) { |
| embeddedartists | 3:3fabfe3339b8 | 57 | * if (touch->read(coord) == TouchPanel::TouchError_Ok) { |
| embeddedartists | 3:3fabfe3339b8 | 58 | * if (btn.handle(coord.x, coord.y, coord.z > 0)) { |
| embeddedartists | 3:3fabfe3339b8 | 59 | * btn.draw(); |
| embeddedartists | 3:3fabfe3339b8 | 60 | * } |
| embeddedartists | 1:46c8df4608c8 | 61 | * } |
| embeddedartists | 1:46c8df4608c8 | 62 | * } |
| embeddedartists | 1:46c8df4608c8 | 63 | * } |
| embeddedartists | 1:46c8df4608c8 | 64 | * @endcode |
| embeddedartists | 1:46c8df4608c8 | 65 | */ |
| embeddedartists | 2:efae611de184 | 66 | class Button : public Clickable { |
| embeddedartists | 0:4977187e90c7 | 67 | public: |
| embeddedartists | 1:46c8df4608c8 | 68 | |
| embeddedartists | 1:46c8df4608c8 | 69 | /** Creates a new button |
| embeddedartists | 1:46c8df4608c8 | 70 | * |
| embeddedartists | 1:46c8df4608c8 | 71 | * This button will use a SWIM window to draw on. That window will use |
| embeddedartists | 1:46c8df4608c8 | 72 | * part of the full size frame buffer to draw on. |
| embeddedartists | 1:46c8df4608c8 | 73 | * |
| embeddedartists | 1:46c8df4608c8 | 74 | * @param caption the button text |
| embeddedartists | 1:46c8df4608c8 | 75 | * @param fb the frame buffer |
| embeddedartists | 1:46c8df4608c8 | 76 | * @param x the upper left corner of the button |
| embeddedartists | 1:46c8df4608c8 | 77 | * @param y the upper left corner of the button |
| embeddedartists | 1:46c8df4608c8 | 78 | * @param width the width of the button |
| embeddedartists | 1:46c8df4608c8 | 79 | * @param height the height of the button |
| embeddedartists | 1:46c8df4608c8 | 80 | */ |
| embeddedartists | 0:4977187e90c7 | 81 | Button(const char* caption, COLOR_T* fb, uint16_t x, uint16_t y, uint16_t width, uint16_t height); |
| embeddedartists | 10:651861441108 | 82 | virtual ~Button() {}; |
| embeddedartists | 1:46c8df4608c8 | 83 | |
| embeddedartists | 1:46c8df4608c8 | 84 | /** Changes the caption |
| embeddedartists | 1:46c8df4608c8 | 85 | * |
| embeddedartists | 1:46c8df4608c8 | 86 | * @param caption the new text on the button |
| embeddedartists | 1:46c8df4608c8 | 87 | */ |
| embeddedartists | 0:4977187e90c7 | 88 | void setCaption(const char* caption); |
| embeddedartists | 1:46c8df4608c8 | 89 | |
| embeddedartists | 1:46c8df4608c8 | 90 | /** Changes the colors |
| embeddedartists | 1:46c8df4608c8 | 91 | * |
| embeddedartists | 1:46c8df4608c8 | 92 | * @param bg background color when not pressed |
| embeddedartists | 1:46c8df4608c8 | 93 | * @param fg text color when pressed |
| embeddedartists | 1:46c8df4608c8 | 94 | * @param bgPressed background color when pressed |
| embeddedartists | 1:46c8df4608c8 | 95 | * @param fgPressed text color when pressed |
| embeddedartists | 1:46c8df4608c8 | 96 | */ |
| embeddedartists | 0:4977187e90c7 | 97 | void setColors(COLOR_T bg, COLOR_T fg, COLOR_T bgPressed, COLOR_T fgPressed); |
| embeddedartists | 1:46c8df4608c8 | 98 | |
| embeddedartists | 6:7917b0894655 | 99 | /** Draws the button (on a new framebuffer if one is specified) |
| embeddedartists | 6:7917b0894655 | 100 | * @param fb the frame buffer |
| embeddedartists | 1:46c8df4608c8 | 101 | */ |
| embeddedartists | 6:7917b0894655 | 102 | virtual void draw(COLOR_T* fb = 0); |
| embeddedartists | 0:4977187e90c7 | 103 | |
| embeddedartists | 0:4977187e90c7 | 104 | private: |
| embeddedartists | 0:4977187e90c7 | 105 | const char* _caption; |
| embeddedartists | 0:4977187e90c7 | 106 | int _capx, _capy; |
| embeddedartists | 0:4977187e90c7 | 107 | COLOR_T _bgCol, _fgCol, _bgColPressed, _fgColPressed; |
| embeddedartists | 0:4977187e90c7 | 108 | }; |
| embeddedartists | 0:4977187e90c7 | 109 | |
| embeddedartists | 1:46c8df4608c8 | 110 | #endif /* BUTTON_H */ |
