-
Notifications
You must be signed in to change notification settings - Fork 5
/
DrawPath.Main.pas
638 lines (567 loc) · 15.4 KB
/
DrawPath.Main.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
unit DrawPath.Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ExtCtrls, Direct2D, D2D1, System.Generics.Collections, HGM.Button,
Vcl.StdCtrls, Vcl.Menus, HGM.Controls.PanelExt;
type
T2DCanvasAdv = class helper for TDirect2DCanvas
procedure Ellipse(Pt: TPoint; Size: Word); overload;
procedure MoveTo(Pt: TPoint); overload;
procedure LineTo(Pt: TPoint); overload;
procedure RoundRect(Rt: TRect; Rnd: Word); overload;
end;
TDrawData = class
Target: TDirect2DCanvas;
FieldRect: TRect;
constructor Create(ATarget: TDirect2DCanvas; AFieldRect: TRect);
end;
TLine = record
private
function GetDistance: Double;
public
UID: Integer;
PointA: TPoint;
PointB: TPoint;
property Distance: Double read GetDistance;
class function Create(A, B: TPoint): TLine; static;
end;
TLines = class(TList<TLine>)
function DeleteWithUID(UID: Integer): Boolean;
function GetLine(UID: Integer; var Line: TLine): Boolean;
end;
TInputMode = (imLine, imSetCenter);
TFormMain = class(TForm)
PanelLeft: TPanel;
DrawPanel: TDrawPanel;
TimerDraw: TTimer;
ButtonFlatClear: TButtonFlat;
Panel1: TPanel;
LabelCoord: TLabel;
LabelCursor: TLabel;
ButtonFlatSetCenter: TButtonFlat;
PopupMenuLine: TPopupMenu;
MenuItemLineDelete: TMenuItem;
LabelLineUnderMouse: TLabel;
btnCoordLines: TButtonFlat;
Label1: TLabel;
Label2: TLabel;
procedure DrawPanelPaint(Sender: TObject);
procedure TimerDrawTimer(Sender: TObject);
procedure DrawPanelMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure DrawPanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ButtonFlatClearClick(Sender: TObject);
procedure ButtonFlatSetCenterClick(Sender: TObject);
procedure MenuItemLineDeleteClick(Sender: TObject);
procedure DrawPanelMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure btnCoordLinesClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
FDrawCurrent: Boolean;
FCurentLineStart: TPoint;
FCurPos: TPoint;
FFieldPos: TPoint;
FLines: TLines;
FInputMode: TInputMode;
FOffsetCoord: TPoint;
FLineUnderMouseUID: Integer;
FCoordsVisible: Boolean;
function AbsolutePoint(Pt: TPoint): TPoint; overload;
function AbsolutePoint(Pt: TPoint; Offset: TPoint): TPoint; overload;
function DisplayFieldPos(Pt: TPoint): TPoint;
procedure DrawField(DrawData: TDrawData);
procedure DrawCoords(DrawData: TDrawData);
procedure DrawCursor(DrawData: TDrawData);
procedure DrawLines(DrawData: TDrawData);
procedure DrawCurrentLine(DrawData: TDrawData);
procedure UpdateInfo;
procedure SetInputMode(const Value: TInputMode);
procedure SetLineUnderMouseUID(const Value: Integer);
procedure DrawLineUnderMouseDate(DrawData: TDrawData);
procedure SetCoordsVisible(const Value: Boolean);
public
procedure Clear;
procedure SetCenter;
property LineUnderMouseUID: Integer read FLineUnderMouseUID write SetLineUnderMouseUID;
property InputMode: TInputMode read FInputMode write SetInputMode;
property CoordsVisible: Boolean read FCoordsVisible write SetCoordsVisible;
end;
const
GridDIV = 40;
var
FormMain: TFormMain;
GUID: Integer = 0;
function Rect(Pt: TPoint; W, H: Word): TRect; overload;
implementation
uses
Math;
{$R *.dfm}
function IndexInList(const Index: Integer; ListCount: Integer): Boolean;
begin
Result := (Index >= 0) and (Index <= ListCount - 1) and (ListCount > 0);
end;
function GetUID: Integer;
begin
Inc(GUID);
Result := GUID;
end;
function Rect(Pt: TPoint; W, H: Word): TRect;
begin
Result := Rect(0, 0, W, H);
Result.Offset(Pt);
end;
function CalcDistance(A, B: TPoint): Double;
begin
Result := Sqrt(Sqr(A.X - B.X) + Sqr(B.Y - A.Y));
end;
function TFormMain.AbsolutePoint(Pt: TPoint): TPoint;
begin
Result.X := Pt.X * GridDIV;
Result.Y := Pt.Y * GridDIV;
end;
function TFormMain.AbsolutePoint(Pt, Offset: TPoint): TPoint;
begin
Result := AbsolutePoint(Pt);
Result.Offset(Offset);
end;
procedure TFormMain.btnCoordLinesClick(Sender: TObject);
begin
CoordsVisible := not CoordsVisible;
end;
procedure TFormMain.ButtonFlatClearClick(Sender: TObject);
begin
Clear;
end;
procedure TFormMain.ButtonFlatSetCenterClick(Sender: TObject);
begin
SetCenter;
end;
procedure TFormMain.Clear;
begin
FLines.Clear;
end;
function TFormMain.DisplayFieldPos(Pt: TPoint): TPoint;
begin
Result := Point(Pt.X - FOffsetCoord.X, Pt.Y - FOffsetCoord.Y);
end;
procedure TFormMain.DrawLineUnderMouseDate(DrawData: TDrawData);
var
W: Integer;
Plate: TPoint;
Str: string;
Rt: TRect;
Line: TLine;
begin
if not FLines.GetLine(FLineUnderMouseUID, Line) then
Exit;
with DrawData.Target do
begin
Pen.Color := $20FFAD4A;
Brush.Color := $20FFBE6E;
Brush.Style := bsSolid;
Pen.Width := 1;
Str := 'Äëèíà ' + Line.Distance.ToString(ffFixed, 6, 2) + ' åä.';
W := TextWidth(Str) + 4;
Plate := AbsolutePoint(FFieldPos);
Plate.Offset(10, -10);
Rt := Rect(Plate, W, 20);
RoundRect(Rt, 5);
Rt.Offset(2, 2);
Font.Color := clWhite;
TextRect(Rt, Str, []);
Brush.Style := bsClear;
Font.Color := $00433D31;
Plate := AbsolutePoint(Line.PointA);
Plate.Offset(5, 5);
Str := 'A';
TextOut(Plate.X, Plate.Y, Str);
Plate := AbsolutePoint(Line.PointB);
Str := 'B';
Plate.Offset(5, 5);
TextOut(Plate.X, Plate.Y, Str);
end;
end;
procedure TFormMain.DrawCurrentLine(DrawData: TDrawData);
var
W: Integer;
Plate: TPoint;
Str: string;
Rt: TRect;
begin
if not FDrawCurrent then
Exit;
with DrawData.Target do
begin
Pen.Color := $0069A9F1;
Brush.Color := Pen.Color;
Brush.Style := bsSolid;
Pen.Width := 2;
MoveTo(AbsolutePoint(FCurentLineStart));
LineTo(AbsolutePoint(FFieldPos));
Ellipse(AbsolutePoint(FCurentLineStart, Point(-5, -5)), 10);
Ellipse(AbsolutePoint(FFieldPos, Point(-5, -5)), 10);
Str := CalcDistance(FCurentLineStart, FFieldPos).ToString(ffFixed, 6, 2) + ' åä.';
W := TextWidth(Str) + 4;
Plate := AbsolutePoint(FFieldPos);
Plate.Offset(10, -10);
Rt := Rect(Plate, W, 20);
RoundRect(Rt, 5);
Rt.Offset(2, 2);
Font.Color := clWhite;
TextRect(Rt, Str, []);
end;
end;
procedure TFormMain.DrawCursor(DrawData: TDrawData);
var
FCur: TPoint;
begin
if FDrawCurrent then
Exit;
with DrawData.Target do
begin
case FInputMode of
imLine:
begin
Brush.Style := bsSolid;
Pen.Color := $0069A9F1;
Brush.Color := Pen.Color;
end;
imSetCenter:
begin
Brush.Style := bsSolid;
Pen.Color := $00FFC61C;
Brush.Color := Pen.Color;
end;
end;
FCur := AbsolutePoint(FFieldPos);
FCur.Offset(-5, -5);
Ellipse(FCur, 11);
end;
end;
procedure TFormMain.DrawField(DrawData: TDrawData);
var
i: Integer;
begin
with DrawData.Target do
begin
Brush.Color := clWhite;
Brush.Style := bsSolid;
FillRect(DrawData.FieldRect);
Pen.Color := $00F6F6F6;
for i := 1 to DrawData.FieldRect.Width div (GridDIV div 2) do
begin
MoveTo(i * (GridDIV div 2), 0);
LineTo(i * (GridDIV div 2), DrawData.FieldRect.Height);
end;
for i := 1 to DrawData.FieldRect.Height div (GridDIV div 2) do
begin
MoveTo(0, i * (GridDIV div 2));
LineTo(DrawData.FieldRect.Width, i * (GridDIV div 2));
end;
Pen.Color := $00EDEDED;
for i := 1 to DrawData.FieldRect.Width div GridDIV do
begin
MoveTo(i * GridDIV, 0);
LineTo(i * GridDIV, DrawData.FieldRect.Height);
end;
for i := 1 to DrawData.FieldRect.Height div GridDIV do
begin
MoveTo(0, i * GridDIV);
LineTo(DrawData.FieldRect.Width, i * GridDIV);
end;
Brush.Style := bsSolid;
Pen.Color := $00C5C5C5;
Brush.Color := Pen.Color;
Ellipse(AbsolutePoint(FOffsetCoord, Point(-2, -2)), 5);
end;
end;
procedure TFormMain.DrawCoords(DrawData: TDrawData);
var
i: Integer;
begin
with DrawData.Target do
begin
Pen.Color := $00C5C5C5;
Brush.Color := Pen.Color;
Pen.Width := 2;
MoveTo(0, AbsolutePoint(FOffsetCoord).Y);
LineTo(DrawData.FieldRect.Right, AbsolutePoint(FOffsetCoord).Y);
MoveTo(AbsolutePoint(FOffsetCoord).X, 0);
LineTo(AbsolutePoint(FOffsetCoord).X, DrawData.FieldRect.Bottom);
end;
end;
procedure TFormMain.DrawLines(DrawData: TDrawData);
var
i: Integer;
x, y, x1, x2, y1, y2, dx, dy, dx1, dy1, L: Integer;
S, Closet: Double;
RT: TRect;
begin
with DrawData.Target do
begin
Closet := 30;
L := -1;
Brush.Style := bsSolid;
Pen.Width := 2;
for i := 0 to FLines.Count - 1 do
begin
if FLines[i].UID = FLineUnderMouseUID then
Pen.Color := $00FF92C4
else
Pen.Color := $0056E28E;
Brush.Color := Pen.Color;
MoveTo(AbsolutePoint(FLines[i].PointA));
LineTo(AbsolutePoint(FLines[i].PointB));
Ellipse(AbsolutePoint(FLines[i].PointA, Point(-5, -5)), 10);
Ellipse(AbsolutePoint(FLines[i].PointB, Point(-5, -5)), 10);
//Âû÷èñëåíèå íàõîæäåíèÿ êóðñîðà íà ëèíèè
//Äëÿ íà÷àëà ïðîâåðèì ïî ïðîñòîìó, âõîäèò ëè òî÷êà â ïðÿìîóãîëüíèê
RT := Rect(AbsolutePoint(FLines[i].PointA).x, AbsolutePoint(FLines[i].PointA).y, AbsolutePoint(FLines[i].PointB).x, AbsolutePoint(FLines[i].PointB).y);
RT.NormalizeRect;
RT.Inflate(3, 3);
if RT.Contains(FCurPos) then
begin
x1 := AbsolutePoint(FLines[i].PointA).x;
y1 := AbsolutePoint(FLines[i].PointA).y;
x2 := AbsolutePoint(FLines[i].PointB).x;
y2 := AbsolutePoint(FLines[i].PointB).y;
x := FCurPos.X;
y := FCurPos.Y;
dx1 := x2 - x1;
dy1 := y2 - y1;
dx := x - x1;
dy := y - y1;
S := ABS(dx1 * dy - dx * dy1);
if S <> 0 then
S := SQRT(S);
if (S < 25) and (S < Closet) then
begin
Closet := S;
L := FLines[i].UID;
end;
end;
end;
FLineUnderMouseUID := L;
end;
end;
procedure TFormMain.DrawPanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Pt: TPoint;
begin
case FInputMode of
imLine:
begin
if Button = mbLeft then
begin
if FDrawCurrent then
begin
//Åñëè íåïðåðûâíî, òî íå óñòàíàâëèâàòü False
//FDrawCurrent:=False;
if FCurentLineStart = FFieldPos then
Exit;
FLines.Add(TLine.Create(FCurentLineStart, FFieldPos));
FCurentLineStart := FFieldPos;
end
else
begin
FDrawCurrent := True;
FCurentLineStart := FFieldPos;
end;
end;
if Button = mbRight then
begin
if FDrawCurrent then
FDrawCurrent := False
else
begin
if FLineUnderMouseUID <> -1 then
begin
Pt := Mouse.CursorPos;
PopupMenuLine.Popup(Pt.X, Pt.Y);
end;
end;
end;
end;
imSetCenter:
begin
if Button = mbLeft then
begin
FOffsetCoord := FFieldPos;
InputMode := imLine;
end;
if Button = mbRight then
begin
InputMode := imLine;
end;
end;
end;
end;
procedure TFormMain.UpdateInfo;
var
Line: TLine;
begin
LabelCursor.Caption := Format('Êóðñîð: %d:%d', [FCurPos.X, FCurPos.Y]);
LabelCoord.Caption := Format('Ïîçèöèÿ: %d:%d', [DisplayFieldPos(FFieldPos).x, DisplayFieldPos(FFieldPos).y]);
if FLines.GetLine(FLineUnderMouseUID, Line) then
LabelLineUnderMouse.Caption := Format('Ïðÿìàÿ: A(%d:%d), B(%d:%d)', [DisplayFieldPos(Line.PointA).x, DisplayFieldPos(Line.PointA).y, DisplayFieldPos(Line.PointB).x, DisplayFieldPos(Line.PointB).y])
else
LabelLineUnderMouse.Caption := '';
end;
procedure TFormMain.DrawPanelMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
FCurPos := Point(X, Y);
FFieldPos := Point(Round(FCurPos.X / GridDIV), Round(FCurPos.Y / GridDIV));
UpdateInfo;
end;
procedure TFormMain.DrawPanelMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
case FInputMode of
imLine:
;
imSetCenter:
;
end;
end;
procedure TFormMain.DrawPanelPaint(Sender: TObject);
var
DData: TDrawData;
Draw2D: TDirect2DCanvas;
begin
Draw2D := TDirect2DCanvas.Create(DrawPanel.Canvas, DrawPanel.ClientRect);
with Draw2D do
begin
DData := TDrawData.Create(Draw2D, DrawPanel.ClientRect);
try
BeginDraw;
DrawField(DData);
if FCoordsVisible then
DrawCoords(DData);
DrawLines(DData);
DrawLineUnderMouseDate(DData);
DrawCurrentLine(DData);
DrawCursor(DData);
finally
DData.Free;
EndDraw;
Free;
end;
end;
end;
procedure TFormMain.FormCreate(Sender: TObject);
begin
FLines := TLines.Create;
FInputMode := imLine;
FOffsetCoord := Point(0, 0);
UpdateInfo;
end;
procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_ESCAPE:
begin
FDrawCurrent := False;
InputMode := imLine;
end;
end;
end;
procedure TFormMain.MenuItemLineDeleteClick(Sender: TObject);
begin
FLines.DeleteWithUID(FLineUnderMouseUID);
end;
procedure TFormMain.SetCenter;
begin
InputMode := imSetCenter;
end;
procedure TFormMain.SetCoordsVisible(const Value: Boolean);
begin
FCoordsVisible := Value;
btnCoordLines.NotifyVisible := FCoordsVisible;
end;
procedure TFormMain.SetInputMode(const Value: TInputMode);
begin
case FInputMode of //Åñëè âäðóã ÷òî íóæíî ïðîâåðèòü ïåðåä ñìåíîé
imLine:
;
imSetCenter:
;
end;
FInputMode := Value;
DrawPanel.Repaint;
end;
procedure TFormMain.SetLineUnderMouseUID(const Value: Integer);
begin
FLineUnderMouseUID := Value;
end;
procedure TFormMain.TimerDrawTimer(Sender: TObject);
begin
DrawPanel.Repaint;
end;
{ TDrawData }
constructor TDrawData.Create(ATarget: TDirect2DCanvas; AFieldRect: TRect);
begin
Target := ATarget;
FieldRect := AFieldRect;
end;
{ T2DCanvasAdv }
procedure T2DCanvasAdv.Ellipse(Pt: TPoint; Size: Word);
var
CRect: TRect;
begin
CRect := Rect(0, 0, Size, Size);
CRect.Offset(Pt);
Ellipse(CRect);
end;
procedure T2DCanvasAdv.LineTo(Pt: TPoint);
begin
LineTo(Pt.X, Pt.Y);
end;
procedure T2DCanvasAdv.MoveTo(Pt: TPoint);
begin
MoveTo(Pt.X, Pt.Y);
end;
procedure T2DCanvasAdv.RoundRect(Rt: TRect; Rnd: Word);
begin
RoundRect(Rt, Rnd, Rnd);
end;
{ TLine }
class function TLine.Create(A, B: TPoint): TLine;
begin
Result.UID := GetUID;
Result.PointA := A;
Result.PointB := B;
end;
function TLine.GetDistance: Double;
begin
Result := CalcDistance(PointA, PointB);
end;
{ TLines }
function TLines.DeleteWithUID(UID: Integer): Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to Count - 1 do
if Items[i].UID = UID then
begin
Delete(i);
Result := True;
Break;
end;
end;
function TLines.GetLine(UID: Integer; var Line: TLine): Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to Count - 1 do
if Items[i].UID = UID then
begin
Line := Items[i];
Result := True;
Break;
end;
end;
end.