llvelocitybar.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @file llvelocitybar.cpp
  3. * @brief A user interface widget that displays user energy level
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llvelocitybar.h"
  34. #include "llgl.h"
  35. #include "llagent.h"
  36. #include "llviewercamera.h"
  37. #include "llviewerwindow.h"
  38. #include "llvoavatarself.h"
  39. LLVelocityBar* gVelocityBarp = NULL;
  40. constexpr S32 BAR_TOP = 24;
  41. constexpr S32 BAR_BOTTOM = 20;
  42. constexpr S32 TICK_BOTTOM = 15;
  43. constexpr S32 TICK_WIDTH = 2;
  44. static const char* velocity_str = "Velocity %.2fm/s = %.1fkm/h = %.1fkt";
  45. static const std::string labels[] = { "0", "2", "4", "6", "8", "10", "12",
  46. "14", "16", "18", "20", "22", "24", "26",
  47. "28", "30", "32m/s" };
  48. constexpr F32 units_to_ticks = 0.5f; // 1 / 2m/s per tick
  49. constexpr S32 labels_size = (S32)LL_ARRAY_SIZE(labels);
  50. LLVelocityBar::LLVelocityBar(const std::string& name)
  51. : LLView(name, false),
  52. mFont(LLFontGL::getFontMonospace())
  53. {
  54. llassert(gVelocityBarp == NULL);
  55. gVelocityBarp = this;
  56. setVisible(false);
  57. setFollowsBottom();
  58. setFollowsLeft();
  59. resize();
  60. if (mFont)
  61. {
  62. // We use a constant width (the width for '0') for speed:
  63. mHalfCharWidth = mFont->getWidth("0") / 2;
  64. }
  65. }
  66. //virtual
  67. LLVelocityBar::~LLVelocityBar()
  68. {
  69. gVelocityBarp = NULL;
  70. }
  71. void LLVelocityBar::resize()
  72. {
  73. mWindowWidth = gViewerWindowp->getVirtualWindowRect().getWidth();
  74. LLRect r;
  75. r.setLeftTopAndSize(VELOCITY_LEFT, VELOCITY_TOP,
  76. mWindowWidth - 2 * VELOCITY_LEFT,
  77. VELOCITY_HEIGHT);
  78. setRect(r);
  79. }
  80. //virtual
  81. void LLVelocityBar::draw()
  82. {
  83. if (gViewerWindowp->getVirtualWindowRect().getWidth() != mWindowWidth)
  84. {
  85. resize();
  86. }
  87. S32 left, top, right, bottom;
  88. S32 width = getRect().getWidth();
  89. F32 velocity = 0.f;
  90. if (gAgentAvatarp && gAgentAvatarp->mIsSitting)
  91. {
  92. // When sitting (presumably on a vehicle), use the speed of the seat.
  93. LLViewerObject* vehiclep = (LLViewerObject*)gAgentAvatarp->getParent();
  94. if (vehiclep && vehiclep->flagUsePhysics())
  95. {
  96. velocity = vehiclep->getVelocity().length();
  97. }
  98. else
  99. {
  100. // For non-physical objects, use the camera speed...
  101. velocity = gViewerCamera.getAverageSpeed();
  102. }
  103. }
  104. else
  105. {
  106. velocity = gAgent.getVelocity().length();
  107. }
  108. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  109. #if 0
  110. // Draw a background box
  111. gGL.color4f(0.f, 0.f, 0.f, 0.25f);
  112. gl_rect_2d(0, getRect().getHeight(), width, 0);
  113. #endif
  114. // Color for the scale and text
  115. LLColor4 color = LLColor4::white;
  116. // Draw the scale
  117. top = BAR_BOTTOM - 1;
  118. bottom = TICK_BOTTOM;
  119. constexpr S32 intervals = labels_size - 1;
  120. for (S32 i = 0; i < labels_size; ++i)
  121. {
  122. left = i * width / intervals;
  123. right = left + TICK_WIDTH;
  124. gl_rect_2d(left, top, right, bottom, color);
  125. }
  126. // Draw labels for the bar
  127. if (mFont)
  128. {
  129. top = BAR_TOP + 15;
  130. left = 0;
  131. constexpr F32 ms_to_kt = 3600.f / 1852.f;
  132. mFont->renderUTF8(llformat(velocity_str, velocity, velocity * 3.6f,
  133. velocity * ms_to_kt),
  134. 0, left, top, color, LLFontGL::LEFT, LLFontGL::TOP);
  135. top = TICK_BOTTOM;
  136. for (S32 i = 0; i < labels_size; ++i)
  137. {
  138. const std::string& label = labels[i];
  139. left = 1 + i * width / intervals -
  140. (S32)label.size() * mHalfCharWidth;
  141. mFont->renderUTF8(label, 0, left, top, color, LLFontGL::LEFT,
  142. LLFontGL::TOP);
  143. }
  144. }
  145. // Draw the speed bar
  146. right = (S32)(velocity * units_to_ticks * (F32)width / (F32)intervals);
  147. if (velocity < 4.f)
  148. {
  149. color = LLColor4::blue; // Walking
  150. }
  151. else if (velocity < 6.f)
  152. {
  153. color = LLColor4::cyan; // Running
  154. }
  155. else if (velocity <= 16.f)
  156. {
  157. color = LLColor4::green; // Flying
  158. }
  159. else if (velocity <= 24.f)
  160. {
  161. color = LLColor4::yellow; // Riding/boosted speed
  162. }
  163. else if (velocity <= 32.f)
  164. {
  165. color = LLColor4::orange; // Riding/boosted speed
  166. }
  167. else
  168. {
  169. color = LLColor4::red; // Out of scale...
  170. right = width;
  171. }
  172. gl_rect_2d(0, BAR_TOP, right, BAR_BOTTOM, color);
  173. }