-
Notifications
You must be signed in to change notification settings - Fork 64
/
DN.Controls.pas
283 lines (249 loc) · 6.88 KB
/
DN.Controls.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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.Controls;
interface
uses
Classes,
Types,
Windows,
Controls,
Graphics,
Generics.Collections;
type
TDNControlState = (dnsMouseIn, dnsMouseLeft);
TDNControlStates = set of TDNControlState;
TDNControlsController = class;
TDNControl = class
private
FWidth: Integer;
FTop: Integer;
FHeight: Integer;
FLeft: Integer;
FState: TDNControlStates;
FOnClick: TNotifyEvent;
FOnChanged: TNotifyEvent;
FVisible: Boolean;
procedure SetVisible(const Value: Boolean);
protected
procedure MouseEnter; virtual;
procedure MouseLeave; virtual;
procedure Click; virtual;
procedure Changed; virtual;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
public
constructor Create();
procedure UpdateMousePos(const APos: TPoint);
procedure MouseDown(AButton: TMouseButton); virtual;
procedure MouseUp(AButton: TMouseButton); virtual;
procedure PaintTo(ACanvas: TCanvas); virtual;
property Left: Integer read FLeft write FLeft;
property Top: Integer read FTop write FTop;
property Width: Integer read FWidth write FWidth;
property Height: Integer read FHeight write FHeight;
property State: TDNControlStates read FState;
property Visible: Boolean read FVisible write SetVisible;
property OnClick: TNotifyEvent read FOnClick write FOnClick;
end;
TDNControlsController = class
private
FControls: TObjectList<TDNControl>;
FParent: TWinControl;
procedure SetParent(const Value: TWinControl);
procedure HandleMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure HandleMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure HandleMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure HandleMouseLeave(Sender: TObject);
procedure HandleControlsChanged(Sender: TObject; const Item: TDNControl;
Action: TCollectionNotification);
procedure HandleControlChanged(Sender: TObject);
public
constructor Create();
destructor Destroy(); override;
procedure PaintTo(ACanvas: TCanvas);
property Parent: TWinControl read FParent write SetParent;
property Controls: TObjectList<TDNControl> read FControls;
end;
implementation
uses
Math;
{ TDNControl }
procedure TDNControl.Changed;
begin
if Assigned(FOnChanged) then
begin
FOnChanged(Self);
end;
end;
procedure TDNControl.Click;
begin
if Visible and Assigned(FOnClick) then
FOnClick(Self);
end;
constructor TDNControl.Create;
begin
inherited;
FVisible := True;
end;
procedure TDNControl.MouseDown(AButton: TMouseButton);
begin
if (dnsMouseIn in FState) and (mbLeft = AButton) then
begin
Include(FState, dnsMouseLeft);
Changed();
end;
end;
procedure TDNControl.MouseEnter;
begin
end;
procedure TDNControl.MouseLeave;
begin
end;
procedure TDNControl.MouseUp(AButton: TMouseButton);
begin
if (dnsMouseLeft in FState) and (mbLeft = AButton) then
begin
Exclude(FState, dnsMouseLeft);
if dnsMouseIn in FState then
Click();
Changed();
end;
end;
procedure TDNControl.PaintTo(ACanvas: TCanvas);
begin
end;
procedure TDNControl.SetVisible(const Value: Boolean);
begin
FVisible := Value;
Changed();
end;
procedure TDNControl.UpdateMousePos(const APos: TPoint);
var
LIsInRect: Boolean;
begin
LIsInRect := PtInRect(Rect(Left, Top, Left+Width, Top+Height), APos);
if LIsInRect then
begin
if not (dnsMouseIn in FState) then
begin
Include(FState, dnsMouseIn);
MouseEnter();
Changed();
end;
end
else
begin
if (dnsMouseIn in FState) then
begin
Exclude(FState, dnsMouseIn);
MouseLeave();
Changed();
end;
end;
end;
{ TDNControlsController }
constructor TDNControlsController.Create;
begin
inherited;
FControls := TObjectList<TDNControl>.Create();
FControls.OnNotify := HandleControlsChanged;
end;
destructor TDNControlsController.Destroy;
begin
Parent := nil;
FControls.Free;
inherited;
end;
procedure TDNControlsController.HandleControlChanged(Sender: TObject);
var
LRect: TRect;
begin
if Assigned(FParent) and FParent.HandleAllocated then
begin
LRect.Left := TDNControl(Sender).Left;
LRect.Top := TDNControl(Sender).Top;
LRect.Right := TDNControl(Sender).Left + TDNControl(Sender).Width;
LRect.Bottom := TDNControl(Sender).Top + TDNControl(Sender).Height;
InvalidateRect(FParent.Handle, @LRect, False);
end;
end;
procedure TDNControlsController.HandleControlsChanged(Sender: TObject;
const Item: TDNControl; Action: TCollectionNotification);
begin
case Action of
cnAdded: Item.OnChanged := HandleControlChanged;
cnRemoved, cnExtracted: Item.OnChanged := nil;
end;
end;
procedure TDNControlsController.HandleMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
LControl: TDNControl;
begin
for LControl in FControls do
begin
LControl.MouseDown(Button);
end;
end;
procedure TDNControlsController.HandleMouseLeave(Sender: TObject);
begin
HandleMouseMove(Sender, [], -1, -1);
end;
procedure TDNControlsController.HandleMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
LControl: TDNControl;
begin
for LControl in FControls do
begin
LControl.UpdateMousePos(Point(X, Y));
end;
end;
procedure TDNControlsController.HandleMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
LControl: TDNControl;
begin
for LControl in FControls do
begin
LControl.MouseUp(Button);
end;
end;
procedure TDNControlsController.PaintTo(ACanvas: TCanvas);
var
LControl: TDNControl;
begin
for LControl in FControls do
begin
if LControl.Visible then
LControl.PaintTo(ACanvas);
end;
end;
type
TOverride = class(TWinControl);
procedure TDNControlsController.SetParent(const Value: TWinControl);
begin
if Assigned(FParent) then
begin
TOverride(FParent).OnMouseMove := nil;
TOverride(FParent).OnMouseUp := nil;
TOverride(FParent).OnMouseDown := nil;
TOverride(FParent).OnMouseLeave := nil;
end;
FParent := Value;
if Assigned(FParent) then
begin
TOverride(FParent).OnMouseMove := HandleMouseMove;
TOverride(FParent).OnMouseUp := HandleMouseUp;
TOverride(FParent).OnMouseDown := HandleMouseDown;
TOverride(FParent).OnMouseLeave := HandleMouseLeave;
end;
end;
end.