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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <stdafx.h>
#include "GlobalFunctions.h"
#include "History.h"
#include "MainFrm.h"
#include "MapAnimator.h"
#include "MapAnimationDlg.h"
#include "MapClass.h"
#include "MapDoc.h"
#include "MapEntity.h"
#include "MapWorld.h"
#include "hammer.h"
#include "Selection.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
BEGIN_MESSAGE_MAP( CMapAnimationDlg, CHammerBar )
//{{AFX_MSG_MAP( CMapAnimationDlg )
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_ANIMATIONPLAY, OnPlay)
ON_BN_CLICKED(IDC_ANIMATIONCREATEKEYFRAME, OnCreateKeyFrame)
ON_UPDATE_COMMAND_UI(IDC_ANIMATIONPLAY, UpdateControl)
ON_UPDATE_COMMAND_UI(IDC_ANIMATIONCREATEKEYFRAME, UpdateControl)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
// Purpose: CMapAnimationDlg contructor
//-----------------------------------------------------------------------------
CMapAnimationDlg::CMapAnimationDlg()
{
m_flAnimationDuration = 5.0f;
m_flAnimationStart = 0.0f;
m_flAnimTime = 0.0f;
m_bPlaying = false;
}
static const int ANIMSLIDER_NUMTICS = 100;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CMapAnimationDlg::Create( CWnd *pParentWnd )
{
//
// create a modeless dialog toolbar
//
if( !( CHammerBar::Create( pParentWnd, IDD, CBRS_RIGHT, IDCB_ANIMATIONBAR ) ) )
{
return false;
}
// to remain consistant with the other toolbars in the editor
SetWindowText( _T( "Animation" ) );
// set dialog bar style
SetBarStyle( GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED );
// enable docking
EnableDocking( CBRS_ALIGN_ANY );
//
// initialize the dialog items
//
InitTimeSlider();
m_Play.SubclassDlgItem( IDC_ANIMATIONPLAY, this );
// show the dialog
ShowWindow( SW_SHOW );
m_bEnabled = false;
// created successfully
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Called every frame, used to update animation time
//-----------------------------------------------------------------------------
void CMapAnimationDlg::RunFrame( void )
{
if ( m_bPlaying )
{
AdvanceAnimationTime();
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CMapAnimationDlg::InitTimeSlider( void )
{
m_TimeSlider.SubclassDlgItem( IDC_TIMESLIDER, this );
m_TimeSlider.SetRange( 0, ANIMSLIDER_NUMTICS );
m_TimeSlider.SetTicFreq( ANIMSLIDER_NUMTICS / 4 );
m_TimeSlider.SetPos( 0 );
m_TimeSlider.EnableWindow( false );
}
//-----------------------------------------------------------------------------
// Purpose: Sets Enable/Disable state for any controls
// Input : *pCmdUI -
//-----------------------------------------------------------------------------
void CMapAnimationDlg::UpdateControl( CCmdUI *pCmdUI )
{
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if ( !pDoc || !m_bEnabled )
{
pCmdUI->Enable( false );
return;
}
else
{
pCmdUI->Enable( true );
}
}
//-----------------------------------------------------------------------------
// Purpose: Communicates to the doc the current animation time
// Input : time -
//-----------------------------------------------------------------------------
void CMapAnimationDlg::UpdateAnimationTime( void )
{
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if( !pDoc )
{
return;
}
pDoc->SetAnimationTime( m_flAnimTime );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CMapAnimationDlg::OnHScroll( UINT nSBCode, UINT nPos, CScrollBar *pScrollBar )
{
// get the new time from the slider bar
m_flAnimTime = ((float)m_TimeSlider.GetPos() / ANIMSLIDER_NUMTICS) * m_flAnimationDuration;
// stop any playback
PausePlayback();
UpdateAnimationTime();
CHammerBar::OnHScroll( nSBCode, nPos, pScrollBar );
}
//-----------------------------------------------------------------------------
// Purpose: Moves the animation time forward with real time
//-----------------------------------------------------------------------------
void CMapAnimationDlg::OnPlay( void )
{
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if ( !pDoc )
{
return;
}
// if we're not playing, start
if ( !m_bPlaying )
{
m_flAnimationStart = pDoc->GetTime() - m_flAnimTime;
m_bPlaying = true;
// change the animation text
SetDlgItemText( IDC_ANIMATIONPLAY, "Stop" );
UpdateAnimationTime();
}
else
{
PausePlayback();
}
}
//-----------------------------------------------------------------------------
// Purpose: pauses the animation playback at the current time
//-----------------------------------------------------------------------------
void CMapAnimationDlg::PausePlayback( void )
{
m_bPlaying = false;
SetDlgItemText( IDC_ANIMATIONPLAY, "Play" );
}
//-----------------------------------------------------------------------------
// Purpose: Creates a new keyframe in the cycle at the current time in the animation
//-----------------------------------------------------------------------------
void CMapAnimationDlg::OnCreateKeyFrame( void )
{
// stop any playback
PausePlayback();
GetHistory()->MarkUndoPosition( NULL, "New Keyframe" );
// get the animating object
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
const CMapObjectList *pSelection = pDoc->GetSelection()->GetList();
for (int i = 0; i < pSelection->Count(); i++)
{
CMapEntity *ent = dynamic_cast<CMapEntity*>( pSelection->Element( i ) );
if ( ent && ent->IsAnimationController() )
{
// tell the animating object to create a new keyframe
CMapAnimator *anim = ent->GetChildOfType( (CMapAnimator*)NULL );
if ( anim )
{
CMapEntity *pNewEntity = anim->CreateNewKeyFrame( m_flAnimTime );
CMapDoc::GetActiveMapDoc()->AddObjectToWorld( pNewEntity );
GetHistory()->KeepNew( pNewEntity );
// change the selection and then update the view
CMapDoc::GetActiveMapDoc()->SelectObject(pNewEntity, scClear|scSaveChanges );
break;
}
}
}
ResetTimeSlider();
}
//-----------------------------------------------------------------------------
// Purpose: moves the current animation time forward, if currently playing
//-----------------------------------------------------------------------------
void CMapAnimationDlg::AdvanceAnimationTime( void )
{
if ( !m_bPlaying )
return;
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if ( !pDoc )
{
return;
}
// make sure the animation is long enough to play
if ( m_flAnimationDuration <= 0.01 )
{
ResetTimeSlider();
return;
}
// calculate the new position along the time slider
m_flAnimTime = pDoc->GetTime() - m_flAnimationStart;
// check to see if we've hit the end of the animation
if ( m_flAnimTime >= m_flAnimationDuration )
{
ResetTimeSlider();
return;
}
// set the new animtion time
m_TimeSlider.SetPos( (m_flAnimTime / m_flAnimationDuration) * ANIMSLIDER_NUMTICS );
UpdateAnimationTime();
}
//-----------------------------------------------------------------------------
// Purpose: Resets the slider bar and all times
//-----------------------------------------------------------------------------
void CMapAnimationDlg::ResetTimeSlider( void )
{
PausePlayback();
m_flAnimTime = 0.0f;
m_flAnimationStart = 0.0f;
m_TimeSlider.SetPos( 0 );
UpdateAnimationTime();
}
//-----------------------------------------------------------------------------
// Purpose: Called whenever the selection changes, so the slider bar can update
// with the selected keyframe info
// Input : &selection -
//-----------------------------------------------------------------------------
void CMapAnimationDlg::SelectionChanged( CMapObjectList &selection )
{
// reset the slider
ResetTimeSlider();
m_bEnabled = false;
// loop through the selection looking for potential animating objects
CMapEntity *ent = NULL;
FOR_EACH_OBJ( selection, pos )
{
ent = dynamic_cast<CMapEntity*>( selection.Element(pos) );
if ( ent )
{
if ( ent->IsAnimationController() && ent->GetChildOfType((CMapAnimator*)NULL) )
{
m_bEnabled = true;
break;
}
}
}
// find out our enabled state
if ( !m_bEnabled )
{
m_TimeSlider.EnableWindow( false );
return;
}
m_TimeSlider.EnableWindow( true );
// set up the slider from the selection
CMapAnimator *anim = ent->GetChildOfType( (CMapAnimator*)NULL );
Assert( anim != NULL );
m_flAnimationDuration = anim->GetRemainingTime();
}
|