Same as original
button_group.cpp@13:af578b53ff0e, 2016-02-22 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Feb 22 13:39:32 2016 +0000
- Revision:
- 13:af578b53ff0e
- Parent:
- 11:204bc17f59cc
- Child:
- 14:3076e383e6b0
14
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:d3038879fed6 | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 5:2cc388e91bde | 2 | // Button group class |
MikamiUitOpen | 0:d3038879fed6 | 3 | // |
MikamiUitOpen | 13:af578b53ff0e | 4 | // 2016/02/22, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:d3038879fed6 | 5 | //----------------------------------------------------------- |
MikamiUitOpen | 0:d3038879fed6 | 6 | |
MikamiUitOpen | 0:d3038879fed6 | 7 | #include "button_group.hpp" |
MikamiUitOpen | 0:d3038879fed6 | 8 | |
MikamiUitOpen | 0:d3038879fed6 | 9 | namespace Mikami |
MikamiUitOpen | 0:d3038879fed6 | 10 | { |
MikamiUitOpen | 0:d3038879fed6 | 11 | // Constructor |
MikamiUitOpen | 0:d3038879fed6 | 12 | ButtonGroup::ButtonGroup(LCD_DISCO_F746NG &lcd, TS_DISCO_F746NG &ts, |
MikamiUitOpen | 0:d3038879fed6 | 13 | uint16_t x0, uint16_t y0, |
MikamiUitOpen | 0:d3038879fed6 | 14 | uint16_t width, uint16_t height, |
MikamiUitOpen | 0:d3038879fed6 | 15 | uint32_t color, uint32_t backColor, |
MikamiUitOpen | 3:d99d9c0324b7 | 16 | uint16_t number, const string str[], |
MikamiUitOpen | 0:d3038879fed6 | 17 | uint16_t spaceX, uint16_t spaceY, |
MikamiUitOpen | 0:d3038879fed6 | 18 | uint16_t column, |
MikamiUitOpen | 0:d3038879fed6 | 19 | sFONT &fonts, uint32_t textColor) |
MikamiUitOpen | 13:af578b53ff0e | 20 | : numberOfButtons_(number), touchedNum_(-1) |
MikamiUitOpen | 0:d3038879fed6 | 21 | { |
MikamiUitOpen | 0:d3038879fed6 | 22 | buttons_ = new Button *[number]; |
MikamiUitOpen | 0:d3038879fed6 | 23 | for (int n=0; n<number; n++) |
MikamiUitOpen | 0:d3038879fed6 | 24 | { |
MikamiUitOpen | 0:d3038879fed6 | 25 | div_t u1 = div(n, column); |
MikamiUitOpen | 0:d3038879fed6 | 26 | uint16_t x = x0 + u1.rem*(width + spaceX); |
MikamiUitOpen | 0:d3038879fed6 | 27 | uint16_t y = y0 + u1.quot*(height + spaceY); |
MikamiUitOpen | 0:d3038879fed6 | 28 | buttons_[n] = new Button(lcd, ts, x, y, width, height, |
MikamiUitOpen | 0:d3038879fed6 | 29 | color, backColor, |
MikamiUitOpen | 0:d3038879fed6 | 30 | str[n], fonts, textColor); |
MikamiUitOpen | 0:d3038879fed6 | 31 | } |
MikamiUitOpen | 0:d3038879fed6 | 32 | } |
MikamiUitOpen | 0:d3038879fed6 | 33 | |
MikamiUitOpen | 0:d3038879fed6 | 34 | // Destructor |
MikamiUitOpen | 0:d3038879fed6 | 35 | ButtonGroup::~ButtonGroup() |
MikamiUitOpen | 0:d3038879fed6 | 36 | { |
MikamiUitOpen | 0:d3038879fed6 | 37 | for (int n=0; n<numberOfButtons_; n++) delete buttons_[n]; |
MikamiUitOpen | 0:d3038879fed6 | 38 | delete[] *buttons_; |
MikamiUitOpen | 0:d3038879fed6 | 39 | } |
MikamiUitOpen | 0:d3038879fed6 | 40 | |
MikamiUitOpen | 13:af578b53ff0e | 41 | // Draw button |
MikamiUitOpen | 13:af578b53ff0e | 42 | bool ButtonGroup::Draw(int num, uint32_t color, uint32_t textColor) |
MikamiUitOpen | 13:af578b53ff0e | 43 | { |
MikamiUitOpen | 13:af578b53ff0e | 44 | if (!Range(num)) return false; |
MikamiUitOpen | 13:af578b53ff0e | 45 | buttons_[num]->Draw(color, textColor); |
MikamiUitOpen | 13:af578b53ff0e | 46 | touchedNum_ = num; |
MikamiUitOpen | 13:af578b53ff0e | 47 | return true; |
MikamiUitOpen | 13:af578b53ff0e | 48 | } |
MikamiUitOpen | 13:af578b53ff0e | 49 | |
MikamiUitOpen | 11:204bc17f59cc | 50 | // Redraw button with original color |
MikamiUitOpen | 11:204bc17f59cc | 51 | bool ButtonGroup::Redraw(int num, uint32_t textColor) |
MikamiUitOpen | 11:204bc17f59cc | 52 | { |
MikamiUitOpen | 11:204bc17f59cc | 53 | if (!Range(num)) return false; |
MikamiUitOpen | 11:204bc17f59cc | 54 | buttons_[num]->Redraw(textColor); |
MikamiUitOpen | 11:204bc17f59cc | 55 | return true; |
MikamiUitOpen | 11:204bc17f59cc | 56 | } |
MikamiUitOpen | 11:204bc17f59cc | 57 | |
MikamiUitOpen | 11:204bc17f59cc | 58 | // Erase button with selected color |
MikamiUitOpen | 11:204bc17f59cc | 59 | bool ButtonGroup::Erase(int num, uint32_t color) |
MikamiUitOpen | 11:204bc17f59cc | 60 | { |
MikamiUitOpen | 11:204bc17f59cc | 61 | if (!Range(num)) return false; |
MikamiUitOpen | 11:204bc17f59cc | 62 | buttons_[num]->Draw(color, color); |
MikamiUitOpen | 11:204bc17f59cc | 63 | return true; |
MikamiUitOpen | 11:204bc17f59cc | 64 | } |
MikamiUitOpen | 11:204bc17f59cc | 65 | |
MikamiUitOpen | 11:204bc17f59cc | 66 | // Check touch detected for specified button |
MikamiUitOpen | 11:204bc17f59cc | 67 | bool ButtonGroup::Touched(int num) |
MikamiUitOpen | 11:204bc17f59cc | 68 | { |
MikamiUitOpen | 11:204bc17f59cc | 69 | if (!Range(num)) return false; |
MikamiUitOpen | 13:af578b53ff0e | 70 | bool touched = buttons_[num]->Touched(); |
MikamiUitOpen | 13:af578b53ff0e | 71 | if (touched) touchedNum_ = num; |
MikamiUitOpen | 13:af578b53ff0e | 72 | return touched; |
MikamiUitOpen | 11:204bc17f59cc | 73 | } |
MikamiUitOpen | 11:204bc17f59cc | 74 | |
MikamiUitOpen | 6:d268555e2f50 | 75 | // Check touch detected for specified button and redraw |
MikamiUitOpen | 6:d268555e2f50 | 76 | bool ButtonGroup::Touched(int num, uint32_t color, |
MikamiUitOpen | 6:d268555e2f50 | 77 | uint32_t textColor) |
MikamiUitOpen | 6:d268555e2f50 | 78 | { |
MikamiUitOpen | 11:204bc17f59cc | 79 | if (!Range(num)) return false; |
MikamiUitOpen | 13:af578b53ff0e | 80 | bool touched = buttons_[num]->Touched(color, textColor); |
MikamiUitOpen | 13:af578b53ff0e | 81 | if (touched) |
MikamiUitOpen | 6:d268555e2f50 | 82 | { |
MikamiUitOpen | 13:af578b53ff0e | 83 | if (Range(touchedNum_) && (num != touchedNum_)) |
MikamiUitOpen | 13:af578b53ff0e | 84 | buttons_[touchedNum_]->Redraw(); |
MikamiUitOpen | 13:af578b53ff0e | 85 | touchedNum_ = num; |
MikamiUitOpen | 6:d268555e2f50 | 86 | } |
MikamiUitOpen | 13:af578b53ff0e | 87 | return touched; |
MikamiUitOpen | 6:d268555e2f50 | 88 | } |
MikamiUitOpen | 6:d268555e2f50 | 89 | |
MikamiUitOpen | 7:6913aebb3905 | 90 | // Get touched number |
MikamiUitOpen | 7:6913aebb3905 | 91 | bool ButtonGroup::GetTouchedNumber(int &num) |
MikamiUitOpen | 7:6913aebb3905 | 92 | { |
MikamiUitOpen | 13:af578b53ff0e | 93 | if (buttons_[0]->PanelTouched()) |
MikamiUitOpen | 13:af578b53ff0e | 94 | { |
MikamiUitOpen | 13:af578b53ff0e | 95 | for (int n=0; n<numberOfButtons_; n++) |
MikamiUitOpen | 13:af578b53ff0e | 96 | if (buttons_[n]->IsOnButton()) |
MikamiUitOpen | 13:af578b53ff0e | 97 | { |
MikamiUitOpen | 13:af578b53ff0e | 98 | num = n; |
MikamiUitOpen | 13:af578b53ff0e | 99 | return true; |
MikamiUitOpen | 13:af578b53ff0e | 100 | } |
MikamiUitOpen | 13:af578b53ff0e | 101 | return false; |
MikamiUitOpen | 13:af578b53ff0e | 102 | } |
MikamiUitOpen | 13:af578b53ff0e | 103 | else |
MikamiUitOpen | 13:af578b53ff0e | 104 | return false; |
MikamiUitOpen | 7:6913aebb3905 | 105 | } |
MikamiUitOpen | 7:6913aebb3905 | 106 | |
MikamiUitOpen | 0:d3038879fed6 | 107 | // Get touched number and redraw button if touched |
MikamiUitOpen | 1:57fe493e8db2 | 108 | bool ButtonGroup::GetTouchedNumber(int &num, uint32_t color) |
MikamiUitOpen | 0:d3038879fed6 | 109 | { |
MikamiUitOpen | 0:d3038879fed6 | 110 | if (GetTouchedNumber(num)) |
MikamiUitOpen | 0:d3038879fed6 | 111 | { |
MikamiUitOpen | 13:af578b53ff0e | 112 | buttons_[num]->Draw(color); |
MikamiUitOpen | 13:af578b53ff0e | 113 | if (Range(touchedNum_) && (num != touchedNum_)) |
MikamiUitOpen | 13:af578b53ff0e | 114 | buttons_[touchedNum_]->Redraw(); |
MikamiUitOpen | 13:af578b53ff0e | 115 | touchedNum_ = num; |
MikamiUitOpen | 13:af578b53ff0e | 116 | return true; |
MikamiUitOpen | 0:d3038879fed6 | 117 | } |
MikamiUitOpen | 0:d3038879fed6 | 118 | else |
MikamiUitOpen | 0:d3038879fed6 | 119 | return false; |
MikamiUitOpen | 0:d3038879fed6 | 120 | } |
MikamiUitOpen | 0:d3038879fed6 | 121 | } |