Skip to content

Selection #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions Pixed/Controls/DragDropListBox.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace Pixed.Controls
{
Expand Down
2 changes: 1 addition & 1 deletion Pixed/Controls/PaintCanvas.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void Frame_MouseUp(object sender, MouseButtonEventArgs e)
Frame frame = (Frame)sender;
var pos = e.GetPosition(frame);

if(e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Released)
if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Released)
{
_viewModel.LeftMouseUp?.Execute(pos);
}
Expand Down
10 changes: 9 additions & 1 deletion Pixed/Global.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Pixed.Models;
using Pixed.Selection;
using Pixed.Services.Keyboard;
using Pixed.Tools;

Expand All @@ -7,9 +8,16 @@ namespace Pixed
internal static class Global
{
public static ShortcutService? ShortcutService { get; set; }
public static SelectionManager? SelectionManager { get; set; }
public static Settings UserSettings { get; set; } = new Settings();
public static BaseTool ToolSelected { get; set; }
public static List<PixedModel> Models { get; } = [];
public static Tool Tool { get; } = new Tool();
public static int CurrentModelIndex { get; set; }
public static int CurrentFrameIndex { get; set; }
public static int CurrentLayerIndex { get; set; }
public static PixedModel CurrentModel => Models[CurrentModelIndex];
public static Frame CurrentFrame => CurrentModel.Frames[CurrentFrameIndex];
public static Layer CurrentLayer => CurrentFrame.Layers[CurrentLayerIndex];
public static ToolSelector ToolSelector { get; set; }
}
}
20 changes: 18 additions & 2 deletions Pixed/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,31 @@ public MainWindow()
private void Initialize()
{
Global.ShortcutService = new ShortcutService();
tool_pen.IsChecked = true;
Global.SelectionManager = new Selection.SelectionManager(ov =>
{
_paintCanvas.ViewModel.Overlay = ov;
});
Global.ToolSelector = new ToolSelector(SelectTool);
Global.ToolSelector.SelectTool("tool_pen");
}

private void SelectTool(string name)
{
var obj = FindName(name);

if (obj is RadioButton radioButton)
{
radioButton.IsChecked = true;
}
}

private void ToolRadio_Checked(object sender, RoutedEventArgs e)
{
RadioButton radio = (RadioButton)sender;
string name = radio.Name;

Global.ToolSelected = Global.Tool.GetTool(name);
Global.ToolSelected = Global.ToolSelector.GetTool(name);
_paintCanvas.ViewModel.ResetOverlay();
}

private void Window_KeyDown(object sender, KeyEventArgs e)
Expand Down
7 changes: 3 additions & 4 deletions Pixed/Models/Frame.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Pixed.Utils;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace Pixed.Models
Expand Down Expand Up @@ -55,7 +54,7 @@ public Frame Clone()
Frame frame = new Frame(Width, Height);
frame._layers.Clear();

foreach(Layer layer in _layers)
foreach (Layer layer in _layers)
{
frame._layers.Add(layer.Clone());
}
Expand Down Expand Up @@ -149,7 +148,7 @@ public void MoveLayerDown(bool toBottom)
int oldIndex = _selectedLayer;
Layer layer = _layers[oldIndex];

if(toBottom)
if (toBottom)
{
_layers.Add(layer);
_layers.RemoveAt(oldIndex);
Expand All @@ -166,7 +165,7 @@ public void MoveLayerDown(bool toBottom)

public void MergeLayerBelow()
{
if(SelectedLayer >= Layers.Count - 1)
if (SelectedLayer >= Layers.Count - 1)
{
return;
}
Expand Down
3 changes: 1 addition & 2 deletions Pixed/Models/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Color = System.Drawing.Color;

Expand Down Expand Up @@ -97,7 +96,7 @@ public void MergeLayers(Layer layer2)
{
int transparent = Constants.TRANSPARENT_COLOR.ToArgb();

for(int a = 0; a < _pixels.Length; a++)
for (int a = 0; a < _pixels.Length; a++)
{
if (layer2._pixels[a] != transparent && _pixels[a] != layer2._pixels[a])
{
Expand Down
10 changes: 5 additions & 5 deletions Pixed/Models/PixedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public void AddHistory(HistoryEntry entry)

public void Undo()
{
if(_history.Count == 0 || _historyIndex < 0)
if (_history.Count == 0 || _historyIndex < 0)
{
return;
}

HistoryEntry currentEntry = _history[_historyIndex];
Frame? frame = Frames.FirstOrDefault(f => f.Id == currentEntry.FrameId, null);

if(frame == null)
if (frame == null)
{
_history.RemoveAt(_historyIndex);
_historyIndex--;
Expand All @@ -51,14 +51,14 @@ public void Undo()
return;
}

for(int a = 0; a < currentEntry.PixelX.Length; a++)
for (int a = 0; a < currentEntry.PixelX.Length; a++)
{
int pixelX = currentEntry.PixelX[a];
int pixelY = currentEntry.PixelY[a];
int oldcolor = currentEntry.OldColor[a];
int newColor = currentEntry.NewColor[a];

if(layer.GetPixel(pixelX, pixelY) == newColor)
if (layer.GetPixel(pixelX, pixelY) == newColor)
{
layer.SetPixel(pixelX, pixelY, oldcolor);
}
Expand All @@ -75,7 +75,7 @@ public void Redo()
return;
}

if(_historyIndex < 0)
if (_historyIndex < 0)
{
_historyIndex = 0;
}
Expand Down
21 changes: 21 additions & 0 deletions Pixed/Models/Pixel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Pixed.Models
{
internal class Pixel
{
public int X { get; set; }
public int Y { get; set; }
public int Color { get; set; }

public Pixel(int x, int y)
{
X = x;
Y = y;
Color = Constants.TRANSPARENT_COLOR.ToArgb();
}

public Pixel(int x, int y, int color) : this(x, y)
{
Color = color;
}
}
}
64 changes: 64 additions & 0 deletions Pixed/Selection/BaseSelection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Pixed.Models;
using System.Drawing;

namespace Pixed.Selection
{
internal class BaseSelection
{
public List<Pixel> Pixels { get; }

public BaseSelection()
{
Pixels = [];
Reset();
}

public void Reset()
{
Pixels.Clear();
}

public void Move(int xDiff, int yDiff)
{
for (int i = 0; i < Pixels.Count; i++)
{
var pixel = Pixels[i];
pixel.X += xDiff;
pixel.Y += yDiff;
Pixels[i] = pixel;
}
}

public void FillSelectionFromFrame(Frame frame)
{
for (int i = 0; i < Pixels.Count; i++)
{
var pixel = Pixels[i];

if (!frame.PointInside(pixel.X, pixel.Y))
{
continue;
}

pixel.Color = frame.GetPixel(Pixels[i].X, Pixels[i].Y);
}
}

public Bitmap ToBitmap()
{
var minX = Pixels.Min(p => p.X);
var minY = Pixels.Min(p => p.Y);
var maxX = Pixels.Max(p => p.X);
var maxY = Pixels.Max(p => p.Y);

Bitmap bitmap = new((maxX - minX) + 1, (maxY - minY) + 1);

foreach (var pixel in Pixels)
{
bitmap.SetPixel(pixel.X - minX, pixel.Y - minY, Color.FromArgb(pixel.Color));
}

return bitmap;
}
}
}
99 changes: 99 additions & 0 deletions Pixed/Selection/LassoSelection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Pixed.Models;
using Pixed.Utils;
using System.Drawing;

namespace Pixed.Selection
{
internal class LassoSelection : BaseSelection
{
private const int OUTSIDE = -1;
private const int INSIDE = 1;
private const int VISITED = 2;

private int[,] _pixels;

public LassoSelection(List<Point> pixels, Frame frame) : base()
{
_pixels = new int[frame.Width, frame.Height];

foreach (var p in pixels)
{
SetPixel(p, INSIDE);
}

Pixels.Clear();
Pixels.AddRange(GetLassoPixels(frame));
}

private List<Pixel> GetLassoPixels(Frame frame)
{
List<Pixel> pixels = [];

for (int x = 0; x < frame.Width; x++)
{
for (int y = 0; y < frame.Height; y++)
{
if (IsInSelection(new Point(x, y), frame))
{
pixels.Add(new Pixel(x, y, frame.GetPixel(x, y)));
}
}
}

return pixels;
}

private bool IsInSelection(Point point, Frame frame)
{
bool visited = GetPixel(point) == VISITED;

if (!visited)
{
VisitPixel(point, frame);
}

return GetPixel(point) == INSIDE;
}

private void VisitPixel(Point point, Frame frame)
{
bool frameBorderReached = false;
var entry = PaintUtils.VisitConnectedPixelsHistory(frame.Layers[frame.SelectedLayer], point.X, point.Y, p =>
{
var alreadyVisited = GetPixel(point);

if (alreadyVisited == VISITED)
{
return false;
}

if (!frame.PointInside(point.X, point.Y))
{
frameBorderReached = true;
return false;
}

SetPixel(point, VISITED);
return true;
});

for (int a = 0; a < entry.PixelX.Count; a++)
{
int x = entry.PixelX[a];
int y = entry.PixelY[a];

SetPixel(new Point(x, y), frameBorderReached ? OUTSIDE : INSIDE);
}
}

private void SetPixel(Point point, int value)
{
_pixels[point.X, point.Y] = value;
}

private int GetPixel(Point point)
{
return _pixels[point.X, point.Y];
}
}
}
31 changes: 31 additions & 0 deletions Pixed/Selection/RectangularSelection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Pixed.Selection
{
internal class RectangularSelection : BaseSelection
{
private int _x1;
private int _y1;
private int _x2;
private int _y2;

public RectangularSelection(int x1, int y1, int x2, int y2) : base()
{
SetOrderedRectangleCoordinates(x1, y1, x2, y2);

for (int x = _x1; x <= _x2; x++)
{
for (int y = _y1; y <= _y2; y++)
{
Pixels.Add(new Models.Pixel(x, y));
}
}
}

private void SetOrderedRectangleCoordinates(int x1, int y1, int x2, int y2)
{
_x1 = Math.Min(x1, x2);
_y1 = Math.Min(y1, y2);
_x2 = Math.Max(x2, x1);
_y2 = Math.Max(y2, y1);
}
}
}
Loading