forked from Frieve-A/Frieve-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawing.cpp
More file actions
920 lines (863 loc) · 26.1 KB
/
Copy pathdrawing.cpp
File metadata and controls
920 lines (863 loc) · 26.1 KB
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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "drawing.h"
#include <math.h>
#include <clipbrd.hpp>
//---------------------------------------------------------------------------
TDrawingItem::TDrawingItem() :
TList(),
m_nType(0),
m_bSelected(false),
m_bExist(false),
m_nPenColor(TColor(0x7fffffff)),
m_nBrushColor(TColor(-1))
{
m_Rect[0] = 0.0f;
m_Rect[1] = 0.0f;
m_Rect[2] = 0.0f;
m_Rect[3] = 0.0f;
}
//---------------------------------------------------------------------------
TDrawingItem::TDrawingItem(TDrawingItem &Source) :
TList(),
m_nType(Source.m_nType),
m_bSelected(Source.m_bSelected),
m_bExist(Source.m_bExist),
m_nPenColor(Source.m_nPenColor),
m_nBrushColor(Source.m_nBrushColor)
{
memcpy(m_Rect, Source.m_Rect, sizeof(m_Rect));
for (int i = 0 ; i < Source.Count ; i++) {
Add(Source.Items[i]);
}
}
//---------------------------------------------------------------------------
__fastcall TDrawingItem::~TDrawingItem()
{
}
//---------------------------------------------------------------------------
TDrawingItem::TDrawingItem(AnsiString S) :
TList(),
m_bSelected(true),
m_bExist(true)
{
//文字列からデコード
m_nType = StrToIntDef(AnsiString("0x") + S.SubString(1, 8), 0);
m_nPenColor = (TColor)StrToIntDef(AnsiString("0x") + S.SubString(9, 8), 0);
m_nBrushColor = (TColor)StrToIntDef(AnsiString("0x") + S.SubString(17, 8), 0);
int count = StrToIntDef(AnsiString("0x") + S.SubString(25, 8), 0);
int pos = 33;
for (int i = 0 ; i < 4 ; i++) {
int l = StrToIntDef(AnsiString("0x") + S.SubString(pos, 8), 0);
pos += 8;
m_Rect[i] = *(float*)&l;
}
for (int i = 0 ; i < count ; i++) {
Add((void*)StrToIntDef(AnsiString("0x") + S.SubString(pos, 8), 0));
pos += 8;
}
}
//---------------------------------------------------------------------------
AnsiString TDrawingItem::Encode()
{
//文字列で記述
AnsiString result;
result += IntToHex(m_nType, 8) + IntToHex(m_nPenColor, 8) + IntToHex(m_nBrushColor, 8) + IntToHex(Count, 8);
for (int i = 0 ; i < 4 ; i++) {
result += IntToHex(*(int*)&m_Rect[i], 8);
}
for (int i = 0 ; i < Count ; i++) {
result += IntToHex((int)Items[i], 8);
}
return result;
}
//---------------------------------------------------------------------------
void TDrawingItem::UpdateFreeHandRect(float x, float y)
{
Add((void*)*(int*)&x);
Add((void*)*(int*)&y);
m_Rect[0] = m_Rect[1] = 0.0f;
m_Rect[2] = m_Rect[3] = 1.0f;
}
//---------------------------------------------------------------------------
void TDrawingItem::FinishFreeHandRect()
{
for (int i = 0 ; i < Count / 2 ; i++){
int ix = (int)Items[i * 2];
float x = *(float*)&ix;
int iy = (int)Items[i * 2 + 1];
float y = *(float*)&iy;
if (i == 0){
//最初の1回
m_Rect[0] = m_Rect[2] = x;
m_Rect[1] = m_Rect[3] = y;
}else{
//矩形を更新
if (m_Rect[0] > x){
m_Rect[0] = x;
}
if (m_Rect[1] > y){
m_Rect[1] = y;
}
if (m_Rect[2] < x){
m_Rect[2] = x;
}
if (m_Rect[3] < y){
m_Rect[3] = y;
}
}
}
//正規化
float w = m_Rect[2] - m_Rect[0];
float h = m_Rect[3] - m_Rect[1];
if (w == 0.0f){
w = 1.0f;
}
if (h == 0.0f){
h = 1.0f;
}
for (int i = 0 ; i < Count / 2 ; i++){
int ix = (int)Items[i * 2];
float x = *(float*)&ix;
int iy = (int)Items[i * 2 + 1];
float y = *(float*)&iy;
x = (x - m_Rect[0]) / w;
y = (y - m_Rect[1]) / h;
Items[i * 2] = (void*)*(int*)&x;
Items[i * 2 + 1] = (void*)*(int*)&y;
}
}
//---------------------------------------------------------------------------
void TDrawingItem::SetType(int nType)
{
m_nType = nType;
}
//---------------------------------------------------------------------------
bool TDrawingItem::MouseDown(float x, float y)
{
bool result = false;//編集されていない
switch (m_nType){
case 1://FreeHand
UpdateFreeHandRect(x, y);
break;
default:
m_Rect[0] = x;
m_Rect[1] = y;
result = true;
break;
}
return result;
}
//---------------------------------------------------------------------------
bool TDrawingItem::MouseMove(float x, float y)
{
bool result = false;//編集されていない
switch (m_nType){
case 1://FreeHand
UpdateFreeHandRect(x, y);
break;
default:
m_Rect[2] = x;
m_Rect[3] = y;
result = true;
break;
}
m_bExist = true;
return result;
}
//---------------------------------------------------------------------------
bool TDrawingItem::MouseUp(float x, float y)
{
bool result = false;//編集されていない
switch (m_nType){
case 1://FreeHand
FinishFreeHandRect();
break;
}
return result;
}
//---------------------------------------------------------------------------
float LineToPoint(float x0, float y0, float x1, float y1, float px, float py)
{
float dx = x1 - x0;
float dy = y1 - y0;
float a = dx * dx + dy * dy;
if (a == 0.0f) {
return sqrt((x0-px)*(x0-px) + (y0-py)*(y0-py));
}
float b = dx * (x0-px) + dy * (y0-py);
float t =- (b / a);
if (t < 0.0f) {
t = 0.0f;
}
if (t > 1.0f) {
t = 1.0f;
}
float x = t * dx + x0;
float y = t * dy + y0;
return sqrt((x-px)*(x-px) + (y-py)*(y-py));
}
int TDrawingItem::CheckDrag(float x, float y, int size)
{
//ドラッグ開始可能かを調べる。ドラッグの種類を返す
m_fDragStartX = x;
m_fDragStartY = y;
memcpy(m_DragRect, m_Rect, sizeof(m_Rect));
//実座標へ変換
int rx = x * size;
int ry = y * size;
//4隅
for (int i = 0 ; i < 4 ; i++){
if (m_nType != 2 || i == 0 || i == 3){
if (rx >= size * m_Rect[(i % 2) * 2] - 4 &&
ry >= size * m_Rect[(i / 2) * 2 + 1] - 4 &&
rx < size * m_Rect[(i % 2) * 2] + 4 &&
ry < size * m_Rect[(i / 2) * 2 + 1] + 4){
return 0x10 | i;
}
}
}
//平行移動
switch(m_nType){
case 1://FreeHand
{
float w2 = m_Rect[2] - m_Rect[0];
float h2 = m_Rect[3] - m_Rect[1];
int result = 0;
if (w2 != 0.0f && h2 != 0.0f){
float *p = new float[Count];
for (int i = 0 ; i < Count / 2 ; i++){
int ix = (int)Items[i * 2];
float fx = *(float*)&ix;
int iy = (int)Items[i * 2 + 1];
float fy = *(float*)&iy;
p[i * 2] = m_Rect[0] + fx * w2;
p[i * 2 + 1] = m_Rect[1] + fy * h2;
}
for (int i = 1 ; i < Count / 2 ; i++){
if (LineToPoint(size * p[(i - 1) * 2], size * p[(i - 1) * 2 + 1], size * p[i * 2], size * p[i * 2 + 1], rx, ry) <= 4){
result = 0x01;
}
}
delete[] p;
}
if (result) {
return result;
}
}
break;
case 2://Line
if (LineToPoint(size * m_Rect[0], size * m_Rect[1], size * m_Rect[2], size * m_Rect[3], rx, ry) <= 4){
return 0x01;
}
break;
case 3://Box
if (LineToPoint(size * m_Rect[0], size * m_Rect[1], size * m_Rect[0], size * m_Rect[3], rx, ry) <= 4 ||
LineToPoint(size * m_Rect[0], size * m_Rect[1], size * m_Rect[2], size * m_Rect[1], rx, ry) <= 4 ||
LineToPoint(size * m_Rect[2], size * m_Rect[1], size * m_Rect[2], size * m_Rect[3], rx, ry) <= 4 ||
LineToPoint(size * m_Rect[0], size * m_Rect[3], size * m_Rect[2], size * m_Rect[3], rx, ry) <= 4){
return 0x01;
}
break;
case 4://Circle
{
if (m_Rect[3] - m_Rect[1] == 0.0f || m_Rect[2] - m_Rect[0] == 0.0f) {
//円が線状である場合
if (LineToPoint(size * m_Rect[0], size * m_Rect[1], size * m_Rect[2], size * m_Rect[3], rx, ry) <= 4){
return 0x01;
}
}else{
double xx = (m_Rect[3] - m_Rect[1]) / (m_Rect[2] - m_Rect[0]);//楕円を円にするためのXにかける係数
double cx = (m_Rect[2] + m_Rect[0]) * 0.5 * xx;//円にした場合のX座標中心
double cy = (m_Rect[3] + m_Rect[1]) * 0.5;//Y座標中心
double dx = x * xx;//円にした場合のカーソル位置
double dist = sqrt((dx - cx) * (dx - cx) + (y - cy) * (y - cy));//円にした場合の中心とカーソルの距離
double r = fabs(m_Rect[3] - m_Rect[1]) * 0.5;//円にした場合の半径
double rd = r / dist;//中心からカーソル位置にrdだけ近づけた点が円周上の点
double ccx = (cx * (1.0 - rd) + dx * rd) / xx;//円周上のX
double ccy = cy * (1.0 - rd) + y * rd;//円周上のY
if (sqrt((x - ccx) * (x - ccx) * size * size + (y - ccy) * (y - ccy) * size * size) <= 6){
//円周上の点からカーソル位置までの距離が小さければドラッグ
return 0x01;
}
}
}
break;
}
return 0;//ドラッグ不能
}
//---------------------------------------------------------------------------
bool TDrawingItem::Drag(float x, float y, int dragmode)
{
bool result = false;
if (dragmode == 0x01) {
//並行移動
m_Rect[0] = m_DragRect[0] + x - m_fDragStartX;
m_Rect[2] = m_DragRect[2] + x - m_fDragStartX;
m_Rect[1] = m_DragRect[1] + y - m_fDragStartY;
m_Rect[3] = m_DragRect[3] + y - m_fDragStartY;
result = true;
}else if (dragmode >= 0x10 && dragmode < 0x14){
//4隅のドラッグ
m_Rect[((dragmode & 0x3) % 2) * 2] = x;
m_Rect[((dragmode & 0x3) / 2) * 2 + 1] = y;
result = true;
}
return result;
}
//---------------------------------------------------------------------------
void TDrawingItem::Draw(TCanvas *C, TRect &R)
{
if (!m_bExist){
//描画に十分な情報がまだない
return;
}
int w = R.right - R.left;
if (w < 1){
w = 1;
}
int h = R.bottom - R.top;
if (h < 1){
h = 1;
}
TPen *P = C->Pen;
TColor pcbak = P->Color;
int pwbak = P->Width;
if (m_nPenColor >= 0){
if (m_nPenColor <= 0xffffff){
P->Color = m_nPenColor;
}
P->Style = psSolid;
}else{
P->Style = psClear;
}
TBrush *B = C->Brush;
TColor bcbak = B->Color;
if (m_nBrushColor >= 0){
if (m_nBrushColor <= 0xffffff){
B->Color = m_nBrushColor;
}
B->Style = bsSolid;
}else{
B->Style = bsClear;
}
switch(m_nType){
case 0://選択
P->Width = 1;
P->Style = psDot;
P->Color = TColor(0xffffff);
P->Mode = pmXor;
B->Style = bsClear;
C->Rectangle(
R.left + w * m_Rect[0],
R.top + h * m_Rect[1],
R.left + w * m_Rect[2],
R.top + h * m_Rect[3]
);
break;
case 1://FreeHand
{
/*
float xmin = 0.0f, xmax = 1.0f, ymin = 0.0f, ymax = 1.0f;
if (m_Rect[0] != 0.0f || m_Rect[1] != 0.0f || m_Rect[2] != 1.0f || m_Rect[3] != 1.0f){
for (int i = 0 ; i < Count / 2 ; i++){
int ix = (int)Items[i * 2];
float x = *(float*)&ix;
int iy = (int)Items[i * 2 + 1];
float y = *(float*)&iy;
if (i == 0 || x < xmin){
xmin = x;
}
if (i == 0 || y < ymin){
ymin = y;
}
if (i == 0 || x > xmax){
xmax = x;
}
if (i == 0 || y > ymax){
ymax = y;
}
}
}
//TPoint用意
float w2 = m_Rect[2] - m_Rect[0];
float h2 = m_Rect[3] - m_Rect[1];
if (w2 >= 0.0f && h2 >= 0.0f && xmin != xmax && ymin != ymax){
TPoint *p = new TPoint[Count / 2];
for (int i = 0 ; i < Count / 2 ; i++){
int ix = (int)Items[i * 2];
float x = *(float*)&ix;
int iy = (int)Items[i * 2 + 1];
float y = *(float*)&iy;
p[i].x = R.Left + w * (m_Rect[0] + (x - xmin) / (xmax - xmin) * w2);
p[i].y = R.Top + h * (m_Rect[1] + (y - ymin) / (ymax - ymin) * h2);
}
for (int i = 0 ; i < Count / 2 ; i++){
if (i == 0){
C->MoveTo(p[i].x, p[i].y);
}else{
C->LineTo(p[i].x, p[i].y);
}
}
delete[] p;
}
*/
//TPoint用意
float w2 = m_Rect[2] - m_Rect[0];
float h2 = m_Rect[3] - m_Rect[1];
if (w2 == 0.0f) {
w2 = 0.1f;
}
if (h2 == 0.0f) {
h2 = 0.1f;
}
TPoint *p = new TPoint[Count / 2];
for (int i = 0 ; i < Count / 2 ; i++){
int ix = (int)Items[i * 2];
float x = *(float*)&ix;
int iy = (int)Items[i * 2 + 1];
float y = *(float*)&iy;
p[i].x = R.Left + w * (m_Rect[0] + x * w2);
p[i].y = R.Top + h * (m_Rect[1] + y * h2);
}
for (int i = 0 ; i < Count / 2 ; i++){
if (i == 0){
C->MoveTo(p[i].x, p[i].y);
}else{
C->LineTo(p[i].x, p[i].y);
}
}
delete[] p;
}
break;
case 2://Line
C->MoveTo(
R.left + w * m_Rect[0],
R.top + h * m_Rect[1]
);
C->LineTo(
R.left + w * m_Rect[2],
R.top + h * m_Rect[3]
);
break;
case 3://Box
C->Rectangle(
R.left + w * m_Rect[0],
R.top + h * m_Rect[1],
R.left + w * m_Rect[2],
R.top + h * m_Rect[3]
);
break;
case 4://Circle
C->Ellipse(
R.left + w * m_Rect[0],
R.top + h * m_Rect[1],
R.left + w * m_Rect[2],
R.top + h * m_Rect[3]
);
break;
}
P->Color = pcbak;
B->Color = bcbak;
//選択矩形を表示
if (m_bSelected){
P->Style = psSolid;
P->Width = 1;
B->Style = bsClear;
for (int i = 0 ; i < 4 ; i++){
if (m_nType != 2 || i == 0 || i == 3){
C->Rectangle(
R.left + w * m_Rect[(i % 2) * 2] - 4,
R.top + h * m_Rect[(i / 2) * 2 + 1] - 4,
R.left + w * m_Rect[(i % 2) * 2] + 4,
R.top + h * m_Rect[(i / 2) * 2 + 1] + 4
);
}
}
}
P->Width = pwbak;
}
//---------------------------------------------------------------------------
int TDrawingItem::SetPenColor(TColor C)
{
//色を変更。変更できたらtrueを返す
switch (m_nType) {
case 1:
case 2:
case 3:
case 4:
if (m_nPenColor != C) {
m_nPenColor = C;
return true;
}
break;
}
return false;
}
//---------------------------------------------------------------------------
int TDrawingItem::SetBrushColor(TColor C)
{
switch (m_nType) {
case 3:
case 4:
if (m_nBrushColor != C) {
m_nBrushColor = C;
return true;
}
break;
}
return false;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
TDrawing::TDrawing() :
TList(),
m_nTool(0),
m_TmpItem(NULL),
m_nDragMode(0),
m_nDragIndex(0),
m_bDrawRequest(true),
m_bModified(false),
m_nPenColor(TColor(0x7fffffff)),
m_nBrushColor(TColor(-1))
{
}
//---------------------------------------------------------------------------
TDrawing::TDrawing(TDrawing &Source) :
TList(),
m_nTool(0),
m_TmpItem(NULL),
m_nDragMode(0),
m_nDragIndex(0),
m_bDrawRequest(true),
m_bModified(false),
m_nPenColor(TColor(0x7fffffff)),
m_nBrushColor(TColor(-1))
{
for (int i = 0 ; i < Source.Count ; i++) {
Add(new TDrawingItem(*Source.DItem(i)));
}
ClearSelection();
}
//---------------------------------------------------------------------------
TDrawing::TDrawing(AnsiString S) :
TList(),
m_nTool(0),
m_TmpItem(NULL),
m_nDragMode(0),
m_nDragIndex(0),
m_bDrawRequest(true),
m_bModified(false),
m_nPenColor(TColor(0x7fffffff)),
m_nBrushColor(TColor(-1))
{
//文字列からデコード
int count = StrToIntDef(AnsiString("0x") + S.SubString(1, 8), 0);
int pos = 9;
for (int i = 0 ; i < count ; i++) {
int len = StrToIntDef(AnsiString("0x") + S.SubString(pos, 8), 0);
pos += 8;
Add(new TDrawingItem(S.SubString(pos, len)));
pos += len;
}
}
//---------------------------------------------------------------------------
AnsiString TDrawing::Encode(bool bAll)
{
//文字列で記述
AnsiString result;
int count = 0;
for (int i = 0 ; i < Count ; i++) {
if (bAll || DItem(i)->m_bSelected) {
//エンコード対象のアイテム
AnsiString S = DItem(i)->Encode();
result += IntToHex(S.Length(), 8) + S;
count++;
}
}
return IntToHex(count, 8) + result;
}
//---------------------------------------------------------------------------
__fastcall TDrawing::~TDrawing()
{
FreeTmpItem();
for (int i = 0 ; i < Count ; i++){
delete DItem(i);
}
}
//---------------------------------------------------------------------------
void TDrawing::FreeTmpItem()
{
if (m_TmpItem){
delete m_TmpItem;
}
m_TmpItem = NULL;
}
//---------------------------------------------------------------------------
TDrawingItem *TDrawing::DItem(int index)
{
return (TDrawingItem*)Items[index];
}
//---------------------------------------------------------------------------
void TDrawing::SetTool(int nToolID)
{
m_nTool = nToolID;
}
//---------------------------------------------------------------------------
void TDrawing::MouseDown(float x, float y, int size, TShiftState Shift)
{
m_nDragMode = 0;
FreeTmpItem();
if (m_nTool == 0 && !Shift.Contains(ssShift)){
//選択操作
//ドラッグ開始判定
//前面から順にループ
for (int i = Count - 1 ; i >= 0 ; i--){
//一度全てのアイテムに対して呼ぶ(m_DragRectを初期化するため。美しくない)
DItem(i)->CheckDrag(x, y, size);
}
for (int i = Count - 1 ; i >= 0 ; i--){
m_nDragMode = DItem(i)->CheckDrag(x, y, size);
if (m_nDragMode){
//ドラッグが開始された
if (!Shift.Contains(ssCtrl)) {
if (!DItem(i)->m_bSelected) {
ClearSelection();
DItem(i)->m_bSelected = true;
}
}else{
//Ctrlを押しながら
DItem(i)->m_bSelected = !DItem(i)->m_bSelected;
}
m_nDragIndex = i;
break;
}
}
if (m_nDragMode){
//ドラッグが開始された
m_bDrawRequest = true;
m_fDragX = x;
m_fDragY = y;
return;
}
}
if (!Shift.Contains(ssCtrl)) {
ClearSelection();
}
m_TmpItem = new TDrawingItem();
m_TmpItem->SetType(m_nTool);
m_TmpItem->m_nPenColor = m_nPenColor;
m_TmpItem->m_nBrushColor = m_nBrushColor;
m_TmpItem->MouseDown(x, y);
/*
if (m_nTool == 0){
//選択操作
}else{
ClearSelection();
m_TmpItem = new TDrawingItem();
m_TmpItem->SetType(m_nTool);
m_TmpItem->MouseDown(x, y);
}
*/
m_bDrawRequest = true;
}
//---------------------------------------------------------------------------
void TDrawing::MouseMove(float x, float y)
{
if (m_nDragMode){
if (m_nDragMode == 0x01){
for (int i = 0 ; i < Count ; i++) if (DItem(i)->m_bSelected){
m_bModified |= DItem(i)->Drag(x, y, m_nDragMode);
}
}else if (m_nDragMode >= 0x10 && m_nDragMode < 0x14){
m_bModified |= DItem(m_nDragIndex)->Drag(x, y, m_nDragMode);
}
}
if (m_TmpItem){
m_TmpItem->MouseMove(x, y);
m_bDrawRequest = true;
}
/*
if (m_nTool == 0){
//選択操作
}else{
if (m_TmpItem){
m_TmpItem->MouseMove(x, y);
}
}
*/
}
//---------------------------------------------------------------------------
void TDrawing::MouseUp(float x, float y)
{
m_nDragMode = 0;
if (m_TmpItem){
if (m_nTool == 0){
//選択操作
if (m_TmpItem->m_bExist){
SelectRect(
m_TmpItem->m_Rect[0], m_TmpItem->m_Rect[1],
m_TmpItem->m_Rect[2], m_TmpItem->m_Rect[3]
);
}
}else{
m_TmpItem->MouseUp(x, y);
if (m_TmpItem->m_bExist){
//新規アイテム追加
m_TmpItem->m_bSelected = true;
Add(m_TmpItem);
m_bModified = true;
m_TmpItem = NULL;
}
}
FreeTmpItem();
m_bDrawRequest = true;
}
}
//---------------------------------------------------------------------------
void TDrawing::Draw(TCanvas *C, TRect &R)
{
for (int i = 0 ; i < Count ; i++){
DItem(i)->Draw(C, R);
}
if (m_TmpItem){
m_TmpItem->Draw(C, R);
}
}
//---------------------------------------------------------------------------
void TDrawing::SelectRect(float x1, float y1, float x2, float y2)
{
float x1_ = x1 < x2 ? x1 : x2;
float x2_ = x1 >= x2 ? x1 : x2;
float y1_ = y1 < y2 ? y1 : y2;
float y2_ = y1 >= y2 ? y1 : y2;
for (int i = 0 ; i < Count ; i++){
TDrawingItem *DI = DItem(i);
DI->m_bSelected = true;
for (int i2 = 0 ; i2 < 2 ; i2++){
DI->m_bSelected &= DI->m_Rect[i2 * 2] >= x1_ && DI->m_Rect[i2 * 2] < x2_;
DI->m_bSelected &= DI->m_Rect[i2 * 2 + 1] >= y1_ && DI->m_Rect[i2 * 2 + 1] < y2_;
}
}
m_bDrawRequest = true;
}
//---------------------------------------------------------------------------
void TDrawing::ClearSelection()
{
for (int i = 0 ; i < Count ; i++){
DItem(i)->m_bSelected = false;
}
m_bDrawRequest = true;
}
//---------------------------------------------------------------------------
void TDrawing::SelectAll()
{
for (int i = 0 ; i < Count ; i++){
DItem(i)->m_bSelected = true;
}
m_bDrawRequest = true;
}
//---------------------------------------------------------------------------
int TDrawing::Selected()
{
int result = 0;
for (int i = 0 ; i < Count ; i++){
if (DItem(i)->m_bSelected){
result++;
}
}
return result;
}
//---------------------------------------------------------------------------
void TDrawing::Cut()
{
Copy();
DDelete();
}
//---------------------------------------------------------------------------
void TDrawing::Copy()
{
Clipboard()->SetTextBuf((AnsiString("Drawing:") + Encode()).c_str());
}
//---------------------------------------------------------------------------
void TDrawing::Paste()
{
if (!Clipboard()->HasFormat(CF_TEXT)){
return;
}
AnsiString S = Clipboard()->AsText;
if (S.SubString(1, 8) != "Drawing:") {
return;
}
ClearSelection();
TDrawing *Source = new TDrawing(S.SubString(9, S.Length()));
int existingnum = Count;
for (int i = 0 ; i < Source->Count ; i++) {
TDrawingItem *Item = Source->DItem(i);
//座標更新
bool found = true;
while (found){//同じ座標のオブジェクトがある間ループ
found = false;
for (int i2 = 0 ; i2 < existingnum ; i2++) {
TDrawingItem *Item2 = DItem(i2);
bool same = true;
for (int i3 = 0 ; i3 < 4 && same ; i3++) {
same &= fabs(Item->m_Rect[i3] - Item2->m_Rect[i3]) < 0.001f;
}
if (same) {
for (int i3 = 0 ; i3 < 4 ; i3++) {
Item->m_Rect[i3] += 1.0f / 32;
}
found = true;
break;
}
}
}
Add(new TDrawingItem(*Item));
m_bModified = true;
m_bDrawRequest = true;
}
delete Source;
}
//---------------------------------------------------------------------------
void TDrawing::DDelete()
{
for (int i = Count - 1 ; i >= 0 ; i--){
if (DItem(i)->m_bSelected){
delete DItem(i);
Delete(i);
}
}
m_bModified = true;
m_bDrawRequest = true;
}
//---------------------------------------------------------------------------
void TDrawing::ChangePenColor(TColor C)
{
bool changed = false;
for (int i = 0 ; i < Count ; i++) {
if (DItem(i)->m_bSelected) {
changed |= DItem(i)->SetPenColor(C);
}
}
if (changed) {
m_bModified = true;
m_bDrawRequest = true;
}
}
//---------------------------------------------------------------------------
void TDrawing::ChangeBrushColor(TColor C)
{
bool changed = false;
for (int i = 0 ; i < Count ; i++) {
if (DItem(i)->m_bSelected) {
changed |= DItem(i)->SetBrushColor(C);
}
}
if (changed) {
m_bModified = true;
m_bDrawRequest = true;
}
}
//---------------------------------------------------------------------------
#pragma package(smart_init)