Skip to content

Commit 67cee0d

Browse files
Add Rectangle and Ellipse erasing support + Shift modifier for perfect squares
Co-authored-by: RuntimeRascal <2422222+RuntimeRascal@users.noreply.github.com>
1 parent 28acc1b commit 67cee0d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Src/GhostDraw/Tools/EraserTool.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ private void EraseAtPosition(Point position, Canvas canvas)
136136
}
137137
}
138138
}
139+
else if (element is System.Windows.Shapes.Rectangle rectangle)
140+
{
141+
// Check if eraser intersects with rectangle bounds
142+
double left = Canvas.GetLeft(rectangle);
143+
double top = Canvas.GetTop(rectangle);
144+
Rect shapeRect = new Rect(left, top, rectangle.Width, rectangle.Height);
145+
146+
if (eraserRect.IntersectsWith(shapeRect))
147+
{
148+
shouldErase = true;
149+
}
150+
}
151+
else if (element is Ellipse ellipse)
152+
{
153+
// Check if eraser intersects with ellipse bounds
154+
double left = Canvas.GetLeft(ellipse);
155+
double top = Canvas.GetTop(ellipse);
156+
Rect ellipseRect = new Rect(left, top, ellipse.Width, ellipse.Height);
157+
158+
if (eraserRect.IntersectsWith(ellipseRect))
159+
{
160+
shouldErase = true;
161+
}
162+
}
139163

140164
if (shouldErase)
141165
{

Src/GhostDraw/Tools/RectangleTool.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void OnMouseMove(Point position, Canvas canvas, MouseButtonState leftButt
4242
if (_isCreatingRectangle && _currentRectangle != null && _rectangleStartPoint.HasValue)
4343
{
4444
// Update rectangle dimensions to follow cursor
45-
UpdateRectangle(_rectangleStartPoint.Value, position);
45+
// Check if Shift key is held down for perfect square
46+
bool isShiftPressed = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
47+
UpdateRectangle(_rectangleStartPoint.Value, position, isShiftPressed);
4648
}
4749
}
4850

@@ -117,7 +119,7 @@ private void StartNewRectangle(Point startPoint, Canvas canvas)
117119
_logger.LogInformation("Rectangle started at ({X:F0}, {Y:F0})", startPoint.X, startPoint.Y);
118120
}
119121

120-
private void UpdateRectangle(Point startPoint, Point currentPoint)
122+
private void UpdateRectangle(Point startPoint, Point currentPoint, bool isPerfectSquare = false)
121123
{
122124
if (_currentRectangle == null)
123125
return;
@@ -128,6 +130,14 @@ private void UpdateRectangle(Point startPoint, Point currentPoint)
128130
double width = Math.Abs(currentPoint.X - startPoint.X);
129131
double height = Math.Abs(currentPoint.Y - startPoint.Y);
130132

133+
// If Shift is held, make it a perfect square (width = height = min dimension)
134+
if (isPerfectSquare)
135+
{
136+
double size = Math.Min(width, height);
137+
width = size;
138+
height = size;
139+
}
140+
131141
Canvas.SetLeft(_currentRectangle, left);
132142
Canvas.SetTop(_currentRectangle, top);
133143
_currentRectangle.Width = width;
@@ -138,7 +148,9 @@ private void FinishRectangle(Point endPoint)
138148
{
139149
if (_currentRectangle != null && _rectangleStartPoint.HasValue)
140150
{
141-
UpdateRectangle(_rectangleStartPoint.Value, endPoint);
151+
// Check if Shift was held on the final click
152+
bool isShiftPressed = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
153+
UpdateRectangle(_rectangleStartPoint.Value, endPoint, isShiftPressed);
142154
_logger.LogInformation("Rectangle finished at ({X:F0}, {Y:F0})", endPoint.X, endPoint.Y);
143155
}
144156

0 commit comments

Comments
 (0)