llstatbar.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llstatbar.h"
00035 
00036 #include "llmath.h"
00037 #include "llui.h"
00038 #include "llgl.h"
00039 #include "llfontgl.h"
00040 
00041 #include "llstat.h"
00042 
00044 
00045 LLStatBar::LLStatBar(const std::string& name, const LLRect& rect)
00046                 :       LLView(name, rect, TRUE)
00047 {
00048         mMinBar = 0.f;
00049         mMaxBar = 50.f;
00050         mStatp = NULL;
00051         mTickSpacing = 10.f;
00052         mLabelSpacing = 10.f;
00053         mPrecision = 0;
00054         mUpdatesPerSec = 5;
00055         mLabel = name;
00056         mPerSec = TRUE;
00057         mValue = 0.f;
00058         mDisplayBar = TRUE;
00059         mDisplayHistory = FALSE;
00060         mDisplayMean = TRUE;
00061 }
00062 
00063 BOOL LLStatBar::handleMouseDown(S32 x, S32 y, MASK mask)
00064 {
00065         if (mDisplayBar)
00066         {
00067                 if (mDisplayHistory)
00068                 {
00069                         mDisplayBar = FALSE;
00070                         mDisplayHistory = FALSE;
00071                 }
00072                 else
00073                 {
00074                         mDisplayHistory = TRUE;
00075                 }
00076         }
00077         else
00078         {
00079                 mDisplayBar = TRUE;
00080         }
00081 
00082         LLView* parent = getParent();
00083         parent->reshape(parent->getRect().getWidth(), parent->getRect().getHeight(), FALSE);
00084 
00085         return FALSE;
00086 }
00087 
00088 void LLStatBar::draw()
00089 {
00090         if (!mStatp)
00091         {
00092 //              llinfos << "No stats for statistics bar!" << llendl;
00093                 return;
00094         }
00095 
00096         // Get the values.
00097         F32 current, min, max, mean;
00098         if (mPerSec)
00099         {
00100                 current = mStatp->getCurrentPerSec();
00101                 min = mStatp->getMinPerSec();
00102                 max = mStatp->getMaxPerSec();
00103                 mean = mStatp->getMeanPerSec();
00104         }
00105         else
00106         {
00107                 current = mStatp->getCurrent();
00108                 min = mStatp->getMin();
00109                 max = mStatp->getMax();
00110                 mean = mStatp->getMean();
00111         }
00112 
00113 
00114         if ((mUpdatesPerSec == 0.f) || (mUpdateTimer.getElapsedTimeF32() > 1.f/mUpdatesPerSec) || (mValue == 0.f))
00115         {
00116                 if (mDisplayMean)
00117                 {
00118                         mValue = mean;
00119                 }
00120                 else
00121                 {
00122                         mValue = current;
00123                 }
00124                 mUpdateTimer.reset();
00125         }
00126 
00127         S32 width = getRect().getWidth() - 40;
00128         S32 max_width = width;
00129         S32 bar_top = getRect().getHeight() - 15; // 16 pixels from top.
00130         S32 bar_height = bar_top - 20;
00131         S32 tick_height = 4;
00132         S32 tick_width = 1;
00133         S32 left, top, right, bottom;
00134 
00135         F32 value_scale = max_width/(mMaxBar - mMinBar);
00136 
00137         LLFontGL::sMonospace->renderUTF8(mLabel, 0, 0, getRect().getHeight(), LLColor4(1.f, 1.f, 1.f, 1.f),
00138                                                         LLFontGL::LEFT, LLFontGL::TOP);
00139 
00140         char value_format[64];          /* Flawfinder: ignore */
00141         char value_str[256];            /* Flawfinder: ignore */
00142         if (!mUnitLabel.empty())
00143         {
00144                 snprintf(value_format, sizeof(value_format), "%%.%df%%s", mPrecision);          /* Flawfinder: ignore */
00145                 snprintf(value_str, sizeof(value_str), value_format, mValue, mUnitLabel.c_str());               /* Flawfinder: ignore */
00146         }
00147         else
00148         {
00149                 snprintf(value_format, sizeof(value_format), "%%.%df", mPrecision);             /* Flawfinder: ignore */
00150                 snprintf(value_str, sizeof(value_str), value_format, mValue);           /* Flawfinder: ignore */
00151         }
00152 
00153         // Draw the value.
00154         LLFontGL::sMonospace->renderUTF8(value_str, 0, width, getRect().getHeight(), 
00155                 LLColor4(1.f, 1.f, 1.f, 0.5f),
00156                 LLFontGL::RIGHT, LLFontGL::TOP);
00157 
00158         snprintf(value_format, sizeof(value_format), "%%.%df", mPrecision);             /* Flawfinder: ignore */
00159         if (mDisplayBar)
00160         {
00161                 char tick_label[256];           /* Flawfinder: ignore */
00162 
00163                 // Draw the tick marks.
00164                 F32 tick_value;
00165                 top = bar_top;
00166                 bottom = bar_top - bar_height - tick_height/2;
00167 
00168                 LLGLSUIDefault gls_ui;
00169                 LLGLSNoTexture gls_no_texture;
00170                 for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mTickSpacing)
00171                 {
00172                         left = llfloor((tick_value - mMinBar)*value_scale);
00173                         right = left + tick_width;
00174                         gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.1f));
00175                 }
00176 
00177                 // Draw the tick labels (and big ticks).
00178                 bottom = bar_top - bar_height - tick_height;
00179                 for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mLabelSpacing)
00180                 {
00181                         left = llfloor((tick_value - mMinBar)*value_scale);
00182                         right = left + tick_width;
00183                         gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.25f));
00184 
00185                         snprintf(tick_label, sizeof(tick_label), value_format, tick_value);             /* Flawfinder: ignore */
00186                         // draw labels for the tick marks
00187                         LLFontGL::sMonospace->renderUTF8(tick_label, 0, left - 1, bar_top - bar_height - tick_height,
00188                                                                                          LLColor4(1.f, 1.f, 1.f, 0.5f),
00189                                                                                          LLFontGL::LEFT, LLFontGL::TOP);
00190                 }
00191 
00192                 // Now, draw the bars
00193                 top = bar_top;
00194                 bottom = bar_top - bar_height;
00195 
00196                 // draw background bar.
00197                 left = 0;
00198                 right = width;
00199                 gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 0.f, 0.f, 0.25f));
00200 
00201                 if (mStatp->getNumValues() == 0)
00202                 {
00203                         // No data, don't draw anything...
00204                         return;
00205                 }
00206                 // draw min and max
00207                 left = (S32) ((min - mMinBar) * value_scale);
00208 
00209                 if (left < 0)
00210                 {
00211                         left = 0;
00212                         llwarns << "Min:" << min << llendl;
00213                 }
00214 
00215                 right = (S32) ((max - mMinBar) * value_scale);
00216                 gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 0.25f));
00217 
00218                 S32 num_values = mStatp->getNumValues() - 1;
00219                 if (mDisplayHistory)
00220                 {
00221                         S32 i;
00222                         for (i = 0; i < num_values; i++)
00223                         {
00224                                 if (i == mStatp->getNextBin())
00225                                 {
00226                                         continue;
00227                                 }
00228                                 if (mPerSec)
00229                                 {
00230                                         left = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale);
00231                                         right = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale) + 1;
00232                                         gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
00233                                 }
00234                                 else
00235                                 {
00236                                         left = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale);
00237                                         right = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale) + 1;
00238                                         gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
00239                                 }
00240                         }
00241                 }
00242                 else
00243                 {
00244                         // draw current
00245                         left = (S32) ((current - mMinBar) * value_scale) - 1;
00246                         right = (S32) ((current - mMinBar) * value_scale) + 1;
00247                         gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 1.f));
00248                 }
00249 
00250                 // draw mean bar
00251                 top = bar_top + 2;
00252                 bottom = bar_top - bar_height - 2;
00253                 left = (S32) ((mean - mMinBar) * value_scale) - 1;
00254                 right = (S32) ((mean - mMinBar) * value_scale) + 1;
00255                 gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 1.f, 0.f, 1.f));
00256         }
00257         
00258         LLView::draw();
00259 }
00260 
00261 const LLString& LLStatBar::getLabel() const
00262 {
00263         return mLabel;
00264 }
00265 
00266 void LLStatBar::setLabel(const LLString& label)
00267 {
00268         mLabel = label;
00269 }
00270 
00271 void LLStatBar::setUnitLabel(const LLString& unit_label)
00272 {
00273         mUnitLabel = unit_label;
00274 }
00275 
00276 LLRect LLStatBar::getRequiredRect()
00277 {
00278         LLRect rect;
00279 
00280         if (mDisplayBar)
00281         {
00282                 if (mDisplayHistory)
00283                 {
00284                         rect.mTop = 67;
00285                 }
00286                 else
00287                 {
00288                         rect.mTop = 40;
00289                 }
00290         }
00291         else
00292         {
00293                 rect.mTop = 14;
00294         }
00295         return rect;
00296 }

Generated on Fri May 16 08:34:02 2008 for SecondLife by  doxygen 1.5.5