Team Fortress 2 Source Code as on 22/4/2020
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.

216 lines
6.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #define PROTECTED_THINGS_DISABLE
  9. #include <vgui/IScheme.h>
  10. #include <vgui/ISurface.h>
  11. #include <vgui/ISystem.h>
  12. #include <vgui/IImage.h>
  13. #include <vgui/IVGui.h>
  14. #include <KeyValues.h>
  15. #include <vgui_controls/AnimatingImagePanel.h>
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. using namespace vgui;
  19. DECLARE_BUILD_FACTORY( AnimatingImagePanel );
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Constructor
  22. //-----------------------------------------------------------------------------
  23. AnimatingImagePanel::AnimatingImagePanel(Panel *parent, const char *name) : Panel(parent, name)
  24. {
  25. m_iCurrentImage = 0;
  26. m_iFrameTimeMillis = 100; // 10Hz frame rate
  27. m_iNextFrameTime = 0;
  28. m_pImageName = NULL;
  29. m_bFiltered = false;
  30. m_bScaleImage = false;
  31. m_bAnimating = false;
  32. ivgui()->AddTickSignal(GetVPanel());
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Purpose: Layout the panel for drawing.
  36. //-----------------------------------------------------------------------------
  37. void AnimatingImagePanel::PerformLayout()
  38. {
  39. Panel::PerformLayout();
  40. Repaint();
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Add an image to the end of the list of animations
  44. //-----------------------------------------------------------------------------
  45. void AnimatingImagePanel::AddImage(IImage *image)
  46. {
  47. m_Frames.AddToTail(image);
  48. if ( !m_bScaleImage && image != NULL )
  49. {
  50. int wide,tall;
  51. image->GetSize(wide,tall);
  52. SetSize(wide,tall);
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose: Load a set of animations by name.
  57. // Input:
  58. // baseName: is the name of the animations without their frame number or
  59. // file extension, (e.g. c1.tga becomes just c.)
  60. // framecount: number of frames in the animation
  61. //-----------------------------------------------------------------------------
  62. void AnimatingImagePanel::LoadAnimation(const char *baseName, int frameCount)
  63. {
  64. m_Frames.RemoveAll();
  65. for (int i = 1; i <= frameCount; i++)
  66. {
  67. char imageName[512];
  68. Q_snprintf(imageName, sizeof( imageName ), "%s%d", baseName, i);
  69. AddImage(scheme()->GetImage(imageName, m_bFiltered));
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose: Draw the current image
  74. //-----------------------------------------------------------------------------
  75. void AnimatingImagePanel::PaintBackground()
  76. {
  77. if ( m_Frames.IsValidIndex( m_iCurrentImage ) && m_Frames[m_iCurrentImage] != NULL )
  78. {
  79. IImage *pImage = m_Frames[m_iCurrentImage];
  80. surface()->DrawSetColor( 255, 255, 255, 255 );
  81. pImage->SetPos(0, 0);
  82. if ( m_bScaleImage )
  83. {
  84. // Image size is stored in the bitmap, so temporarily set its size
  85. // to our panel size and then restore after we draw it.
  86. int imageWide, imageTall;
  87. pImage->GetSize( imageWide, imageTall );
  88. int wide, tall;
  89. GetSize( wide, tall );
  90. pImage->SetSize( wide, tall );
  91. pImage->SetColor( Color( 255,255,255,255 ) );
  92. pImage->Paint();
  93. pImage->SetSize( imageWide, imageTall );
  94. }
  95. else
  96. {
  97. pImage->Paint();
  98. }
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose: Called every frame the panel is visible
  103. //-----------------------------------------------------------------------------
  104. void AnimatingImagePanel::OnTick()
  105. {
  106. if (m_bAnimating && system()->GetTimeMillis() >= m_iNextFrameTime)
  107. {
  108. m_iNextFrameTime = system()->GetTimeMillis() + m_iFrameTimeMillis;
  109. m_iCurrentImage++;
  110. if (!m_Frames.IsValidIndex(m_iCurrentImage))
  111. {
  112. m_iCurrentImage = 0;
  113. }
  114. Repaint();
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose: Get control settings for editing
  119. // Output: outResourceData- a set of keyvalues of imagenames.
  120. //-----------------------------------------------------------------------------
  121. void AnimatingImagePanel::GetSettings(KeyValues *outResourceData)
  122. {
  123. BaseClass::GetSettings(outResourceData);
  124. if (m_pImageName)
  125. {
  126. outResourceData->SetString("image", m_pImageName);
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose: Applies resource settings
  131. //-----------------------------------------------------------------------------
  132. void AnimatingImagePanel::ApplySettings(KeyValues *inResourceData)
  133. {
  134. BaseClass::ApplySettings(inResourceData);
  135. const char *imageName = inResourceData->GetString("image", NULL);
  136. if (imageName)
  137. {
  138. m_bScaleImage = ( inResourceData->GetInt( "scaleImage", 0 ) == 1 );
  139. delete [] m_pImageName;
  140. int len = Q_strlen(imageName) + 1;
  141. m_pImageName = new char[len];
  142. Q_strncpy(m_pImageName, imageName, len);
  143. // add in the command
  144. LoadAnimation(m_pImageName, inResourceData->GetInt("frames"));
  145. }
  146. m_iFrameTimeMillis = inResourceData->GetInt( "anim_framerate", 100 );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose: Get editing details
  150. //-----------------------------------------------------------------------------
  151. const char *AnimatingImagePanel::GetDescription()
  152. {
  153. static char buf[1024];
  154. Q_snprintf(buf, sizeof(buf), "%s, string image", BaseClass::GetDescription());
  155. return buf;
  156. }
  157. //-----------------------------------------------------------------------------
  158. // Purpose: Starts the image doing its animation
  159. //-----------------------------------------------------------------------------
  160. void AnimatingImagePanel::StartAnimation()
  161. {
  162. m_bAnimating = true;
  163. // ivgui()->AddTickSignal(GetVPanel());
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose: Stops the images animation
  167. //-----------------------------------------------------------------------------
  168. void AnimatingImagePanel::StopAnimation()
  169. {
  170. m_bAnimating = false;
  171. // ivgui()->RemoveTickSignal(GetVPanel());
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose: Resets the animation to the start of the sequence.
  175. //-----------------------------------------------------------------------------
  176. void AnimatingImagePanel::ResetAnimation(int frame)
  177. {
  178. if(m_Frames.IsValidIndex(frame))
  179. {
  180. m_iCurrentImage = frame;
  181. }
  182. else
  183. {
  184. m_iCurrentImage = 0;
  185. }
  186. Repaint();
  187. }