John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ProgressBar.cpp Source File

ProgressBar.cpp

00001 #include "ProgressBar.h"
00002 
00003 #include <math.h>
00004 
00005 /*
00006     Default constructor
00007 */
00008 ProgressBar::ProgressBar()
00009 {
00010     barX = 0;
00011     barY = 0;
00012     
00013     barW = 1;
00014     barH = 1;
00015     
00016     orientation = horizontal;
00017     
00018     barPosition = 0;
00019     calibratedRange = 1.0;
00020     
00021     barColor = 0x8888;
00022     backColor = 0xFFFF;
00023     borderColor = 0;
00024 
00025     calibrationFactor = calibratedRange / (double)barW;
00026     
00027     previousBarPositionDisplayed = -1;
00028     
00029     fillBackground = false; 
00030 }
00031 
00032 /*
00033     Constructor with coordinates, etc, specified explicitly
00034 */
00035 ProgressBar::ProgressBar(int x, int y, int w, int h, progressBarOrientation o, double newCalibratedRange, GuiConst_INTCOLOR brColor, GuiConst_INTCOLOR bkColor, GuiConst_INTCOLOR brdColor)
00036 {
00037     barX = x;
00038     barY = y;
00039     
00040     barW = w;
00041     barH = h;
00042     
00043     orientation = o;
00044     
00045     barPosition = 0;
00046     calibratedRange = newCalibratedRange;
00047     
00048     barColor = brColor;
00049     backColor = bkColor;
00050     borderColor = brdColor;
00051     
00052     calibrationFactor = calibratedRange / (double)barW;
00053     
00054     previousBarPositionDisplayed = -1;
00055     
00056     fillBackground = false;
00057 }
00058 
00059 /*
00060     Change the calibrated end position of the bar to the new value,
00061     having applied the relevant checks to make sure it is valid.
00062     
00063     Then display the bar in its new position.
00064     
00065     If specified by the caller, (re)display the whole progress bar, regardless of whether or not 
00066     its end position has changed.
00067     
00068     Args: the new calibrated position
00069           boolean true to force the entire bar to be redisplayed, false if this is not necessary
00070 */
00071 void ProgressBar::UpdateCalibratedPosition(double newCalibratedPosition, bool forceFullDisplay)
00072 {
00073     if(newCalibratedPosition < 0.0) newCalibratedPosition = 0.0;
00074     
00075     if(newCalibratedPosition > calibratedRange) newCalibratedPosition = calibratedRange;
00076     
00077     barPosition = (int) floor((newCalibratedPosition / calibrationFactor) + 0.5);
00078     
00079     if(forceFullDisplay) {
00080         previousBarPositionDisplayed = -1;
00081     }
00082     
00083     DisplayNewPosition();
00084 }
00085 
00086 /*
00087     Display the bar with its position at the right hand end, 
00088     showing that the process is complete
00089     
00090     If specified by the caller, (re)display the whole progress bar, 
00091     regardless of how far it has moved since it was previously displayed.
00092     
00093     Args: a boolean - true to redisplay the whole bar, 
00094                       false to display only the area that has changed
00095 */
00096 void ProgressBar::DisplayBarComplete(bool forceFullDisplay)
00097 {
00098     barPosition = barW + 1; // '+ 1' to make sure the bar fills the bounding box
00099         
00100     if(forceFullDisplay) {
00101         previousBarPositionDisplayed = -1;
00102     }
00103     
00104     DisplayNewPosition();
00105 }
00106 
00107 
00108 /*
00109     Change the calibrated range of the bar to the new value,
00110     having applied the relevant checks to make sure it is valid.
00111     
00112     Then re-display the bar.
00113     
00114     Args: the new calibrated range
00115 */
00116 void ProgressBar::SetCalibratedRange(double newCalibratedRange)
00117 {
00118     if(newCalibratedRange < 0.0) {
00119         newCalibratedRange = 0.0;
00120     }
00121 
00122     calibratedRange = newCalibratedRange;
00123 
00124     calibrationFactor = calibratedRange / (double)barW;
00125     
00126     // Force the full bar to be re-displayed
00127     previousBarPositionDisplayed = -1;
00128     
00129     DisplayNewPosition();
00130 }
00131 
00132 /*
00133     After updating the position of the (end of) the bar, display it.
00134     
00135     If possible, display only the part that has moved, otherwise display the whole bar.
00136 */
00137 void ProgressBar::DisplayNewPosition(void)
00138 {
00139     if(previousBarPositionDisplayed == -1) {
00140         DisplayFullBar();
00141     } else {
00142         DisplayBarChangeOnly();
00143     }
00144     
00145     previousBarPositionDisplayed = barPosition;
00146 }
00147 
00148 /*
00149     Display the complete bar, including the border rectangle
00150 */
00151 void ProgressBar::DisplayFullBar(void)
00152 {
00153     // First display the border rectangle
00154     const int borderMargin = 1;
00155     GuiLib_Box(barX - borderMargin, barY - borderMargin, (barX + barW + borderMargin), (barY + barH + borderMargin), borderColor);
00156             
00157     if( barPosition > 0) {
00158         int barX1, barY1, barX2, barY2;
00159         int backX1, backY1, backX2, backY2;
00160         
00161         if( orientation == vertical) {
00162             barX1 = barX;
00163             barX2 = barX + barW;
00164             
00165             backX1 = barX1;
00166             backX2 = barX2;
00167             
00168             
00169             // We fill from the bottom, not the top
00170             barY1 = barY + barH - barPosition;
00171             barY2 = barY + barH;
00172             if(barY1 > barY2) { barY1 = barY2; }
00173             
00174             backY1 = barY;
00175             backY2 = barY1;
00176         } else {
00177             barX1 = barX;
00178             barX2 = barX + barPosition;
00179             if( barX2 < barX1) { barX2 = barX1; }
00180             
00181             backX1 = barX2;
00182             backX2 = barX + barW;
00183             
00184             barY1 = barY;
00185             barY2 = barY + barH;
00186             
00187             backY1 = barY1;
00188             backY2 = barY2;
00189         }
00190         
00191         GuiLib_FillBox(barX1, barY1, barX2, barY2, barColor);
00192         if(fillBackground) {
00193             GuiLib_FillBox(backX1, backY1, backX2, backY2, backColor);
00194         }
00195     } else {
00196         // Bar position is zero - display background only
00197         if(fillBackground) {
00198             GuiLib_FillBox(barX, barY, (barX + barW), (barY + barH), backColor);
00199         }
00200     }
00201 }
00202 
00203 /*
00204     Display only the part of the bar that has changed
00205     (minimises flickering on the display).
00206 */
00207 void ProgressBar::DisplayBarChangeOnly(void)
00208 {
00209     // We are displaying only the changed portion of the bar and background rectangle.
00210     // Assume we do not therefore need to (re)display the border box.
00211     // Note also that 'barPosition' is in display units (pixels) 
00212     // relative to the left hand end (horizontal bar) or bottom (vertical)
00213     
00214     if(barPosition != previousBarPositionDisplayed) {
00215             
00216         if(barPosition > previousBarPositionDisplayed) {
00217             // Need to extend bar
00218             int barX1, barY1, barX2, barY2;
00219             
00220             if( orientation == vertical) {
00221                 barX1 = barX;
00222                 barX2 = barX + barW;
00223 
00224                 
00225                 // We fill from the bottom, not the top
00226                 barY1 = barY + barH - barPosition;
00227                 barY2 = barY + barH - previousBarPositionDisplayed;
00228                 
00229                 if(barY1 < barY) barY1 = barY;
00230                 if(barY2 > (barY + barH)) barY2 = barY + barH;
00231                 
00232                 if(barY1 > barY2) { barY1 = barY2; }
00233                 
00234             } else {
00235                 barX1 = barX + previousBarPositionDisplayed;
00236                 barX2 = barX + barPosition;
00237                 
00238                 if(barX1 < barX) barX1 = barX;
00239                 if(barX2 > (barX + barW)) barX2 = barX + barW;
00240                 
00241                 if( barX2 < barX1) { barX2 = barX1; }
00242 
00243                 
00244                 barY1 = barY;
00245                 barY2 = barY + barH;
00246             }
00247             
00248             GuiLib_FillBox(barX1, barY1, barX2, barY2, barColor);
00249         } else {
00250             if(fillBackground) {
00251                 // Bar has reduced in length - need to extend background bitmap/fill
00252                 int backX1, backY1, backX2, backY2;
00253                 
00254                 if( orientation == vertical) {
00255                     backX1 = barX;
00256                     backX2 = barX + barW;
00257     
00258                     
00259                     // We fill from the bottom, not the top
00260                     backY1 = barY + barH - previousBarPositionDisplayed;
00261                     backY2 = barY + barH - barPosition;
00262                     
00263                     if(backY1 < barY) backY1 = barY;
00264                     if(backY2 > (barY + barH)) backY2 = barY + barH;
00265                     
00266                     if(backY1 > backY2) { backY1 = backY2; }
00267                     
00268                 } else {
00269                     backX1 = barX + barPosition;
00270                     backX2 = barX + previousBarPositionDisplayed;
00271                     
00272                     if(backX1 < barX) backX1 = barX;
00273                     if(backX2 > (barX + barW)) backX2 = barX + barW;
00274                     
00275                     if( backX2 < backX1) { backX2 = backX1; }
00276                     
00277     
00278                     backY1 = barY;
00279                     backY2 = barY + barH;
00280                 }
00281                 
00282                 GuiLib_FillBox(backX1, backY1, backX2, backY2, backColor);
00283             }
00284         }
00285     }
00286     // else no change - nothing to do
00287 }