Skip to content

Commit 47212ba

Browse files
progress
1 parent 77ee96b commit 47212ba

File tree

3 files changed

+103
-39
lines changed

3 files changed

+103
-39
lines changed

ASECII/ASECII.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<TargetFramework>net5.0</TargetFramework>
77
<RootNamespace>ASECII</RootNamespace>
88
<AssemblyName>ASECII</AssemblyName>
9+
<StartupObject>ASECII.Program</StartupObject>
910
</PropertyGroup>
1011
<ItemGroup>
1112
<None Include="Content\IBMCGA.png">

ASECII/SaveMenu.cs

+96-34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Console = SadConsole.Console;
1717
using ArchConsole;
1818
using Newtonsoft.Json.Serialization;
19+
using System.Threading.Tasks;
1920

2021
namespace ASECII {
2122
public interface FileMode {
@@ -77,11 +78,26 @@ public void Enter(Console console, string filepath) {
7778
}
7879
}
7980
}
81+
public interface ILoadResult {}
82+
public class LoadFailure : ILoadResult {
83+
public string message;
84+
public LoadFailure(string message) => this.message = message;
85+
}
86+
public class LoadBusy : ILoadResult {
87+
public LoadBusy() { }
88+
}
89+
public class LoadNonexistent : ILoadResult {
90+
public LoadNonexistent() { }
91+
}
92+
public class LoadSuccess : ILoadResult {
93+
public SpriteModel preview;
94+
public LoadSuccess(SpriteModel preview) => this.preview = preview;
95+
}
8096
class FileMenu : ControlsConsole {
8197
public static string RECENTFILES = "RecentFiles.json";
8298

8399
SpriteModel hoveredFile;
84-
Dictionary<string, SpriteModel> preloaded;
100+
Dictionary<string, ILoadResult> preloaded;
85101

86102
HashSet<string> recentFiles;
87103
List<LabelButton> recentListing;
@@ -92,13 +108,15 @@ class FileMenu : ControlsConsole {
92108

93109
int folderListingX;
94110

111+
string console = "";
112+
95113
public FileMenu(int width, int height, FileMode mode) : base(width, height) {
96114

97115
DefaultBackground = Color.Black;
98116

99117

100118
this.recentFiles = File.Exists(RECENTFILES) ? ASECIILoader.DeserializeObject<HashSet<string>>(File.ReadAllText(RECENTFILES)).Where(f => File.Exists(f)).ToHashSet() : new HashSet<string>();
101-
this.preloaded = new Dictionary<string, SpriteModel>();
119+
this.preloaded = new Dictionary<string, ILoadResult>();
102120
this.recentListing = new List<LabelButton>();
103121
int n = 3;
104122

@@ -135,7 +153,6 @@ void Load() {
135153
FocusOnMouseClick = true;
136154
folderListing = new List<LabelButton>();
137155

138-
139156
textbox = new TextField(width - folderListingX) {
140157
Position = new Point(folderListingX, 1),
141158

@@ -144,9 +161,8 @@ void Load() {
144161
IsFocused = true,
145162
text = mode.InitialPath,
146163
};
147-
textbox.TextChanged += (tf) => {
148-
UpdateListing(textbox.text);
149-
};
164+
textbox.TextChanged += tf => UpdateListing(textbox.text);
165+
textbox.EnterPressed += tf => EnterFile();
150166
this.Children.Add(textbox);
151167
UpdateListing(textbox.text);
152168
}
@@ -214,38 +230,70 @@ void ShowFiles(IEnumerable<string> files) {
214230
folderListing.Add(b);
215231

216232
void Load() {
217-
mode.Enter(this, file);
218-
AddRecentFile(file);
233+
EnterFile(file);
219234
}
220235
}
221236
}
222237
}
223-
238+
public void Log(string message) {
239+
console = $"{message}";
240+
}
224241
public void ShowPreview(string file) {
225-
if (preloaded.TryGetValue(file, out hoveredFile)) {
226-
return;
242+
if (preloaded.TryGetValue(file, out var result)) {
243+
Handle(file, result);
227244
} else {
228-
preloaded[file] = null;
229-
230-
System.Threading.Tasks.Task.Run(StartLoad);
231-
void StartLoad() {
232-
try {
233-
var model = ASECIILoader.DeserializeObject<SpriteModel>(File.ReadAllText(file));
234-
if (model?.filepath == null) {
235-
preloaded[file] = null;
236-
hoveredFile = null;
237-
return;
238-
}
239-
preloaded[file] = model;
240-
hoveredFile = model;
241-
} catch (Exception e) {
242-
preloaded[file] = null;
243-
hoveredFile = null;
245+
Load(file);
246+
}
247+
}
248+
public void Handle(string file, ILoadResult result) {
249+
switch (result) {
250+
case LoadBusy:
251+
Log($"[{file}]\nLoading...");
252+
hoveredFile = null;
253+
break;
254+
case LoadFailure f:
255+
Log($"[{file}]\n{f.message}");
256+
hoveredFile = null;
257+
break;
258+
case LoadSuccess s:
259+
Log($"[{file}]\nLoaded successfully");
260+
hoveredFile = s.preview;
261+
break;
262+
263+
case LoadNonexistent s:
264+
Log($"[{file}]\nDoes not exist");
265+
hoveredFile = null;
266+
break;
267+
}
268+
}
269+
public async Task Load(string file) {
270+
if(!File.Exists(file)) {
271+
preloaded[file] = new LoadNonexistent();
272+
return;
273+
}
274+
275+
preloaded[file] = new LoadBusy();
276+
await Task.Run(StartLoad);
277+
void StartLoad() {
278+
try {
279+
var model = ASECIILoader.DeserializeObject<SpriteModel>(File.ReadAllText(file));
280+
if (model?.filepath == null) {
281+
Failed("Filepath does not exist");
282+
return;
244283
}
284+
var s = new LoadSuccess(model);
285+
preloaded[file] = s;
286+
Handle(file, s);
287+
} catch (Exception e) {
288+
Failed(e.Message);
289+
}
290+
void Failed(string message) {
291+
var f = new LoadFailure(message);
292+
preloaded[file] = f;
293+
Handle(file, f);
245294
}
246295
}
247296
}
248-
249297
public override void Render(TimeSpan delta) {
250298
base.Render(delta);
251299
this.Clear();
@@ -258,13 +306,10 @@ public override void Render(TimeSpan delta) {
258306
}
259307
}
260308
if (hoveredFile != null && hoveredFile.sprite != null) {
261-
262309
var s = hoveredFile.sprite;
263-
264310
var previewX = (Width - (s.end - s.origin).X) < 64 ? 0 : 64;
265311
var previewY = 0;
266312
var origin = hoveredFile.sprite.origin;
267-
268313

269314
var previewStart = new Point(previewX, previewY);
270315
for (int x = previewX; x < Width; x++) {
@@ -277,15 +322,32 @@ public override void Render(TimeSpan delta) {
277322
}
278323
}
279324
}
325+
int yy = Height - 16;
326+
foreach(var line in console.Split('\n')) {
327+
this.Print(16, yy++, line, Color.White, Color.Black);
328+
}
280329
}
281330
public override bool ProcessKeyboard(Keyboard keyboard) {
282331
if (keyboard.IsKeyPressed(Enter)) {
283-
var f = textbox.text;
284-
mode.Enter(this, f);
285-
AddRecentFile(f);
332+
EnterFile();
286333
}
287334
return base.ProcessKeyboard(keyboard);
288335
}
336+
public async Task EnterFile() => await EnterFile(textbox.text);
337+
338+
public async Task EnterFile(string f) {
339+
if (preloaded.TryGetValue(f, out var result)) {
340+
if (result is LoadSuccess || result is LoadNonexistent) {
341+
mode.Enter(this, f);
342+
AddRecentFile(f);
343+
} else {
344+
Handle(f, result);
345+
}
346+
} else {
347+
await Load(f);
348+
EnterFile(f);
349+
}
350+
}
289351
}
290352

291353
}

ASECII/SpriteMenu.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1413,10 +1413,11 @@ public void Save(Console renderer) {
14131413
}
14141414
} else {
14151415
c = new Console(width, height);
1416-
for (int y = 0; y <= height; y++) {
1417-
for (int x = 0; x <= width; x++) {
1416+
for (int y = 0; y < height; y++) {
1417+
for (int x = 0; x < width; x++) {
14181418
if (preview.TryGetValue((x, y), out var tile)) {
1419-
c.SetCellAppearance(x - sprite.origin.X, y - sprite.origin.Y, tile);
1419+
//c.SetCellAppearance(x - sprite.origin.X, y - sprite.origin.Y, tile);
1420+
c.SetCellAppearance(x, y, tile);
14201421
}
14211422
}
14221423
}
@@ -2213,7 +2214,7 @@ public void ProcessKeyboard(Keyboard info) {
22132214
var p = keyCursor ?? model.cursor;
22142215
if(model.IsEditable(p)) {
22152216
if (c != 0) {
2216-
var layer = sprite.layers[0];
2217+
var layer = model.currentLayer;
22172218
var tile = new TileValue(brush.foreground, brush.background, c);
22182219
var e = new SingleEdit(p, layer, tile);
22192220
Add(e);
@@ -2223,7 +2224,7 @@ public void ProcessKeyboard(Keyboard info) {
22232224
}
22242225

22252226
} else if (info.IsKeyDown(Back)) {
2226-
var layer = sprite.layers[0];
2227+
var layer = model.currentLayer;
22272228
var p = keyCursor ?? model.cursor;
22282229
var e = new SingleEdit(p, layer, null);
22292230
Add(e);

0 commit comments

Comments
 (0)