Skip to content

Commit 7763640

Browse files
Rectangle (#15)
* Rectangle tool; Shape Tool class for shapes * VS Cleanup
1 parent f9b08a4 commit 7763640

File tree

7 files changed

+131
-85
lines changed

7 files changed

+131
-85
lines changed

Pixed/Selection/LassoSelection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void VisitPixel(Point point, Frame frame)
7878
return true;
7979
});
8080

81-
foreach(var pixel in visited)
81+
foreach (var pixel in visited)
8282
{
8383
SetPixel(new Point(pixel.X, pixel.Y), frameBorderReached ? OUTSIDE : INSIDE);
8484
}

Pixed/Tools/ShapeTool.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Pixed.Input;
2+
using Pixed.Models;
3+
using Pixed.Services.History;
4+
using Pixed.Utils;
5+
using System;
6+
using System.Drawing;
7+
8+
namespace Pixed.Tools
9+
{
10+
internal abstract class ShapeTool : BaseTool
11+
{
12+
protected int _startX = -1;
13+
protected int _startY = -1;
14+
15+
public override void ApplyTool(int x, int y, Frame frame, ref Bitmap overlay)
16+
{
17+
_startX = x;
18+
_startY = y;
19+
20+
overlay.SetPixel(x, y, GetToolColor());
21+
}
22+
23+
public override void MoveTool(int x, int y, Frame frame, ref Bitmap overlay)
24+
{
25+
overlay.Clear();
26+
bool isShift = Keyboard.Modifiers.HasFlag(Avalonia.Input.KeyModifiers.Shift);
27+
var color = GetToolColor();
28+
29+
if (color == UniColor.Transparent)
30+
{
31+
color = new UniColor(50, 160, 215, 240);
32+
}
33+
34+
Draw(x, y, color, isShift, ref overlay);
35+
}
36+
37+
public override void ReleaseTool(int x, int y, Frame frame, ref Bitmap overlay)
38+
{
39+
bool isShift = Keyboard.Modifiers.HasFlag(Avalonia.Input.KeyModifiers.Shift);
40+
var color = GetToolColor();
41+
42+
var historyEntry = Draw(x, y, color, isShift, frame);
43+
Global.CurrentModel.AddHistory(historyEntry);
44+
45+
overlay.Clear();
46+
Subjects.RefreshCanvas.OnNext(null);
47+
}
48+
49+
protected void Draw(int x, int y, int color, bool isShift, ref Bitmap overlay)
50+
{
51+
Bitmap bitmap = overlay;
52+
Draw(x, y, color, isShift, (x, y, color) =>
53+
{
54+
bitmap.SetPixel(x, y, (UniColor)color);
55+
});
56+
57+
overlay = bitmap;
58+
}
59+
60+
protected HistoryEntry Draw(int x, int y, int color, bool isShift, Frame frame)
61+
{
62+
DynamicHistoryEntry entry = new()
63+
{
64+
FrameId = frame.Id,
65+
LayerId = frame.Layers[frame.SelectedLayer].Id
66+
};
67+
Draw(x, y, color, isShift, (x1, y1, _) =>
68+
{
69+
int oldColor = frame.GetPixel(x1, y1);
70+
entry.Add(x1, y1, oldColor, color);
71+
frame.SetPixel(x1, y1, color);
72+
});
73+
74+
return entry.ToEntry();
75+
}
76+
protected abstract void Draw(int x, int y, int color, bool isShift, Action<int, int, int> setPixelAction);
77+
}
78+
}

Pixed/Tools/ToolRectangle.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Pixed.Utils;
2+
using System;
3+
4+
namespace Pixed.Tools
5+
{
6+
internal class ToolRectangle : ShapeTool
7+
{
8+
protected override void Draw(int x, int y, int color, bool isShift, Action<int, int, int> setPixelAction)
9+
{
10+
var rectangle = MathUtils.GetOrderedRectangle(_startX, _startY, x, y);
11+
12+
if (isShift)
13+
{
14+
int width = Math.Abs(rectangle[2] - rectangle[0]);
15+
int height = Math.Abs(rectangle[3] - rectangle[1]);
16+
int size = Math.Min(width, height);
17+
18+
rectangle[2] = rectangle[0] + size;
19+
rectangle[3] = rectangle[1] + size;
20+
}
21+
22+
for (int rx = rectangle[0]; rx <= rectangle[2]; rx++)
23+
{
24+
for (int ry = rectangle[1]; ry <= rectangle[3]; ry++)
25+
{
26+
if (rx == rectangle[0] || rx == rectangle[2] || ry == rectangle[1] || ry == rectangle[3])
27+
{
28+
setPixelAction.Invoke(rx, ry, color);
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}

Pixed/Tools/ToolSelector.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public ToolSelector(Action<string> selectToolAction)
2222
_tools.Add("tool_mirror_pen", new ToolVerticalPen());
2323
_tools.Add("tool_colorswap", new ToolColorSwap());
2424
_tools.Add("tool_stroke", new ToolStroke());
25+
_tools.Add("tool_rectangle", new ToolRectangle());
2526
}
2627

2728
public void SelectTool(string name)
@@ -33,11 +34,7 @@ public void SelectTool(string name)
3334
}
3435
public BaseTool? GetTool(string name)
3536
{
36-
if (_tools.ContainsKey(name))
37-
{
38-
return _tools[name];
39-
}
40-
41-
return null;
37+
_tools.TryGetValue(name, out BaseTool? value);
38+
return value;
4239
}
4340
}

Pixed/Tools/ToolStroke.cs

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,17 @@
1-
using Pixed.Input;
2-
using Pixed.Models;
3-
using Pixed.Services.History;
4-
using Pixed.Utils;
1+
using Pixed.Utils;
52
using System;
63
using System.Collections.Generic;
74
using System.Drawing;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
115

126
namespace Pixed.Tools
137
{
14-
internal class ToolStroke : BaseTool
8+
internal class ToolStroke : ShapeTool
159
{
16-
private int _startX = -1;
17-
private int _startY = -1;
18-
19-
public override void ApplyTool(int x, int y, Frame frame, ref Bitmap overlay)
20-
{
21-
_startX = x;
22-
_startY = y;
23-
24-
overlay.SetPixel(x, y, GetToolColor());
25-
}
26-
27-
public override void MoveTool(int x, int y, Frame frame, ref Bitmap overlay)
28-
{
29-
overlay.Clear();
30-
bool isStraight = Keyboard.Modifiers.HasFlag(Avalonia.Input.KeyModifiers.Shift);
31-
var color = GetToolColor();
32-
33-
if(color == UniColor.Transparent)
34-
{
35-
color = new UniColor(50, 160, 215, 240);
36-
}
37-
38-
DrawLine(x, y, color, isStraight, overlay);
39-
}
40-
41-
public override void ReleaseTool(int x, int y, Frame frame, ref Bitmap overlay)
42-
{
43-
bool isStraight = Keyboard.Modifiers.HasFlag(Avalonia.Input.KeyModifiers.Shift);
44-
var color = GetToolColor();
45-
46-
var historyEntry = DrawLine(x, y, color, isStraight, frame);
47-
Global.CurrentModel.AddHistory(historyEntry);
48-
49-
overlay.Clear();
50-
Subjects.RefreshCanvas.OnNext(null);
51-
}
52-
53-
private void DrawLine(int x, int y, int color, bool isStraight, Bitmap overlay)
54-
{
55-
DrawLine(x, y, color, isStraight, (x, y, color) =>
56-
{
57-
overlay.SetPixel(x, y, (UniColor)color);
58-
});
59-
}
60-
61-
private HistoryEntry DrawLine(int x, int y, int color, bool isStraight, Frame frame)
62-
{
63-
DynamicHistoryEntry entry = new DynamicHistoryEntry();
64-
entry.FrameId = frame.Id;
65-
entry.LayerId = frame.Layers[frame.SelectedLayer].Id;
66-
DrawLine(x, y, color, isStraight, (x1, y1, _) =>
67-
{
68-
int oldColor = frame.GetPixel(x1, y1);
69-
entry.Add(x1, y1, oldColor, color);
70-
frame.SetPixel(x1, y1, color);
71-
});
72-
73-
return entry.ToEntry();
74-
}
75-
76-
private void DrawLine(int x, int y, int color, bool isStraight, Action<int, int, int> setPixelAction)
10+
protected override void Draw(int x, int y, int color, bool isShift, Action<int, int, int> setPixelAction)
7711
{
7812
List<Point> linePixels;
7913

80-
if (isStraight)
14+
if (isShift)
8115
{
8216
linePixels = MathUtils.GetUniformLinePixels(_startX, _startY, x, y);
8317
}

Pixed/Utils/MathUtils.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using Avalonia.Media.TextFormatting.Unicode;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Drawing;
5-
using System.Numerics;
64

75
namespace Pixed.Utils;
86

@@ -55,7 +53,7 @@ public static List<Point> GetUniformLinePixels(int x0, int y0, int x1, int y1)
5553

5654
var ratio = (double)Math.Max(dx, dy) / (double)Math.Min(dx, dy);
5755
var step = Math.Round(ratio);
58-
if(step > Math.Min(dx, dy))
56+
if (step > Math.Min(dx, dy))
5957
{
6058
step = double.MaxValue;
6159
}
@@ -66,7 +64,7 @@ public static List<Point> GetUniformLinePixels(int x0, int y0, int x1, int y1)
6664
var y = y0;
6765
int i = 0;
6866

69-
while(true)
67+
while (true)
7068
{
7169
i++;
7270
pixels.Add(new Point(x, y));
@@ -77,12 +75,12 @@ public static List<Point> GetUniformLinePixels(int x0, int y0, int x1, int y1)
7775

7876
bool isAtStep = (int)(i % step) == 0;
7977

80-
if(dx >= dy || isAtStep)
78+
if (dx >= dy || isAtStep)
8179
{
8280
x += sx;
8381
}
8482

85-
if(dy >= dx || isAtStep)
83+
if (dy >= dx || isAtStep)
8684
{
8785
y += sy;
8886
}
@@ -97,4 +95,9 @@ public static double Distance(int x0, int y0, int x1, int y1)
9795
var dy = Math.Abs((double)y1 - (double)y0);
9896
return Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
9997
}
98+
99+
public static int[] GetOrderedRectangle(int x0, int y0, int x1, int y1)
100+
{
101+
return [Math.Min(x0, x1), Math.Min(y0, y1), Math.Max(x0, x1), Math.Max(y0, y1)];
102+
}
100103
}

Pixed/Utils/PaintUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static DynamicHistoryEntry PaintSimiliarConnected(Layer layer, int x, int
4848
return false;
4949
});
5050

51-
foreach(var pixel in pixels)
51+
foreach (var pixel in pixels)
5252
{
5353
layer.SetPixel(pixel.X, pixel.Y, replacementColor);
5454
entry.Add(pixel.X, pixel.Y, pixel.Color, replacementColor);

0 commit comments

Comments
 (0)