-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScreen.cs
138 lines (123 loc) · 5.37 KB
/
MainScreen.cs
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
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DeloP.Containers;
using DeloP.Controls;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.IO.Stores;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SkiaSharp;
namespace DeloP
{
public class MainScreen : osu.Framework.Game
{
const int ToolPanelWidth = 68, ToolSettingsHeight = 32, MenuHeight = 24;
[Resolved] Game Game { get; set; } = null!;
readonly FullCanvas FullCanvas = new FullCanvas() { RelativeSizeAxes = Axes.Both, Y = ToolSettingsHeight + 2, X = ToolPanelWidth + 2 };
[BackgroundDependencyLoader]
void Load()
{
Resources.AddStore(new DllResourceStore("DeloP.dll"));
Fonts.AddStore(new GlyphStore(Resources, "Fonts/Ubuntu"));
var tools = Enumerable.Empty<ITool>()
.Append(new MoveTool(FullCanvas))
.Append(new PencilTool(FullCanvas))
.Append(new EraserTool(FullCanvas))
.Append(new PipetteTool(FullCanvas))
.Append(new LineTool(FullCanvas))
.Append(new RectangleTool(FullCanvas))
.Append(new TriangleTool(FullCanvas))
.Append(new FillTool(FullCanvas))
.ToImmutableArray();
Window.Title = "DeloP";
Children = new Drawable[]
{
new Box() { RelativeSizeAxes = Axes.Both, Colour = Colors.DarkBackground },
new DeloMenu(Direction.Horizontal, true) { RelativeSizeAxes = Axes.X, Height = MenuHeight, Depth = -2, Items = CreateMenuItems() },
new ToolSettingsPanel(FullCanvas) { RelativeSizeAxes = Axes.X, Y = MenuHeight, Height = ToolSettingsHeight, Depth = -1 },
new ToolPanel(FullCanvas.Canvas, tools) { RelativeSizeAxes = Axes.Y, Y = ToolSettingsHeight + MenuHeight, Width = ToolPanelWidth, Depth = -1 },
FullCanvas,
new LayersWindow(FullCanvas) { Width = 300, Height = 600 },
};
FullCanvas.Canvas.CurrentTool = tools.First();
}
MenuItem[] CreateMenuItems()
{
string? openedPath = null;
static void save(SKBitmap image, string file)
{
using var stream = File.OpenWrite(file);
image.Encode(stream, SKEncodedImageFormat.Png, 9);
}
void clickNew()
{
FullCanvas.Canvas.Image.Clear();
FullCanvas.Canvas.Image.UpdateActiveLayer();
}
void clickOpen()
{
var selector = new BackgroundDrawable<DeloFileSelector>(new DeloFileSelector()) { RelativeSizeAxes = Axes.Both, Depth = -99, Background = Colors.Background };
selector.Child.CurrentFile.ValueChanged += e => Task.Run(() =>
{
try
{
FullCanvas.Canvas.Image.Clear();
FullCanvas.Canvas.Image.AddLayer(SKBitmap.FromImage(SKImage.FromEncodedData(File.OpenWrite(e.NewValue.FullName))));
openedPath = e.NewValue.FullName;
Schedule(() => Remove(selector));
}
catch { }
});
Schedule(() => Add(selector));
}
void clickSave() => Task.Run(() =>
{
if (openedPath is null) clickSaveAs();
else save(FullCanvas.Canvas.Image.AsOne(), openedPath);
});
void clickSaveAs()
{
var selector = new BackgroundDrawable<DeloFileSaveSelector>(new DeloFileSaveSelector()) { RelativeSizeAxes = Axes.Both, Depth = -99, Background = Colors.Background };
selector.Child.OnSelect += p => Task.Run(() =>
{
save(FullCanvas.Canvas.Image.AsOne(), openedPath = p);
Schedule(() => Remove(selector));
});
Schedule(() => Add(selector));
}
void clickExit() => Game.Exit();
return new[]
{
new MenuItem("Файл", () => { })
{
Items = new[]
{
new MenuItem("Новый", clickNew),
new MenuItem("Открыть", clickOpen),
new MenuItem("Сохранить", clickSave),
new MenuItem("Сохранить как...", clickSaveAs),
new MenuItem("Выйти", clickExit),
}
},
new MenuItem("Нефайл", () => { })
{
Items = new[]
{
new MenuItem("хуй", () => {}),
}
}
};
}
protected override bool OnMouseDown(MouseDownEvent e) => FullCanvas.MouseDown(e);
protected override void OnMouseUp(MouseUpEvent e) => FullCanvas.MouseUp(e);
protected override bool OnMouseMove(MouseMoveEvent e) => FullCanvas.MouseMove(e);
}
}