Buttons changing color when tapped.
Dependencies: LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI BSP_DISCO_F429ZI
button.cpp@0:6d5a5f60d228, 2019-04-04 (annotated)
- Committer:
- fzajdel
- Date:
- Thu Apr 04 15:26:11 2019 +0000
- Revision:
- 0:6d5a5f60d228
- Child:
- 2:bd49dd9c7bd6
Working Buttons changing Color on Tap
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 | 0:6d5a5f60d228 | 35 | switch(State){ |
fzajdel | 0:6d5a5f60d228 | 36 | case PRESSED: |
fzajdel | 0:6d5a5f60d228 | 37 | OnPress(); |
fzajdel | 0:6d5a5f60d228 | 38 | break; |
fzajdel | 0:6d5a5f60d228 | 39 | case RELEASED: |
fzajdel | 0:6d5a5f60d228 | 40 | OnRelease(); |
fzajdel | 0:6d5a5f60d228 | 41 | break; |
fzajdel | 0:6d5a5f60d228 | 42 | } |
fzajdel | 0:6d5a5f60d228 | 43 | |
fzajdel | 0:6d5a5f60d228 | 44 | State = (CheckRectRange(X,Y) ? PRESSED : RELEASED); |
fzajdel | 0:6d5a5f60d228 | 45 | }; |
fzajdel | 0:6d5a5f60d228 | 46 | |
fzajdel | 0:6d5a5f60d228 | 47 | void Button::OnPress(){ |
fzajdel | 0:6d5a5f60d228 | 48 | |
fzajdel | 0:6d5a5f60d228 | 49 | MyRectangle->SetFillingColor(BUTTON_PUSH_COLOR); |
fzajdel | 0:6d5a5f60d228 | 50 | if(NULL != CbOnPress){ |
fzajdel | 0:6d5a5f60d228 | 51 | CbOnPress->Execute(); |
fzajdel | 0:6d5a5f60d228 | 52 | } |
fzajdel | 0:6d5a5f60d228 | 53 | } |
fzajdel | 0:6d5a5f60d228 | 54 | |
fzajdel | 0:6d5a5f60d228 | 55 | void Button::OnRelease(){ |
fzajdel | 0:6d5a5f60d228 | 56 | |
fzajdel | 0:6d5a5f60d228 | 57 | MyRectangle->SetFillingColor(BUTTON_RELEASE_COLOR); |
fzajdel | 0:6d5a5f60d228 | 58 | if(NULL != CbOnRelease) { |
fzajdel | 0:6d5a5f60d228 | 59 | CbOnRelease->Execute(); |
fzajdel | 0:6d5a5f60d228 | 60 | } |
fzajdel | 0:6d5a5f60d228 | 61 | } |
fzajdel | 0:6d5a5f60d228 | 62 | |
fzajdel | 0:6d5a5f60d228 | 63 | Figure *Button::GetFigure(){ |
fzajdel | 0:6d5a5f60d228 | 64 | return MyRectangle; |
fzajdel | 0:6d5a5f60d228 | 65 | } |