-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse.cpp
1271 lines (1085 loc) · 34.3 KB
/
mouse.cpp
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
/*
* PROJECT: PAINT for ReactOS
* LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
* PURPOSE: Things which should not be in the mouse event handler itself
* COPYRIGHT: Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
* Copyright 2021-2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
*/
#include "precomp.h"
#include <atlalloc.h>
static SIZE_T s_cPoints = 0;
static CHeapPtr<POINT, CLocalAllocator> s_dynamicPoints;
static POINT s_staticPoints[512]; // 512 is enough
static SIZE_T s_maxPoints = _countof(s_staticPoints);
static LPPOINT s_pPoints = s_staticPoints;
static POINT g_ptStart, g_ptEnd;
/* FUNCTIONS ********************************************************/
void
regularize(LONG x0, LONG y0, LONG& x1, LONG& y1)
{
if (labs(x1 - x0) >= labs(y1 - y0))
y1 = y0 + (y1 > y0 ? labs(x1 - x0) : -labs(x1 - x0));
else
x1 = x0 + (x1 > x0 ? labs(y1 - y0) : -labs(y1 - y0));
}
void
roundTo8Directions(LONG x0, LONG y0, LONG& x1, LONG& y1)
{
if (labs(x1 - x0) >= labs(y1 - y0))
{
if (labs(y1 - y0) * 5 < labs(x1 - x0) * 2)
y1 = y0;
else
y1 = y0 + (y1 > y0 ? labs(x1 - x0) : -labs(x1 - x0));
}
else
{
if (labs(x1 - x0) * 5 < labs(y1 - y0) * 2)
x1 = x0;
else
x1 = x0 + (x1 > x0 ? labs(y1 - y0) : -labs(y1 - y0));
}
}
BOOL nearlyEqualPoints(INT x0, INT y0, INT x1, INT y1)
{
INT cxThreshold = toolsModel.GetLineWidth() + UnZoomed(GetSystemMetrics(SM_CXDRAG));
INT cyThreshold = toolsModel.GetLineWidth() + UnZoomed(GetSystemMetrics(SM_CYDRAG));
return (abs(x1 - x0) <= cxThreshold) && (abs(y1 - y0) <= cyThreshold);
}
void getBoundaryOfPoints(RECT& rcBoundary, SIZE_T cPoints, const POINT *pPoints)
{
POINT ptMin = { MAXLONG, MAXLONG }, ptMax = { (LONG)MINLONG, (LONG)MINLONG };
while (cPoints-- > 0)
{
LONG x = pPoints->x, y = pPoints->y;
ptMin = { min(x, ptMin.x), min(y, ptMin.y) };
ptMax = { max(x, ptMax.x), max(y, ptMax.y) };
++pPoints;
}
ptMax.x += 1;
ptMax.y += 1;
CRect rc(ptMin, ptMax);
rcBoundary = rc;
}
void ShiftPoints(INT dx, INT dy)
{
for (SIZE_T i = 0; i < s_cPoints; ++i)
{
POINT& pt = s_pPoints[i];
pt.x += dx;
pt.y += dy;
}
}
void BuildMaskFromPoints()
{
CRect rc;
getBoundaryOfPoints(rc, s_cPoints, s_pPoints);
ShiftPoints(-rc.left, -rc.top);
HDC hdcMem = ::CreateCompatibleDC(NULL);
HBITMAP hbmMask = ::CreateBitmap(rc.Width(), rc.Height(), 1, 1, NULL);
HGDIOBJ hbmOld = ::SelectObject(hdcMem, hbmMask);
::FillRect(hdcMem, &rc, (HBRUSH)::GetStockObject(BLACK_BRUSH));
HGDIOBJ hPenOld = ::SelectObject(hdcMem, GetStockObject(NULL_PEN));
HGDIOBJ hbrOld = ::SelectObject(hdcMem, GetStockObject(WHITE_BRUSH));
::Polygon(hdcMem, s_pPoints, (INT)s_cPoints);
::SelectObject(hdcMem, hbrOld);
::SelectObject(hdcMem, hPenOld);
::SelectObject(hdcMem, hbmOld);
::DeleteDC(hdcMem);
selectionModel.setMask(rc, hbmMask);
}
void ToolBase::reset()
{
if (s_pPoints != s_staticPoints)
{
s_dynamicPoints.Free();
s_pPoints = s_staticPoints;
s_maxPoints = _countof(s_staticPoints);
}
s_cPoints = 0;
g_ptEnd = g_ptStart = { -1, -1 };
if (selectionModel.m_bShow)
{
selectionModel.Landing();
selectionModel.HideSelection();
}
}
void ToolBase::OnEndDraw(BOOL bCancel)
{
reset();
imageModel.NotifyImageChanged();
}
void ToolBase::beginEvent()
{
m_hdc = imageModel.GetDC();
m_fg = paletteModel.GetFgColor();
m_bg = paletteModel.GetBgColor();
}
void ToolBase::endEvent()
{
m_hdc = NULL;
}
static void pushToPoints(LONG x, LONG y)
{
if (s_cPoints + 1 >= s_maxPoints)
{
SIZE_T newMax = s_maxPoints + 512;
SIZE_T cbNew = newMax * sizeof(POINT);
if (!s_dynamicPoints.ReallocateBytes(cbNew))
{
ATLTRACE("%d, %d, %d\n", (INT)s_cPoints, (INT)s_maxPoints, (INT)cbNew);
return;
}
if (s_pPoints == s_staticPoints)
CopyMemory(s_dynamicPoints, s_staticPoints, s_cPoints * sizeof(POINT));
s_pPoints = s_dynamicPoints;
s_maxPoints = newMax;
}
s_pPoints[s_cPoints++] = { x, y };
}
/* TOOLS ********************************************************/
struct TwoPointDrawTool : ToolBase
{
BOOL m_bLeftButton = FALSE;
BOOL m_bDrawing = FALSE;
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
m_bLeftButton = bLeftButton;
m_bDrawing = TRUE;
imageModel.NotifyImageChanged();
}
BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) override
{
imageModel.NotifyImageChanged();
return TRUE;
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
CRect rcPartial(g_ptStart, g_ptEnd);
rcPartial.NormalizeRect();
SIZE size = toolsModel.GetToolSize();
rcPartial.InflateRect((size.cx + 1) / 2, (size.cy + 1) / 2);
imageModel.PushImageForUndo(rcPartial);
OnDrawOverlayOnImage(m_hdc);
m_bDrawing = FALSE;
imageModel.NotifyImageChanged();
return TRUE;
}
void OnEndDraw(BOOL bCancel) override
{
m_bDrawing = FALSE;
ToolBase::OnEndDraw(bCancel);
}
void OnSpecialTweak(BOOL bMinus) override
{
toolsModel.MakeLineThickerOrThinner(bMinus);
}
};
typedef enum DIRECTION
{
NO_DIRECTION = -1,
DIRECTION_HORIZONTAL,
DIRECTION_VERTICAL,
DIRECTION_DIAGONAL_RIGHT_DOWN,
DIRECTION_DIAGONAL_RIGHT_UP,
} DIRECTION;
#define THRESHOULD_DEG 15
static DIRECTION
GetDirection(LONG x0, LONG y0, LONG x1, LONG y1)
{
LONG dx = x1 - x0, dy = y1 - y0;
if (labs(dx) <= 8 && labs(dy) <= 8)
return NO_DIRECTION;
double radian = atan2((double)dy, (double)dx);
if (radian < DEG2RAD(-180 + THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_HORIZONTAL: %ld\n", RAD2DEG(radian));
return DIRECTION_HORIZONTAL;
}
if (radian < DEG2RAD(-90 - THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_DIAGONAL_RIGHT_DOWN: %ld\n", RAD2DEG(radian));
return DIRECTION_DIAGONAL_RIGHT_DOWN;
}
if (radian < DEG2RAD(-90 + THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_VERTICAL: %ld\n", RAD2DEG(radian));
return DIRECTION_VERTICAL;
}
if (radian < DEG2RAD(-THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_DIAGONAL_RIGHT_UP: %ld\n", RAD2DEG(radian));
return DIRECTION_DIAGONAL_RIGHT_UP;
}
if (radian < DEG2RAD(+THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_HORIZONTAL: %ld\n", RAD2DEG(radian));
return DIRECTION_HORIZONTAL;
}
if (radian < DEG2RAD(+90 - THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_DIAGONAL_RIGHT_DOWN: %ld\n", RAD2DEG(radian));
return DIRECTION_DIAGONAL_RIGHT_DOWN;
}
if (radian < DEG2RAD(+90 + THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_VERTICAL: %ld\n", RAD2DEG(radian));
return DIRECTION_VERTICAL;
}
if (radian < DEG2RAD(+180 - THRESHOULD_DEG))
{
ATLTRACE("DIRECTION_DIAGONAL_RIGHT_UP: %ld\n", RAD2DEG(radian));
return DIRECTION_DIAGONAL_RIGHT_UP;
}
ATLTRACE("DIRECTION_HORIZONTAL: %ld\n", RAD2DEG(radian));
return DIRECTION_HORIZONTAL;
}
static void
RestrictDrawDirection(DIRECTION dir, LONG x0, LONG y0, LONG& x1, LONG& y1)
{
switch (dir)
{
case NO_DIRECTION:
default:
return;
case DIRECTION_HORIZONTAL:
y1 = y0;
break;
case DIRECTION_VERTICAL:
x1 = x0;
break;
case DIRECTION_DIAGONAL_RIGHT_DOWN:
y1 = y0 + (x1 - x0);
break;
case DIRECTION_DIAGONAL_RIGHT_UP:
x1 = x0 - (y1 - y0);
break;
}
}
struct SmoothDrawTool : ToolBase
{
DIRECTION m_direction = NO_DIRECTION;
BOOL m_bShiftDown = FALSE;
BOOL m_bLeftButton = FALSE;
virtual void OnDraw(HDC hdc, BOOL bLeftButton, POINT pt0, POINT pt1) = 0;
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
m_direction = NO_DIRECTION;
m_bShiftDown = (::GetKeyState(VK_SHIFT) & 0x8000); // Is Shift key pressed?
m_bLeftButton = bLeftButton;
s_cPoints = 0;
pushToPoints(x, y);
pushToPoints(x, y); // We have to draw the first point
imageModel.NotifyImageChanged();
}
BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) override
{
if (!m_bShiftDown)
{
pushToPoints(x, y);
imageModel.NotifyImageChanged();
return TRUE;
}
if (m_direction == NO_DIRECTION)
{
m_direction = GetDirection(g_ptStart.x, g_ptStart.y, x, y);
if (m_direction == NO_DIRECTION)
return FALSE;
}
RestrictDrawDirection(m_direction, g_ptStart.x, g_ptStart.y, x, y);
pushToPoints(x, y);
imageModel.NotifyImageChanged();
return TRUE;
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
if (m_bShiftDown && m_direction != NO_DIRECTION)
RestrictDrawDirection(m_direction, g_ptStart.x, g_ptStart.y, x, y);
pushToPoints(x, y);
CRect rcPartial;
getBoundaryOfPoints(rcPartial, s_cPoints, s_pPoints);
SIZE size = toolsModel.GetToolSize();
rcPartial.InflateRect((size.cx + 1) / 2, (size.cy + 1) / 2);
imageModel.PushImageForUndo(rcPartial);
OnDrawOverlayOnImage(m_hdc);
imageModel.NotifyImageChanged();
OnEndDraw(FALSE);
return TRUE;
}
void OnDrawOverlayOnImage(HDC hdc) override
{
for (SIZE_T i = 1; i < s_cPoints; ++i)
{
OnDraw(hdc, m_bLeftButton, s_pPoints[i - 1], s_pPoints[i]);
}
}
};
struct SelectionBaseTool : ToolBase
{
BOOL m_bLeftButton = FALSE;
BOOL m_bCtrlKey = FALSE;
BOOL m_bShiftKey = FALSE;
BOOL m_bDrawing = FALSE;
BOOL m_bNoDrawBack = FALSE;
HITTEST m_hitSelection = HIT_NONE;
BOOL isRectSelect() const
{
return (toolsModel.GetActiveTool() == TOOL_RECTSEL);
}
void OnDrawOverlayOnImage(HDC hdc) override
{
if (selectionModel.IsLanded() || !selectionModel.m_bShow)
return;
if (!m_bNoDrawBack)
selectionModel.DrawBackground(hdc, selectionModel.m_rgbBack);
selectionModel.DrawSelection(hdc, paletteModel.GetBgColor(), toolsModel.IsBackgroundTransparent());
}
void OnDrawOverlayOnCanvas(HDC hdc) override
{
if (m_bDrawing || selectionModel.m_bShow)
selectionModel.drawFrameOnCanvas(hdc);
}
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
m_bLeftButton = bLeftButton;
m_bCtrlKey = (::GetKeyState(VK_CONTROL) < 0);
m_bShiftKey = (::GetKeyState(VK_SHIFT) < 0);
m_bDrawing = FALSE;
m_hitSelection = HIT_NONE;
POINT pt = { x, y };
if (!m_bLeftButton) // Show context menu on Right-click
{
canvasWindow.ImageToCanvas(pt);
canvasWindow.ClientToScreen(&pt);
mainWindow.TrackPopupMenu(pt, 0);
return;
}
POINT ptCanvas = pt;
canvasWindow.ImageToCanvas(ptCanvas);
HITTEST hit = selectionModel.hitTest(ptCanvas);
if (hit != HIT_NONE) // Dragging of selection started?
{
if (m_bCtrlKey || m_bShiftKey)
{
imageModel.PushImageForUndo();
toolsModel.OnDrawOverlayOnImage(imageModel.GetDC());
}
m_hitSelection = hit;
selectionModel.m_ptHit = pt;
selectionModel.TakeOff();
m_bNoDrawBack |= (m_bCtrlKey || m_bShiftKey);
imageModel.NotifyImageChanged();
return;
}
selectionModel.Landing();
m_bDrawing = TRUE;
imageModel.Clamp(pt);
if (isRectSelect())
{
selectionModel.SetRectFromPoints(g_ptStart, pt);
}
else
{
s_cPoints = 0;
pushToPoints(pt.x, pt.y);
}
imageModel.NotifyImageChanged();
}
BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) override
{
POINT pt = { x, y };
if (!m_bLeftButton)
return TRUE;
if (m_hitSelection != HIT_NONE) // Now dragging selection?
{
if (m_bShiftKey)
toolsModel.OnDrawOverlayOnImage(imageModel.GetDC());
selectionModel.Dragging(m_hitSelection, pt);
imageModel.NotifyImageChanged();
return TRUE;
}
if (isRectSelect() && ::GetKeyState(VK_SHIFT) < 0)
regularize(g_ptStart.x, g_ptStart.y, pt.x, pt.y);
imageModel.Clamp(pt);
if (isRectSelect())
selectionModel.SetRectFromPoints(g_ptStart, pt);
else
pushToPoints(pt.x, pt.y);
imageModel.NotifyImageChanged();
return TRUE;
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
POINT pt = { x, y };
m_bDrawing = FALSE;
if (!m_bLeftButton)
return TRUE;
if (m_hitSelection != HIT_NONE) // Dragging of selection ended?
{
if (m_bShiftKey)
toolsModel.OnDrawOverlayOnImage(imageModel.GetDC());
selectionModel.Dragging(m_hitSelection, pt);
m_hitSelection = HIT_NONE;
imageModel.NotifyImageChanged();
return TRUE;
}
if (isRectSelect() && ::GetKeyState(VK_SHIFT) < 0)
regularize(g_ptStart.x, g_ptStart.y, pt.x, pt.y);
imageModel.Clamp(pt);
if (isRectSelect())
{
selectionModel.SetRectFromPoints(g_ptStart, pt);
selectionModel.m_bShow = !selectionModel.m_rc.IsRectEmpty();
}
else
{
if (s_cPoints > 2)
{
BuildMaskFromPoints();
selectionModel.m_bShow = TRUE;
}
else
{
s_cPoints = 0;
selectionModel.m_bShow = FALSE;
}
}
m_bNoDrawBack = FALSE;
imageModel.NotifyImageChanged();
return TRUE;
}
void OnEndDraw(BOOL bCancel) override
{
if (bCancel)
selectionModel.HideSelection();
else
selectionModel.Landing();
m_bDrawing = FALSE;
m_hitSelection = HIT_NONE;
ToolBase::OnEndDraw(bCancel);
}
void OnSpecialTweak(BOOL bMinus) override
{
selectionModel.StretchSelection(bMinus);
}
};
// TOOL_FREESEL
struct FreeSelTool : SelectionBaseTool
{
void OnDrawOverlayOnImage(HDC hdc) override
{
SelectionBaseTool::OnDrawOverlayOnImage(hdc);
if (!selectionModel.m_bShow && m_bDrawing)
{
/* Draw the freehand selection inverted/xored */
Poly(hdc, s_pPoints, (INT)s_cPoints, 0, 0, 2, 0, FALSE, TRUE);
}
}
};
// TOOL_RECTSEL
struct RectSelTool : SelectionBaseTool
{
void OnDrawOverlayOnImage(HDC hdc) override
{
SelectionBaseTool::OnDrawOverlayOnImage(hdc);
if (!selectionModel.m_bShow && m_bDrawing)
{
CRect& rc = selectionModel.m_rc;
if (!rc.IsRectEmpty())
RectSel(hdc, rc.left, rc.top, rc.right, rc.bottom);
}
}
};
// TOOL_RUBBER
struct RubberTool : SmoothDrawTool
{
void OnDraw(HDC hdc, BOOL bLeftButton, POINT pt0, POINT pt1) override
{
if (bLeftButton)
Erase(hdc, pt0.x, pt0.y, pt1.x, pt1.y, m_bg, toolsModel.GetRubberRadius());
else
Replace(hdc, pt0.x, pt0.y, pt1.x, pt1.y, m_fg, m_bg, toolsModel.GetRubberRadius());
}
void OnSpecialTweak(BOOL bMinus) override
{
toolsModel.MakeRubberThickerOrThinner(bMinus);
}
};
// TOOL_FILL
struct FillTool : ToolBase
{
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
imageModel.PushImageForUndo();
Fill(m_hdc, x, y, bLeftButton ? m_fg : m_bg);
}
};
// TOOL_COLOR
struct ColorTool : ToolBase
{
void fetchColor(BOOL bLeftButton, LONG x, LONG y)
{
COLORREF rgbColor;
if (0 <= x && x < imageModel.GetWidth() && 0 <= y && y < imageModel.GetHeight())
rgbColor = GetPixel(m_hdc, x, y);
else
rgbColor = RGB(255, 255, 255); // Outside is white
if (bLeftButton)
paletteModel.SetFgColor(rgbColor);
else
paletteModel.SetBgColor(rgbColor);
}
BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) override
{
fetchColor(bLeftButton, x, y);
return TRUE;
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
fetchColor(bLeftButton, x, y);
toolsModel.SetActiveTool(toolsModel.GetOldActiveTool());
return TRUE;
}
};
// TOOL_ZOOM
struct ZoomTool : ToolBase
{
BOOL m_bZoomed = FALSE;
BOOL getNewZoomRect(CRect& rcView, INT newZoom);
void OnDrawOverlayOnCanvas(HDC hdc) override
{
CRect rcView;
INT oldZoom = toolsModel.GetZoom();
if (oldZoom < MAX_ZOOM && getNewZoomRect(rcView, oldZoom * 2))
DrawXorRect(hdc, &rcView);
}
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
INT newZoom, oldZoom = toolsModel.GetZoom();
if (bLeftButton)
newZoom = (oldZoom < MAX_ZOOM) ? (oldZoom * 2) : MIN_ZOOM;
else
newZoom = (oldZoom > MIN_ZOOM) ? (oldZoom / 2) : MAX_ZOOM;
m_bZoomed = FALSE;
if (oldZoom != newZoom)
{
CRect rcView;
if (getNewZoomRect(rcView, newZoom))
{
canvasWindow.zoomTo(newZoom, rcView.left, rcView.top);
m_bZoomed = TRUE;
}
}
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
if (m_bZoomed)
toolsModel.SetActiveTool(toolsModel.GetOldActiveTool());
return TRUE;
}
};
BOOL ZoomTool::getNewZoomRect(CRect& rcView, INT newZoom)
{
CPoint pt;
::GetCursorPos(&pt);
canvasWindow.ScreenToClient(&pt);
canvasWindow.getNewZoomRect(rcView, newZoom, pt);
CRect rc;
canvasWindow.GetImageRect(rc);
canvasWindow.ImageToCanvas(rc);
return rc.PtInRect(pt);
}
// TOOL_PEN
struct PenTool : SmoothDrawTool
{
void OnDraw(HDC hdc, BOOL bLeftButton, POINT pt0, POINT pt1) override
{
COLORREF rgb = bLeftButton ? m_fg : m_bg;
Line(hdc, pt0.x, pt0.y, pt1.x, pt1.y, rgb, toolsModel.GetPenWidth());
}
void OnSpecialTweak(BOOL bMinus) override
{
toolsModel.MakePenThickerOrThinner(bMinus);
}
};
// TOOL_BRUSH
struct BrushTool : SmoothDrawTool
{
void OnDraw(HDC hdc, BOOL bLeftButton, POINT pt0, POINT pt1) override
{
COLORREF rgb = bLeftButton ? m_fg : m_bg;
Brush(hdc, pt0.x, pt0.y, pt1.x, pt1.y, rgb, toolsModel.GetBrushStyle(),
toolsModel.GetBrushWidth());
}
void OnSpecialTweak(BOOL bMinus) override
{
toolsModel.MakeBrushThickerOrThinner(bMinus);
}
};
// TOOL_AIRBRUSH
struct AirBrushTool : SmoothDrawTool
{
DWORD m_dwTick = 0;
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
m_dwTick = GetTickCount();
SmoothDrawTool::OnButtonDown(bLeftButton, x, y, bDoubleClick);
}
void OnDrawOverlayOnImage(HDC hdc) override
{
srand(m_dwTick);
SmoothDrawTool::OnDrawOverlayOnImage(hdc);
}
void OnDraw(HDC hdc, BOOL bLeftButton, POINT pt0, POINT pt1) override
{
COLORREF rgb = bLeftButton ? m_fg : m_bg;
Airbrush(hdc, pt1.x, pt1.y, rgb, toolsModel.GetAirBrushRadius());
}
void OnSpecialTweak(BOOL bMinus) override
{
toolsModel.MakeAirBrushThickerOrThinner(bMinus);
}
};
// TOOL_TEXT
struct TextTool : ToolBase
{
void OnDrawOverlayOnImage(HDC hdc) override
{
if (canvasWindow.m_drawing)
{
CRect& rc = selectionModel.m_rc;
if (!rc.IsRectEmpty())
RectSel(hdc, rc.left, rc.top, rc.right, rc.bottom);
}
}
void UpdatePoint(LONG x, LONG y)
{
POINT pt = { x, y };
imageModel.Clamp(pt);
selectionModel.SetRectFromPoints(g_ptStart, pt);
imageModel.NotifyImageChanged();
}
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
if (!textEditWindow.IsWindow())
textEditWindow.Create(canvasWindow);
UpdatePoint(x, y);
}
BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) override
{
UpdatePoint(x, y);
return TRUE;
}
void draw(HDC hdc)
{
CStringW szText;
textEditWindow.GetWindowText(szText);
CRect rc;
textEditWindow.InvalidateEditRect();
textEditWindow.GetEditRect(&rc);
rc.InflateRect(-GRIP_SIZE / 2, -GRIP_SIZE / 2);
// Draw the text
INT style = (toolsModel.IsBackgroundTransparent() ? 0 : 1);
Text(hdc, rc.left, rc.top, rc.right, rc.bottom, m_fg, m_bg, szText,
textEditWindow.GetFont(), style);
}
void quit()
{
if (textEditWindow.IsWindow())
textEditWindow.ShowWindow(SW_HIDE);
selectionModel.HideSelection();
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
POINT pt = { x, y };
imageModel.Clamp(pt);
selectionModel.SetRectFromPoints(g_ptStart, pt);
BOOL bTextBoxShown = ::IsWindowVisible(textEditWindow);
if (bTextBoxShown)
{
if (textEditWindow.GetWindowTextLength() > 0)
{
imageModel.PushImageForUndo();
draw(m_hdc);
}
if (selectionModel.m_rc.IsRectEmpty())
{
quit();
return TRUE;
}
}
if (registrySettings.ShowTextTool)
{
if (!fontsDialog.IsWindow())
fontsDialog.Create(mainWindow);
fontsDialog.ShowWindow(SW_SHOWNOACTIVATE);
}
CRect rc = selectionModel.m_rc;
// Enlarge if tool small
INT cxMin = CX_MINTEXTEDIT, cyMin = CY_MINTEXTEDIT;
if (selectionModel.m_rc.IsRectEmpty())
{
rc.SetRect(x, y, x + cxMin, y + cyMin);
}
else
{
if (rc.right - rc.left < cxMin)
rc.right = rc.left + cxMin;
if (rc.bottom - rc.top < cyMin)
rc.bottom = rc.top + cyMin;
}
if (!textEditWindow.IsWindow())
textEditWindow.Create(canvasWindow);
textEditWindow.SetWindowText(NULL);
textEditWindow.ValidateEditRect(&rc);
textEditWindow.ShowWindow(SW_SHOWNOACTIVATE);
textEditWindow.SetFocus();
return TRUE;
}
void OnEndDraw(BOOL bCancel) override
{
if (!bCancel)
{
if (::IsWindowVisible(textEditWindow) &&
textEditWindow.GetWindowTextLength() > 0)
{
imageModel.PushImageForUndo();
draw(m_hdc);
}
}
quit();
ToolBase::OnEndDraw(bCancel);
}
};
// TOOL_LINE
struct LineTool : TwoPointDrawTool
{
void OnDrawOverlayOnImage(HDC hdc) override
{
if (!m_bDrawing)
return;
if (GetAsyncKeyState(VK_SHIFT) < 0)
roundTo8Directions(g_ptStart.x, g_ptStart.y, g_ptEnd.x, g_ptEnd.y);
COLORREF rgb = m_bLeftButton ? m_fg : m_bg;
Line(hdc, g_ptStart.x, g_ptStart.y, g_ptEnd.x, g_ptEnd.y, rgb, toolsModel.GetLineWidth());
}
};
// TOOL_BEZIER
struct BezierTool : ToolBase
{
BOOL m_bLeftButton = FALSE;
void OnDrawOverlayOnImage(HDC hdc)
{
COLORREF rgb = (m_bLeftButton ? m_fg : m_bg);
switch (s_cPoints)
{
case 2:
Line(hdc, s_pPoints[0].x, s_pPoints[0].y, s_pPoints[1].x, s_pPoints[1].y, rgb,
toolsModel.GetLineWidth());
break;
case 3:
Bezier(hdc, s_pPoints[0], s_pPoints[2], s_pPoints[2], s_pPoints[1], rgb, toolsModel.GetLineWidth());
break;
case 4:
Bezier(hdc, s_pPoints[0], s_pPoints[2], s_pPoints[3], s_pPoints[1], rgb, toolsModel.GetLineWidth());
break;
}
}
void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
m_bLeftButton = bLeftButton;
if (s_cPoints == 0)
{
pushToPoints(x, y);
pushToPoints(x, y);
}
else
{
s_pPoints[s_cPoints - 1] = { x, y };
}
imageModel.NotifyImageChanged();
}
BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) override
{
if (s_cPoints > 0)
s_pPoints[s_cPoints - 1] = { x, y };
imageModel.NotifyImageChanged();
return TRUE;
}
BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) override
{
if (s_cPoints >= 4)
{
OnEndDraw(FALSE);
return TRUE;
}
pushToPoints(x, y);
imageModel.NotifyImageChanged();
return TRUE;
}
void OnEndDraw(BOOL bCancel) override
{
if (!bCancel && s_cPoints > 1)
{
// FIXME: I couldn't calculate boundary rectangle from Bezier curve
imageModel.PushImageForUndo();
OnDrawOverlayOnImage(m_hdc);
}
ToolBase::OnEndDraw(bCancel);
}
void OnSpecialTweak(BOOL bMinus) override
{
toolsModel.MakeLineThickerOrThinner(bMinus);
}
};
// TOOL_RECT
struct RectTool : TwoPointDrawTool
{
void OnDrawOverlayOnImage(HDC hdc) override
{
if (!m_bDrawing)
return;
if (GetAsyncKeyState(VK_SHIFT) < 0)
regularize(g_ptStart.x, g_ptStart.y, g_ptEnd.x, g_ptEnd.y);
if (m_bLeftButton)
Rect(hdc, g_ptStart.x, g_ptStart.y, g_ptEnd.x, g_ptEnd.y, m_fg, m_bg, toolsModel.GetLineWidth(), toolsModel.GetShapeStyle());
else
Rect(hdc, g_ptStart.x, g_ptStart.y, g_ptEnd.x, g_ptEnd.y, m_bg, m_fg, toolsModel.GetLineWidth(), toolsModel.GetShapeStyle());
}
};
// TOOL_SHAPE
struct ShapeTool : ToolBase