Repository for import to local machine
Dependencies: DMBasicGUI DMSupport
ProgressBar.cpp@8:26e49e6955bd, 2017-07-31 (annotated)
- Committer:
- jmitc91516
- Date:
- Mon Jul 31 15:37:57 2017 +0000
- Revision:
- 8:26e49e6955bd
- Parent:
- 1:a5258871b33d
Method ramp scrolling improved, and more bitmaps moved to QSPI memory
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmitc91516 | 1:a5258871b33d | 1 | #include "ProgressBar.h" |
jmitc91516 | 1:a5258871b33d | 2 | |
jmitc91516 | 1:a5258871b33d | 3 | #include <math.h> |
jmitc91516 | 1:a5258871b33d | 4 | |
jmitc91516 | 1:a5258871b33d | 5 | /* |
jmitc91516 | 1:a5258871b33d | 6 | Default constructor |
jmitc91516 | 1:a5258871b33d | 7 | */ |
jmitc91516 | 1:a5258871b33d | 8 | ProgressBar::ProgressBar() |
jmitc91516 | 1:a5258871b33d | 9 | { |
jmitc91516 | 1:a5258871b33d | 10 | barX = 0; |
jmitc91516 | 1:a5258871b33d | 11 | barY = 0; |
jmitc91516 | 1:a5258871b33d | 12 | |
jmitc91516 | 1:a5258871b33d | 13 | barW = 1; |
jmitc91516 | 1:a5258871b33d | 14 | barH = 1; |
jmitc91516 | 1:a5258871b33d | 15 | |
jmitc91516 | 1:a5258871b33d | 16 | orientation = horizontal; |
jmitc91516 | 1:a5258871b33d | 17 | |
jmitc91516 | 1:a5258871b33d | 18 | barPosition = 0; |
jmitc91516 | 1:a5258871b33d | 19 | calibratedRange = 1.0; |
jmitc91516 | 1:a5258871b33d | 20 | |
jmitc91516 | 1:a5258871b33d | 21 | barColor = 0x8888; |
jmitc91516 | 1:a5258871b33d | 22 | backColor = 0xFFFF; |
jmitc91516 | 1:a5258871b33d | 23 | borderColor = 0; |
jmitc91516 | 1:a5258871b33d | 24 | |
jmitc91516 | 1:a5258871b33d | 25 | calibrationFactor = calibratedRange / (double)barW; |
jmitc91516 | 1:a5258871b33d | 26 | |
jmitc91516 | 1:a5258871b33d | 27 | previousBarPositionDisplayed = -1; |
jmitc91516 | 1:a5258871b33d | 28 | |
jmitc91516 | 1:a5258871b33d | 29 | fillBackground = false; |
jmitc91516 | 1:a5258871b33d | 30 | } |
jmitc91516 | 1:a5258871b33d | 31 | |
jmitc91516 | 1:a5258871b33d | 32 | /* |
jmitc91516 | 1:a5258871b33d | 33 | Constructor with coordinates, etc, specified explicitly |
jmitc91516 | 1:a5258871b33d | 34 | */ |
jmitc91516 | 1:a5258871b33d | 35 | ProgressBar::ProgressBar(int x, int y, int w, int h, progressBarOrientation o, double newCalibratedRange, GuiConst_INTCOLOR brColor, GuiConst_INTCOLOR bkColor, GuiConst_INTCOLOR brdColor) |
jmitc91516 | 1:a5258871b33d | 36 | { |
jmitc91516 | 1:a5258871b33d | 37 | barX = x; |
jmitc91516 | 1:a5258871b33d | 38 | barY = y; |
jmitc91516 | 1:a5258871b33d | 39 | |
jmitc91516 | 1:a5258871b33d | 40 | barW = w; |
jmitc91516 | 1:a5258871b33d | 41 | barH = h; |
jmitc91516 | 1:a5258871b33d | 42 | |
jmitc91516 | 1:a5258871b33d | 43 | orientation = o; |
jmitc91516 | 1:a5258871b33d | 44 | |
jmitc91516 | 1:a5258871b33d | 45 | barPosition = 0; |
jmitc91516 | 1:a5258871b33d | 46 | calibratedRange = newCalibratedRange; |
jmitc91516 | 1:a5258871b33d | 47 | |
jmitc91516 | 1:a5258871b33d | 48 | barColor = brColor; |
jmitc91516 | 1:a5258871b33d | 49 | backColor = bkColor; |
jmitc91516 | 1:a5258871b33d | 50 | borderColor = brdColor; |
jmitc91516 | 1:a5258871b33d | 51 | |
jmitc91516 | 1:a5258871b33d | 52 | calibrationFactor = calibratedRange / (double)barW; |
jmitc91516 | 1:a5258871b33d | 53 | |
jmitc91516 | 1:a5258871b33d | 54 | previousBarPositionDisplayed = -1; |
jmitc91516 | 1:a5258871b33d | 55 | |
jmitc91516 | 1:a5258871b33d | 56 | fillBackground = false; |
jmitc91516 | 1:a5258871b33d | 57 | } |
jmitc91516 | 1:a5258871b33d | 58 | |
jmitc91516 | 1:a5258871b33d | 59 | /* |
jmitc91516 | 1:a5258871b33d | 60 | Change the calibrated end position of the bar to the new value, |
jmitc91516 | 1:a5258871b33d | 61 | having applied the relevant checks to make sure it is valid. |
jmitc91516 | 1:a5258871b33d | 62 | |
jmitc91516 | 1:a5258871b33d | 63 | Then display the bar in its new position. |
jmitc91516 | 1:a5258871b33d | 64 | |
jmitc91516 | 1:a5258871b33d | 65 | If specified by the caller, (re)display the whole progress bar, regardless of whether or not |
jmitc91516 | 1:a5258871b33d | 66 | its end position has changed. |
jmitc91516 | 1:a5258871b33d | 67 | |
jmitc91516 | 1:a5258871b33d | 68 | Args: the new calibrated position |
jmitc91516 | 1:a5258871b33d | 69 | boolean true to force the entire bar to be redisplayed, false if this is not necessary |
jmitc91516 | 1:a5258871b33d | 70 | */ |
jmitc91516 | 1:a5258871b33d | 71 | void ProgressBar::UpdateCalibratedPosition(double newCalibratedPosition, bool forceFullDisplay) |
jmitc91516 | 1:a5258871b33d | 72 | { |
jmitc91516 | 1:a5258871b33d | 73 | if(newCalibratedPosition < 0.0) newCalibratedPosition = 0.0; |
jmitc91516 | 1:a5258871b33d | 74 | |
jmitc91516 | 1:a5258871b33d | 75 | if(newCalibratedPosition > calibratedRange) newCalibratedPosition = calibratedRange; |
jmitc91516 | 1:a5258871b33d | 76 | |
jmitc91516 | 1:a5258871b33d | 77 | barPosition = (int) floor((newCalibratedPosition / calibrationFactor) + 0.5); |
jmitc91516 | 1:a5258871b33d | 78 | |
jmitc91516 | 1:a5258871b33d | 79 | if(forceFullDisplay) { |
jmitc91516 | 1:a5258871b33d | 80 | previousBarPositionDisplayed = -1; |
jmitc91516 | 1:a5258871b33d | 81 | } |
jmitc91516 | 1:a5258871b33d | 82 | |
jmitc91516 | 1:a5258871b33d | 83 | DisplayNewPosition(); |
jmitc91516 | 1:a5258871b33d | 84 | } |
jmitc91516 | 1:a5258871b33d | 85 | |
jmitc91516 | 1:a5258871b33d | 86 | /* |
jmitc91516 | 1:a5258871b33d | 87 | Display the bar with its position at the right hand end, |
jmitc91516 | 1:a5258871b33d | 88 | showing that the process is complete |
jmitc91516 | 1:a5258871b33d | 89 | |
jmitc91516 | 1:a5258871b33d | 90 | If specified by the caller, (re)display the whole progress bar, |
jmitc91516 | 1:a5258871b33d | 91 | regardless of how far it has moved since it was previously displayed. |
jmitc91516 | 1:a5258871b33d | 92 | |
jmitc91516 | 1:a5258871b33d | 93 | Args: a boolean - true to redisplay the whole bar, |
jmitc91516 | 1:a5258871b33d | 94 | false to display only the area that has changed |
jmitc91516 | 1:a5258871b33d | 95 | */ |
jmitc91516 | 1:a5258871b33d | 96 | void ProgressBar::DisplayBarComplete(bool forceFullDisplay) |
jmitc91516 | 1:a5258871b33d | 97 | { |
jmitc91516 | 1:a5258871b33d | 98 | barPosition = barW + 1; // '+ 1' to make sure the bar fills the bounding box |
jmitc91516 | 1:a5258871b33d | 99 | |
jmitc91516 | 1:a5258871b33d | 100 | if(forceFullDisplay) { |
jmitc91516 | 1:a5258871b33d | 101 | previousBarPositionDisplayed = -1; |
jmitc91516 | 1:a5258871b33d | 102 | } |
jmitc91516 | 1:a5258871b33d | 103 | |
jmitc91516 | 1:a5258871b33d | 104 | DisplayNewPosition(); |
jmitc91516 | 1:a5258871b33d | 105 | } |
jmitc91516 | 1:a5258871b33d | 106 | |
jmitc91516 | 1:a5258871b33d | 107 | |
jmitc91516 | 1:a5258871b33d | 108 | /* |
jmitc91516 | 1:a5258871b33d | 109 | Change the calibrated range of the bar to the new value, |
jmitc91516 | 1:a5258871b33d | 110 | having applied the relevant checks to make sure it is valid. |
jmitc91516 | 1:a5258871b33d | 111 | |
jmitc91516 | 1:a5258871b33d | 112 | Then re-display the bar. |
jmitc91516 | 1:a5258871b33d | 113 | |
jmitc91516 | 1:a5258871b33d | 114 | Args: the new calibrated range |
jmitc91516 | 1:a5258871b33d | 115 | */ |
jmitc91516 | 1:a5258871b33d | 116 | void ProgressBar::SetCalibratedRange(double newCalibratedRange) |
jmitc91516 | 1:a5258871b33d | 117 | { |
jmitc91516 | 1:a5258871b33d | 118 | if(newCalibratedRange < 0.0) { |
jmitc91516 | 1:a5258871b33d | 119 | newCalibratedRange = 0.0; |
jmitc91516 | 1:a5258871b33d | 120 | } |
jmitc91516 | 1:a5258871b33d | 121 | |
jmitc91516 | 1:a5258871b33d | 122 | calibratedRange = newCalibratedRange; |
jmitc91516 | 1:a5258871b33d | 123 | |
jmitc91516 | 1:a5258871b33d | 124 | calibrationFactor = calibratedRange / (double)barW; |
jmitc91516 | 1:a5258871b33d | 125 | |
jmitc91516 | 1:a5258871b33d | 126 | // Force the full bar to be re-displayed |
jmitc91516 | 1:a5258871b33d | 127 | previousBarPositionDisplayed = -1; |
jmitc91516 | 1:a5258871b33d | 128 | |
jmitc91516 | 1:a5258871b33d | 129 | DisplayNewPosition(); |
jmitc91516 | 1:a5258871b33d | 130 | } |
jmitc91516 | 1:a5258871b33d | 131 | |
jmitc91516 | 1:a5258871b33d | 132 | /* |
jmitc91516 | 1:a5258871b33d | 133 | After updating the position of the (end of) the bar, display it. |
jmitc91516 | 1:a5258871b33d | 134 | |
jmitc91516 | 1:a5258871b33d | 135 | If possible, display only the part that has moved, otherwise display the whole bar. |
jmitc91516 | 1:a5258871b33d | 136 | */ |
jmitc91516 | 1:a5258871b33d | 137 | void ProgressBar::DisplayNewPosition(void) |
jmitc91516 | 1:a5258871b33d | 138 | { |
jmitc91516 | 1:a5258871b33d | 139 | if(previousBarPositionDisplayed == -1) { |
jmitc91516 | 1:a5258871b33d | 140 | DisplayFullBar(); |
jmitc91516 | 1:a5258871b33d | 141 | } else { |
jmitc91516 | 1:a5258871b33d | 142 | DisplayBarChangeOnly(); |
jmitc91516 | 1:a5258871b33d | 143 | } |
jmitc91516 | 1:a5258871b33d | 144 | |
jmitc91516 | 1:a5258871b33d | 145 | previousBarPositionDisplayed = barPosition; |
jmitc91516 | 1:a5258871b33d | 146 | } |
jmitc91516 | 1:a5258871b33d | 147 | |
jmitc91516 | 1:a5258871b33d | 148 | /* |
jmitc91516 | 1:a5258871b33d | 149 | Display the complete bar, including the border rectangle |
jmitc91516 | 1:a5258871b33d | 150 | */ |
jmitc91516 | 1:a5258871b33d | 151 | void ProgressBar::DisplayFullBar(void) |
jmitc91516 | 1:a5258871b33d | 152 | { |
jmitc91516 | 1:a5258871b33d | 153 | // First display the border rectangle |
jmitc91516 | 1:a5258871b33d | 154 | const int borderMargin = 1; |
jmitc91516 | 1:a5258871b33d | 155 | GuiLib_Box(barX - borderMargin, barY - borderMargin, (barX + barW + borderMargin), (barY + barH + borderMargin), borderColor); |
jmitc91516 | 1:a5258871b33d | 156 | |
jmitc91516 | 1:a5258871b33d | 157 | if( barPosition > 0) { |
jmitc91516 | 1:a5258871b33d | 158 | int barX1, barY1, barX2, barY2; |
jmitc91516 | 1:a5258871b33d | 159 | int backX1, backY1, backX2, backY2; |
jmitc91516 | 1:a5258871b33d | 160 | |
jmitc91516 | 1:a5258871b33d | 161 | if( orientation == vertical) { |
jmitc91516 | 1:a5258871b33d | 162 | barX1 = barX; |
jmitc91516 | 1:a5258871b33d | 163 | barX2 = barX + barW; |
jmitc91516 | 1:a5258871b33d | 164 | |
jmitc91516 | 1:a5258871b33d | 165 | backX1 = barX1; |
jmitc91516 | 1:a5258871b33d | 166 | backX2 = barX2; |
jmitc91516 | 1:a5258871b33d | 167 | |
jmitc91516 | 1:a5258871b33d | 168 | |
jmitc91516 | 1:a5258871b33d | 169 | // We fill from the bottom, not the top |
jmitc91516 | 1:a5258871b33d | 170 | barY1 = barY + barH - barPosition; |
jmitc91516 | 1:a5258871b33d | 171 | barY2 = barY + barH; |
jmitc91516 | 1:a5258871b33d | 172 | if(barY1 > barY2) { barY1 = barY2; } |
jmitc91516 | 1:a5258871b33d | 173 | |
jmitc91516 | 1:a5258871b33d | 174 | backY1 = barY; |
jmitc91516 | 1:a5258871b33d | 175 | backY2 = barY1; |
jmitc91516 | 1:a5258871b33d | 176 | } else { |
jmitc91516 | 1:a5258871b33d | 177 | barX1 = barX; |
jmitc91516 | 1:a5258871b33d | 178 | barX2 = barX + barPosition; |
jmitc91516 | 1:a5258871b33d | 179 | if( barX2 < barX1) { barX2 = barX1; } |
jmitc91516 | 1:a5258871b33d | 180 | |
jmitc91516 | 1:a5258871b33d | 181 | backX1 = barX2; |
jmitc91516 | 1:a5258871b33d | 182 | backX2 = barX + barW; |
jmitc91516 | 1:a5258871b33d | 183 | |
jmitc91516 | 1:a5258871b33d | 184 | barY1 = barY; |
jmitc91516 | 1:a5258871b33d | 185 | barY2 = barY + barH; |
jmitc91516 | 1:a5258871b33d | 186 | |
jmitc91516 | 1:a5258871b33d | 187 | backY1 = barY1; |
jmitc91516 | 1:a5258871b33d | 188 | backY2 = barY2; |
jmitc91516 | 1:a5258871b33d | 189 | } |
jmitc91516 | 1:a5258871b33d | 190 | |
jmitc91516 | 1:a5258871b33d | 191 | GuiLib_FillBox(barX1, barY1, barX2, barY2, barColor); |
jmitc91516 | 1:a5258871b33d | 192 | if(fillBackground) { |
jmitc91516 | 1:a5258871b33d | 193 | GuiLib_FillBox(backX1, backY1, backX2, backY2, backColor); |
jmitc91516 | 1:a5258871b33d | 194 | } |
jmitc91516 | 1:a5258871b33d | 195 | } else { |
jmitc91516 | 1:a5258871b33d | 196 | // Bar position is zero - display background only |
jmitc91516 | 1:a5258871b33d | 197 | if(fillBackground) { |
jmitc91516 | 1:a5258871b33d | 198 | GuiLib_FillBox(barX, barY, (barX + barW), (barY + barH), backColor); |
jmitc91516 | 1:a5258871b33d | 199 | } |
jmitc91516 | 1:a5258871b33d | 200 | } |
jmitc91516 | 1:a5258871b33d | 201 | } |
jmitc91516 | 1:a5258871b33d | 202 | |
jmitc91516 | 1:a5258871b33d | 203 | /* |
jmitc91516 | 1:a5258871b33d | 204 | Display only the part of the bar that has changed |
jmitc91516 | 1:a5258871b33d | 205 | (minimises flickering on the display). |
jmitc91516 | 1:a5258871b33d | 206 | */ |
jmitc91516 | 1:a5258871b33d | 207 | void ProgressBar::DisplayBarChangeOnly(void) |
jmitc91516 | 1:a5258871b33d | 208 | { |
jmitc91516 | 1:a5258871b33d | 209 | // We are displaying only the changed portion of the bar and background rectangle. |
jmitc91516 | 1:a5258871b33d | 210 | // Assume we do not therefore need to (re)display the border box. |
jmitc91516 | 1:a5258871b33d | 211 | // Note also that 'barPosition' is in display units (pixels) |
jmitc91516 | 1:a5258871b33d | 212 | // relative to the left hand end (horizontal bar) or bottom (vertical) |
jmitc91516 | 1:a5258871b33d | 213 | |
jmitc91516 | 1:a5258871b33d | 214 | if(barPosition != previousBarPositionDisplayed) { |
jmitc91516 | 1:a5258871b33d | 215 | |
jmitc91516 | 1:a5258871b33d | 216 | if(barPosition > previousBarPositionDisplayed) { |
jmitc91516 | 1:a5258871b33d | 217 | // Need to extend bar |
jmitc91516 | 1:a5258871b33d | 218 | int barX1, barY1, barX2, barY2; |
jmitc91516 | 1:a5258871b33d | 219 | |
jmitc91516 | 1:a5258871b33d | 220 | if( orientation == vertical) { |
jmitc91516 | 1:a5258871b33d | 221 | barX1 = barX; |
jmitc91516 | 1:a5258871b33d | 222 | barX2 = barX + barW; |
jmitc91516 | 1:a5258871b33d | 223 | |
jmitc91516 | 1:a5258871b33d | 224 | |
jmitc91516 | 1:a5258871b33d | 225 | // We fill from the bottom, not the top |
jmitc91516 | 1:a5258871b33d | 226 | barY1 = barY + barH - barPosition; |
jmitc91516 | 1:a5258871b33d | 227 | barY2 = barY + barH - previousBarPositionDisplayed; |
jmitc91516 | 1:a5258871b33d | 228 | |
jmitc91516 | 1:a5258871b33d | 229 | if(barY1 < barY) barY1 = barY; |
jmitc91516 | 1:a5258871b33d | 230 | if(barY2 > (barY + barH)) barY2 = barY + barH; |
jmitc91516 | 1:a5258871b33d | 231 | |
jmitc91516 | 1:a5258871b33d | 232 | if(barY1 > barY2) { barY1 = barY2; } |
jmitc91516 | 1:a5258871b33d | 233 | |
jmitc91516 | 1:a5258871b33d | 234 | } else { |
jmitc91516 | 1:a5258871b33d | 235 | barX1 = barX + previousBarPositionDisplayed; |
jmitc91516 | 1:a5258871b33d | 236 | barX2 = barX + barPosition; |
jmitc91516 | 1:a5258871b33d | 237 | |
jmitc91516 | 1:a5258871b33d | 238 | if(barX1 < barX) barX1 = barX; |
jmitc91516 | 1:a5258871b33d | 239 | if(barX2 > (barX + barW)) barX2 = barX + barW; |
jmitc91516 | 1:a5258871b33d | 240 | |
jmitc91516 | 1:a5258871b33d | 241 | if( barX2 < barX1) { barX2 = barX1; } |
jmitc91516 | 1:a5258871b33d | 242 | |
jmitc91516 | 1:a5258871b33d | 243 | |
jmitc91516 | 1:a5258871b33d | 244 | barY1 = barY; |
jmitc91516 | 1:a5258871b33d | 245 | barY2 = barY + barH; |
jmitc91516 | 1:a5258871b33d | 246 | } |
jmitc91516 | 1:a5258871b33d | 247 | |
jmitc91516 | 1:a5258871b33d | 248 | GuiLib_FillBox(barX1, barY1, barX2, barY2, barColor); |
jmitc91516 | 1:a5258871b33d | 249 | } else { |
jmitc91516 | 1:a5258871b33d | 250 | if(fillBackground) { |
jmitc91516 | 1:a5258871b33d | 251 | // Bar has reduced in length - need to extend background bitmap/fill |
jmitc91516 | 1:a5258871b33d | 252 | int backX1, backY1, backX2, backY2; |
jmitc91516 | 1:a5258871b33d | 253 | |
jmitc91516 | 1:a5258871b33d | 254 | if( orientation == vertical) { |
jmitc91516 | 1:a5258871b33d | 255 | backX1 = barX; |
jmitc91516 | 1:a5258871b33d | 256 | backX2 = barX + barW; |
jmitc91516 | 1:a5258871b33d | 257 | |
jmitc91516 | 1:a5258871b33d | 258 | |
jmitc91516 | 1:a5258871b33d | 259 | // We fill from the bottom, not the top |
jmitc91516 | 1:a5258871b33d | 260 | backY1 = barY + barH - previousBarPositionDisplayed; |
jmitc91516 | 1:a5258871b33d | 261 | backY2 = barY + barH - barPosition; |
jmitc91516 | 1:a5258871b33d | 262 | |
jmitc91516 | 1:a5258871b33d | 263 | if(backY1 < barY) backY1 = barY; |
jmitc91516 | 1:a5258871b33d | 264 | if(backY2 > (barY + barH)) backY2 = barY + barH; |
jmitc91516 | 1:a5258871b33d | 265 | |
jmitc91516 | 1:a5258871b33d | 266 | if(backY1 > backY2) { backY1 = backY2; } |
jmitc91516 | 1:a5258871b33d | 267 | |
jmitc91516 | 1:a5258871b33d | 268 | } else { |
jmitc91516 | 1:a5258871b33d | 269 | backX1 = barX + barPosition; |
jmitc91516 | 1:a5258871b33d | 270 | backX2 = barX + previousBarPositionDisplayed; |
jmitc91516 | 1:a5258871b33d | 271 | |
jmitc91516 | 1:a5258871b33d | 272 | if(backX1 < barX) backX1 = barX; |
jmitc91516 | 1:a5258871b33d | 273 | if(backX2 > (barX + barW)) backX2 = barX + barW; |
jmitc91516 | 1:a5258871b33d | 274 | |
jmitc91516 | 1:a5258871b33d | 275 | if( backX2 < backX1) { backX2 = backX1; } |
jmitc91516 | 1:a5258871b33d | 276 | |
jmitc91516 | 1:a5258871b33d | 277 | |
jmitc91516 | 1:a5258871b33d | 278 | backY1 = barY; |
jmitc91516 | 1:a5258871b33d | 279 | backY2 = barY + barH; |
jmitc91516 | 1:a5258871b33d | 280 | } |
jmitc91516 | 1:a5258871b33d | 281 | |
jmitc91516 | 1:a5258871b33d | 282 | GuiLib_FillBox(backX1, backY1, backX2, backY2, backColor); |
jmitc91516 | 1:a5258871b33d | 283 | } |
jmitc91516 | 1:a5258871b33d | 284 | } |
jmitc91516 | 1:a5258871b33d | 285 | } |
jmitc91516 | 1:a5258871b33d | 286 | // else no change - nothing to do |
jmitc91516 | 1:a5258871b33d | 287 | } |