16
16
using Console = SadConsole . Console ;
17
17
using ArchConsole ;
18
18
using Newtonsoft . Json . Serialization ;
19
+ using System . Threading . Tasks ;
19
20
20
21
namespace ASECII {
21
22
public interface FileMode {
@@ -77,11 +78,26 @@ public void Enter(Console console, string filepath) {
77
78
}
78
79
}
79
80
}
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
+ }
80
96
class FileMenu : ControlsConsole {
81
97
public static string RECENTFILES = "RecentFiles.json" ;
82
98
83
99
SpriteModel hoveredFile ;
84
- Dictionary < string , SpriteModel > preloaded ;
100
+ Dictionary < string , ILoadResult > preloaded ;
85
101
86
102
HashSet < string > recentFiles ;
87
103
List < LabelButton > recentListing ;
@@ -92,13 +108,15 @@ class FileMenu : ControlsConsole {
92
108
93
109
int folderListingX ;
94
110
111
+ string console = "" ;
112
+
95
113
public FileMenu ( int width , int height , FileMode mode ) : base ( width , height ) {
96
114
97
115
DefaultBackground = Color . Black ;
98
116
99
117
100
118
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 > ( ) ;
102
120
this . recentListing = new List < LabelButton > ( ) ;
103
121
int n = 3 ;
104
122
@@ -135,7 +153,6 @@ void Load() {
135
153
FocusOnMouseClick = true ;
136
154
folderListing = new List < LabelButton > ( ) ;
137
155
138
-
139
156
textbox = new TextField ( width - folderListingX ) {
140
157
Position = new Point ( folderListingX , 1 ) ,
141
158
@@ -144,9 +161,8 @@ void Load() {
144
161
IsFocused = true ,
145
162
text = mode . InitialPath ,
146
163
} ;
147
- textbox . TextChanged += ( tf ) => {
148
- UpdateListing ( textbox . text ) ;
149
- } ;
164
+ textbox . TextChanged += tf => UpdateListing ( textbox . text ) ;
165
+ textbox . EnterPressed += tf => EnterFile ( ) ;
150
166
this . Children . Add ( textbox ) ;
151
167
UpdateListing ( textbox . text ) ;
152
168
}
@@ -214,38 +230,70 @@ void ShowFiles(IEnumerable<string> files) {
214
230
folderListing . Add ( b ) ;
215
231
216
232
void Load ( ) {
217
- mode . Enter ( this , file ) ;
218
- AddRecentFile ( file ) ;
233
+ EnterFile ( file ) ;
219
234
}
220
235
}
221
236
}
222
237
}
223
-
238
+ public void Log ( string message ) {
239
+ console = $ "{ message } ";
240
+ }
224
241
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 ) ;
227
244
} 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 } ]\n Loading...") ;
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 } ]\n Loaded successfully") ;
260
+ hoveredFile = s . preview ;
261
+ break ;
262
+
263
+ case LoadNonexistent s :
264
+ Log ( $ "[{ file } ]\n Does 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 ;
244
283
}
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 ) ;
245
294
}
246
295
}
247
296
}
248
-
249
297
public override void Render ( TimeSpan delta ) {
250
298
base . Render ( delta ) ;
251
299
this . Clear ( ) ;
@@ -258,13 +306,10 @@ public override void Render(TimeSpan delta) {
258
306
}
259
307
}
260
308
if ( hoveredFile != null && hoveredFile . sprite != null ) {
261
-
262
309
var s = hoveredFile . sprite ;
263
-
264
310
var previewX = ( Width - ( s . end - s . origin ) . X ) < 64 ? 0 : 64 ;
265
311
var previewY = 0 ;
266
312
var origin = hoveredFile . sprite . origin ;
267
-
268
313
269
314
var previewStart = new Point ( previewX , previewY ) ;
270
315
for ( int x = previewX ; x < Width ; x ++ ) {
@@ -277,15 +322,32 @@ public override void Render(TimeSpan delta) {
277
322
}
278
323
}
279
324
}
325
+ int yy = Height - 16 ;
326
+ foreach ( var line in console . Split ( '\n ' ) ) {
327
+ this . Print ( 16 , yy ++ , line , Color . White , Color . Black ) ;
328
+ }
280
329
}
281
330
public override bool ProcessKeyboard ( Keyboard keyboard ) {
282
331
if ( keyboard . IsKeyPressed ( Enter ) ) {
283
- var f = textbox . text ;
284
- mode . Enter ( this , f ) ;
285
- AddRecentFile ( f ) ;
332
+ EnterFile ( ) ;
286
333
}
287
334
return base . ProcessKeyboard ( keyboard ) ;
288
335
}
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
+ }
289
351
}
290
352
291
353
}
0 commit comments