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