PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): LGPL-2.1
  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. //#include "llviewerprecompiledheaders.h"
  27. #include "linden_common.h"
  28. #include "llstatbar.h"
  29. #include "llmath.h"
  30. #include "llui.h"
  31. #include "llgl.h"
  32. #include "llfontgl.h"
  33. #include "llstat.h"
  34. #include "lluictrlfactory.h"
  35. ///////////////////////////////////////////////////////////////////////////////////
  36. LLStatBar::LLStatBar(const Params& p)
  37. : LLView(p),
  38. mLabel(p.label),
  39. mUnitLabel(p.unit_label),
  40. mMinBar(p.bar_min),
  41. mMaxBar(p.bar_max),
  42. mStatp(LLStat::getStat(p.stat)),
  43. mTickSpacing(p.tick_spacing),
  44. mLabelSpacing(p.label_spacing),
  45. mPrecision(p.precision),
  46. mUpdatesPerSec(p.update_rate),
  47. mPerSec(p.show_per_sec),
  48. mDisplayBar(p.show_bar),
  49. mDisplayHistory(p.show_history),
  50. mDisplayMean(p.show_mean)
  51. {
  52. }
  53. BOOL LLStatBar::handleMouseDown(S32 x, S32 y, MASK mask)
  54. {
  55. if (mDisplayBar)
  56. {
  57. if (mDisplayHistory)
  58. {
  59. mDisplayBar = FALSE;
  60. mDisplayHistory = FALSE;
  61. }
  62. else
  63. {
  64. mDisplayHistory = TRUE;
  65. }
  66. }
  67. else
  68. {
  69. mDisplayBar = TRUE;
  70. }
  71. LLView* parent = getParent();
  72. parent->reshape(parent->getRect().getWidth(), parent->getRect().getHeight(), FALSE);
  73. return FALSE;
  74. }
  75. void LLStatBar::draw()
  76. {
  77. if (!mStatp)
  78. {
  79. // llinfos << "No stats for statistics bar!" << llendl;
  80. return;
  81. }
  82. // Get the values.
  83. F32 current, min, max, mean;
  84. if (mPerSec)
  85. {
  86. current = mStatp->getCurrentPerSec();
  87. min = mStatp->getMinPerSec();
  88. max = mStatp->getMaxPerSec();
  89. mean = mStatp->getMeanPerSec();
  90. }
  91. else
  92. {
  93. current = mStatp->getCurrent();
  94. min = mStatp->getMin();
  95. max = mStatp->getMax();
  96. mean = mStatp->getMean();
  97. }
  98. if ((mUpdatesPerSec == 0.f) || (mUpdateTimer.getElapsedTimeF32() > 1.f/mUpdatesPerSec) || (mValue == 0.f))
  99. {
  100. if (mDisplayMean)
  101. {
  102. mValue = mean;
  103. }
  104. else
  105. {
  106. mValue = current;
  107. }
  108. mUpdateTimer.reset();
  109. }
  110. S32 width = getRect().getWidth() - 40;
  111. S32 max_width = width;
  112. S32 bar_top = getRect().getHeight() - 15; // 16 pixels from top.
  113. S32 bar_height = bar_top - 20;
  114. S32 tick_height = 4;
  115. S32 tick_width = 1;
  116. S32 left, top, right, bottom;
  117. F32 value_scale = max_width/(mMaxBar - mMinBar);
  118. LLFontGL::getFontMonospace()->renderUTF8(mLabel, 0, 0, getRect().getHeight(), LLColor4(1.f, 1.f, 1.f, 1.f),
  119. LLFontGL::LEFT, LLFontGL::TOP);
  120. std::string value_format;
  121. std::string value_str;
  122. if (!mUnitLabel.empty())
  123. {
  124. value_format = llformat( "%%.%df%%s", mPrecision);
  125. value_str = llformat( value_format.c_str(), mValue, mUnitLabel.c_str());
  126. }
  127. else
  128. {
  129. value_format = llformat( "%%.%df", mPrecision);
  130. value_str = llformat( value_format.c_str(), mValue);
  131. }
  132. // Draw the value.
  133. LLFontGL::getFontMonospace()->renderUTF8(value_str, 0, width, getRect().getHeight(),
  134. LLColor4(1.f, 1.f, 1.f, 0.5f),
  135. LLFontGL::RIGHT, LLFontGL::TOP);
  136. value_format = llformat( "%%.%df", mPrecision);
  137. if (mDisplayBar)
  138. {
  139. std::string tick_label;
  140. // Draw the tick marks.
  141. F32 tick_value;
  142. top = bar_top;
  143. bottom = bar_top - bar_height - tick_height/2;
  144. LLGLSUIDefault gls_ui;
  145. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  146. for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mTickSpacing)
  147. {
  148. left = llfloor((tick_value - mMinBar)*value_scale);
  149. right = left + tick_width;
  150. gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.1f));
  151. }
  152. // Draw the tick labels (and big ticks).
  153. bottom = bar_top - bar_height - tick_height;
  154. for (tick_value = mMinBar; tick_value <= mMaxBar; tick_value += mLabelSpacing)
  155. {
  156. left = llfloor((tick_value - mMinBar)*value_scale);
  157. right = left + tick_width;
  158. gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 1.f, 1.f, 0.25f));
  159. tick_label = llformat( value_format.c_str(), tick_value);
  160. // draw labels for the tick marks
  161. LLFontGL::getFontMonospace()->renderUTF8(tick_label, 0, left - 1, bar_top - bar_height - tick_height,
  162. LLColor4(1.f, 1.f, 1.f, 0.5f),
  163. LLFontGL::LEFT, LLFontGL::TOP);
  164. }
  165. // Now, draw the bars
  166. top = bar_top;
  167. bottom = bar_top - bar_height;
  168. // draw background bar.
  169. left = 0;
  170. right = width;
  171. gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 0.f, 0.f, 0.25f));
  172. if (mStatp->getNumValues() == 0)
  173. {
  174. // No data, don't draw anything...
  175. return;
  176. }
  177. // draw min and max
  178. left = (S32) ((min - mMinBar) * value_scale);
  179. if (left < 0)
  180. {
  181. left = 0;
  182. llwarns << "Min:" << min << llendl;
  183. }
  184. right = (S32) ((max - mMinBar) * value_scale);
  185. gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 0.25f));
  186. S32 num_values = mStatp->getNumValues() - 1;
  187. if (mDisplayHistory)
  188. {
  189. S32 i;
  190. for (i = 0; i < num_values; i++)
  191. {
  192. if (i == mStatp->getNextBin())
  193. {
  194. continue;
  195. }
  196. if (mPerSec)
  197. {
  198. left = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale);
  199. right = (S32)((mStatp->getPrevPerSec(i) - mMinBar) * value_scale) + 1;
  200. gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
  201. }
  202. else
  203. {
  204. left = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale);
  205. right = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale) + 1;
  206. gl_rect_2d(left, bottom+i+1, right, bottom+i, LLColor4(1.f, 0.f, 0.f, 1.f));
  207. }
  208. }
  209. }
  210. else
  211. {
  212. // draw current
  213. left = (S32) ((current - mMinBar) * value_scale) - 1;
  214. right = (S32) ((current - mMinBar) * value_scale) + 1;
  215. gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 1.f));
  216. }
  217. // draw mean bar
  218. top = bar_top + 2;
  219. bottom = bar_top - bar_height - 2;
  220. left = (S32) ((mean - mMinBar) * value_scale) - 1;
  221. right = (S32) ((mean - mMinBar) * value_scale) + 1;
  222. gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 1.f, 0.f, 1.f));
  223. }
  224. LLView::draw();
  225. }
  226. void LLStatBar::setRange(F32 bar_min, F32 bar_max, F32 tick_spacing, F32 label_spacing)
  227. {
  228. mMinBar = bar_min;
  229. mMaxBar = bar_max;
  230. mTickSpacing = tick_spacing;
  231. mLabelSpacing = label_spacing;
  232. }
  233. LLRect LLStatBar::getRequiredRect()
  234. {
  235. LLRect rect;
  236. if (mDisplayBar)
  237. {
  238. if (mDisplayHistory)
  239. {
  240. rect.mTop = 67;
  241. }
  242. else
  243. {
  244. rect.mTop = 40;
  245. }
  246. }
  247. else
  248. {
  249. rect.mTop = 14;
  250. }
  251. return rect;
  252. }