-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathse.game.sprite.pas
1630 lines (1487 loc) · 43.9 KB
/
se.game.sprite.pas
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
{******************************************************************************}
{ }
{ SE Network Development Framework }
{ }
{ Copyright (c) 2018 looper(2031056602@qq.com) }
{ }
{ Source: https://github.com/looper/se-framework }
{ Homepage: http://www.asphyre.cn }
{ }
{ Original author in Asphyre : DraculaLin (2014) }
{ Modified version in PXL : looper (2018) }
{ }
{******************************************************************************}
unit se.game.sprite;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.Math,
System.Types, System.UITypes, System.Generics.Defaults,
FMX.Types, FMX.Forms,
PXL.Types, se.utils.client,
se.game.types, se.game.helper, se.game.assetsmanager,
se.game.font.types, se.game.font, se.game.font.classes;
type
TSpriteManager = class;
TCustomSpriteClass = class of TCustomSprite;
TCustomSprite = class
private
FParent: TCustomSprite;
FChildList: TList<TCustomSprite>;
FDeaded: Boolean;
FMargins: TBounds;
FAlign: TAlignMode;
procedure DoMarginsChanged(Sender: TObject);
procedure DoLayoutChanged;
private
FName: string;
FImage: TEngineImage;
FWidth, FHeight, FZOrder, FTag, FPatternIndex: Integer;
FX, FY, FDrawX, FDrawY: Single;
FBlendMode: TEngineBlendingEffect;
FMoveable, FTruncDraw, FVisible: Boolean;
FOffset: TPoint2f;
FCollidePos: TPoint2f;
FCollideRadius: Integer;
FCollideRect: TFloatRect;
FCollideQuadrangle: TQuad;
FCollidePolygon: TPolygon;
FCollideMode: TCollideMode;
FCollisioned: Boolean;
FCanFocus, FFocused, FHitTest: Boolean;
FFixedLayout: Boolean;
FGroupName: string;
procedure SetParent(const Value: TCustomSprite);
procedure SetImage(const Value: TEngineImage);
procedure SetX(const Value: Single);
procedure SetY(const Value: Single);
procedure SetHeight(const Value: Integer);
procedure SetWidth(const Value: Integer);
procedure SetZOrder(const Value: Integer);
procedure SetAlign(const Value: TAlignMode);
procedure SetGroupName(const Value: string);
function GetWorldX: Single;
function GetWorldY: Single;
function GetPatternSize: TPoint2i;
function GetPatternCount: Integer;
private
FZOrderAutoSort: Boolean;
procedure Render;
procedure Add(const ASprite: TCustomSprite);
procedure Remove(const ASprite: TCustomSprite);
procedure ResetDrawOrder;
procedure SetFocus(const AFocused: Boolean);
function SpriteAtPos(const X, Y: Integer; const ACheckVisible,
ACheckHitTest: Boolean): TCustomSprite;
private
FOnClick, FOnDblClick, FOnMouseLeave: TNotifyEvent;
FOnMouseDown, FOnMouseUp: TMouseEvent;
FOnMouseMove: TMouseMoveEvent;
FOnMouseEnter: TNotifyEvent;
FOnMouseWheel: TMouseWheelEvent;
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Single); virtual;
procedure MouseMove(Shift: TShiftState; X, Y: Single); virtual;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Single); virtual;
procedure MouseWheel(Shift: TShiftState; WheelDelta: Integer;
var Handled: Boolean); virtual;
procedure MouseEnter; virtual;
procedure MouseLeave; virtual;
protected
FManager: TSpriteManager;
procedure DoDraw; virtual;
procedure DoMove(const AMoveCount: Single); virtual;
procedure DoCollision(const ASprite: TCustomSprite); virtual;
function GetBoundsRect: TIntRect; virtual;
procedure Resize; virtual;
// 对齐方式
property Align: TAlignMode read FAlign write SetAlign;
// 边缘
property Margins: TBounds read FMargins;
public
constructor Create(const AManager: TSpriteManager); virtual;
destructor Destroy; override;
// 批量操作前锁定<禁止子列表ZOrder排序>
procedure BeginUpdate;
// 批量操作前解锁<排序>
procedure EndUpdate;
// 死亡
procedure Dead;
// 移动
procedure Move(const AMoveCount: Single);
// 碰撞检测
procedure Collision(const AOther: TCustomSprite); overload; virtual;
procedure Collision; overload; virtual;
public
// 父级精灵
property Parent: TCustomSprite read FParent write SetParent;
// 名称
property Name: string read FName write FName;
// 图片
property Image: TEngineImage read FImage write FImage;
// 坐标
property X: Single read FX write SetX;
property Y: Single read FY write SetY;
// 世界坐标
property WorldX: Single read GetWorldX;
property WorldY: Single read GetWorldY;
// 渲染顺序
property ZOrder: Integer read FZOrder write SetZOrder;
// 宽度
property Width: Integer read FWidth write SetWidth;
// 高度
property Height: Integer read FHeight write SetHeight;
// 偏移
property Offset: TPoint2f read FOffset write FOffset;
// 混合模式
property BlendMode: TEngineBlendingEffect read FBlendMode write FBlendMode;
// 是否可移动
property Moveable: Boolean read FMoveable write FMoveable;
// 是否对坐标进行Trunc取整
property TruncDraw: Boolean read FTruncDraw write FTruncDraw;
// 是否显示
property Visible: Boolean read FVisible write FVisible;
// 图块索引
property PatternIndex: Integer read FPatternIndex write FPatternIndex;
// 图块大小
property PatternSize: TPoint2i read GetPatternSize;
// 图块数量
property PatternCount: Integer read GetPatternCount;
// Tag
property Tag: Integer read FTag write FTag;
// 碰撞相关属性
property CollideMode: TCollideMode read FCollideMode write FCollideMode;
property CollidePos: TPoint2f read FCollidePos write FCollidePos;
property CollideRadius: Integer read FCollideRadius write FCollideRadius;
property CollideRect: TFloatRect read FCollideRect write FCollideRect;
property CollideQuadrangle: TQuad read FCollideQuadrangle write FCollideQuadrangle;
property CollidePolygon: TPolygon read FCollidePolygon write FCollidePolygon;
property Collisioned: Boolean read FCollisioned write FCollisioned;
// 包围盒
property BoundsRect: TIntRect read GetBoundsRect;
// 是否可以获得焦点
property CanFocus: Boolean read FCanFocus write FCanFocus;
// 是否可以命中
property HitTest: Boolean read FHitTest write FHitTest;
// 是否固定位置(位置不根据世界坐标改变而改变, 通常用于GUI对象)
property FixedLayout: Boolean read FFixedLayout write FFixedLayout;
// 组名称
property GroupName: string read FGroupName write SetGroupName;
public
property OnClick: TNotifyEvent read FOnClick write FOnClick;
property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
property OnMouseUp: TMouseEvent read FOnMouseUp write FOnMouseUp;
property OnMouseWheel: TMouseWheelEvent read FOnMouseWheel write FOnMouseWheel;
end;
TSprite = class(TCustomSprite)
FColor: TFloatColor;
FAngle: Single;
FAngle360: Integer;
FScale: TPoint2f;
FDrawMode: TDrawMode;
FTransformQuad: TQuad;
FMirror, FFlip, FCentered, FSelected, FCollisionable: Boolean;
procedure SetAngle360(Value: Integer);
procedure SetSelected(const Value: Boolean);
protected
procedure DoDraw; override;
public
constructor Create(const AManager: TSpriteManager); override;
destructor Destroy; override;
// 颜色
property Color: TFloatColor read FColor write FColor;
// 角度
property Angle: Single read FAngle write FAngle;
// 弧度
property Angle360: Integer read FAngle360 write SetAngle360;
// 比例
property Scale: TPoint2f read FScale write FScale;
// 渲染模式
property DrawMode: TDrawMode read FDrawMode write FDrawMode;
// DrawMode=dmTransform时, 需要设置此属性
property TransformQuad: TQuad read FTransformQuad write FTransformQuad;
// 是否居中
property Centered: Boolean read FCentered write FCentered;
// 是否水平镜像
property Mirror: Boolean read FMirror write FMirror;
// 是否垂直翻转
property Flip: Boolean read FFlip write FFlip;
// 是否选中
property Selected: Boolean read FSelected write SetSelected;
// 是否可碰撞
property Collisionable: Boolean read FCollisionable write FCollisionable;
end;
//动画精灵
TAnimatedSprite = class(TSprite)
private
FFrameStart, FFrameAmount: Integer;
FFrameIndex: Single;
FSpeed: Single;
FPlayMode: TAnimPlayMode;
FLooped, FActive: Boolean;
FPlayFlag1, FPlayFlag2: Boolean;
procedure SetFrameStart(const Value: Integer);
function GetEnded: Boolean;
protected
procedure SetActive(const Value: Boolean); virtual;
procedure DoStart; virtual;
procedure DoEnd; virtual;
procedure DoMove(const AMoveCount: Single); override;
public
constructor Create(const AManager: TSpriteManager); override;
procedure Play(const AImage: TEngineImage;
const AFrameStart, AFrameAmount: Integer;
const ASpeed: Single;
const ALooped, AMirror, AActive: Boolean;
const APlayMode: TAnimPlayMode = pmForward); overload; virtual;
procedure Play(const AImage: TEngineImage;
const AFrameStart, AFrameAmount: Integer;
const ASpeed: Single;
const ALooped: Boolean;
const APlayMode: TAnimPlayMode = pmForward); overload; virtual;
// 起始帧
property FrameStart: Integer read FFrameStart write SetFrameStart;
// 总帧数
property FrameAmount: Integer read FFrameAmount write FFrameAmount;
// 当前帧
property FrameIndex: Single read FFrameIndex write FFrameIndex;
// 播放速度
property Speed: Single read FSpeed write FSpeed;
// 播放模式
property PlayMode: TAnimPlayMode read FPlayMode write FPlayMode;
// 是否循环播放
property Looped: Boolean read FLooped write FLooped;
// 是否激活
property Active: Boolean read FActive write SetActive;
// 是否已结束
property Ended: Boolean read GetEnded;
end;
//文本精灵
TTextSprite = class(TSprite)
private
FFont: TFontInstance;
FFontStyle: TFontCharStyle;
FFontRenderer: TFontInstance.TFontRenderer;
FFontSizeScale: Single;
private
FText: string;
FFontSize: Word;
FFontName: string;
FFontColor: TIntColor;
FTextAlign: TTextAlignMode;
procedure SetText(const Value: string);
procedure SetFontName(const Value: string);
procedure SetFontColor(const Value: TIntColor);
procedure SetFontSize(const Value: Word);
protected
procedure Resize; override;
procedure DoDraw; override;
public
constructor Create(const AManager: TSpriteManager); override;
property X: Single read FX;
property Y: Single read FY;
published
property Text: string read FText write SetText;
property FontName: string read FFontName write SetFontName;
property FontSize: Word read FFontSize write SetFontSize;
property FontColor: TIntColor read FFontColor write SetFontColor;
property TextAlign: TTextAlignMode read FTextAlign write FTextAlign;
property Name;
property Align;
property Margins;
property ZOrder;
end;
//GUI精灵
TGUISprite = class(TAnimatedSprite)
private
FText: TTextSprite;
procedure SetText(const Value: TTextSprite);
protected
procedure DoDraw; override;
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Single); override;
procedure MouseMove(Shift: TShiftState; X, Y: Single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Single); override;
procedure MouseWheel(Shift: TShiftState; WheelDelta: Integer;
var Handled: Boolean); override;
procedure MouseEnter; override;
procedure MouseLeave; override;
public
constructor Create(const AManager: TSpriteManager); override;
property X: Single read FX;
property Y: Single read FY;
published
property Name;
property Image;
property Align;
property Margins;
property HitTest;
property ZOrder;
property Text: TTextSprite read FText write SetText;
end;
//精灵管理器
TSpriteManager = class
private
FCanvas: TEngineCanvas;
FFontMap: TObjectDictionary<string, TFontInstance>;
FViewPort: TPoint2i;
FAllCount, FDrawCount: Integer;
FSpriteList: TObjectList<TCustomSprite>;
FDeadList: TList<TCustomSprite>;
FSelectedList: TList<TCustomSprite>;
FGroupMap: TObjectDictionary<string, TList<TCustomSprite>>;
FWorldX, FWorldY: Single;
FTouchPoint: TPoint2i;
FZOrderAutoSort: Boolean;
FOnPrint: TNotifyInfoEvent;
FPadding: TIntRect;
FDefaultFont: TFontInstance;
function GetSprite(const Name: string): TCustomSprite;
function GetScale: Single;
function GetDefaultFont: TFontInstance;
procedure SetPadding(const Value: TIntRect);
private
procedure Add(const ASprite: TCustomSprite);
procedure Remove(const ASprite: TCustomSprite);
function SpriteAtPos(const X, Y: Single; const ACheckVisible,
ACheckHitTest: Boolean): TCustomSprite;
// 加入分组
procedure Grouping(const AGroupName: string; const ASprite: TCustomSprite);
// 移出分组
procedure OutGroup(const AGroupName: string; const ASprite: TCustomSprite);
// 解散分组
procedure BreakGroup(const AGroupName: string);
private
FOnFormMouseDown, FOnFormMouseUp: TMouseEvent;
FOnFormMouseMove: TMouseMoveEvent;
FOnFormMouseWheel: TMouseWheelEvent;
FActiveSprite, FMouseEnterSprite: TCustomSprite;
procedure AcquireEvents(const AForm: TForm);
procedure DoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
procedure DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
procedure DoMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
procedure DoMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
protected
procedure ResetDrawOrder;
public
constructor Create(const AForm: TForm);
destructor Destroy; override;
// 批量更新(增加)前锁定
procedure BeginUpdate;
// 批量更新(增加)前解锁
procedure EndUpdate;
// 渲染
procedure Render;
// 移动
procedure Move(const AMoveCount: Integer);
// 清理死亡对象
procedure Dead;
// 计算两个Sprite之间的距离
function SpriteDistance(const S1, S2: TCustomSprite): Real;
// 重置大小
procedure Resize(const ANewWidth, ANewHeight: Integer);
// 释放整个分组中的所有Sprite
procedure DeadGroup(const AGroupName: string);
public
// 世界坐标
property WorldX: Single read FWorldX write FWorldX ;
property WorldY: Single read FWorldY write FWorldY;
// 画布
property Canvas: TEngineCanvas read FCanvas write FCanvas;
// 信息打印
property OnPrint: TNotifyInfoEvent read FOnPrint write FOnPrint;
// 可视区域大小
property ViewPort: TPoint2i read FViewPort write FViewPort;
// 点击屏幕的位置
property TouchPoint: TPoint2i read FTouchPoint write FTouchPoint;
// 根据名称获取Sprite
property Sprite[const Name: string]: TCustomSprite read GetSprite;
// 边缘区域
property Padding: TIntRect read FPadding write SetPadding;
// 默认字体对象
property DefaultFont: TFontInstance read GetDefaultFont;
// 屏幕比例
property Scale: Single read GetScale;
end;
implementation
function Angles(X, Y: Integer): Real;
begin
Result := Abs(((Arctan2(X, Y) * 40.5)) - 128);
end;
{$REGION 'TCustomSprite'}
{ TCustomSprite }
constructor TCustomSprite.Create(const AManager: TSpriteManager);
begin
inherited Create;
FManager:= AManager;
FChildList:= TList<TCustomSprite>.Create;
FParent:= nil;
FMargins:= TBounds.Create(RectF(0, 0, 0, 0));
FMargins.OnChange:= DoMarginsChanged;
FZOrderAutoSort:= True;
//
FName:= '';
FImage:= nil;
FX:= 0;
FY:= 0;
FDrawX:= 0;
FDrawY:= 0;
FZOrder:= 0;
if Self.FZOrder = 0 then Self.FZOrder:= 1;
FWidth:= 8;
FHeight:= 8;
FBlendMode:= TEngineBlendingEffect.Normal;
FMoveable:= True;
FTruncDraw:= True;
FVisible:= True;
FOffset:= ZeroPoint2f;
FPatternIndex:= 0;
FTag:= 0;
FCollideMode:= TCollideMode.cmRect;
FCollisioned:= False;
FCanFocus:= False;
FFocused:= False;
FHitTest:= True;
FAlign:= TAlignMode.amNone;
FFixedLayout:= False;
FGroupName:= '';
//
AManager.Add(Self);
end;
destructor TCustomSprite.Destroy;
begin
FreeAndNil(FMargins);
FreeAndNil(FChildList);
inherited;
end;
procedure TCustomSprite.BeginUpdate;
begin
FZOrderAutoSort:= False;
end;
procedure TCustomSprite.EndUpdate;
begin
FZOrderAutoSort:= True;
Self.ResetDrawOrder;
end;
procedure TCustomSprite.SetParent(const Value: TCustomSprite);
begin
if FParent = Value then Exit;
if Assigned(FParent) then
FParent.Remove(Self);
FParent:= Value;
FParent.Add(Self);
FParent.ResetDrawOrder;
DoLayoutChanged;
end;
procedure TCustomSprite.SetWidth(const Value: Integer);
begin
FWidth:= Round(Value * FManager.Scale);
end;
procedure TCustomSprite.SetAlign(const Value: TAlignMode);
begin
if FAlign <> Value then
begin
FAlign:= Value;
DoLayoutChanged;
end;
end;
procedure TCustomSprite.SetFocus(const AFocused: Boolean);
begin
FFocused:= False;
if FCanFocus then FFocused:= AFocused;
end;
procedure TCustomSprite.SetGroupName(const Value: string);
begin
if FGroupName <> Value then
begin
if Value = '' then
FManager.OutGroup(FGroupName, Self)
else
begin
FGroupName:= Value;
FManager.Grouping(FGroupName, Self);
end;
end;
end;
procedure TCustomSprite.SetHeight(const Value: Integer);
begin
FHeight:= Round(Value * FManager.Scale);
end;
procedure TCustomSprite.SetImage(const Value: TEngineImage);
begin
FImage:= Value;
end;
procedure TCustomSprite.SetX(const Value: Single);
begin
FX:= Value * FManager.Scale;
if Assigned(FParent) then
FDrawX:= FParent.FDrawX + FX
else
FDrawX:= FX;
end;
procedure TCustomSprite.SetY(const Value: Single);
begin
FY:= Value * FManager.Scale;
if Assigned(FParent) then
FDrawY:= FParent.FDrawY + FY
else
FDrawY:= FY;
end;
procedure TCustomSprite.SetZOrder(const Value: Integer);
begin
if FZOrder <> Value then
begin
FZOrder:= Value;
if FParent <> nil then
FParent.ResetDrawOrder
else
FManager.ResetDrawOrder
end;
end;
function TCustomSprite.SpriteAtPos(const X, Y: Integer; const ACheckVisible,
ACheckHitTest: Boolean): TCustomSprite;
var
I: Integer;
begin
Result:= nil;
if ACheckVisible and not Self.Visible then Exit;
if ACheckHitTest and not Self.HitTest then Exit;
if Self.BoundsRect.Contains(Point2i(X, Y)) then
Exit(Self)
else
for I:= FChildList.Count -1 downto 0 do
begin
Result:= FChildList[I].SpriteAtPos(X, Y, ACheckVisible, ACheckHitTest);
if Result <> nil then
Break;
end;
end;
procedure TCustomSprite.ResetDrawOrder;
begin
if not FZOrderAutoSort then Exit;
FChildList.Sort(
TComparer<TCustomSprite>.Construct(
function (const L, R: TCustomSprite): Integer
begin
Result:= L.ZOrder - R.ZOrder;
end
)
);
end;
procedure TCustomSprite.Resize;
var
I: Integer;
begin
DoLayoutChanged;
for I:= 0 to FChildList.Count -1 do
FChildList[I].Resize;
end;
procedure TCustomSprite.Render;
var
I: Integer;
begin
if FVisible then
begin
if (FDrawX + FOffset.X > ifthen(FFixedLayout,0,FManager.WorldX) - FWidth) and
(FDrawY + FOffset.Y > ifthen(FFixedLayout,0,FManager.WorldY) - FHeight) and
(FDrawX + FOffset.X < ifthen(FFixedLayout,0,FManager.WorldX) + FManager.ViewPort.X) and
(FDrawY + FOffset.Y < ifthen(FFixedLayout,0,FManager.WorldY) + FManager.ViewPort.Y) then
begin
DoDraw;
Inc(FManager.FDrawCount);
end;
//
for I:= 0 to FChildList.Count - 1 do
FChildList[I].Render;
end;
end;
procedure TCustomSprite.DoDraw;
var
LTargetQuad: TQuad;
begin
if FVisible and Assigned(FImage) then
begin
LTargetQuad:= Quad(FDrawX - ifthen(FFixedLayout,0,FManager.WorldX),
FDrawY - ifthen(FFixedLayout,0,FManager.WorldY),
FWidth, FHeight);
LTargetQuad.Trunc(FTruncDraw);
FManager.Canvas.DrawImage(FImage, LTargetQuad);
end;
end;
procedure TCustomSprite.DoMove(const AMoveCount: Single);
begin
end;
function TCustomSprite.GetPatternSize: TPoint2i;
begin
if Assigned(FImage) then
Result:= FImage.PatternRect[0].Size
else
Result:= Point2i(8,8);
end;
function TCustomSprite.GetBoundsRect: TIntRect;
begin
Result:= IntRectBDS(Round(FDrawX), Round(FDrawY),
Round(FDrawX + FWidth), Round(FDrawY + FHeight));
end;
function TCustomSprite.GetPatternCount: Integer;
begin
if Assigned(FImage) then
Result:= FImage.Regions.Count
else
Result:= 0;
end;
function TCustomSprite.GetWorldX: Single;
begin
Result:= FManager.WorldX + FDrawX;
end;
function TCustomSprite.GetWorldY: Single;
begin
Result:= FManager.WorldY + FDrawY;
end;
procedure TCustomSprite.DoCollision(const ASprite: TCustomSprite);
begin
end;
procedure TCustomSprite.Add(const ASprite: TCustomSprite);
begin
if Assigned(ASprite) and not FChildList.Contains(ASprite) then
begin
ASprite.FDrawX:= Self.FDrawX + ASprite.X;
ASprite.FDrawY:= Self.FDrawY + ASprite.Y;
FChildList.Add(ASprite);
end;
end;
procedure TCustomSprite.Remove(const ASprite: TCustomSprite);
begin
if Assigned(ASprite) and FChildList.Contains(ASprite) then
begin
ASprite.FDrawX:= ASprite.X;
ASprite.FDrawY:= ASprite.Y;
ASprite.FParent:= nil;
FChildList.Remove(ASprite);
end;
end;
procedure TCustomSprite.Collision(const AOther: TCustomSprite);
var
LDelta: Real;
LCollided: Boolean;
begin
LCollided := False;
if FCollisioned and AOther.Collisioned and (not FDeaded) and (not AOther.FDeaded) then
begin
case FCollideMode of
TCollideMode.cmCircle:
begin
LDelta:= Sqrt(Sqr(FCollidePos.X - AOther.CollidePos.X) +
Sqr(FCollidePos.Y - AOther.CollidePos.Y));
LCollided:= LDelta < (FCollideRadius + AOther.CollideRadius);
end;
TCollideMode.cmRect:
LCollided:= FCollideRect.Overlaps(AOther.CollideRect);
TCollideMode.cmQuadrangle:
LCollided:= FCollideQuadrangle.Overlaps(AOther.CollideQuadrangle);
TCollideMode.cmPolygon:
LCollided:= FCollidePolygon.Overlaps(AOther.CollidePolygon);
end;
//
if LCollided then
begin
Self.DoCollision(AOther);
AOther.DoCollision(Self);
end;
end;
end;
procedure TCustomSprite.Collision;
var
I: Integer;
begin
if FCollisioned and not FDeaded then
begin
for I:= 0 to FManager.FSpriteList.Count - 1 do
Self.Collision(FManager.FSpriteList[I]);
end;
end;
procedure TCustomSprite.Dead;
begin
if not FDeaded then
begin
FDeaded:= True;
FManager.FDeadList.Add(Self);
end;
end;
procedure TCustomSprite.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Single);
begin
if Assigned(FOnMouseDown) then
FOnMouseDown(Self, Button, Shift, X, Y);
end;
procedure TCustomSprite.MouseEnter;
begin
end;
procedure TCustomSprite.MouseLeave;
begin
end;
procedure TCustomSprite.MouseMove(Shift: TShiftState; X, Y: Single);
begin
if Assigned(FOnMouseMove) then
FOnMouseMove(Self, Shift, X, Y);
end;
procedure TCustomSprite.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Single);
begin
if Assigned(FOnMouseUp) then
FOnMouseUp(Self, Button, Shift, X, Y);
//
if BoundsRect.Contains(Point2i(Round(X*FManager.Scale),
Round(Y*FManager.Scale))) and
Assigned(FOnClick) then FOnClick(Self);
end;
procedure TCustomSprite.MouseWheel(Shift: TShiftState; WheelDelta: Integer;
var Handled: Boolean);
begin
if Assigned(FOnMouseWheel) then
FOnMouseWheel(Self, Shift, WheelDelta, Handled);
end;
procedure TCustomSprite.Move(const AMoveCount: Single);
var
I: Integer;
begin
if FMoveable then
begin
DoMove(AMoveCount);
for I:= 0 to FChildList.Count - 1 do
FChildList[I].Move(AMoveCount);
end;
end;
procedure TCustomSprite.DoMarginsChanged(Sender: TObject);
begin
DoLayoutChanged;
end;
procedure TCustomSprite.DoLayoutChanged;
var
LMargins: TFloatRect;
LContainerSize: TPoint2i;
begin
LMargins.Left := FMargins.Left * FManager.Scale;
LMargins.Top := FMargins.Top * FManager.Scale;
LMargins.Right := FMargins.Right * FManager.Scale;
LMargins.Bottom:= FMargins.Bottom * FManager.Scale;
if Assigned(FParent) then
LContainerSize:= Point2i(FParent.Width, FParent.Height)
else
LContainerSize:= FManager.ViewPort;
//
if FAlign = amClient then
begin
FWidth := LContainerSize.X;
FHeight:= LContainerSize.Y;
end;
//
case FAlign of
amRightTop:
begin
FDrawX:= LContainerSize.X - FWidth - LMargins.Right - FManager.Padding.Right;
FDrawY:= 0 + LMargins.Top + FManager.Padding.Top;
end;
amLeftBottom:
begin
FDrawX:= 0 + LMargins.Left + FManager.Padding.Left;
FDrawY:= LContainerSize.Y - FHeight - LMargins.Bottom - FManager.Padding.Bottom;
end;
amRightBottom:
begin
FDrawX:= LContainerSize.X - FWidth - LMargins.Right - FManager.Padding.Right;
FDrawY:= LContainerSize.Y - FHeight - LMargins.Bottom - FManager.Padding.Bottom;
end;
amCenter:
begin
FDrawX:= LContainerSize.X / 2 - FWidth / 2;
FDrawY:= LContainerSize.Y / 2 - FHeight / 2;
end;
amCenterTop:
begin
FDrawX:= LContainerSize.X / 2 - FWidth / 2;
FDrawY:= 0 + LMargins.Top + FManager.Padding.Top;
end;
amCenterBottom:
begin
FDrawX:= LContainerSize.X / 2 - FWidth / 2;
FDrawY:= LContainerSize.Y - FHeight - LMargins.Bottom - FManager.Padding.Bottom;
end;
amCenterLeft:
begin
FDrawX:= 0 + LMargins.Left + FManager.Padding.Left;
FDrawY:= LContainerSize.Y / 2 - FHeight / 2;
end;
amCenterRight:
begin
FDrawX:= LContainerSize.X - FWidth - LMargins.Right - FManager.Padding.Right;
FDrawY:= LContainerSize.Y / 2 - FHeight / 2;
end;
else
begin
FDrawX:= 0 + LMargins.Left + FManager.Padding.Left;
FDrawY:= 0 + LMargins.Top + FManager.Padding.Top;
end;
end;
//
if Assigned(FParent) then
begin
FDrawX:= FParent.FDrawX + FDrawX;
FDrawY:= FParent.FDrawY + FDrawY;
end;
end;
{$ENDREGION}
{$REGION 'TSprite'}
{ TSprite }
constructor TSprite.Create(const AManager: TSpriteManager);
begin
inherited;
FColor:= FloatColorWhite;
FAngle:= 0;
FAngle360:= 0;
FScale:= Point2f(1.0, 1.0);
FCentered:= False;
FDrawMode:= TDrawMode.dmColor;
end;
destructor TSprite.Destroy;
begin
SetSelected(False);
inherited Destroy;
end;
procedure TSprite.DoDraw;
var
LX, LY: Single;
LTargetQuad: TQuad;
begin
if not Assigned(FImage) then Exit;
if not Assigned(FManager) then Exit;
//
LX:= FDrawX + FOffset.X - ifthen(FFixedLayout,0,FManager.WorldX);
LY:= FDrawY + FOffset.Y - ifthen(FFixedLayout,0,FManager.WorldY);
case FDrawMode of
TDrawMode.dmColor:
begin
FManager.Canvas.DrawImage(FImage, FPatternIndex,
Quad(LX, LY, FWidth, FHeight), FColor.ToInt, FMirror, FFlip, 0, 1, FBlendMode);
end;
TDrawMode.dmRotate:
begin
FManager.Canvas.DrawImage(FImage, FPatternIndex,
Quad(LX, LY, FWidth, FHeight), FColor.ToInt, FMirror, FFlip, FAngle, FScale.X, FBlendMode);
end;
TDrawMode.dmTransform:
begin
LTargetQuad.Values[0].X:= Trunc(FTransformQuad.Values[0].X + FOffset.X) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldX));
LTargetQuad.Values[0].Y:= Trunc(FTransformQuad.Values[0].Y + FOffset.Y) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldY));
LTargetQuad.Values[1].X:= Trunc(FTransformQuad.Values[1].X + FOffset.X) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldX));
LTargetQuad.Values[1].Y:= Trunc(FTransformQuad.Values[1].Y + FOffset.Y) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldY));
LTargetQuad.Values[2].X:= Trunc(FTransformQuad.Values[2].X + FOffset.X) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldX));
LTargetQuad.Values[2].Y:= Trunc(FTransformQuad.Values[2].Y + FOffset.Y) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldY));
LTargetQuad.Values[3].X:= Trunc(FTransformQuad.Values[3].X + FOffset.X) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldX));
LTargetQuad.Values[3].Y:= Trunc(FTransformQuad.Values[3].Y + FOffset.Y) -
Trunc(ifthen(FFixedLayout,0,FManager.WorldY));
FManager.Canvas.DrawImage(FImage, FPatternIndex, LTargetQuad,
FColor.ToInt, FMirror, FFlip, FAngle, FScale.X, FBlendMode);
end;
end;
end;
procedure TSprite.SetAngle360(Value: Integer);
begin
if FAngle360 <> Value then
FAngle:= DegToRad(Value);
end;
procedure TSprite.SetSelected(const Value: Boolean);
begin
if Value <> FSelected then
begin
FSelected:= Value;
if FSelected then
FManager.FSelectedList.Add(Self)
else
FManager.FSelectedList.Remove(Self);
end;
end;
{$ENDREGION}
{$REGION 'TAnimatedSprite'}
{ TAnimatedSprite }
constructor TAnimatedSprite.Create(const AManager: TSpriteManager);
begin
inherited;
FFrameStart:= 0;