From 39ed87570bdb2f86969d4be821c94b722dc71179 Mon Sep 17 00:00:00 2001 From: Joe Ludwig Date: Wed, 26 Jun 2013 15:22:04 -0700 Subject: First version of the SOurce SDK 2013 --- mp/src/vgui2/vgui_controls/AnimatingImagePanel.cpp | 216 +++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 mp/src/vgui2/vgui_controls/AnimatingImagePanel.cpp (limited to 'mp/src/vgui2/vgui_controls/AnimatingImagePanel.cpp') diff --git a/mp/src/vgui2/vgui_controls/AnimatingImagePanel.cpp b/mp/src/vgui2/vgui_controls/AnimatingImagePanel.cpp new file mode 100644 index 00000000..564219a5 --- /dev/null +++ b/mp/src/vgui2/vgui_controls/AnimatingImagePanel.cpp @@ -0,0 +1,216 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +//=============================================================================// + +#include +#define PROTECTED_THINGS_DISABLE + +#include +#include +#include +#include +#include +#include + +#include + +// memdbgon must be the last include file in a .cpp file!!! +#include + +using namespace vgui; + +DECLARE_BUILD_FACTORY( AnimatingImagePanel ); + +//----------------------------------------------------------------------------- +// Purpose: Constructor +//----------------------------------------------------------------------------- +AnimatingImagePanel::AnimatingImagePanel(Panel *parent, const char *name) : Panel(parent, name) +{ + m_iCurrentImage = 0; + m_iFrameTimeMillis = 100; // 10Hz frame rate + m_iNextFrameTime = 0; + m_pImageName = NULL; + m_bFiltered = false; + m_bScaleImage = false; + m_bAnimating = false; + ivgui()->AddTickSignal(GetVPanel()); +} + +//----------------------------------------------------------------------------- +// Purpose: Layout the panel for drawing. +//----------------------------------------------------------------------------- +void AnimatingImagePanel::PerformLayout() +{ + Panel::PerformLayout(); + Repaint(); +} + +//----------------------------------------------------------------------------- +// Purpose: Add an image to the end of the list of animations +//----------------------------------------------------------------------------- +void AnimatingImagePanel::AddImage(IImage *image) +{ + m_Frames.AddToTail(image); + + if ( !m_bScaleImage && image != NULL ) + { + int wide,tall; + image->GetSize(wide,tall); + SetSize(wide,tall); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Load a set of animations by name. +// Input: +// baseName: is the name of the animations without their frame number or +// file extension, (e.g. c1.tga becomes just c.) +// framecount: number of frames in the animation +//----------------------------------------------------------------------------- +void AnimatingImagePanel::LoadAnimation(const char *baseName, int frameCount) +{ + m_Frames.RemoveAll(); + for (int i = 1; i <= frameCount; i++) + { + char imageName[512]; + Q_snprintf(imageName, sizeof( imageName ), "%s%d", baseName, i); + AddImage(scheme()->GetImage(imageName, m_bFiltered)); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Draw the current image +//----------------------------------------------------------------------------- +void AnimatingImagePanel::PaintBackground() +{ + if ( m_Frames.IsValidIndex( m_iCurrentImage ) && m_Frames[m_iCurrentImage] != NULL ) + { + IImage *pImage = m_Frames[m_iCurrentImage]; + + surface()->DrawSetColor( 255, 255, 255, 255 ); + pImage->SetPos(0, 0); + + if ( m_bScaleImage ) + { + // Image size is stored in the bitmap, so temporarily set its size + // to our panel size and then restore after we draw it. + + int imageWide, imageTall; + pImage->GetSize( imageWide, imageTall ); + + int wide, tall; + GetSize( wide, tall ); + pImage->SetSize( wide, tall ); + + pImage->SetColor( Color( 255,255,255,255 ) ); + pImage->Paint(); + + pImage->SetSize( imageWide, imageTall ); + } + else + { + pImage->Paint(); + } + } +} + +//----------------------------------------------------------------------------- +// Purpose: Called every frame the panel is visible +//----------------------------------------------------------------------------- +void AnimatingImagePanel::OnTick() +{ + if (m_bAnimating && system()->GetTimeMillis() >= m_iNextFrameTime) + { + m_iNextFrameTime = system()->GetTimeMillis() + m_iFrameTimeMillis; + m_iCurrentImage++; + if (!m_Frames.IsValidIndex(m_iCurrentImage)) + { + m_iCurrentImage = 0; + } + Repaint(); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Get control settings for editing +// Output: outResourceData- a set of keyvalues of imagenames. +//----------------------------------------------------------------------------- +void AnimatingImagePanel::GetSettings(KeyValues *outResourceData) +{ + BaseClass::GetSettings(outResourceData); + if (m_pImageName) + { + outResourceData->SetString("image", m_pImageName); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Applies resource settings +//----------------------------------------------------------------------------- +void AnimatingImagePanel::ApplySettings(KeyValues *inResourceData) +{ + BaseClass::ApplySettings(inResourceData); + + const char *imageName = inResourceData->GetString("image", NULL); + if (imageName) + { + m_bScaleImage = ( inResourceData->GetInt( "scaleImage", 0 ) == 1 ); + + delete [] m_pImageName; + int len = Q_strlen(imageName) + 1; + m_pImageName = new char[len]; + Q_strncpy(m_pImageName, imageName, len); + + // add in the command + LoadAnimation(m_pImageName, inResourceData->GetInt("frames")); + } + + m_iFrameTimeMillis = inResourceData->GetInt( "anim_framerate", 100 ); +} + +//----------------------------------------------------------------------------- +// Purpose: Get editing details +//----------------------------------------------------------------------------- +const char *AnimatingImagePanel::GetDescription() +{ + static char buf[1024]; + Q_snprintf(buf, sizeof(buf), "%s, string image", BaseClass::GetDescription()); + return buf; +} + +//----------------------------------------------------------------------------- +// Purpose: Starts the image doing its animation +//----------------------------------------------------------------------------- +void AnimatingImagePanel::StartAnimation() +{ + m_bAnimating = true; +// ivgui()->AddTickSignal(GetVPanel()); +} + +//----------------------------------------------------------------------------- +// Purpose: Stops the images animation +//----------------------------------------------------------------------------- +void AnimatingImagePanel::StopAnimation() +{ + m_bAnimating = false; +// ivgui()->RemoveTickSignal(GetVPanel()); +} + +//----------------------------------------------------------------------------- +// Purpose: Resets the animation to the start of the sequence. +//----------------------------------------------------------------------------- +void AnimatingImagePanel::ResetAnimation(int frame) +{ + if(m_Frames.IsValidIndex(frame)) + { + m_iCurrentImage = frame; + } + else + { + m_iCurrentImage = 0; + } + Repaint(); +} -- cgit v1.2.3