/
DXAnimatedButtons.pas
124 lines (101 loc) · 2.5 KB
/
DXAnimatedButtons.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
unit DXAnimatedButtons;
interface
uses
tickcount, typex, systemx, colorconversion, advancedgraphics_dx, stringx, geometry, pxl.types, math, maths, numbers, graphics;
const
MAX_BALLS = 8;
type
TParticle = record
v: TVector4;
pos: TVector4;
sz: single;
end;
TDXBounceButton = class(TDXButton)
strict protected
animstarttime: ticker;
lastwidth, lastheight: single;
balls: array[0..MAX_BALLS] of TParticle;
procedure InternalAnimation(rDeltaTime: Cardinal); override;
procedure OnStartAnimation; override;
procedure OnStopAnimiation; override;
public
procedure DoDrawAnimation; override;
procedure PlaceRandomStuff;
end;
implementation
{ TDXBounceButton }
procedure TDXBounceButton.DoDrawAnimation;
var
t: ni;
x1,y1,x2,y2: single;
p: TParticle;
begin
inherited;
try
if abs(width-lastwidth) > 1.0 then
exit;
if abs(height-lastheight) > 1.0 then
exit;
for t:= 0 to High(balls) do begin
p := balls[t];
x1 := p.pos.x+self.Left-p.sz;
y1 := p.pos.y+self.Top-p.sz;
x2 := p.pos.x+self.Left+p.sz;
y2 := p.pos.y+self.Top+p.sz;
x1 := Self.DX.ScreenToGlobalX(x1);
y1 := Self.DX.ScreenToGlobalX(y1);
x2 := Self.DX.ScreenToGlobalX(x2);
y2 := Self.DX.ScreenToGlobalX(y2);
self.dx.Rectangle_Fill(x1,y1,x2,y2,clWhite, lesserof(0.25,gettimesince(animstarttime)/4000));
end;
finally
lastWidth := width;
lastHeight := height;
end;
end;
procedure TDXBounceButton.InternalAnimation(rDeltaTime: Cardinal);
VAR
t: ni;
speed: single;
p: Tparticle;
begin
inherited;
speed := rDeltaTime/4000;
for t := 0 to high(Self.balls) do begin
p := balls[t];
p.pos := p.pos + ((p.v * speed) * p.sz);
if p.pos.x >= width then p.v.x := 0-p.v.x;
if p.pos.y >= height then p.v.y := 0-p.v.y;
if p.pos.x < 0 then p.v.x := 0-p.v.x;
if p.pos.y < 0 then p.v.y := 0-p.v.y;
balls[t] := p;
end;
end;
procedure TDXBounceButton.OnStartAnimation;
begin
inherited;
animstarttime := getticker;
placeRandomStuff;
end;
procedure TDXBounceButton.OnStopAnimiation;
begin
inherited;
//
end;
procedure TDXBounceButton.PlaceRandomStuff;
var
t: ni;
p: TParticle;
begin
for t := 0 to high(Self.balls) do begin
p.v.Init;
p.v.x := Random(10000)/10000;
p.v.y := Random(10000)/10000;
p.pos.Init;
p.pos.x := random(round(width));
p.pos.y := random(round(height));
p.sz := random(round(greaterof(width, height) / 2));
balls[t] := p;
end;
end;
end.