public async Task Load(string file) { if (!File.Exists(file)) { preloaded[file] = new LoadNonexistent(); return; } preloaded[file] = new LoadBusy(); await Task.Run(StartLoad); void StartLoad() { try { var model = ASECIILoader.DeserializeObject <SpriteModel>(File.ReadAllText(file)); if (model?.filepath == null) { Failed("Filepath does not exist"); return; } var s = new LoadSuccess(model); preloaded[file] = s; Handle(file, s); } catch (Exception e) { Failed(e.Message); } void Failed(string message) { var f = new LoadFailure(message); preloaded[file] = f; Handle(file, f); } } }
public void Enter(Console console, string filepath) { var Width = console.Width; var Height = console.Height; if (File.Exists(filepath)) { try { var sprite = ASECIILoader.DeserializeObject <SpriteModel>(File.ReadAllText(filepath)); if (sprite.filepath != filepath) { sprite.filepath = filepath; File.WriteAllText($"{filepath}", ASECIILoader.SerializeObject(sprite)); } sprite.OnLoad(); Game.Instance.Screen = new EditorMain(Width, Height, sprite); Program.SaveState(new EditorState(filepath)); } catch { throw; } } else { var model = new SpriteModel(Width, Height) { filepath = filepath }; model.sprite.layers.Add(new Layer()); File.WriteAllText(filepath, ASECIILoader.SerializeObject(model)); console.Children.Add(new EditorMain(Width, Height, model)); } }
public FileMenu(int width, int height, FileMode mode) : base(width, height) { DefaultBackground = Color.Black; this.recentFiles = File.Exists(RECENTFILES) ? ASECIILoader.DeserializeObject <HashSet <string> >(File.ReadAllText(RECENTFILES)).Where(f => File.Exists(f)).ToHashSet() : new HashSet <string>(); this.preloaded = new Dictionary <string, ILoadResult>(); this.recentListing = new List <LabelButton>(); int n = 3; if (recentFiles.Any()) { folderListingX = 32; foreach (var f in recentFiles) { var p = Path.GetFileName(f); var b = new LabelButton(p, Load, () => textbox.text = f) { Position = new Point(4, n), }; b.MouseEnter += (e, args) => { ShowPreview(f); }; this.Children.Add(b); recentListing.Add(b); n++; void Load() { mode.Enter(this, f); //AddRecentFile(f); } } } else { folderListingX = 8; } this.mode = mode; UseMouse = true; UseKeyboard = true; IsFocused = true; FocusOnMouseClick = true; folderListing = new List <LabelButton>(); textbox = new TextField(width - folderListingX) { Position = new Point(folderListingX, 1), UseKeyboard = true, UseMouse = true, IsFocused = true, text = mode.InitialPath, }; textbox.TextChanged += tf => UpdateListing(textbox.text); textbox.EnterPressed += tf => EnterFile(); this.Children.Add(textbox); UpdateListing(textbox.text); }