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 EWidgetType LLStatBar::getWidgetType() const
00089 {
00090 return WIDGET_TYPE_STAT_BAR;
00091 }
00092
00093 LLString LLStatBar::getWidgetTag() const
00094 {
00095 return LL_STAT_BAR_TAG;
00096 }
00097
00098 void LLStatBar::draw()
00099 {
00100 if (!mStatp)
00101 {
00102
00103 return;
00104 }
00105
00106
00107 F32 current, min, max, mean;
00108 if (mPerSec)
00109 {
00110 current = mStatp->getCurrentPerSec();
00111 min = mStatp->getMinPerSec();
00112 max = mStatp->getMaxPerSec();
00113 mean = mStatp->getMeanPerSec();
00114 }
00115 else
00116 {
00117 current = mStatp->getCurrent();
00118 min = mStatp->getMin();
00119 max = mStatp->getMax();
00120 mean = mStatp->getMean();
00121 }
00122
00123
00124 if ((mUpdatesPerSec == 0.f) || (mUpdateTimer.getElapsedTimeF32() > 1.f/mUpdatesPerSec) || (mValue == 0.f))
00125 {
00126 if (mDisplayMean)
00127 {
00128 mValue = mean;
00129 }
00130 else
00131 {
00132 mValue = current;
00133 }
00134 mUpdateTimer.reset();
00135 }
00136
00137 S32 width = mRect.getWidth() - 40;
00138 S32 max_width = width;
00139 S32 bar_top = mRect.getHeight() - 15;
00140 S32 bar_height = bar_top - 20;
00141 S32 tick_height = 4;
00142 S32 tick_width = 1;
00143 S32 left, top, right, bottom;
00144
00145 F32 value_scale = max_width/(mMaxBar - mMinBar);
00146
00147 LLFontGL::sMonospace->renderUTF8(mLabel, 0, 0, mRect.getHeight(), LLColor4(1.f, 1.f, 1.f, 1.f),
00148 LLFontGL::LEFT, LLFontGL::TOP);
00149
00150 char value_format[64];
00151 char value_str[256];
00152 if (!mUnitLabel.empty())
00153 {
00154 snprintf(value_format, sizeof(value_format), "%%.%df%%s", mPrecision);
00155 snprintf(value_str, sizeof(value_str), value_format, mValue, mUnitLabel.c_str());
00156 }
00157 else
00158 {
00159 snprintf(value_format, sizeof(value_format), "%%.%df", mPrecision);
00160 snprintf(value_str, sizeof(value_str), value_format, mValue);
00161 }
00162
00163
00164 LLFontGL::sMonospace->renderUTF8(value_str, 0, width, mRect.getHeight(),
00165 LLColor4(1.f, 1.f, 1.f, 0.5f),
00166 LLFontGL::RIGHT, LLFontGL::TOP);
00167
00168 snprintf(value_format, sizeof(value_format), "%%.%df", mPrecision);
00169 if (mDisplayBar)
00170 {
00171 char tick_label[256];
00172
00173
00174 F32 tick_value;
00175 top = bar_top;
00176 bottom = bar_top - bar_height - tick_height/2;
00177
00178 LLGLSUIDefault gls_ui;
00179 LLGLSNoTexture gls_no_texture;
00180 for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mTickSpacing)
00181 {
00182 left = llfloor((tick_value - mMinBar)*value_scale);
00183 right = left + tick_width;
00184 gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.1f));
00185 }
00186
00187
00188 bottom = bar_top - bar_height - tick_height;
00189 for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mLabelSpacing)
00190 {
00191 left = llfloor((tick_value - mMinBar)*value_scale);
00192 right = left + tick_width;
00193 gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.25f));
00194
00195 snprintf(tick_label, sizeof(tick_label), value_format, tick_value);
00196
00197 LLFontGL::sMonospace->renderUTF8(tick_label, 0, left - 1, bar_top - bar_height - tick_height,
00198 LLColor4(1.f, 1.f, 1.f, 0.5f),
00199 LLFontGL::LEFT, LLFontGL::TOP);
00200 }
00201
00202
00203 top = bar_top;
00204 bottom = bar_top - bar_height;
00205
00206
00207 left = 0;
00208 right = width;
00209 gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 0.f, 0.f, 0.25f));
00210
00211 if (mStatp->getNumValues() == 0)
00212 {
00213
00214 return;
00215 }
00216
00217 left = (S32) ((min - mMinBar) * value_scale);
00218
00219 if (left < 0)
00220 {
00221 left = 0;
00222 llwarns << "Min:" << min << llendl;
00223 }
00224
00225 right = (S32) ((max - mMinBar) * value_scale);
00226 gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 0.25f));
00227
00228 S32 num_values = mStatp->getNumValues() - 1;
00229 if (mDisplayHistory)
00230 {
00231 S32 i;
00232 for (i = 0; i < num_values; i++)
00233 {
00234 if (i == mStatp->getNextBin())
00235 {
00236 continue;
00237 }
00238 if (mPerSec)
00239 {
00240 left = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale);
00241 right = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale) + 1;
00242 gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
00243 }
00244 else
00245 {
00246 left = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale);
00247 right = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale) + 1;
00248 gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
00249 }
00250 }
00251 }
00252 else
00253 {
00254
00255 left = (S32) ((current - mMinBar) * value_scale) - 1;
00256 right = (S32) ((current - mMinBar) * value_scale) + 1;
00257 gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 1.f));
00258 }
00259
00260
00261 top = bar_top + 2;
00262 bottom = bar_top - bar_height - 2;
00263 left = (S32) ((mean - mMinBar) * value_scale) - 1;
00264 right = (S32) ((mean - mMinBar) * value_scale) + 1;
00265 gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 1.f, 0.f, 1.f));
00266 }
00267
00268 LLView::draw();
00269 }
00270
00271 const LLString& LLStatBar::getLabel() const
00272 {
00273 return mLabel;
00274 }
00275
00276 void LLStatBar::setLabel(const LLString& label)
00277 {
00278 mLabel = label;
00279 }
00280
00281 void LLStatBar::setUnitLabel(const LLString& unit_label)
00282 {
00283 mUnitLabel = unit_label;
00284 }
00285
00286 LLRect LLStatBar::getRequiredRect()
00287 {
00288 LLRect rect;
00289
00290 if (mDisplayBar)
00291 {
00292 if (mDisplayHistory)
00293 {
00294 rect.mTop = 67;
00295 }
00296 else
00297 {
00298 rect.mTop = 40;
00299 }
00300 }
00301 else
00302 {
00303 rect.mTop = 14;
00304 }
00305 return rect;
00306 }