Skip to content

Commit

Permalink
Editor autosave (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony authored Dec 20, 2024
2 parents 2f73357 + b6ed822 commit bc9e050
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/BoardSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public static class BoardSaver
{
public static string GetLevelSavePath()
{
// TODO add a function to get %localappdata%/nonoSharp
// this is because right now in auto save we save it in <level save path>/..
// which assumes that the data folder is one directory up the custom levels folder
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "nonoSharp", "CustomLevels");
}

Expand Down
48 changes: 48 additions & 0 deletions src/Editor/AutoSaveNotice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NonoSharp.UI;

namespace NonoSharp.Editor;

public class AutoSaveNotice
{
public static readonly int ButtonSpacing = 10;

public Button ContinueButton { get; private set; }
public Button RestartButton { get; private set; }
public Button CancelButton { get; private set; }

public AutoSaveNotice()
{
ContinueButton = new(0, 0, 0, 45, StringManager.GetString("continue"), Settings.GetDarkAccentColor(), Settings.GetAccentColor(), true);
RestartButton = new(0, 0, 0, 45, StringManager.GetString("restart"), Settings.GetDarkAccentColor(), Settings.GetAccentColor(), true);
CancelButton = new(0, 0, 0, 45, StringManager.GetString("cancel"), Settings.GetDarkAccentColor(), Settings.GetAccentColor(), true);
}

public void Draw(SpriteBatch sprBatch)
{
TextRenderer.DrawTextCenter(sprBatch, "DefaultFont", 0.6f, StringManager.GetString("autoSaveNotice"), Color.White, new(0, sprBatch.GraphicsDevice.Viewport.Bounds.Height / 2 - 41, sprBatch.GraphicsDevice.Viewport.Bounds.Width, 2));
ContinueButton.Draw(sprBatch);
RestartButton.Draw(sprBatch);
CancelButton.Draw(sprBatch);
}

public void Update(GraphicsDevice graphDev, MouseState mouse, MouseState mouseOld, KeyboardState kb, KeyboardState kbOld)
{
ContinueButton.Update(mouse, mouseOld, kb, kbOld);
RestartButton.Update(mouse, mouseOld, kb, kbOld);
CancelButton.Update(mouse, mouseOld, kb, kbOld);

int totalWidth = ContinueButton.width + RestartButton.width + CancelButton.width + ButtonSpacing * 2;

ContinueButton.x = graphDev.Viewport.Bounds.Width / 2 - totalWidth / 2;
RestartButton.x = ContinueButton.x + ContinueButton.width + ButtonSpacing;
CancelButton.x = RestartButton.x + RestartButton.width + ButtonSpacing;

int buttonY = graphDev.Viewport.Bounds.Height / 2;
ContinueButton.y = buttonY;
RestartButton.y = buttonY;
CancelButton.y = buttonY;
}
}
46 changes: 45 additions & 1 deletion src/Editor/Editor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
Expand All @@ -9,15 +10,17 @@ public enum EditorState
SetSize,
Editor,
SaveLevel,
AutoSaveNotice,
}

public class Editor : IGameState
{
private EditorState _state;
// TODO do the same thing as done with regular game states but for editor
// TODO tda refactor for editor
private SetSizeState _setSize;
private readonly EditorMain _main;
private SaveLevelState _saveLevel;
private AutoSaveNotice _autoSaveNotice;
private bool _editingExistingLevel;
private string _levelName; // when editing existing level

Expand All @@ -29,6 +32,8 @@ public Editor()
_setSize = new();
_main = new();
_saveLevel = new();
_autoSaveNotice = new();
checkAutoSave();
}

public Editor(string levelName)
Expand All @@ -39,6 +44,8 @@ public Editor(string levelName)
_setSize = new();
_main = new(levelName);
_saveLevel = new();
_autoSaveNotice = new();
checkAutoSave();
}

public IGameState? Update(MouseState mouse, MouseState mouseOld, KeyboardState kb, KeyboardState kbOld, GraphicsDevice graphDev, ref LevelMetadata levelMetadata, bool hasFocus)
Expand All @@ -51,6 +58,7 @@ public Editor(string levelName)
{
_state = EditorState.Editor;
_main.Board.Make(_setSize.GetSize());
_main.EnableAutoSaveTimer(true);
}
if (_setSize.BackButton.IsClicked)
{
Expand All @@ -63,6 +71,7 @@ public Editor(string levelName)
_main.Update(mouse, mouseOld, kb, kbOld, graphDev);
if (_main.SaveButton.IsClicked)
{
_main.EnableAutoSaveTimer(false);
if (_editingExistingLevel)
{
BoardSaver.SaveBoard(_main.Board, _levelName, _main.MaxHintsBox.GetNumberValue());
Expand All @@ -75,14 +84,18 @@ public Editor(string levelName)
}
if (_main.BackButton.IsClicked)
{
_main.EnableAutoSaveTimer(false);
if (_editingExistingLevel)
{
LevelSelect levelSelect = new();
levelSelect.FindLevels();
return levelSelect;
}
else
{
_main.AutoSave();
return new MainMenu();
}
}
break;
case EditorState.SaveLevel:
Expand All @@ -92,6 +105,18 @@ public Editor(string levelName)
if (_saveLevel.BackButton.IsClicked)
_state = EditorState.Editor;
break;
case EditorState.AutoSaveNotice:
_autoSaveNotice.Update(graphDev, mouse, mouseOld, kb, kbOld);
if (_autoSaveNotice.CancelButton.IsClicked)
return new MainMenu();
if (_autoSaveNotice.RestartButton.IsClicked)
_state = EditorState.SetSize;
if (_autoSaveNotice.ContinueButton.IsClicked)
{
_main.LoadAutoSave();
_state = EditorState.Editor;
}
break;
}

return null;
Expand All @@ -110,6 +135,9 @@ public void Draw(SpriteBatch sprBatch)
case EditorState.SaveLevel:
_saveLevel.Draw(sprBatch);
break;
case EditorState.AutoSaveNotice:
_autoSaveNotice.Draw(sprBatch);
break;
}
}

Expand All @@ -128,4 +156,20 @@ public void UpdateInput(TextInputEventArgs tiea)
break;
}
}

public void AutoSave()
{
if (_state == EditorState.Editor)
{
_main.AutoSave();
}
}

private void checkAutoSave()
{
if (File.Exists(Path.Combine(BoardSaver.GetLevelSavePath(), "..", "EditorAutosave.nono")))
{
_state = EditorState.AutoSaveNotice;
}
}
}
39 changes: 39 additions & 0 deletions src/Editor/EditorMain.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.IO;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NonoSharp.UI;
using Serilog;

namespace NonoSharp.Editor;

Expand All @@ -16,17 +20,21 @@ public class EditorMain
public NumberTextBox MaxHintsBox { get; private set; } = null!;
public EditorBoard Board { get; private set; }

private Timer _autoSaveTimer = null!;

public EditorMain()
{
Board = new();
makeButtons();
initAutoSaveTimer();
}

public EditorMain(string levelName)
{
Board = new();
Board.Make(levelName);
makeButtons();
initAutoSaveTimer();
}

public void Update(MouseState mouse, MouseState mouseOld, KeyboardState kb, KeyboardState kbOld, GraphicsDevice graphDev)
Expand Down Expand Up @@ -106,6 +114,24 @@ public void Draw(SpriteBatch sprBatch)
}
}

public void EnableAutoSaveTimer(bool enabled)
{
_autoSaveTimer.Enabled = enabled;
}

public void AutoSave()
{
Log.Logger.Information("Auto saving level");
BoardSaver.SaveBoard(Board, Path.Combine("..", "EditorAutosave"), Board.maxHints);
}

public void LoadAutoSave()
{
Log.Logger.Information("Loading auto save");
Board = new();
Board.Make(Path.Combine(BoardSaver.GetLevelSavePath(), "..", "EditorAutosave"));
}

private void makeButtons()
{
SaveButton = new(10, 10, 0, 45, StringManager.GetString("save"), Settings.GetDarkAccentColor(), Settings.GetAccentColor(), Keys.S, true);
Expand All @@ -116,4 +142,17 @@ private void makeButtons()
TestResetButton = new(0, 0, 0, 45, StringManager.GetString("restart"), Settings.GetDarkAccentColor(), Settings.GetAccentColor(), Keys.R, true);
MaxHintsBox = new(10, 0, 195, Color.Gray, Color.DarkGray, Color.White, Color.White, Color.DarkGray, Color.LightGray, StringManager.GetString("maxHintsPlaceholder"));
}

private void autoSave(object? source, ElapsedEventArgs eea)
{
AutoSave();
}

private void initAutoSaveTimer()
{
_autoSaveTimer = new(Settings.GetInt("editorAutoSaveInterval")); // TODO dispose of it properly
_autoSaveTimer.Elapsed += autoSave;
_autoSaveTimer.AutoReset = true;
_autoSaveTimer.Enabled = false;
}
}
1 change: 1 addition & 0 deletions src/Editor/SaveLevelState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void Update(MouseState mouse, MouseState mouseOld, KeyboardState kb, Keyb
if (_saveButton.IsClicked)
{
BoardSaver.SaveBoard(board, _levelNameBox.text, maxHints);
File.Delete(Path.Combine(BoardSaver.GetLevelSavePath(), "..", "EditorAutosave.nono"));
_saved = true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/NonoSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ protected override void Draw(GameTime gameTime)

protected override void OnExiting(object sender, ExitingEventArgs eea)
{
if (_currentState is Editor.Editor editor)
editor.AutoSave();

PlayState.StopSolveTimeThread();
Settings.Save();

Expand Down
14 changes: 12 additions & 2 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static class Settings
{"accentColor", "0;128;0"},
{"enableHints", "yes"},
{"showBgGrid", "yes"},
{"sound", "yes"}
{"sound", "yes"},
{"editorAutoSaveInterval", "180"}
};

public const float AccentColorDefaultDarkerAmount = 0.3f;
Expand Down Expand Up @@ -93,14 +94,23 @@ public static string Get(string key)
return _settings[key];

// If it's not found, look it up in default settings
return DefaultSettings[key];
return DefaultSettings[key]; // TODO handle cases where setting is not found in default too
}

public static bool GetBool(string key)
{
return Get(key) == "yes";
}

public static int GetInt(string key)
{
string val = Get(key);
int valInt;
if (int.TryParse(val, out valInt))
return valInt;
return -1;
}

public static Color ParseColorSettingString(string colorString)
{
string[] parts = colorString.Split(';');
Expand Down

0 comments on commit bc9e050

Please sign in to comment.