-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaintPower_Engine.cs
More file actions
342 lines (273 loc) · 9.26 KB
/
PaintPower_Engine.cs
File metadata and controls
342 lines (273 loc) · 9.26 KB
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
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using PaintPower.Dialogs;
using PaintPower.Editors;
using PaintPower.FileExplorer;
using PaintPower.Logging;
using PaintPower.Networking;
using PaintPower.ProjectSystem;
using PaintPower.SpriteEditor;
using PaintPower.Time;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using PaintPower.Accessibility.Translation;
using System.Reflection;
namespace PaintPower;
public class PaintPower_Engine
{
public static readonly string versionNumber = "1.0.1.0";
public static readonly string buildTime = new Date().getBuildTimestamp();
public static readonly string devStatus = "Pre-Alpha";
public static string MajorVersion = $"{Translator.Map(devStatus)} {versionNumber}";
public static string version = $"{Translator.Map("Version")}: {MajorVersion} {Translator.Map("build")} {buildTime}";
public void translateVersion()
{
MajorVersion = $"{Translator.Map("Pre-Alpha")} 1.0.1.0";
version = $"{Translator.Map("Version")}: {MajorVersion} {Translator.Map("build")} {buildTime}";
}
private Editor _editorManager;
private PaintProject _project;
private EditorBase _editor;
public Server server;
public bool isNewProject = true;
private SpriteEditorView _spriteEditorView;
public bool saveNeeded = false;
public static PaintPower_Engine App { get; private set; }
public static MainWindow window;
public PaintPower_Engine()
{
_project = new PaintProject();
_editorManager = new Editor(_project.Workspace);
server = new Server();
Log.QuickLog(version);
// After, make a static reference.
App = this;
}
public void setupTranslation()
{
// Set-up translation
Translator.load(null);
var langs = Translator.GetAvailableLanguages();
foreach (var pair in langs)
{
string fullName = pair.Key;
string code = pair.Value;
var item = new MenuItem { Header = fullName };
item.Click += (_, __) =>
{
Translator.load(code);
};
window.LanguageDropdown.Items.Add(item);
}
}
public void attachWindow(MainWindow w)
{
window = w;
}
public void StatusClicked(object sender, EventArgs e)
{
if (_project != null && saveNeeded)
{
Save();
}
}
public string SetProjectStatus(string status)
{
window.ProjectStatus.Text = status;
window.InvalidateVisual();
return status;
}
private void OnSpriteSelected(PaintSprite sprite)
{
// Create the sprite editor panel
_spriteEditorView = new SpriteEditorView(sprite, _project.Workspace);
// Replace the center panel with the sprite editor
window.CenterHost.Content = _spriteEditorView;
SetProjectStatus($"{Translator.Translate("Editing Sprite:")} {sprite.Name}");
}
public void OpenProjectFile()
{
var openPicker = window.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = Translator.Map("Open PaintPower Project"),
AllowMultiple = false,
FileTypeFilter = new[] { new FilePickerFileType(Translator.Map("PaintPower Project")) { Patterns = new[] { "*.xPaint" } } }
}).ContinueWith(async t =>
{
var result = await t;
if (result.Count > 0)
{
string path = result[0].Path.LocalPath;
_project.Load(path);
_project.ProjectPath = path;
window.Title = $"{Translator.Map("PaintPower")} - {_project.Metadata.name}";
}
});
}
public async void newProject()
{
if (saveNeeded)
{
var dialog = new SaveBeforeContinueDialog();
var result = await dialog.ShowAsync(MainWindow.window);
switch (result)
{
case "save":
await Save();
break;
case "saveas":
SaveAs();
break;
case "dontsave":
break; // continue without saving
case null:
return; // cancel new project
}
}
// Reset everything
_project = new PaintProject();
_editorManager = new Editor(_project.Workspace);
server = new Server();
Start();
}
public async void Start()
{
await Task.Yield();
CloseEditor();
_project.CreateNew();
window.Title = $"{Translator.Map("PaintPower")} - {_project.Metadata.name}";
SetProjectStatus(Translator.Translate("Not edited yet."));
window.SpriteManager.Initialize(_project);
window.SpriteManager.SpriteSelected += OnSpriteSelected;
// Set up translation
setupTranslation();
}
/*public void OpenEditor(EditorBase editor)
{
_editor = editor;
CenterHost.Content = editor;
}*/
public void OpenFile(string path)
{
var editor = _editorManager.GetEditorFromFileType(path);
if (_spriteEditorView != null)
_spriteEditorView.OpenEditor(editor, path);
}
public void CloseEditor()
{
window.CenterHost.Content = null;
_editor = null;
}
public bool _isSavingAnimationRunning = false;
public bool _isUploadAnimationRunning = false;
public async Task RunSavingAnimation()
{
_isSavingAnimationRunning = true;
string[] frames = new[]
{
"Saving Project",
"Saving Project.",
"Saving Project..",
"Saving Project..."
};
int index = 0;
while (_isSavingAnimationRunning)
{
SetProjectStatus(Translator.Map(frames[index]));
index = (index + 1) % frames.Length;
await Task.Delay(300); // smooth animation
}
}
// Save function. Don't even care about what it returns, but C#
// Requires it to be a Task in order to await it. C# core.
async public Task Save()
{
if (!saveNeeded && !isNewProject) return;
try
{
var dialog = new DoSaveWindowDialog();
var result = await dialog.ShowAsync(window);
if (result == "saveas")
{
SaveAs();
return;
}
if (result != "save")
{
Log.QuickLog($"Not saving. {result}");
return;
}
// If no path yet -> ask user where to save
if (string.IsNullOrWhiteSpace(_project.ProjectPath) || isNewProject)
{
isNewProject = true; // Keep isNewProject checks
var res = await _project.SaveNewProject(MainWindow.window);
if (string.IsNullOrWhiteSpace(res.Path))
return; // user cancelled
_project.ProjectPath = res.Path;
isNewProject = false;
}
Log.QuickLog("Saving Project...");
// Start animation (non-blocking)
var animationTask = RunSavingAnimation();
// Run save off UI thread
await ProjectSaver.Save(_project, _editor);
// Stop animation
_isSavingAnimationRunning = false;
await animationTask; // wait for animation loop to exit
// Final status
Log.QuickLog(SetProjectStatus(Translator.Map("Project Saved!")));
}
catch (Exception ex)
{
Log.QuickLog($"Error while saving project! {ex}");
}
}
async public void SaveAs()
{
var savePicker = await window.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = $"Save {_project.Metadata.name} As",
DefaultExtension = "xPaint",
SuggestedFileName = $"{_project.Metadata.name}.xPaint",
ShowOverwritePrompt = true
});
if (savePicker == null)
return;
string newPath = savePicker.Path.LocalPath;
// Update project path
_project.ProjectPath = newPath;
try
{
// Start animation
var animationTask = RunSavingAnimation();
// Run save off UI thread
await ProjectSaver.Save(_project, _editor);
// Stop animation
_isSavingAnimationRunning = false;
await animationTask;
// Update metadata + UI
_project.Metadata.name = Path.GetFileNameWithoutExtension(newPath);
window.Title = $"PaintPower - {_project.Metadata.name}";
SetProjectStatus("Project Saved!");
Log.QuickLog($"Project saved as {newPath}");
}
catch (Exception ex)
{
Log.QuickLog($"SaveAs failed: {ex}");
}
}
public async void SaveToServer()
{
var doSave = false;
if (!doSave) return;
SetProjectStatus("Uploading Project to Server...");
window.InvalidateVisual();
ProjectSaver.PublishToServer(_project, _editor, server);
SetProjectStatus("");
}
}