SPKT
ButtonGroup.cpp@0:4e245f4bc8ac, 2019-06-07 (annotated)
- Committer:
- phungductung
- Date:
- Fri Jun 07 05:06:42 2019 +0000
- Revision:
- 0:4e245f4bc8ac
spkt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phungductung | 0:4e245f4bc8ac | 1 | //----------------------------------------------------------- |
phungductung | 0:4e245f4bc8ac | 2 | // ButtonGroup class |
phungductung | 0:4e245f4bc8ac | 3 | // |
phungductung | 0:4e245f4bc8ac | 4 | // 2017/01/25, Copyright (c) 2017 MIKAMI, Naoki |
phungductung | 0:4e245f4bc8ac | 5 | //----------------------------------------------------------- |
phungductung | 0:4e245f4bc8ac | 6 | |
phungductung | 0:4e245f4bc8ac | 7 | #include "ButtonGroup.hpp" |
phungductung | 0:4e245f4bc8ac | 8 | |
phungductung | 0:4e245f4bc8ac | 9 | namespace Mikami |
phungductung | 0:4e245f4bc8ac | 10 | { |
phungductung | 0:4e245f4bc8ac | 11 | // Constructor |
phungductung | 0:4e245f4bc8ac | 12 | ButtonGroup::ButtonGroup( |
phungductung | 0:4e245f4bc8ac | 13 | uint16_t x0, uint16_t y0, |
phungductung | 0:4e245f4bc8ac | 14 | uint16_t width, uint16_t height, |
phungductung | 0:4e245f4bc8ac | 15 | uint16_t number, const string str[], |
phungductung | 0:4e245f4bc8ac | 16 | uint16_t spaceX, uint16_t spaceY, |
phungductung | 0:4e245f4bc8ac | 17 | uint16_t column, int touched, |
phungductung | 0:4e245f4bc8ac | 18 | sFONT &fonts, |
phungductung | 0:4e245f4bc8ac | 19 | uint32_t textColor, uint32_t backColor, |
phungductung | 0:4e245f4bc8ac | 20 | uint32_t createdColor, uint32_t touchedColor, |
phungductung | 0:4e245f4bc8ac | 21 | uint32_t inactiveColor, uint32_t inactiveTextColor) |
phungductung | 0:4e245f4bc8ac | 22 | : TEXT_COLOR_(textColor), CREATED_COLOR_(createdColor), |
phungductung | 0:4e245f4bc8ac | 23 | TOUCHED_COLOR_(touchedColor), |
phungductung | 0:4e245f4bc8ac | 24 | NUMBER_(number), prevNum_(touched) |
phungductung | 0:4e245f4bc8ac | 25 | { |
phungductung | 0:4e245f4bc8ac | 26 | buttons_.SetSize(number); |
phungductung | 0:4e245f4bc8ac | 27 | for (int n=0; n<number; n++) |
phungductung | 0:4e245f4bc8ac | 28 | { |
phungductung | 0:4e245f4bc8ac | 29 | div_t u1 = div(n, column); |
phungductung | 0:4e245f4bc8ac | 30 | uint16_t x = x0 + u1.rem*(width + spaceX); |
phungductung | 0:4e245f4bc8ac | 31 | uint16_t y = y0 + u1.quot*(height + spaceY); |
phungductung | 0:4e245f4bc8ac | 32 | buttons_[n] = |
phungductung | 0:4e245f4bc8ac | 33 | new Button(x, y, width, height, str[n], fonts, |
phungductung | 0:4e245f4bc8ac | 34 | textColor, backColor, |
phungductung | 0:4e245f4bc8ac | 35 | createdColor, touchedColor, |
phungductung | 0:4e245f4bc8ac | 36 | inactiveColor, inactiveTextColor); |
phungductung | 0:4e245f4bc8ac | 37 | } |
phungductung | 0:4e245f4bc8ac | 38 | // On created, set touched color as needed |
phungductung | 0:4e245f4bc8ac | 39 | if (touched >= 0) TouchedColor(touched); |
phungductung | 0:4e245f4bc8ac | 40 | } |
phungductung | 0:4e245f4bc8ac | 41 | |
phungductung | 0:4e245f4bc8ac | 42 | // Draw button |
phungductung | 0:4e245f4bc8ac | 43 | bool ButtonGroup::Draw(int num, uint32_t color, uint32_t textColor) |
phungductung | 0:4e245f4bc8ac | 44 | { |
phungductung | 0:4e245f4bc8ac | 45 | if (!Range(num)) return false; |
phungductung | 0:4e245f4bc8ac | 46 | buttons_[num]->Draw(color, textColor); |
phungductung | 0:4e245f4bc8ac | 47 | return true; |
phungductung | 0:4e245f4bc8ac | 48 | } |
phungductung | 0:4e245f4bc8ac | 49 | |
phungductung | 0:4e245f4bc8ac | 50 | // Change to touched color |
phungductung | 0:4e245f4bc8ac | 51 | bool ButtonGroup::TouchedColor(int num) |
phungductung | 0:4e245f4bc8ac | 52 | { |
phungductung | 0:4e245f4bc8ac | 53 | if (prevNum_ != num) prevNum_ = num; |
phungductung | 0:4e245f4bc8ac | 54 | return Draw(num, TOUCHED_COLOR_, TEXT_COLOR_); |
phungductung | 0:4e245f4bc8ac | 55 | } |
phungductung | 0:4e245f4bc8ac | 56 | |
phungductung | 0:4e245f4bc8ac | 57 | // Erase button |
phungductung | 0:4e245f4bc8ac | 58 | bool ButtonGroup::Erase(int num) |
phungductung | 0:4e245f4bc8ac | 59 | { |
phungductung | 0:4e245f4bc8ac | 60 | if (!Range(num)) return false; |
phungductung | 0:4e245f4bc8ac | 61 | buttons_[num]->Erase(); |
phungductung | 0:4e245f4bc8ac | 62 | return true; |
phungductung | 0:4e245f4bc8ac | 63 | } |
phungductung | 0:4e245f4bc8ac | 64 | |
phungductung | 0:4e245f4bc8ac | 65 | // Check touch detected for specified button |
phungductung | 0:4e245f4bc8ac | 66 | bool ButtonGroup::Touched(int num) |
phungductung | 0:4e245f4bc8ac | 67 | { |
phungductung | 0:4e245f4bc8ac | 68 | if (!Range(num)) return false; |
phungductung | 0:4e245f4bc8ac | 69 | if (!buttons_[num]->IsActive()) return false; |
phungductung | 0:4e245f4bc8ac | 70 | int touched; |
phungductung | 0:4e245f4bc8ac | 71 | if (!GetTouchedNumber(touched)) return false; |
phungductung | 0:4e245f4bc8ac | 72 | bool rtn = (touched == num) ? true : false; |
phungductung | 0:4e245f4bc8ac | 73 | return rtn; |
phungductung | 0:4e245f4bc8ac | 74 | } |
phungductung | 0:4e245f4bc8ac | 75 | |
phungductung | 0:4e245f4bc8ac | 76 | // Get touched number, return value: true or false |
phungductung | 0:4e245f4bc8ac | 77 | bool ButtonGroup::GetTouchedNumber(int &num) |
phungductung | 0:4e245f4bc8ac | 78 | { |
phungductung | 0:4e245f4bc8ac | 79 | bool rtn = false; |
phungductung | 0:4e245f4bc8ac | 80 | if (GuiBase::PanelTouched()) |
phungductung | 0:4e245f4bc8ac | 81 | { |
phungductung | 0:4e245f4bc8ac | 82 | for (int n=0; n<NUMBER_; n++) |
phungductung | 0:4e245f4bc8ac | 83 | if (buttons_[n]->IsOnButton() && |
phungductung | 0:4e245f4bc8ac | 84 | buttons_[n]->IsActive()) |
phungductung | 0:4e245f4bc8ac | 85 | { |
phungductung | 0:4e245f4bc8ac | 86 | num = n; |
phungductung | 0:4e245f4bc8ac | 87 | rtn = true; |
phungductung | 0:4e245f4bc8ac | 88 | } |
phungductung | 0:4e245f4bc8ac | 89 | |
phungductung | 0:4e245f4bc8ac | 90 | if (!rtn) return false; |
phungductung | 0:4e245f4bc8ac | 91 | } |
phungductung | 0:4e245f4bc8ac | 92 | else |
phungductung | 0:4e245f4bc8ac | 93 | return false; |
phungductung | 0:4e245f4bc8ac | 94 | |
phungductung | 0:4e245f4bc8ac | 95 | buttons_[num]->Draw(TOUCHED_COLOR_); |
phungductung | 0:4e245f4bc8ac | 96 | if ((prevNum_ >= 0) && (prevNum_ != num)) |
phungductung | 0:4e245f4bc8ac | 97 | buttons_[prevNum_]->Draw(); |
phungductung | 0:4e245f4bc8ac | 98 | if (prevNum_ != num) prevNum_ = num; |
phungductung | 0:4e245f4bc8ac | 99 | return true; |
phungductung | 0:4e245f4bc8ac | 100 | } |
phungductung | 0:4e245f4bc8ac | 101 | |
phungductung | 0:4e245f4bc8ac | 102 | // Get touched number, return value: touched number |
phungductung | 0:4e245f4bc8ac | 103 | int ButtonGroup::GetTouchedNumber() |
phungductung | 0:4e245f4bc8ac | 104 | { |
phungductung | 0:4e245f4bc8ac | 105 | int num; |
phungductung | 0:4e245f4bc8ac | 106 | if (GetTouchedNumber(num)) |
phungductung | 0:4e245f4bc8ac | 107 | return num; |
phungductung | 0:4e245f4bc8ac | 108 | else |
phungductung | 0:4e245f4bc8ac | 109 | return -1; |
phungductung | 0:4e245f4bc8ac | 110 | } |
phungductung | 0:4e245f4bc8ac | 111 | |
phungductung | 0:4e245f4bc8ac | 112 | // Wait until touched and get touched number |
phungductung | 0:4e245f4bc8ac | 113 | int ButtonGroup::WaitTouchedAndGet() |
phungductung | 0:4e245f4bc8ac | 114 | { |
phungductung | 0:4e245f4bc8ac | 115 | int num; |
phungductung | 0:4e245f4bc8ac | 116 | while (!GetTouchedNumber(num)) {} |
phungductung | 0:4e245f4bc8ac | 117 | return num; |
phungductung | 0:4e245f4bc8ac | 118 | } |
phungductung | 0:4e245f4bc8ac | 119 | |
phungductung | 0:4e245f4bc8ac | 120 | // Activate and inactivate button(s) |
phungductung | 0:4e245f4bc8ac | 121 | bool ButtonGroup::Activate(int num) |
phungductung | 0:4e245f4bc8ac | 122 | { |
phungductung | 0:4e245f4bc8ac | 123 | if (!Range(num)) return false; |
phungductung | 0:4e245f4bc8ac | 124 | buttons_[num]->Activate(); |
phungductung | 0:4e245f4bc8ac | 125 | return true; |
phungductung | 0:4e245f4bc8ac | 126 | } |
phungductung | 0:4e245f4bc8ac | 127 | bool ButtonGroup::Inactivate(int num) |
phungductung | 0:4e245f4bc8ac | 128 | { |
phungductung | 0:4e245f4bc8ac | 129 | if (!Range(num)) return false; |
phungductung | 0:4e245f4bc8ac | 130 | buttons_[num]->Inactivate(); |
phungductung | 0:4e245f4bc8ac | 131 | return true; |
phungductung | 0:4e245f4bc8ac | 132 | } |
phungductung | 0:4e245f4bc8ac | 133 | } |
phungductung | 0:4e245f4bc8ac | 134 |