Same as original
Diff: button_group.cpp
- Revision:
- 13:af578b53ff0e
- Parent:
- 11:204bc17f59cc
- Child:
- 14:3076e383e6b0
--- a/button_group.cpp Thu Feb 18 10:04:20 2016 +0000 +++ b/button_group.cpp Mon Feb 22 13:39:32 2016 +0000 @@ -1,7 +1,7 @@ //----------------------------------------------------------- // Button group class // -// 2016/02/17, Copyright (c) 2016 MIKAMI, Naoki +// 2016/02/22, Copyright (c) 2016 MIKAMI, Naoki //----------------------------------------------------------- #include "button_group.hpp" @@ -17,7 +17,7 @@ uint16_t spaceX, uint16_t spaceY, uint16_t column, sFONT &fonts, uint32_t textColor) - : numberOfButtons_(number) + : numberOfButtons_(number), touchedNum_(-1) { buttons_ = new Button *[number]; for (int n=0; n<number; n++) @@ -38,6 +38,15 @@ delete[] *buttons_; } + // Draw button + bool ButtonGroup::Draw(int num, uint32_t color, uint32_t textColor) + { + if (!Range(num)) return false; + buttons_[num]->Draw(color, textColor); + touchedNum_ = num; + return true; + } + // Redraw button with original color bool ButtonGroup::Redraw(int num, uint32_t textColor) { @@ -58,7 +67,9 @@ bool ButtonGroup::Touched(int num) { if (!Range(num)) return false; - return buttons_[num]->Touched(); + bool touched = buttons_[num]->Touched(); + if (touched) touchedNum_ = num; + return touched; } // Check touch detected for specified button and redraw @@ -66,26 +77,31 @@ uint32_t textColor) { if (!Range(num)) return false; - if (buttons_[num]->Touched(color, textColor)) + bool touched = buttons_[num]->Touched(color, textColor); + if (touched) { - for (int n=0; n<numberOfButtons_; n++) - if (n != num) buttons_[n]->Redraw(); - return true; + if (Range(touchedNum_) && (num != touchedNum_)) + buttons_[touchedNum_]->Redraw(); + touchedNum_ = num; } - else - return false; + return touched; } // Get touched number bool ButtonGroup::GetTouchedNumber(int &num) { - for (int n=0; n<numberOfButtons_; n++) - if (buttons_[n]->Touched()) - { - num = n; - return true; - } - return false; + if (buttons_[0]->PanelTouched()) + { + for (int n=0; n<numberOfButtons_; n++) + if (buttons_[n]->IsOnButton()) + { + num = n; + return true; + } + return false; + } + else + return false; } // Get touched number and redraw button if touched @@ -93,23 +109,13 @@ { if (GetTouchedNumber(num)) { - for (int n=0; n<numberOfButtons_; n++) - if (n == num) - buttons_[n]->Draw(color); - else - buttons_[n]->Redraw(); - return true; + buttons_[num]->Draw(color); + if (Range(touchedNum_) && (num != touchedNum_)) + buttons_[touchedNum_]->Redraw(); + touchedNum_ = num; + return true; } else return false; } - - // Check range of argument - bool ButtonGroup::Range(int n) - { - if ( (n >= 0) && (n < numberOfButtons_) ) - return true; - else - return false; - } }