-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSprite.razor.cs
1245 lines (1092 loc) · 41.5 KB
/
Sprite.razor.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataJuggler.UltimateHelper;
using System.Timers;
using Microsoft.AspNetCore.Components;
using DataJuggler.Blazor.Components.Enumerations;
using DataJuggler.Blazor.Components.Interfaces;
#endregion
namespace DataJuggler.Blazor.Components
{
#region class Sprite
/// <summary>
/// This class is the code behind for the Sprite component
/// </summary>
public partial class Sprite : IBlazorComponent
{
#region Private Variables
private Timer timer;
private double xIncrement;
private double yIncrement;
private double xPosition;
private double yPosition;
private string xPositionStyle;
private string yPositionStyle;
private double rotation;
private double rotationIncrement;
private string rotationStyle;
private int interval;
private bool started;
private string display;
private bool visible;
private double height;
private double width;
private string widthStyle;
private string heightStyle;
private string name;
private bool notificaitonInProgress;
private string imageUrl;
private int zIndex;
private double scale;
private string position;
private ISpriteSubscriber subscriber;
private double opacity;
private bool applyTransforms;
private int currentTick;
private bool completed;
private bool stopOnCompleted;
private bool busy;
private List<Transform> transforms;
private IBlazorComponentParent parent;
private string spriteStyle;
#endregion
#region Constructor
/// <summary>
/// Create a new instance of a Sprite object
/// </summary>
public Sprite()
{
// Perform initializations for this object
Init();
}
#endregion
#region Events
#region Timer_Elapsed(object sender, ElapsedEventArgs e)
/// <summary>
/// event is fired when Timer _ Elapsed
/// </summary>
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
// avoid duplicate processing in case it is too fast, but mostly for
// debugging so it single process at a time
if (!Busy)
{
// Set Busy to true
Busy = true;
try
{
// Increment the value for CurrentTick
CurrentTick++;
// locals
AdjustmentResult result = null;
List<Transform> nextTransforms = new List<Transform>();
// if the Transforms exists
if ((ApplyTransforms) && (HasTransforms))
{
// Iterate the collection of Transform objects
foreach (Transform transform in Transforms)
{
// if this transform has not completed
if (!transform.Completed)
{
// if there are one or more Adjustments
if (ListHelper.HasOneOrMoreItems(transform.Adjustments))
{
// iterate the Adjustments
foreach (Adjustment adjustment in transform.Adjustments)
{
// Apply this adjustment
result = ApplyAdjustment(adjustment);
if (result.Status == AdjustmentStatusEnum.AppliedAndCompleted)
{
// Set the value
adjustment.Completed = true;
// Check all the other adjustments
transform.Completed = transform.CheckCompleted();
}
}
}
// if the transform has finished and the NextTransforms collection exists and has one or more items
if ((transform.Completed) && (ListHelper.HasOneOrMoreItems(transform.NextTransforms)))
{
// iterate the collection of Transform objects
foreach (Transform nextTransform in transform.NextTransforms)
{
// Add this item
nextTransforms.Add(nextTransform);
}
}
}
}
// if any new Transforms just got created
if (ListHelper.HasOneOrMoreItems(nextTransforms))
{
// iterate the collection of Transform objects
foreach (Transform nextTransform in nextTransforms)
{
// Add this nextTransform
this.Transforms.Add(nextTransform);
}
}
// Check if all the Transforms have completed
this.Completed = CheckCompleted();
// if this Sprite has completed all the transforms and StopOnCompleted is true
if (Completed && StopOnCompleted)
{
// Stop the timer
Stop();
}
}
// if a subscriber
if (HasSubscriber)
{
// Notify Subscriber
Subscriber.Refresh();
}
}
catch (Exception error)
{
// for debugging only for now
DebugHelper.WriteDebugError("Timer_Elapsed", "Sprite.razor.cs", error);
}
finally
{
// no longer busy
Busy = false;
}
}
}
#endregion
#endregion
#region Methods
#region ApplyAdjustment(Adjustment adjustment)
/// <summary>
/// Apply Adjustment
/// </summary>
public AdjustmentResult ApplyAdjustment(Adjustment adjustment)
{
// local
AdjustmentResult result = null;
// If the adjustment object exists
if (NullHelper.Exists(adjustment))
{
// determine the action by the AdjustmentType
switch (adjustment.AdjustmentType)
{
case AdjustmentTypeEnum.X:
// Attempt to apply the Adjustment
result = ApplyXAdjustment(adjustment);
// required
break;
case AdjustmentTypeEnum.Y:
// Attempt to apply the Adjustment
result = ApplyYAdjustment(adjustment);
// required
break;
case AdjustmentTypeEnum.Opacity:
// Attempt to apply the Adjustment
result = ApplyOpacityAdjustment(adjustment);
// required
break;
case AdjustmentTypeEnum.Rotation:
// Attempt to apply the Adjustment
result = ApplyRotationAdjustment(adjustment);
// required
break;
}
// if the Adjustment just reached its max
if (adjustment.IsIncrement && adjustment.EndOnMax && result.MaximumSet)
{
// This adjustment just completed
adjustment.Completed = true;
}
else if (!adjustment.IsIncrement && adjustment.EndOnMin && result.MinimumSet)
{
// This adjustment just completed
adjustment.Completed = true;
}
if (adjustment.Completed)
{
// this result has been completed
result.Status = AdjustmentStatusEnum.AppliedAndCompleted;
}
}
// return value
return result;
}
#endregion
#region ApplyOpacityAdjustment(Adjustment adjustment)
/// <summary>
/// returns the Opacity Adjustment
/// </summary>
public AdjustmentResult ApplyOpacityAdjustment(Adjustment adjustment)
{
// initial value
AdjustmentResult result = new AdjustmentResult();
// local
double temp = 0;
// Safeguards
if ((NullHelper.Exists(result)) && (adjustment.AdjustmentType == AdjustmentTypeEnum.Opacity))
{
// if the adjustmentAmount is a positive number
if (adjustment.IsIncrement)
{
// increment
// Get a temp value of what the result will be if applied
temp = Opacity + adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp < adjustment.Max)
{
// Set the value
Opacity = temp;
}
else
{
// The Maximum has been reached
result.MaximumSet = true;
// Set the value
Opacity = adjustment.Max;
}
}
else
{
// decrement
// Get a temp value of what the result will be if applied
temp = Opacity - adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp > adjustment.Min)
{
// Set the value
Opacity = temp;
}
else
{
// The Minimum has been reached
result.MinimumSet = true;
// Set the value
Opacity = adjustment.Min;
}
}
// Set the new value
result.NewValue = Opacity;
}
// return value
return result;
}
#endregion
#region ApplyRotationAdjustment(Adjustment adjustment)
/// <summary>
/// returns the Rotation Adjustment
/// </summary>
public AdjustmentResult ApplyRotationAdjustment(Adjustment adjustment)
{
// initial value
AdjustmentResult result = new AdjustmentResult();
// local
double temp = 0;
// Safeguards
if ((NullHelper.Exists(result)) && (adjustment.AdjustmentType == AdjustmentTypeEnum.Rotation))
{
// if the adjustmentAmount is a positive number
if (adjustment.IsIncrement)
{
// increment
// Get a temp value of what the result will be if applied
temp = Rotation + adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp < adjustment.Max)
{
// Set the value
Rotation = temp;
}
else
{
// The Maximum has been reached
result.MaximumSet = true;
// Set the value
Rotation = adjustment.Max;
}
}
else
{
// decrement
// Get a temp value of what the result will be if applied
temp = Rotation - adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp > adjustment.Min)
{
// Set the value
Rotation = temp;
}
else
{
// The Minimum has been reached
result.MinimumSet = true;
// Set the value
Rotation = adjustment.Min;
}
}
// Set the new value
result.NewValue = Rotation;
}
// return value
return result;
}
#endregion
#region ApplyXAdjustment(Adjustment adjustment)
/// <summary>
/// returns the X Adjustment
/// </summary>
public AdjustmentResult ApplyXAdjustment(Adjustment adjustment)
{
// initial value
AdjustmentResult result = new AdjustmentResult();
// local
double temp = 0;
// Safeguards
if ((NullHelper.Exists(result)) && (adjustment.AdjustmentType == AdjustmentTypeEnum.X))
{
// if the adjustmentAmount is a positive number
if (adjustment.IsIncrement)
{
// increment
// Get a temp value of what the result will be if applied
temp = XPosition + adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp < adjustment.Max)
{
// Set the value
XPosition = temp;
}
else
{
// The Maximum has been reached
result.MaximumSet = true;
// Set the value
XPosition = adjustment.Max;
}
}
else
{
// decrement
// Get a temp value of what the result will be if applied
temp = XPosition - adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp > adjustment.Min)
{
// Set the value
XPosition = temp;
}
else
{
// The Minimum has been reached
result.MinimumSet = true;
// Set the value
XPosition = adjustment.Min;
}
// Was applied
result.Status = AdjustmentStatusEnum.Applied;
}
// Set the new value
result.NewValue = XPosition;
}
// return value
return result;
}
#endregion
#region ApplyYAdjustment(Adjustment adjustment)
/// <summary>
/// returns the Y Adjustment
/// </summary>
public AdjustmentResult ApplyYAdjustment(Adjustment adjustment)
{
// initial value
AdjustmentResult result = new AdjustmentResult();
// local
double temp = 0;
// Safeguards
if ((NullHelper.Exists(result)) && (adjustment.AdjustmentType == AdjustmentTypeEnum.Y))
{
// if the adjustmentAmount is a positive number
if (adjustment.IsIncrement)
{
// increment
// Get a temp value of what the result will be if applied
temp = YPosition + adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp < adjustment.Max)
{
// Set the value
YPosition = temp;
}
else
{
// The Maximum has been reached
result.MaximumSet = true;
// Set the value
YPosition = adjustment.Max;
}
}
else
{
// decrement
// Get a temp value of what the result will be if applied
temp = YPosition - adjustment.AdjustmentAmount;
// if the newValue will be below Max
if (temp > adjustment.Min)
{
// Set the value
YPosition = temp;
}
else
{
// The Minimum has been reached
result.MinimumSet = true;
// Set the value
YPosition = adjustment.Min;
}
}
// Set the new value
result.NewValue = YPosition;
}
// return value
return result;
}
#endregion
#region CheckCompleted()
/// <summary>
/// This method checks if all the Transforms have completed.
/// If yes, then the Timer is turned off if EndOnComplete is true.
/// </summary>
public bool CheckCompleted()
{
// initial value
bool completed = false;
// return value
return completed;
}
#endregion
#region Init()
/// <summary>
/// This method performs initializations for this object.
/// </summary>
public void Init()
{
// Defaults
XPosition = 0;
YPosition = 0;
Scale = 1;
Visible = true;
Interval = 100;
Display = "block";
Opacity = 1;
// In front of a background in theory
ZIndex = 1;
}
#endregion
#region ReceiveData(Message message)
/// <summary>
/// method returns the Data
/// </summary>
public void ReceiveData(Message message)
{
throw new NotImplementedException();
}
#endregion
#region Refresh()
/// <summary>
/// method Refresh
/// </summary>
public void Refresh()
{
// Update the UI
InvokeAsync(() =>
{
StateHasChanged();
});
}
#endregion
#region SetRotationIncrement(double rotationIncrement)
/// <summary>
/// Set Y Increment
/// </summary>
public void SetRotationIncrement(double rotationIncrement)
{
// store
RotationIncrement = rotationIncrement;
}
#endregion
#region SetVisible(bool visible)
/// <summary>
/// Set Visible
/// </summary>
public void SetVisible(bool visible)
{
// set the value
Visible = visible;
// update the UI
Refresh();
}
#endregion
#region SetXIncrement(double xIncrement)
/// <summary>
/// Set X Increment
/// </summary>
public void SetXIncrement(double xIncrement)
{
// store
XIncrement = xIncrement;
}
#endregion
#region SetYIncrement(double yIncrement)
/// <summary>
/// Set Y Increment
/// </summary>
public void SetYIncrement(double yIncrement)
{
// store
YIncrement = yIncrement;
}
#endregion
#region Start()
/// <summary>
/// method Start
/// </summary>
public void Start()
{
this.Timer = new Timer();
this.Timer.Interval = this.interval;
this.Timer.Elapsed += Timer_Elapsed;
this.Timer.Start();
}
#endregion
#region Stop(bool hide = true)
/// <summary>
/// method Stops the Timer
/// </summary>
public void Stop(bool hide = true)
{
// if the value for HasTimer is true
if (HasTimer)
{
// Stop the Timer
Timer.Stop();
// if the value for hide is true
if (hide)
{
// hide
Visible = false;
}
}
}
#endregion
#endregion
#region Properties
#region ApplyTransforms
/// <summary>
/// This property gets or sets the value for 'ApplyTransforms'.
/// If true, the X, Y and Rotation increments are applied on a tick.
/// </summary>
public bool ApplyTransforms
{
get { return applyTransforms; }
set { applyTransforms = value; }
}
#endregion
#region Busy
/// <summary>
/// This property gets or sets the value for 'Busy'.
/// </summary>
public bool Busy
{
get { return busy; }
set { busy = value; }
}
#endregion
#region Completed
/// <summary>
/// This property gets or sets the value for 'Completed'.
/// </summary>
public bool Completed
{
get { return completed; }
set { completed = value; }
}
#endregion
#region CurrentTick
/// <summary>
/// This property gets or sets the value for 'CurrentTick'.
/// </summary>
public int CurrentTick
{
get { return currentTick; }
set { currentTick = value; }
}
#endregion
#region Display
/// <summary>
/// This property gets or sets the value for 'Display'.
/// </summary>
public string Display
{
get { return display; }
set { display = value; }
}
#endregion
#region HasParent
/// <summary>
/// This property returns true if this object has a 'Parent'.
/// </summary>
public bool HasParent
{
get
{
// initial value
bool hasParent = (this.Parent != null);
// return value
return hasParent;
}
}
#endregion
#region HasSubscriber
/// <summary>
/// This property returns true if this object has a 'Subscriber'.
/// </summary>
public bool HasSubscriber
{
get
{
// initial value
bool hasSubscriber = (this.Subscriber != null);
// return value
return hasSubscriber;
}
}
#endregion
#region HasTimer
/// <summary>
/// This property returns true if this object has a 'Timer'.
/// </summary>
public bool HasTimer
{
get
{
// initial value
bool hasTimer = (this.Timer != null);
// return value
return hasTimer;
}
}
#endregion
#region HasTransforms
/// <summary>
/// This property returns true if this object has a 'Transforms'.
/// </summary>
public bool HasTransforms
{
get
{
// initial value
bool hasTransforms = (this.Transforms != null);
// return value
return hasTransforms;
}
}
#endregion
#region Height
/// <summary>
/// This property gets or sets the value for 'Height'.
/// </summary>
[Parameter]
public double Height
{
get { return height; }
set
{
// set the value
height = value;
// Set the value of HeightStyle
HeightStyle = height.ToString() + "vh";
}
}
#endregion
#region HeightStyle
/// <summary>
/// This property gets or sets the value for 'HeightStyle'.
/// </summary>
public string HeightStyle
{
get { return heightStyle; }
set { heightStyle = value; }
}
#endregion
#region ImageUrl
/// <summary>
/// This property gets or sets the value for 'ImageUrl'.
/// </summary>
[Parameter]
public string ImageUrl
{
get { return imageUrl; }
set { imageUrl = value; }
}
#endregion
#region Interval
/// <summary>
/// This property gets or sets the value for 'Interval'.
/// </summary>
[Parameter]
public int Interval
{
get { return interval; }
set { interval = value; }
}
#endregion
#region Name
/// <summary>
/// This property gets or sets the value for 'Name'.
/// </summary>
[Parameter]
public string Name
{
get { return name; }
set { name = value; }
}
#endregion
#region NotificaitonInProgress
/// <summary>
/// This property gets or sets the value for 'NotificaitonInProgress'.
/// </summary>
public bool NotificaitonInProgress
{
get { return notificaitonInProgress; }
set { notificaitonInProgress = value; }
}
#endregion
#region Opacity
/// <summary>
/// This property gets or sets the value for 'Opacity'.
/// The values are 1 fully visible to 0 transparent.
/// </summary>
[Parameter]
public double Opacity
{
get { return opacity; }
set
{
// if greater than 1
if (value > 1)
{
// set to 1
value = 1;
}
else if (value < 0)
{
// set to 0
value = 0;
}
opacity = value;
}
}
#endregion
#region Parent
/// <summary>
/// This property gets or sets the value for 'Parent'.
/// </summary>
[Parameter]
public IBlazorComponentParent Parent
{
get { return parent; }
set
{
// set the value
parent = value;
// if the value for HasParent is true
if (HasParent)
{
// Register with the parent
Parent.Register(this);
}
}
}
#endregion
#region Position
/// <summary>
/// This property gets or sets the value for 'Position'.
/// </summary>
[Parameter]
public string Position
{
get { return position; }
set { position = value; }
}
#endregion
#region Rotation
/// <summary>
/// This property gets or sets the value for 'Rotation'.
/// </summary>
public double Rotation
{
get { return rotation; }
set
{
// make sure the range doesn't exceed range of a circle
value = NumericHelper.EnsureInRange(value, 0, 360);
// set the value
rotation = value;
// set the headerStyle
RotationStyle = rotationStyle + "deg";
}
}
#endregion
#region RotationIncrement
/// <summary>
/// This property gets or sets the value for 'RotationIncrement'.
/// </summary>
[Parameter]
public double RotationIncrement
{
get { return rotationIncrement; }
set { rotationIncrement = value; }
}
#endregion
#region RotationStyle
/// <summary>
/// This property gets or sets the value for 'RotationStyle'.
/// </summary>
public string RotationStyle
{
get { return rotationStyle; }
set { rotationStyle = value; }
}
#endregion
#region Scale
/// <summary>
/// This property gets or sets the value for 'Scale'.
/// </summary>
[Parameter]