Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
14 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <vgui_controls/AnalogBar.h>
  10. #include <vgui_controls/Controls.h>
  11. #include <vgui/ILocalize.h>
  12. #include <vgui/IScheme.h>
  13. #include <vgui/ISurface.h>
  14. #include <keyvalues.h>
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include <tier0/memdbgon.h>
  17. using namespace vgui;
  18. DECLARE_BUILD_FACTORY( AnalogBar );
  19. #define ANALOG_BAR_HOME_SIZE 4
  20. #define ANALOG_BAR_HOME_GAP 2
  21. #define ANALOG_BAR_LESS_TALL ( ANALOG_BAR_HOME_SIZE + ANALOG_BAR_HOME_GAP )
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Constructor
  24. //-----------------------------------------------------------------------------
  25. AnalogBar::AnalogBar(Panel *parent, const char *panelName) : Panel(parent, panelName)
  26. {
  27. _analogValue = 0.0f;
  28. m_pszDialogVar = NULL;
  29. SetSegmentInfo( 2, 6 );
  30. SetBarInset( 0 );
  31. m_iAnalogValueDirection = PROGRESS_EAST;
  32. m_fHomeValue = 2.0f;
  33. m_HomeColor = GetFgColor();
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Destructor
  37. //-----------------------------------------------------------------------------
  38. AnalogBar::~AnalogBar()
  39. {
  40. delete [] m_pszDialogVar;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: data accessor
  44. //-----------------------------------------------------------------------------
  45. void AnalogBar::SetSegmentInfo( int gap, int width )
  46. {
  47. _segmentGap = gap;
  48. _segmentWide = width;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: returns the number of segment blocks drawn
  52. //-----------------------------------------------------------------------------
  53. int AnalogBar::GetDrawnSegmentCount()
  54. {
  55. int wide, tall;
  56. GetSize(wide, tall);
  57. int segmentTotal = wide / (_segmentGap + _segmentWide);
  58. return (int)(segmentTotal * _analogValue);
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: returns the total number of segment blocks drawn (active and inactive)
  62. //-----------------------------------------------------------------------------
  63. int AnalogBar::GetTotalSegmentCount()
  64. {
  65. int wide, tall;
  66. GetSize(wide, tall);
  67. int segmentTotal = wide / (_segmentGap + _segmentWide);
  68. return segmentTotal;
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. //-----------------------------------------------------------------------------
  73. void AnalogBar::PaintBackground()
  74. {
  75. // Don't draw a background
  76. }
  77. void AnalogBar::PaintSegment( int &x, int &y, int tall, int wide, Color color, bool bHome )
  78. {
  79. switch( m_iAnalogValueDirection )
  80. {
  81. case PROGRESS_EAST:
  82. x += _segmentGap;
  83. if ( bHome )
  84. {
  85. surface()->DrawSetColor( GetHomeColor() );
  86. surface()->DrawFilledRect(x, y, x + _segmentWide, y + ANALOG_BAR_HOME_SIZE );
  87. surface()->DrawFilledRect(x, y + tall - (y * 2) - ANALOG_BAR_HOME_SIZE, x + _segmentWide, y + tall - (y * 2) );
  88. }
  89. surface()->DrawSetColor( color );
  90. surface()->DrawFilledRect(x, y + ANALOG_BAR_LESS_TALL, x + _segmentWide, y + tall - (y * 2) - ANALOG_BAR_LESS_TALL );
  91. x += _segmentWide;
  92. break;
  93. case PROGRESS_WEST:
  94. x -= _segmentGap + _segmentWide;
  95. if ( bHome )
  96. {
  97. surface()->DrawSetColor( GetHomeColor() );
  98. surface()->DrawFilledRect(x, y, x + _segmentWide, y + ANALOG_BAR_HOME_SIZE );
  99. surface()->DrawFilledRect(x, y + tall - (y * 2) - ANALOG_BAR_HOME_SIZE, x + _segmentWide, y + tall - (y * 2) );
  100. }
  101. surface()->DrawSetColor( color );
  102. surface()->DrawFilledRect(x, y + ANALOG_BAR_LESS_TALL, x + _segmentWide, y + tall - (y * 2) - ANALOG_BAR_LESS_TALL );
  103. break;
  104. case PROGRESS_NORTH:
  105. y -= _segmentGap + _segmentWide;
  106. if ( bHome )
  107. {
  108. surface()->DrawSetColor( GetHomeColor() );
  109. surface()->DrawFilledRect(x, y, x + ANALOG_BAR_HOME_SIZE, y + _segmentWide );
  110. surface()->DrawFilledRect(x + wide - (x * 2) - ANALOG_BAR_HOME_SIZE, y, x + wide - (x * 2), y + _segmentWide );
  111. }
  112. surface()->DrawSetColor( color );
  113. surface()->DrawFilledRect(x + ANALOG_BAR_LESS_TALL, y, x + wide - (x * 2) - ANALOG_BAR_LESS_TALL, y + _segmentWide);
  114. break;
  115. case PROGRESS_SOUTH:
  116. y += _segmentGap;
  117. if ( bHome )
  118. {
  119. surface()->DrawSetColor( GetHomeColor() );
  120. surface()->DrawFilledRect(x, y, x + ANALOG_BAR_HOME_SIZE, y + _segmentWide );
  121. surface()->DrawFilledRect(x + wide - (x * 2) - ANALOG_BAR_HOME_SIZE, y, x + wide - (x * 2), y + _segmentWide );
  122. }
  123. surface()->DrawSetColor( color );
  124. surface()->DrawFilledRect(x + ANALOG_BAR_LESS_TALL, y, x + wide - (x * 2) - ANALOG_BAR_LESS_TALL, y + _segmentWide);
  125. y += _segmentWide;
  126. break;
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose:
  131. //-----------------------------------------------------------------------------
  132. void AnalogBar::Paint()
  133. {
  134. int wide, tall;
  135. GetSize(wide, tall);
  136. // gaps
  137. int segmentTotal = 0, segmentsDrawn = 0;
  138. int x = 0, y = 0;
  139. switch( m_iAnalogValueDirection )
  140. {
  141. case PROGRESS_WEST:
  142. x = wide;
  143. y = m_iBarInset;
  144. segmentTotal = wide / (_segmentGap + _segmentWide);
  145. segmentsDrawn = (int)(segmentTotal * _analogValue + 0.5f);
  146. break;
  147. case PROGRESS_EAST:
  148. x = 0;
  149. y = m_iBarInset;
  150. segmentTotal = wide / (_segmentGap + _segmentWide);
  151. segmentsDrawn = (int)(segmentTotal * _analogValue + 0.5f);
  152. break;
  153. case PROGRESS_NORTH:
  154. x = m_iBarInset;
  155. y = tall;
  156. segmentTotal = tall / (_segmentGap + _segmentWide);
  157. segmentsDrawn = (int)(segmentTotal * _analogValue + 0.5f);
  158. break;
  159. case PROGRESS_SOUTH:
  160. x = m_iBarInset;
  161. y = 0;
  162. segmentTotal = tall / (_segmentGap + _segmentWide);
  163. segmentsDrawn = (int)(segmentTotal * _analogValue + 0.5f);
  164. break;
  165. }
  166. int iHomeIndex = (int)( segmentTotal * m_fHomeValue + 0.5f ) - 1;
  167. if ( iHomeIndex < 0 )
  168. iHomeIndex = 0;
  169. for (int i = 0; i < segmentsDrawn; i++)
  170. PaintSegment( x, y, tall, wide, GetFgColor(), i == iHomeIndex );
  171. for (int i = segmentsDrawn; i < segmentTotal; i++)
  172. PaintSegment( x, y, tall, wide, GetBgColor(), i == iHomeIndex );
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose:
  176. //-----------------------------------------------------------------------------
  177. void AnalogBar::SetAnalogValue(float analogValue)
  178. {
  179. if (analogValue != _analogValue)
  180. {
  181. // clamp the analogValue value within the range
  182. if (analogValue < 0.0f)
  183. {
  184. analogValue = 0.0f;
  185. }
  186. else if (analogValue > 1.0f)
  187. {
  188. analogValue = 1.0f;
  189. }
  190. _analogValue = analogValue;
  191. Repaint();
  192. }
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose: data accessor
  196. //-----------------------------------------------------------------------------
  197. float AnalogBar::GetAnalogValue()
  198. {
  199. return _analogValue;
  200. }
  201. //-----------------------------------------------------------------------------
  202. // Purpose:
  203. //-----------------------------------------------------------------------------
  204. void AnalogBar::ApplySchemeSettings(IScheme *pScheme)
  205. {
  206. Panel::ApplySchemeSettings(pScheme);
  207. SetBgColor( Color( 255 - GetFgColor().r(), 255 - GetFgColor().g(), 255 - GetFgColor().b(), GetFgColor().a() ) );
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Purpose: utility function for calculating a time remaining string
  211. //-----------------------------------------------------------------------------
  212. bool AnalogBar::ConstructTimeRemainingString(wchar_t *output, int outputBufferSizeInBytes, float startTime, float currentTime, float currentAnalogValue, float lastAnalogValueUpdateTime, bool addRemainingSuffix)
  213. {
  214. Assert(lastAnalogValueUpdateTime <= currentTime);
  215. output[0] = 0;
  216. // calculate pre-extrapolation values
  217. float timeElapsed = lastAnalogValueUpdateTime - startTime;
  218. float totalTime = timeElapsed / currentAnalogValue;
  219. // calculate seconds
  220. int secondsRemaining = (int)(totalTime - timeElapsed);
  221. if (lastAnalogValueUpdateTime < currentTime)
  222. {
  223. // old update, extrapolate
  224. float analogValueRate = currentAnalogValue / timeElapsed;
  225. float extrapolatedAnalogValue = analogValueRate * (currentTime - startTime);
  226. float extrapolatedTotalTime = (currentTime - startTime) / extrapolatedAnalogValue;
  227. secondsRemaining = (int)(extrapolatedTotalTime - timeElapsed);
  228. }
  229. // if there's some time, make sure it's at least one second left
  230. if ( secondsRemaining == 0 && ( ( totalTime - timeElapsed ) > 0 ) )
  231. {
  232. secondsRemaining = 1;
  233. }
  234. // calculate minutes
  235. int minutesRemaining = 0;
  236. while (secondsRemaining >= 60)
  237. {
  238. minutesRemaining++;
  239. secondsRemaining -= 60;
  240. }
  241. char minutesBuf[16];
  242. Q_snprintf(minutesBuf, sizeof( minutesBuf ), "%d", minutesRemaining);
  243. char secondsBuf[16];
  244. Q_snprintf(secondsBuf, sizeof( secondsBuf ), "%d", secondsRemaining);
  245. if (minutesRemaining > 0)
  246. {
  247. wchar_t unicodeMinutes[16];
  248. g_pVGuiLocalize->ConvertANSIToUnicode(minutesBuf, unicodeMinutes, sizeof( unicodeMinutes ));
  249. wchar_t unicodeSeconds[16];
  250. g_pVGuiLocalize->ConvertANSIToUnicode(secondsBuf, unicodeSeconds, sizeof( unicodeSeconds ));
  251. const char *unlocalizedString = "#vgui_TimeLeftMinutesSeconds";
  252. if (minutesRemaining == 1 && secondsRemaining == 1)
  253. {
  254. unlocalizedString = "#vgui_TimeLeftMinuteSecond";
  255. }
  256. else if (minutesRemaining == 1)
  257. {
  258. unlocalizedString = "#vgui_TimeLeftMinuteSeconds";
  259. }
  260. else if (secondsRemaining == 1)
  261. {
  262. unlocalizedString = "#vgui_TimeLeftMinutesSecond";
  263. }
  264. char unlocString[64];
  265. Q_strncpy(unlocString, unlocalizedString,sizeof( unlocString ));
  266. if (addRemainingSuffix)
  267. {
  268. Q_strncat(unlocString, "Remaining", sizeof(unlocString ), COPY_ALL_CHARACTERS);
  269. }
  270. g_pVGuiLocalize->ConstructString(output, outputBufferSizeInBytes, g_pVGuiLocalize->Find(unlocString), 2, unicodeMinutes, unicodeSeconds);
  271. }
  272. else if (secondsRemaining > 0)
  273. {
  274. wchar_t unicodeSeconds[16];
  275. g_pVGuiLocalize->ConvertANSIToUnicode(secondsBuf, unicodeSeconds, sizeof( unicodeSeconds ));
  276. const char *unlocalizedString = "#vgui_TimeLeftSeconds";
  277. if (secondsRemaining == 1)
  278. {
  279. unlocalizedString = "#vgui_TimeLeftSecond";
  280. }
  281. char unlocString[64];
  282. Q_strncpy(unlocString, unlocalizedString,sizeof(unlocString));
  283. if (addRemainingSuffix)
  284. {
  285. Q_strncat(unlocString, "Remaining",sizeof(unlocString), COPY_ALL_CHARACTERS);
  286. }
  287. g_pVGuiLocalize->ConstructString(output, outputBufferSizeInBytes, g_pVGuiLocalize->Find(unlocString), 1, unicodeSeconds);
  288. }
  289. else
  290. {
  291. return false;
  292. }
  293. return true;
  294. }
  295. //-----------------------------------------------------------------------------
  296. // Purpose: data accessor
  297. //-----------------------------------------------------------------------------
  298. void AnalogBar::SetBarInset( int pixels )
  299. {
  300. m_iBarInset = pixels;
  301. }
  302. //-----------------------------------------------------------------------------
  303. // Purpose: data accessor
  304. //-----------------------------------------------------------------------------
  305. int AnalogBar::GetBarInset( void )
  306. {
  307. return m_iBarInset;
  308. }
  309. //-----------------------------------------------------------------------------
  310. // Purpose:
  311. //-----------------------------------------------------------------------------
  312. void AnalogBar::ApplySettings(KeyValues *inResourceData)
  313. {
  314. _analogValue = inResourceData->GetFloat("analogValue", 0.0f);
  315. const char *dialogVar = inResourceData->GetString("variable", "");
  316. if (dialogVar && *dialogVar)
  317. {
  318. m_pszDialogVar = new char[strlen(dialogVar) + 1];
  319. strcpy(m_pszDialogVar, dialogVar);
  320. }
  321. BaseClass::ApplySettings(inResourceData);
  322. }
  323. //-----------------------------------------------------------------------------
  324. // Purpose:
  325. //-----------------------------------------------------------------------------
  326. void AnalogBar::GetSettings(KeyValues *outResourceData)
  327. {
  328. BaseClass::GetSettings(outResourceData);
  329. outResourceData->SetFloat("analogValue", _analogValue );
  330. if (m_pszDialogVar)
  331. {
  332. outResourceData->SetString("variable", m_pszDialogVar);
  333. }
  334. }
  335. //-----------------------------------------------------------------------------
  336. // Purpose: Returns a string description of the panel fields for use in the UI
  337. //-----------------------------------------------------------------------------
  338. const char *AnalogBar::GetDescription( void )
  339. {
  340. static char buf[1024];
  341. _snprintf(buf, sizeof(buf), "%s, string analogValue, string variable", BaseClass::GetDescription());
  342. return buf;
  343. }
  344. //-----------------------------------------------------------------------------
  345. // Purpose: updates analogValue bar bases on values
  346. //-----------------------------------------------------------------------------
  347. void AnalogBar::OnDialogVariablesChanged(KeyValues *dialogVariables)
  348. {
  349. if (m_pszDialogVar)
  350. {
  351. int val = dialogVariables->GetInt(m_pszDialogVar, -1);
  352. if (val >= 0.0f)
  353. {
  354. SetAnalogValue(val / 100.0f);
  355. }
  356. }
  357. }
  358. DECLARE_BUILD_FACTORY( ContinuousAnalogBar );
  359. //-----------------------------------------------------------------------------
  360. // Purpose: Constructor
  361. //-----------------------------------------------------------------------------
  362. ContinuousAnalogBar::ContinuousAnalogBar(Panel *parent, const char *panelName) : AnalogBar(parent, panelName)
  363. {
  364. }
  365. //-----------------------------------------------------------------------------
  366. // Purpose:
  367. //-----------------------------------------------------------------------------
  368. void ContinuousAnalogBar::Paint()
  369. {
  370. int x = 0, y = 0;
  371. int wide, tall;
  372. GetSize(wide, tall);
  373. surface()->DrawSetColor(GetFgColor());
  374. switch( m_iAnalogValueDirection )
  375. {
  376. case PROGRESS_EAST:
  377. surface()->DrawFilledRect( x, y, x + (int)( wide * _analogValue ), y + tall );
  378. break;
  379. case PROGRESS_WEST:
  380. surface()->DrawFilledRect( x + (int)( wide * ( 1.0f - _analogValue ) ), y, x + wide, y + tall );
  381. break;
  382. case PROGRESS_NORTH:
  383. surface()->DrawFilledRect( x, y + (int)( tall * ( 1.0f - _analogValue ) ), x + wide, y + tall );
  384. break;
  385. case PROGRESS_SOUTH:
  386. surface()->DrawFilledRect( x, y, x + wide, y + (int)( tall * _analogValue ) );
  387. break;
  388. }
  389. }