Skip to content

Commit

Permalink
Add item items (#338)
Browse files Browse the repository at this point in the history
Co-authored-by: William <[email protected]>
  • Loading branch information
jonko0493 and WiIIiam278 authored Jan 28, 2024
1 parent 8516ab2 commit b8279b2
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/Items/BackgroundItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BackgroundItem(string name, int id, BgTableEntry entry, Project project)
if (cgEntry is not null)
{
CgName = cgEntry?.Name?.GetSubstitutedString(project);
ExtrasShort = cgEntry?.Flag ?? 0;
ExtrasShort = (short)(cgEntry?.Flag ?? 0);
ExtrasInt = cgEntry?.Unknown04 ?? 0;
}
PopulateScriptUses(project.Evt);
Expand Down
1 change: 1 addition & 0 deletions src/SerialLoops.Lib/Items/ItemDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum ItemType
Chess,
Chibi,
Group_Selection,
Item,
Map,
Place,
Puzzle,
Expand Down
71 changes: 71 additions & 0 deletions src/SerialLoops.Lib/Items/ItemItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using HaruhiChokuretsuLib.Archive.Graphics;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib.Util;
using SkiaSharp;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SerialLoops.Lib.Items
{
public class ItemItem : Item, IPreviewableGraphic
{
public GraphicsFile ItemGraphic { get; set; }
public int ItemIndex { get; set; }

public ItemItem(string name) : base(name, ItemType.Item)
{
}
public ItemItem(string name, int itemIndex, short grpIndex, Project project) : base(name, ItemType.Item)
{
ItemGraphic = project.Grp.Files[grpIndex - 1];
ItemIndex = itemIndex;
}

public SKBitmap GetImage()
{
return ItemGraphic.GetImage(transparentIndex: 0);
}

public void SetImage(SKBitmap image, IProgressTracker tracker, ILogger log)
{
tracker.Focus("Setting CG single image...", 1);
List<SKColor> palette = Helpers.GetPaletteFromImage(image, 255, log);
ItemGraphic.SetPalette(palette, 0);
ItemGraphic.SetImage(image);
tracker.Finished++;
}

public void Write(Project project, ILogger log)
{
using MemoryStream grp1Stream = new();
ItemGraphic.GetImage().Encode(grp1Stream, SKEncodedImageFormat.Png, 1);
IO.WriteBinaryFile(Path.Combine("assets", "graphics", $"{ItemGraphic.Index:X3}.png"), grp1Stream.ToArray(), project, log);
IO.WriteStringFile(Path.Combine("assets", "graphics", $"{ItemGraphic.Index:X3}_pal.csv"),
string.Join(',', ItemGraphic.Palette.Select(c => c.ToString())), project, log);
}

SKBitmap IPreviewableGraphic.GetPreview(Project project)
{
return GetImage();
}

public override void Refresh(Project project, ILogger log)
{
}

public enum ItemLocation
{
Exit = 221,
Left = 222,
Center = 223,
Right = 224,
}

public enum ItemTransition
{
Slide = 225,
Fade = 226,
}
}
}
13 changes: 12 additions & 1 deletion src/SerialLoops.Lib/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ public LoadProjectResult LoadArchives(ILogger log, IProgressTracker tracker)
log.LogException($"Failed to load BGM tracks", ex);
return new(LoadProjectState.FAILED);
}

try
{
string[] voiceFiles = SoundDS.VoiceSection.Where(vce => vce is not null).Select(vce => Path.Combine(IterativeDirectory, "rom", "data", vce)).ToArray(); /*Directory.GetFiles(Path.Combine(IterativeDirectory, "rom", "data", "vce")).OrderBy(s => s).ToArray();*/
Expand Down Expand Up @@ -491,6 +490,18 @@ public LoadProjectResult LoadArchives(ILogger log, IProgressTracker tracker)
return new(LoadProjectState.FAILED);
}

try
{
ItemFile itemFile = Dat.Files.First(f => f.Name == "ITEMS").CastTo<ItemFile>();
tracker.Focus("Items", 1);
Items.AddRange(itemFile.Items.Where(i => i > 0).Select((i, idx) => new ItemItem(Grp.Files[i - 1].Name, idx, i, this)));
}
catch (Exception ex)
{
log.LogException($"Failed to load item file", ex);
return new(LoadProjectState.FAILED);
}

try
{
tracker.Focus("Character Sprites", 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HaruhiChokuretsuLib.Archive.Event;
using SerialLoops.Lib.Items;

namespace SerialLoops.Lib.Script.Parameters
{
public class ItemLocationScriptParameter(string name, short itemLocation) : ScriptParameter(name, ParameterType.ITEM_LOCATION)
{
public ItemItem.ItemLocation Location { get; set; } = (ItemItem.ItemLocation)itemLocation;
public override short[] GetValues(object obj = null) => [(short)Location];

public override ItemLocationScriptParameter Clone(Project project, EventFile eventFile)
{
return new(Name, (short)Location);
}
}
}
9 changes: 2 additions & 7 deletions src/SerialLoops.Lib/Script/Parameters/ItemScriptParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

namespace SerialLoops.Lib.Script.Parameters
{
public class ItemScriptParameter : ScriptParameter
public class ItemScriptParameter(string name, short itemIndex) : ScriptParameter(name, ParameterType.ITEM)
{
public short ItemIndex { get; set; }
public short ItemIndex { get; set; } = itemIndex;
public override short[] GetValues(object obj = null) => new short[] { ItemIndex };

public ItemScriptParameter(string name, short itemIndex) : base(name, ParameterType.ITEM)
{
ItemIndex = itemIndex;
}

public override ItemScriptParameter Clone(Project project, EventFile eventFile)
{
return new(Name, ItemIndex);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HaruhiChokuretsuLib.Archive.Event;
using SerialLoops.Lib.Items;

namespace SerialLoops.Lib.Script.Parameters
{
public class ItemTransitionScriptParameter(string name, short itemTransition) : ScriptParameter(name, ParameterType.ITEM_TRANSITION)
{
public ItemItem.ItemTransition Transition { get; set; } = (ItemItem.ItemTransition)itemTransition;
public override short[] GetValues(object obj = null) => [(short)Transition];

public override ItemTransitionScriptParameter Clone(Project project, EventFile eventFile)
{
return new(Name, (short)Transition);
}
}
}
2 changes: 2 additions & 0 deletions src/SerialLoops.Lib/Script/Parameters/ScriptParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public enum ParameterType
FLAG,
FRIENDSHIP_LEVEL,
ITEM,
ITEM_LOCATION,
ITEM_TRANSITION,
MAP,
OPTION,
PALETTE_EFFECT,
Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops.Lib/Script/ScriptItemCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ private static List<ScriptParameter> GetScriptParameters(ScriptCommandInvocation
parameters.Add(new ItemScriptParameter("Item", parameter));
break;
case 1:
parameters.Add(new ShortScriptParameter("X", parameter));
parameters.Add(new ItemLocationScriptParameter("Location", parameter));
break;
case 2:
parameters.Add(new ShortScriptParameter("Y", parameter));
parameters.Add(new ItemTransitionScriptParameter("Transition", parameter));
break;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/SerialLoops.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="BunLabs.NAudio.Flac" Version="2.0.1" />
<PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="7.3.0.1" />
<PackageReference Include="HarfBuzzSharp.NativeAssets.macOS" Version="7.3.0.1" />
<PackageReference Include="HaruhiChokuretsuLib" Version="0.35.3" />
<PackageReference Include="HaruhiChokuretsuLib" Version="0.36.6" />
<PackageReference Include="NAudio.Vorbis" Version="1.5.0" />
<PackageReference Include="NitroPacker.Core" Version="2.4.4" />
<PackageReference Include="NLayer" Version="1.15.0" />
Expand Down
2 changes: 2 additions & 0 deletions src/SerialLoops/Controls/EditorTabsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private DocumentPage CreateTab(ItemDescription item, Project project, ILogger lo
return new ChibiEditor((ChibiItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Group_Selection:
return new GroupSelectionEditor((GroupSelectionItem)project.Items.First(i => i.Name == item.Name), log, project, this);
case ItemDescription.ItemType.Item:
return new ItemEditor((ItemItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Map:
return new MapEditor((MapItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Place:
Expand Down
6 changes: 3 additions & 3 deletions src/SerialLoops/Controls/ItemEditorControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public CommandGraphicSelectionButton(EditorTabsPanel editorTabs, ILogger log)
_log = log;

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

Expand All @@ -144,7 +144,7 @@ public CommandGraphicSelectionButton(IPreviewableGraphic selected, EditorTabsPan
Selected = selected;

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

Expand Down Expand Up @@ -199,7 +199,7 @@ public GraphicSelectionButton(string text, ILogger log)
_text = text;

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

Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops/Editors/BackgroundEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override Container GetEditorPanel()
private void ExportButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new();
saveFileDialog.Filters.Add(new() { Name = "PNG Image", Extensions = new string[] { ".png" } });
saveFileDialog.Filters.Add(new() { Name = "PNG Image", Extensions = [".png"] });
if (saveFileDialog.ShowAndReportIfFileSelected(this))
{
try
Expand All @@ -90,7 +90,7 @@ private void ReplaceButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new();
SKBitmap original = _bg.GetBackground();
openFileDialog.Filters.Add(new() { Name = "Supported Images", Extensions = new string[] { ".bmp", ".gif", ".heif", ".jpg", ".jpeg", ".png", ".webp", } });
openFileDialog.Filters.Add(new() { Name = "Supported Images", Extensions = [".bmp", ".gif", ".heif", ".jpg", ".jpeg", ".png", ".webp",] });
if (openFileDialog.ShowAndReportIfFileSelected(this))
{
ImageCropResizeDialog bgResizeDialog = new(SKBitmap.Decode(openFileDialog.FileName), original.Width, original.Height, _log);
Expand Down
98 changes: 98 additions & 0 deletions src/SerialLoops/Editors/ItemEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Dialogs;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using SerialLoops.Utility;
using SkiaSharp;
using System;
using System.IO;

namespace SerialLoops.Editors
{
public class ItemEditor(ItemItem item, Project project, ILogger log) : Editor(item,log, project)
{
private ItemItem _item;

public override Container GetEditorPanel()
{
_item = (ItemItem)Description;

Button exportButton = new() { Text = "Export" };
exportButton.Click += ExportButton_Click;

Button replaceButton = new() { Text = "Replace" };
replaceButton.Click += ReplaceButton_Click;

return new Scrollable
{
Content = new StackLayout
{
Orientation = Orientation.Vertical,
Spacing = 5,
Items =
{
new ImageView() { Image = new SKGuiImage(_item.GetImage()) },
new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 3,
Items =
{
exportButton,
replaceButton,
}
},
}
}
};
}

private void ExportButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new();
saveFileDialog.Filters.Add(new() { Name = "PNG Image", Extensions = [".png"] });
if (saveFileDialog.ShowAndReportIfFileSelected(this))
{
try
{
using FileStream fs = File.Create(saveFileDialog.FileName);
_item.GetImage().Encode(fs, SKEncodedImageFormat.Png, 1);
}
catch (Exception ex)
{
_log.LogError($"Failed to export background {_item.DisplayName} to file {saveFileDialog.FileName}: {ex.Message}\n\n{ex.StackTrace}");
}
}
}

private void ReplaceButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new();
SKBitmap original = _item.GetImage();
openFileDialog.Filters.Add(new() { Name = "Supported Images", Extensions = [".bmp", ".gif", ".heif", ".jpg", ".jpeg", ".png", ".webp",] });
if (openFileDialog.ShowAndReportIfFileSelected(this))
{
ImageCropResizeDialog itemResizeDialog = new(SKBitmap.Decode(openFileDialog.FileName), original.Width, original.Height, _log);
itemResizeDialog.Closed += (sender, args) =>
{
if (itemResizeDialog.SaveChanges)
{
try
{
LoopyProgressTracker tracker = new();
_ = new ProgressDialog(() => _item.SetImage(itemResizeDialog.FinalImage, tracker, _log),
() => Content = GetEditorPanel(), tracker, $"Replacing {_item.DisplayName}...");
UpdateTabTitle(false);
}
catch (Exception ex)
{
_log.LogError($"Failed to replace background {_item.DisplayName} with file {openFileDialog.FileName}: {ex.Message}\n\n{ex.StackTrace}");
}
}
};
itemResizeDialog.ShowModal();
}
}
}
}
Loading

0 comments on commit b8279b2

Please sign in to comment.