PageRenderTime 47ms CodeModel.GetById 18ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llstatbar.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 287 lines | 213 code | 35 blank | 39 comment | 24 complexity | b54f247a398c4e2f35b1659501ad6c8f MD5 | raw file
  1/** 
  2 * @file llstatbar.cpp
  3 * @brief A little map of the world with network information
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27//#include "llviewerprecompiledheaders.h"
 28#include "linden_common.h"
 29
 30#include "llstatbar.h"
 31
 32#include "llmath.h"
 33#include "llui.h"
 34#include "llgl.h"
 35#include "llfontgl.h"
 36
 37#include "llstat.h"
 38#include "lluictrlfactory.h"
 39
 40///////////////////////////////////////////////////////////////////////////////////
 41
 42LLStatBar::LLStatBar(const Params& p)
 43	: LLView(p),
 44	  mLabel(p.label),
 45	  mUnitLabel(p.unit_label),
 46	  mMinBar(p.bar_min),
 47	  mMaxBar(p.bar_max),
 48	  mStatp(LLStat::getStat(p.stat)),
 49	  mTickSpacing(p.tick_spacing),
 50	  mLabelSpacing(p.label_spacing),
 51	  mPrecision(p.precision),
 52	  mUpdatesPerSec(p.update_rate),
 53	  mPerSec(p.show_per_sec),
 54	  mDisplayBar(p.show_bar),
 55	  mDisplayHistory(p.show_history),
 56	  mDisplayMean(p.show_mean)
 57{
 58}
 59
 60BOOL LLStatBar::handleMouseDown(S32 x, S32 y, MASK mask)
 61{
 62	if (mDisplayBar)
 63	{
 64		if (mDisplayHistory)
 65		{
 66			mDisplayBar = FALSE;
 67			mDisplayHistory = FALSE;
 68		}
 69		else
 70		{
 71			mDisplayHistory = TRUE;
 72		}
 73	}
 74	else
 75	{
 76		mDisplayBar = TRUE;
 77	}
 78
 79	LLView* parent = getParent();
 80	parent->reshape(parent->getRect().getWidth(), parent->getRect().getHeight(), FALSE);
 81
 82	return FALSE;
 83}
 84
 85void LLStatBar::draw()
 86{
 87	if (!mStatp)
 88	{
 89//		llinfos << "No stats for statistics bar!" << llendl;
 90		return;
 91	}
 92
 93	// Get the values.
 94	F32 current, min, max, mean;
 95	if (mPerSec)
 96	{
 97		current = mStatp->getCurrentPerSec();
 98		min = mStatp->getMinPerSec();
 99		max = mStatp->getMaxPerSec();
100		mean = mStatp->getMeanPerSec();
101	}
102	else
103	{
104		current = mStatp->getCurrent();
105		min = mStatp->getMin();
106		max = mStatp->getMax();
107		mean = mStatp->getMean();
108	}
109
110
111	if ((mUpdatesPerSec == 0.f) || (mUpdateTimer.getElapsedTimeF32() > 1.f/mUpdatesPerSec) || (mValue == 0.f))
112	{
113		if (mDisplayMean)
114		{
115			mValue = mean;
116		}
117		else
118		{
119			mValue = current;
120		}
121		mUpdateTimer.reset();
122	}
123
124	S32 width = getRect().getWidth() - 40;
125	S32 max_width = width;
126	S32 bar_top = getRect().getHeight() - 15; // 16 pixels from top.
127	S32 bar_height = bar_top - 20;
128	S32 tick_height = 4;
129	S32 tick_width = 1;
130	S32 left, top, right, bottom;
131
132	F32 value_scale = max_width/(mMaxBar - mMinBar);
133
134	LLFontGL::getFontMonospace()->renderUTF8(mLabel, 0, 0, getRect().getHeight(), LLColor4(1.f, 1.f, 1.f, 1.f),
135											 LLFontGL::LEFT, LLFontGL::TOP);
136
137	std::string value_format;
138	std::string value_str;
139	if (!mUnitLabel.empty())
140	{
141		value_format = llformat( "%%.%df%%s", mPrecision);
142		value_str = llformat( value_format.c_str(), mValue, mUnitLabel.c_str());
143	}
144	else
145	{
146		value_format = llformat( "%%.%df", mPrecision);
147		value_str = llformat( value_format.c_str(), mValue);
148	}
149
150	// Draw the value.
151	LLFontGL::getFontMonospace()->renderUTF8(value_str, 0, width, getRect().getHeight(), 
152											 LLColor4(1.f, 1.f, 1.f, 0.5f),
153											 LLFontGL::RIGHT, LLFontGL::TOP);
154
155	value_format = llformat( "%%.%df", mPrecision);
156	if (mDisplayBar)
157	{
158		std::string tick_label;
159
160		// Draw the tick marks.
161		F32 tick_value;
162		top = bar_top;
163		bottom = bar_top - bar_height - tick_height/2;
164
165		LLGLSUIDefault gls_ui;
166		gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
167
168		for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mTickSpacing)
169		{
170			left = llfloor((tick_value - mMinBar)*value_scale);
171			right = left + tick_width;
172			gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.1f));
173		}
174
175		// Draw the tick labels (and big ticks).
176		bottom = bar_top - bar_height - tick_height;
177		for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mLabelSpacing)
178		{
179			left = llfloor((tick_value - mMinBar)*value_scale);
180			right = left + tick_width;
181			gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.25f));
182
183			tick_label = llformat( value_format.c_str(), tick_value);
184			// draw labels for the tick marks
185			LLFontGL::getFontMonospace()->renderUTF8(tick_label, 0, left - 1, bar_top - bar_height - tick_height,
186													 LLColor4(1.f, 1.f, 1.f, 0.5f),
187													 LLFontGL::LEFT, LLFontGL::TOP);
188		}
189
190		// Now, draw the bars
191		top = bar_top;
192		bottom = bar_top - bar_height;
193
194		// draw background bar.
195		left = 0;
196		right = width;
197		gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 0.f, 0.f, 0.25f));
198
199		if (mStatp->getNumValues() == 0)
200		{
201			// No data, don't draw anything...
202			return;
203		}
204		// draw min and max
205		left = (S32) ((min - mMinBar) * value_scale);
206
207		if (left < 0)
208		{
209			left = 0;
210			llwarns << "Min:" << min << llendl;
211		}
212
213		right = (S32) ((max - mMinBar) * value_scale);
214		gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 0.25f));
215
216		S32 num_values = mStatp->getNumValues() - 1;
217		if (mDisplayHistory)
218		{
219			S32 i;
220			for (i = 0; i < num_values; i++)
221			{
222				if (i == mStatp->getNextBin())
223				{
224					continue;
225				}
226				if (mPerSec)
227				{
228					left = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale);
229					right = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale) + 1;
230					gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
231				}
232				else
233				{
234					left = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale);
235					right = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale) + 1;
236					gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
237				}
238			}
239		}
240		else
241		{
242			// draw current
243			left = (S32) ((current - mMinBar) * value_scale) - 1;
244			right = (S32) ((current - mMinBar) * value_scale) + 1;
245			gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 1.f));
246		}
247
248		// draw mean bar
249		top = bar_top + 2;
250		bottom = bar_top - bar_height - 2;
251		left = (S32) ((mean - mMinBar) * value_scale) - 1;
252		right = (S32) ((mean - mMinBar) * value_scale) + 1;
253		gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 1.f, 0.f, 1.f));
254	}
255	
256	LLView::draw();
257}
258
259void LLStatBar::setRange(F32 bar_min, F32 bar_max, F32 tick_spacing, F32 label_spacing)
260{
261	mMinBar = bar_min;
262	mMaxBar = bar_max;
263	mTickSpacing = tick_spacing;
264	mLabelSpacing = label_spacing;
265}
266
267LLRect LLStatBar::getRequiredRect()
268{
269	LLRect rect;
270
271	if (mDisplayBar)
272	{
273		if (mDisplayHistory)
274		{
275			rect.mTop = 67;
276		}
277		else
278		{
279			rect.mTop = 40;
280		}
281	}
282	else
283	{
284		rect.mTop = 14;
285	}
286	return rect;
287}