Skip to content

Commit

Permalink
Initial map items/editors work, add map characters tab to script edit…
Browse files Browse the repository at this point in the history
…or (#45)
  • Loading branch information
jonko0493 authored Mar 8, 2023
1 parent 932af58 commit 094a8c8
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/SerialLoops.Lib/Items/ChibiItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace SerialLoops.Lib.Items
{
public class ChibiItem : Item
public class ChibiItem : Item, IPreviewableGraphic
{
public Chibi Chibi { get; set; }
public int ChibiIndex { get; set; }
Expand Down Expand Up @@ -59,6 +59,11 @@ public void PopulateScriptUses(ArchiveFile<EventFile> evt)

ScriptUses = list.ToArray();
}

SKBitmap IPreviewableGraphic.GetPreview(Project project)
{
return ChibiAnimations.First().Value.First().Frame;
}
}


Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops.Lib/Items/IPreviewableGraphic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public SKBitmap GetPreview(Project project, int maxWidth, int maxHeight)

public class NonePreviewableGraphic : ItemDescription, IPreviewableGraphic
{
public static NonePreviewableGraphic BACKGROUND = new(ItemType.Background);
public static NonePreviewableGraphic CHARACTER_SPRITE = new(ItemType.Character_Sprite);
public static readonly NonePreviewableGraphic BACKGROUND = new(ItemType.Background);
public static readonly NonePreviewableGraphic CHARACTER_SPRITE = new(ItemType.Character_Sprite);

private NonePreviewableGraphic(ItemType type) : base("NONE", type, "NONE") { }

Expand Down
86 changes: 80 additions & 6 deletions src/SerialLoops.Lib/Items/MapItem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using HaruhiChokuretsuLib.Archive;
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Archive.Graphics;
using System;
using System.Collections.Generic;
using SkiaSharp;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialLoops.Lib.Items
{
Expand All @@ -24,9 +20,87 @@ public MapItem(MapFile map, int qmapIndex) : base(map.Name[0..^1], ItemType.Map)
QmapIndex = qmapIndex;
}

public SKPoint GetOrigin(ArchiveFile<GraphicsFile> grp)
{
GraphicsFile layout = grp.Files.First(f => f.Index == Map.Settings.LayoutFileIndex);
return new SKPoint(layout.LayoutEntries[Map.Settings.LayoutSizeDefinitionIndex].ScreenX, layout.LayoutEntries[Map.Settings.LayoutSizeDefinitionIndex].ScreenY);
}

public SKBitmap GetMapImage(ArchiveFile<GraphicsFile> grp, bool displayPathingMap)
{
SKBitmap map;
if (Map.Settings.BackgroundLayoutStartIndex > 0)
{
map = Map.GetMapImages(grp, 0, Map.Settings.BackgroundLayoutStartIndex);
}
else
{
map = Map.GetMapImages(grp, 0, grp.Files.First(f => f.Index == Map.Settings.LayoutFileIndex).LayoutEntries.Count);
}
SKBitmap mapWithGrid = new(map.Width, map.Height);
SKCanvas canvas = new(mapWithGrid);
canvas.DrawBitmap(map, new SKPoint(0, 0));

if (displayPathingMap)
{
SKPoint gridZero = GetOrigin(grp);

if (Map.Settings.SlgMode)
{
for (int y = 0; y < Map.PathingMap.Length; y++)
{
for (int x = 0; x < Map.PathingMap[y].Length; x++)
{
SKPoint origin = new(gridZero.X - x * 32 + y * 32, gridZero.Y + x * 16 + y * 16);
SKPath diamond = new();
diamond.AddPoly(new SKPoint[]
{
origin,
new SKPoint(origin.X - 32, origin.Y + 16),
new SKPoint(origin.X, origin.Y + 32),
new SKPoint(origin.X + 32, origin.Y + 16)
});
canvas.DrawRegion(new SKRegion(diamond), GetPathingCellPaint(x, y));
}
}
}
else
{
for (int x = 0; x < Map.PathingMap.Length; x++)
{
for (int y = 0; y < Map.PathingMap[x].Length; y++)
{
SKPoint origin = new(gridZero.X - x * 16 + y * 16, gridZero.Y + x * 8 + y * 8);
SKPath diamond = new();
diamond.AddPoly(new SKPoint[]
{
origin,
new SKPoint(origin.X - 16, origin.Y + 8),
new SKPoint(origin.X, origin.Y + 16),
new SKPoint(origin.X + 16, origin.Y + 8),
});
canvas.DrawRegion(new SKRegion(diamond), GetPathingCellPaint(x, y));
}
}
}
}

canvas.Flush();
return mapWithGrid;
}

private SKPaint GetPathingCellPaint(int x, int y)
{
return Map.PathingMap[x][y] switch
{
1 => new() { Color = new SKColor(0, 128, 0, 186) }, // walkable
2 => new() { Color = new SKColor(0, 200, 200, 186) }, // spawnable
_ => new() { Color = new SKColor(255, 0, 0, 186) }, // unwalkable
};
}

public override void Refresh(Project project)
{
throw new NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/SerialLoops.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HaruhiChokuretsuLib" Version="0.20.1" />
<PackageReference Include="HaruhiChokuretsuLib" Version="0.20.2" />
<PackageReference Include="NitroPacker.Core" Version="2.0.1" />
<PackageReference Include="QuikGraph" Version="2.5.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/SerialLoops/Controls/EditorTabsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal DocumentPage CreateTab(ItemDescription item, Project project, ILogger l
case ItemDescription.ItemType.Group_Selection:
return new GroupSelectionEditor((GroupSelectionItem)project.Items.First(i => i.Name == item.Name), log, project, this);
case ItemDescription.ItemType.Map:
return new MapEditor((MapItem)project.Items.First(i => i.Name == item.Name), log);
return new MapEditor((MapItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Puzzle:
return new PuzzleEditor((PuzzleItem)project.Items.First(i => i.Name == item.Name), project, this, log);
case ItemDescription.ItemType.Scenario:
Expand Down
75 changes: 74 additions & 1 deletion src/SerialLoops/Controls/ItemEditorControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SerialLoops.Lib.Items;
using SerialLoops.Lib.Script;
using SerialLoops.Utility;
using SkiaSharp;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -91,17 +92,34 @@ public void RemoveAllClickEvents()
}
}

public class ChibiStackLayout : StackLayout
{
public int ChibiIndex { get; set; }
public SKBitmap ChibiBitmap { get; set; }
}

public class CommandGraphicSelectionButton : Panel
{
private readonly EditorTabsPanel _editorTabs;
private readonly ILogger _log;
public IPreviewableGraphic Selected { get; private set; }
public int SelectedIndex { get; set; }
public ScriptItemCommand Command { get; set; }
public int ParameterIndex { get; set; }
public Command SelectedChanged { get; }
public List<IPreviewableGraphic> Items { get; }
public Project Project { get; set; }

public CommandGraphicSelectionButton(EditorTabsPanel editorTabs, ILogger log)
{
_editorTabs = editorTabs;
_log = log;

SelectedChanged = new();
Items = new List<IPreviewableGraphic>();
InitializeComponent();
}

public CommandGraphicSelectionButton(IPreviewableGraphic selected, EditorTabsPanel editorTabs, ILogger log)
{
_editorTabs = editorTabs;
Expand Down Expand Up @@ -130,7 +148,61 @@ private StackLayout GetButtonPanel()
Items =
{
button,
ControlGenerator.GetFileLink(((ItemDescription)Selected), _editorTabs, _log)
ControlGenerator.GetFileLink((ItemDescription)Selected, _editorTabs, _log)
}
};
}

private void GraphicSelectionButton_Click(object sender, EventArgs e)
{
GraphicSelectionDialog dialog = new(new(Items), Selected, Project, _log);
IPreviewableGraphic description = dialog.ShowModal(this);
if (description == null) return;
Selected = description;
SelectedIndex = Items.IndexOf(Selected);
SelectedChanged?.Execute();
Content = GetButtonPanel();
}
}

public class GraphicSelectionButton : Panel
{
private readonly ILogger _log;
public IPreviewableGraphic Selected { get; private set; }
public int SelectedIndex { get; set; }
public Command SelectedChanged { get; }
public List<IPreviewableGraphic> Items { get; }
public Project Project { get; set; }

private string _text;

public GraphicSelectionButton(string text, ILogger log)
{
_log = log;
_text = text;

SelectedChanged = new();
Items = new List<IPreviewableGraphic>();
InitializeComponent();
}

private void InitializeComponent()
{
Content = GetButtonPanel();
}

private StackLayout GetButtonPanel()
{
Button button = new() { Text = _text };
button.Click += GraphicSelectionButton_Click;

return new StackLayout
{
Spacing = 10,
Orientation = Orientation.Horizontal,
Items =
{
button
}
};
}
Expand All @@ -141,6 +213,7 @@ private void GraphicSelectionButton_Click(object sender, EventArgs e)
IPreviewableGraphic description = dialog.ShowModal(this);
if (description == null) return;
Selected = description;
SelectedIndex = Items.IndexOf(Selected);
SelectedChanged?.Execute();
Content = GetButtonPanel();
}
Expand Down
23 changes: 21 additions & 2 deletions src/SerialLoops/Editors/MapEditor.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using SerialLoops.Utility;
using System;

namespace SerialLoops.Editors
{
public class MapEditor : Editor
{
private MapItem _map;

public MapEditor(MapItem map, ILogger log) : base(map, log)
public MapEditor(MapItem map, Project project, ILogger log) : base(map, log, project)
{
}

public override Container GetEditorPanel()
{
_map = (MapItem)Description;

StackLayout mapLayout = new()
{
Items =
{
new SKGuiImage(_map.GetMapImage(_project.Grp, false)),
}
};

CheckBox drawPathingMapBox = new() { Text = "Draw Pathing Map", Checked = false };
drawPathingMapBox.CheckedChanged += (obj, args) =>
{
mapLayout.Content = new SKGuiImage(_map.GetMapImage(_project.Grp, drawPathingMapBox.Checked ?? true));
};

return new StackLayout
{
Orientation = Orientation.Vertical,
Items =
{
new Label { Text = "Map Editor..todo!" }
mapLayout,
drawPathingMapBox,
}
};
}
Expand Down
Loading

0 comments on commit 094a8c8

Please sign in to comment.