Buttons changing color when tapped.
Dependencies: LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI BSP_DISCO_F429ZI
button.cpp@2:bd49dd9c7bd6, 2019-05-19 (annotated)
- Committer:
- fzajdel
- Date:
- Sun May 19 11:59:32 2019 +0000
- Revision:
- 2:bd49dd9c7bd6
- Parent:
- 0:6d5a5f60d228
Touch screen with buttons
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fzajdel | 0:6d5a5f60d228 | 1 | #include "button.h" |
fzajdel | 0:6d5a5f60d228 | 2 | |
fzajdel | 0:6d5a5f60d228 | 3 | Button::Button(uint8_t x, uint8_t y, uint8_t width, uint8_t height, string Text){ |
fzajdel | 0:6d5a5f60d228 | 4 | |
fzajdel | 0:6d5a5f60d228 | 5 | MyRectangle = new Rectangle(x,y,width, height); |
fzajdel | 0:6d5a5f60d228 | 6 | MyRectangle->SetText(Text, BUTTON_TEXT_COLOR, BUTTON_TEXT_BACK_COLOR); |
fzajdel | 0:6d5a5f60d228 | 7 | |
fzajdel | 0:6d5a5f60d228 | 8 | CbOnPress = NULL; |
fzajdel | 0:6d5a5f60d228 | 9 | CbOnRelease = NULL; |
fzajdel | 0:6d5a5f60d228 | 10 | |
fzajdel | 0:6d5a5f60d228 | 11 | State = RELEASED; |
fzajdel | 0:6d5a5f60d228 | 12 | } |
fzajdel | 0:6d5a5f60d228 | 13 | |
fzajdel | 0:6d5a5f60d228 | 14 | Button::Button(uint8_t x, uint8_t y, uint8_t width, uint8_t height, |
fzajdel | 0:6d5a5f60d228 | 15 | WidgetCallback* CbOnPress, WidgetCallback* CbOnRelease, |
fzajdel | 0:6d5a5f60d228 | 16 | string Text): CbOnPress(CbOnPress), CbOnRelease(CbOnRelease) { |
fzajdel | 0:6d5a5f60d228 | 17 | |
fzajdel | 0:6d5a5f60d228 | 18 | MyRectangle = new Rectangle(x,y,width, height); |
fzajdel | 0:6d5a5f60d228 | 19 | MyRectangle->SetText(Text, BUTTON_TEXT_COLOR, BUTTON_TEXT_BACK_COLOR); |
fzajdel | 0:6d5a5f60d228 | 20 | |
fzajdel | 0:6d5a5f60d228 | 21 | State = RELEASED; |
fzajdel | 0:6d5a5f60d228 | 22 | } |
fzajdel | 0:6d5a5f60d228 | 23 | |
fzajdel | 0:6d5a5f60d228 | 24 | bool Button::CheckRectRange(uint16_t X, uint16_t Y){ |
fzajdel | 0:6d5a5f60d228 | 25 | uint16_t RectX = MyRectangle->GetX(); |
fzajdel | 0:6d5a5f60d228 | 26 | uint16_t RectY = MyRectangle->GetY(); |
fzajdel | 0:6d5a5f60d228 | 27 | uint16_t RectHeight = MyRectangle->GetHeight(); |
fzajdel | 0:6d5a5f60d228 | 28 | uint16_t RectWidth = MyRectangle->GetHeight(); |
fzajdel | 0:6d5a5f60d228 | 29 | |
fzajdel | 0:6d5a5f60d228 | 30 | return (X>=RectX && X<RectX+RectWidth && Y>=RectY && Y<RectY+RectHeight); |
fzajdel | 0:6d5a5f60d228 | 31 | } |
fzajdel | 0:6d5a5f60d228 | 32 | |
fzajdel | 0:6d5a5f60d228 | 33 | void Button::Check(uint16_t X, uint16_t Y){ |
fzajdel | 0:6d5a5f60d228 | 34 | |
fzajdel | 2:bd49dd9c7bd6 | 35 | State = (CheckRectRange(X,Y) ? PRESSED : RELEASED); |
fzajdel | 2:bd49dd9c7bd6 | 36 | |
fzajdel | 0:6d5a5f60d228 | 37 | switch(State){ |
fzajdel | 0:6d5a5f60d228 | 38 | case PRESSED: |
fzajdel | 0:6d5a5f60d228 | 39 | OnPress(); |
fzajdel | 0:6d5a5f60d228 | 40 | break; |
fzajdel | 0:6d5a5f60d228 | 41 | case RELEASED: |
fzajdel | 0:6d5a5f60d228 | 42 | OnRelease(); |
fzajdel | 0:6d5a5f60d228 | 43 | break; |
fzajdel | 0:6d5a5f60d228 | 44 | } |
fzajdel | 2:bd49dd9c7bd6 | 45 | }; |
fzajdel | 2:bd49dd9c7bd6 | 46 | |
fzajdel | 2:bd49dd9c7bd6 | 47 | bool Button::isPressed(uint16_t X, uint16_t Y){ |
fzajdel | 2:bd49dd9c7bd6 | 48 | return (CheckRectRange(X,Y) ? PRESSED : RELEASED); |
fzajdel | 2:bd49dd9c7bd6 | 49 | } |
fzajdel | 0:6d5a5f60d228 | 50 | |
fzajdel | 0:6d5a5f60d228 | 51 | void Button::OnPress(){ |
fzajdel | 0:6d5a5f60d228 | 52 | |
fzajdel | 0:6d5a5f60d228 | 53 | MyRectangle->SetFillingColor(BUTTON_PUSH_COLOR); |
fzajdel | 2:bd49dd9c7bd6 | 54 | |
fzajdel | 0:6d5a5f60d228 | 55 | if(NULL != CbOnPress){ |
fzajdel | 0:6d5a5f60d228 | 56 | CbOnPress->Execute(); |
fzajdel | 0:6d5a5f60d228 | 57 | } |
fzajdel | 0:6d5a5f60d228 | 58 | } |
fzajdel | 0:6d5a5f60d228 | 59 | |
fzajdel | 0:6d5a5f60d228 | 60 | void Button::OnRelease(){ |
fzajdel | 0:6d5a5f60d228 | 61 | |
fzajdel | 0:6d5a5f60d228 | 62 | MyRectangle->SetFillingColor(BUTTON_RELEASE_COLOR); |
fzajdel | 2:bd49dd9c7bd6 | 63 | |
fzajdel | 0:6d5a5f60d228 | 64 | if(NULL != CbOnRelease) { |
fzajdel | 0:6d5a5f60d228 | 65 | CbOnRelease->Execute(); |
fzajdel | 0:6d5a5f60d228 | 66 | } |
fzajdel | 0:6d5a5f60d228 | 67 | } |
fzajdel | 0:6d5a5f60d228 | 68 | |
fzajdel | 0:6d5a5f60d228 | 69 | Figure *Button::GetFigure(){ |
fzajdel | 0:6d5a5f60d228 | 70 | return MyRectangle; |
fzajdel | 0:6d5a5f60d228 | 71 | } |