1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <stdio.h>
#define PROTECTED_THINGS_DISABLE
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/IImage.h>
#include <vgui/IVGui.h>
#include <KeyValues.h>
#include <vgui_controls/AnimatingImagePanel.h>
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
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();
}
|