Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wildham0 committed Mar 18, 2024
1 parent 2b819f0 commit f107929
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 270 deletions.
40 changes: 22 additions & 18 deletions DockPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ namespace FFRMapEditorMono
{
public class DockPicker : OptionPicker
{
public DockPicker(Texture2D _window, Texture2D _selector, Texture2D _placedicons, SpriteFont _font)
private Overworld overworld;
public DockPicker(Texture2D _window, Texture2D _selector, Texture2D _placedicons, SpriteFont _font, Overworld _overworld)
{
optionsWindow = _window;
optionSelector = _selector;
optionIcons = _placedicons;
optionFont = _font;
overworld = _overworld;

Position = new Vector2(64, 0);
zoom = 1.0f;
Expand All @@ -28,7 +30,7 @@ public DockPicker(Texture2D _window, Texture2D _selector, Texture2D _placedicons
new List<EditorTask>() {
new EditorTask() { Type = EditorTasks.DocksUpdate, Value = i } },
new List<EditorTask>() {
new EditorTask() { Type = EditorTasks.DocksUpdate, Value = i } }
new EditorTask() { Type = EditorTasks.DocksRemove, Value = i } }
)).ToList();

Show = false;
Expand All @@ -38,26 +40,28 @@ public DockPicker(Texture2D _window, Texture2D _selector, Texture2D _placedicons
SetOptionTextLength();
showPlaced = true;
}
public void UpdatePlaced(Overworld overworld)
public override void ProcessTasks(List<EditorTask> tasks)
{
if (!overworld.UpdatePlacedDocks)
{
return;
}
var validtasks = tasks.ToList();

placedOptions = overworld.GetShipData().Select(d => (int)d.TeleporterIndex).ToList();
if (placedOptions.Contains((int)OverworldTeleportIndex.None))
foreach (var task in validtasks)
{
placedOptions.RemoveAll(o => o == (int)OverworldTeleportIndex.None);
placedOptions.Add((int)OverworldTeleportIndex.DefaultLocation);
unplacedOptions = new();
if (task.Type == EditorTasks.UpdatePlacedDocksOverlay)
{
placedOptions = overworld.GetShipData().Select(d => (int)d.TeleporterIndex).ToList();
if (placedOptions.Contains((int)OverworldTeleportIndex.None))
{
placedOptions.RemoveAll(o => o == (int)OverworldTeleportIndex.None);
placedOptions.Add((int)OverworldTeleportIndex.DefaultLocation);
unplacedOptions = new();
}
else
{
unplacedOptions.Add((int)OverworldTeleportIndex.DefaultLocation);
}
tasks.Remove(task);
}
}
else
{
unplacedOptions.Add((int)OverworldTeleportIndex.DefaultLocation);
}

overworld.UpdatePlacedDocks = false;
}
}
}
96 changes: 96 additions & 0 deletions EditorTasks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FFRMapEditorMono
{
public enum EditorTasks
{
None,

ToggleInfoWindow,
HideAllWindows,

FileCreateNewMap,
FileLoadMap,
FileSaveMap,
//FileSaveMapAs,

OverworldBlueMap,
OverworldLoadMap,

TilesOpen,
TilesToggle,
TilesSetTool,
TilesUpdate,
TilesPickerUpdate,

BrushesOpen,
BrushesToggle,
BrushesSetTool,
BrushesUpdate,
BrushesPickerUpdate,
BrushesUpdateSize,

DocksOpen,
DocksUpdate,
DocksSetTool,
DocksRemove,

DomainsOpen,
DomainsUpdate,
DomainsSetTool,

TemplatesOpen,
TemplatesUpdate,
//TemplatesSetTool,

MapObjectsOpen,
MapObjectsUpdate,
MapObjectsSetTool,
MapObjectsRemove,

WindowsClose,

ExitWarningOpen,
ExitWarningClose,

SaveWarningOpen,
SaveWarningClose,
SaveNoWarning,
SetSavingMode,
SaveWarningUpdate,

NewMapWarningOpen,
NewMapWarningClose,

LoadMapWarningOpen,
LoadMapWarningClose,

PaintingUndo,
PaintingRedo,

UpdatePlacedObjectsOverlay,
UpdatePlacedDocksOverlay,
UpdatePlacedTilesOverlay,

SaveBackupMap,
ResetBackupCounter,

ExitProgramHard,
ExitProgram

}

public struct EditorTask
{
public EditorTasks Type { get; set; }
public int Value { get; set; }
}
}
65 changes: 31 additions & 34 deletions Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ public class Game1 : Game
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Overworld overworld;
private TilePicker tilePicker;
private BrushPicker brushPicker;
private DomainPicker domainsPicker;
private DockPicker docksPicker;
private MapObjectPicker mapObjectsPicker;
private TemplatePicker templatesPicker;
private ToolsMenu toolsMenu;
private InfoWindow infoWindow;

private List<WarningWindow> warningWindows;
private List<OptionPicker> optionPickers;

private Point windowSize = new(1200, 800);
private SpriteFont font;
Expand All @@ -33,6 +28,7 @@ public class Game1 : Game
private MouseState mouse;
private List<EditorTask> editorTasks;
private WindowsManager windowsManager;

private bool LastActiveState = true;
private List<string> unplacedTiles;
private int suspendKeyboard = 0;
Expand Down Expand Up @@ -96,14 +92,18 @@ protected override void LoadContent()

// Create menus
toolsMenu = new(toolsTexture, selectors32, font);
domainsPicker = new(domainGroupsIcons, selectors32, font);
docksPicker = new(docksIcons, selectors32, placingIcons, font);
brushPicker = new(brushesTexture, selectors16, font);
templatesPicker = new(templatesIcons, selectors32, font);
infoWindow = new(infowindowtexture, font, buttonTexture, windowSize);
mapObjectsPicker = new(mapobjectsIcons, selectors16, placingIcons, font);
currentTool = new();
tilePicker = new(tileSetTexture, selectors16, placingIcons, font);

optionPickers = new()
{
new DomainPicker(domainGroupsIcons, selectors32, font),
new TemplatePicker(templatesIcons, selectors32, font),
new DockPicker(docksIcons, selectors32, placingIcons, font, overworld),
new MapObjectPicker(mapobjectsIcons, selectors16, placingIcons, font, overworld),
new BrushPicker(brushesTexture, selectors16, font),
new TilePicker(tileSetTexture, selectors16, placingIcons, font, overworld)
};

// Warning Windows
warningWindows = new()
Expand All @@ -115,8 +115,9 @@ protected override void LoadContent()
};

// Create Windows manager
windowsManager = new(toolsMenu, tilePicker, brushPicker, domainsPicker, docksPicker, mapObjectsPicker, templatesPicker, infoWindow);
windowsManager = new(toolsMenu, infoWindow);
windowsManager.RegisterWarningWindows(warningWindows);
windowsManager.RegisterOptionPickers(optionPickers);

}
protected override void Update(GameTime gameTime)
Expand Down Expand Up @@ -183,31 +184,29 @@ protected override void Update(GameTime gameTime)
}
}


// Remove None tasks
editorTasks.RemoveAll(t => t.Type == EditorTasks.None);

// Update Mouse Statuts
mouse.Update();

// Select Options
editorTasks.AddRange(tilePicker.PickOption(mouse));
editorTasks.AddRange(toolsMenu.PickOption(mouse));
editorTasks.AddRange(domainsPicker.PickOption(mouse));
editorTasks.AddRange(docksPicker.PickOption(mouse));
editorTasks.AddRange(mapObjectsPicker.PickOption(mouse));
editorTasks.AddRange(brushPicker.PickOption(mouse));
editorTasks.AddRange(templatesPicker.PickOption(mouse));
editorTasks.AddRange(infoWindow.ProcessInput(mouse));

foreach (var picker in optionPickers)
{
editorTasks.AddRange(picker.PickOption(mouse));
}

foreach (var warning in warningWindows)
{
editorTasks.AddRange(warning.ProcessInput(mouse));
}

// Update Selected Tool
currentTool.Update(editorTasks);
currentTool.GetTemplate(editorTasks, templatesPicker.CurrentTemplate);
currentTool.GetTemplate(editorTasks, optionPickers.OfType<TemplatePicker>().First().CurrentTemplate);
toolsMenu.UpdateBrushSize(currentTool.BrushSize + 1);

// Interact with the map
Expand All @@ -218,21 +217,20 @@ protected override void Update(GameTime gameTime)
overworld.UpdateDomain(mouse, currentTool);
overworld.UpdateDock(mouse, currentTool);
overworld.UpdateMapObject(mouse, currentTool);
mapObjectsPicker.UpdatePlaced(overworld);
docksPicker.UpdatePlaced(overworld);
unplacedTiles = tilePicker.UpdatePlaced(overworld);
editorTasks.AddRange(overworld.GetTile(mouse));
}

// Update selected tiles
tilePicker.ProcessTasks(editorTasks);
brushPicker.ProcessTasks(editorTasks);
foreach (var picker in optionPickers)
{
picker.ProcessTasks(editorTasks);
}

// Update Windows
windowsManager.ProcessTasks(editorTasks, overworld);

// Update File management
warningWindows.OfType<SaveWarningWindow>().First().ProcessTasks(overworld, unplacedTiles, windowSize, editorTasks);
warningWindows.OfType<SaveWarningWindow>().First().ProcessTasks(overworld, optionPickers.OfType<TilePicker>().First().GetUnplacedTiles(), windowSize, editorTasks);

fileManager.ProcessTasks(overworld, unplacedTiles, editorTasks);
overworld.ProcessTasks(fileManager.OverworldData, editorTasks);
Expand Down Expand Up @@ -273,17 +271,16 @@ protected override void Draw(GameTime gameTime)

// Draw map+overlays
overworld.Draw(_spriteBatch, windowsManager, currentTool, mouse);

// Draw menus
tilePicker.Draw(_spriteBatch, font, mouse.Position);
brushPicker.Draw(_spriteBatch, font, mouse.Position);
domainsPicker.Draw(_spriteBatch, font, mouse.Position);
docksPicker.Draw(_spriteBatch, font, mouse.Position);
mapObjectsPicker.Draw(_spriteBatch, font, mouse.Position);
templatesPicker.Draw(_spriteBatch, font, mouse.Position);
toolsMenu.Draw(_spriteBatch, font, mouse.Position);
infoWindow.Draw(_spriteBatch);

foreach (var picker in optionPickers)
{
picker.Draw(_spriteBatch, font, mouse.Position);
}

foreach (var warning in warningWindows)
{
warning.Draw(_spriteBatch);
Expand Down
25 changes: 15 additions & 10 deletions MapObjectPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ namespace FFRMapEditorMono
{
public class MapObjectPicker : OptionPicker
{
public MapObjectPicker(Texture2D _window, Texture2D _selector, Texture2D _placingicons, SpriteFont _font)
private Overworld overworld;
public MapObjectPicker(Texture2D _window, Texture2D _selector, Texture2D _placingicons, SpriteFont _font, Overworld _overworld)
{
optionsWindow = _window;
optionSelector = _selector;
optionIcons = _placingicons;
optionFont = _font;

overworld = _overworld;

Position = new Vector2(64, 0);
zoom = 2.0f;
optionsRows = 1;
Expand All @@ -25,10 +28,9 @@ public MapObjectPicker(Texture2D _window, Texture2D _selector, Texture2D _placin

options = mapObjectsNames.Select((o, i) => (o.Item1,
new List<EditorTask>() {
new EditorTask() { Type = EditorTasks.MapObjectsUpdate, Value = i },
new EditorTask() { Type = EditorTasks.WindowsClose, Value = 10 } },
new EditorTask() { Type = EditorTasks.MapObjectsUpdate, Value = i } },
new List<EditorTask>() {
new EditorTask() { Type = EditorTasks.MapObjectsUpdate, Value = i } }
new EditorTask() { Type = EditorTasks.MapObjectsRemove, Value = i } }
)).ToList();

Show = false;
Expand All @@ -48,15 +50,18 @@ public MapObjectPicker(Texture2D _window, Texture2D _selector, Texture2D _placin
("Ship (Unused)", EditorTasks.None, EditorTasks.None),
("Airship", EditorTasks.None, EditorTasks.None),
};
public void UpdatePlaced(Overworld overworld)
public override void ProcessTasks(List<EditorTask> tasks)
{
if (!overworld.UpdatePlacedMapObjects)
var validtasks = tasks.ToList();

foreach (var task in validtasks)
{
return;
if (task.Type == EditorTasks.UpdatePlacedObjectsOverlay)
{
placedOptions = overworld.GetPlacedMapObjects().Select(o => (int)o).ToList();
tasks.Remove(task);
}
}

placedOptions = overworld.GetPlacedMapObjects().Select(o => (int)o).ToList();
overworld.UpdatePlacedMapObjects = false;
}
}
}
1 change: 1 addition & 0 deletions OptionPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public bool MouseHovering(Vector2 mousecursor)

return window.Contains(mousecursor) && Show;
}
public virtual void ProcessTasks(List<EditorTask> tasks) { }
public virtual void Draw(SpriteBatch spriteBatch, SpriteFont font, Vector2 mouseCursor)
{
if (!Show)
Expand Down
Loading

0 comments on commit f107929

Please sign in to comment.