@@ -106,24 +106,40 @@ public bool IsLoading
106
106
get => isLoading ;
107
107
}
108
108
109
- private bool enableAnimation = false ;
110
- public bool EnableAnimation
109
+ private bool isPlaying = false ;
110
+ public bool IsPlaying
111
+ {
112
+ private set => SetValue ( ref isPlaying , value ) ;
113
+ get => isPlaying ;
114
+ }
115
+
116
+ private float startTime ;
117
+ public float StartTime
118
+ {
119
+ private set => SetValue ( ref startTime , value ) ;
120
+ get => startTime ;
121
+ }
122
+
123
+ private float endTime ;
124
+ public float EndTime
125
+ {
126
+ private set => SetValue ( ref endTime , value ) ;
127
+ get => endTime ;
128
+ }
129
+
130
+ private float currAnimationTime = 0 ;
131
+ public float CurrAnimationTime
111
132
{
112
133
set
113
134
{
114
- if ( SetValue ( ref enableAnimation , value ) )
135
+ if ( EndTime == 0 )
136
+ { return ; }
137
+ if ( SetValue ( ref currAnimationTime , value % EndTime + StartTime ) )
115
138
{
116
- if ( value )
117
- {
118
- StartAnimation ( ) ;
119
- }
120
- else
121
- {
122
- StopAnimation ( ) ;
123
- }
139
+ animationUpdater ? . Update ( value , 1 ) ;
124
140
}
125
141
}
126
- get { return enableAnimation ; }
142
+ get => currAnimationTime ;
127
143
}
128
144
129
145
public ObservableCollection < IAnimationUpdater > Animations { get ; } = new ObservableCollection < IAnimationUpdater > ( ) ;
@@ -138,20 +154,19 @@ public IAnimationUpdater SelectedAnimation
138
154
if ( SetValue ( ref selectedAnimation , value ) )
139
155
{
140
156
StopAnimation ( ) ;
157
+ CurrAnimationTime = 0 ;
141
158
if ( value != null )
142
159
{
143
160
animationUpdater = value ;
144
161
animationUpdater . Reset ( ) ;
145
162
animationUpdater . RepeatMode = AnimationRepeatMode . Loop ;
146
- animationUpdater . Speed = Speed ;
163
+ StartTime = value . StartTime ;
164
+ EndTime = value . EndTime ;
147
165
}
148
166
else
149
167
{
150
168
animationUpdater = null ;
151
- }
152
- if ( enableAnimation )
153
- {
154
- StartAnimation ( ) ;
169
+ StartTime = EndTime = 0 ;
155
170
}
156
171
}
157
172
}
@@ -166,18 +181,15 @@ public float Speed
166
181
{
167
182
set
168
183
{
169
- if ( SetValue ( ref speed , value ) )
170
- {
171
- if ( animationUpdater != null )
172
- animationUpdater . Speed = value ;
173
- }
184
+ SetValue ( ref speed , value ) ;
174
185
}
175
186
get => speed ;
176
187
}
188
+
177
189
private Point3D modelCentroid = default ;
178
190
public Point3D ModelCentroid
179
191
{
180
- private set => SetValue ( ref modelCentroid , value ) ;
192
+ private set => SetValue ( ref modelCentroid , value ) ;
181
193
get => modelCentroid ;
182
194
}
183
195
private BoundingBox modelBound = new BoundingBox ( ) ;
@@ -188,13 +200,16 @@ public BoundingBox ModelBound
188
200
}
189
201
public TextureModel EnvironmentMap { get ; }
190
202
203
+ public ICommand PlayCommand { get ; }
204
+
191
205
private SynchronizationContext context = SynchronizationContext . Current ;
192
206
private HelixToolkitScene scene ;
193
207
private IAnimationUpdater animationUpdater ;
194
208
private List < BoneSkinMeshNode > boneSkinNodes = new List < BoneSkinMeshNode > ( ) ;
195
209
private List < BoneSkinMeshNode > skeletonNodes = new List < BoneSkinMeshNode > ( ) ;
196
210
private CompositionTargetEx compositeHelper = new CompositionTargetEx ( ) ;
197
-
211
+ private long initTimeStamp = 0 ;
212
+
198
213
private MainWindow mainWindow = null ;
199
214
200
215
public MainViewModel ( MainWindow window )
@@ -223,6 +238,18 @@ public MainViewModel(MainWindow window)
223
238
CopyAsHiresBitmapCommand = new DelegateCommand ( ( ) => { CopyAsHiResBitmapToClipBoard ( mainWindow . view ) ; } ) ;
224
239
225
240
EnvironmentMap = TextureModel . Create ( "Cubemap_Grandcanyon.dds" ) ;
241
+
242
+ PlayCommand = new DelegateCommand ( ( ) =>
243
+ {
244
+ if ( ! IsPlaying && SelectedAnimation != null )
245
+ {
246
+ StartAnimation ( ) ;
247
+ }
248
+ else
249
+ {
250
+ StopAnimation ( ) ;
251
+ }
252
+ } ) ;
226
253
}
227
254
228
255
private void CopyAsBitmapToClipBoard ( Viewport3DX viewport )
@@ -299,8 +326,8 @@ private void OpenFile()
299
326
var oldNode = GroupModel . SceneNode . Items . ToArray ( ) ;
300
327
GroupModel . Clear ( false ) ;
301
328
Task . Run ( ( ) =>
302
- {
303
- foreach ( var node in oldNode )
329
+ {
330
+ foreach ( var node in oldNode )
304
331
{ node . Dispose ( ) ; }
305
332
} ) ;
306
333
if ( scene != null )
@@ -348,19 +375,23 @@ private void OpenFile()
348
375
349
376
public void StartAnimation ( )
350
377
{
378
+ initTimeStamp = Stopwatch . GetTimestamp ( ) ;
351
379
compositeHelper . Rendering += CompositeHelper_Rendering ;
380
+ IsPlaying = true ;
352
381
}
353
382
354
383
public void StopAnimation ( )
355
384
{
385
+ IsPlaying = false ;
356
386
compositeHelper . Rendering -= CompositeHelper_Rendering ;
357
387
}
358
388
359
389
private void CompositeHelper_Rendering ( object sender , System . Windows . Media . RenderingEventArgs e )
360
390
{
361
391
if ( animationUpdater != null )
362
392
{
363
- animationUpdater . Update ( Stopwatch . GetTimestamp ( ) , Stopwatch . Frequency ) ;
393
+ var elapsed = ( Stopwatch . GetTimestamp ( ) - initTimeStamp ) * speed ;
394
+ CurrAnimationTime = elapsed / Stopwatch . Frequency ;
364
395
}
365
396
}
366
397
0 commit comments