Skip to content

Commit

Permalink
Fixup systex replacement (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonko0493 authored Jul 18, 2024
1 parent a36dbcb commit cd468e8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/Defaults/DefaultNames.en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@
"SYSTEX_SYS_CMN_B17": "SYSTEX_DIALOGUE_ARROW",
"SYSTEX_SYS_CMN_B18": "SYSTEX_INFORMATION",
"SYSTEX_SYS_CMN_B22": "SYSTEX_CHESS_STUFF",
"SYSTEX_SYS_CMN_B24": "SYSTEX_PUZZLE_TEXT",
"SYSTEX_SYS_CMN_B24": "SYSTEX_COMMON_TEXT",
"SYSTEX_SYS_CMN_B25": "SYSTEX_TOPIC_BROWSER",
"SYSTEX_SYS_CMN_B27": "SYSTEX_FORWARD",
"SYSTEX_SYS_CMN_B30": "SYSTEX_COMPANION_SELECT",
Expand Down
27 changes: 23 additions & 4 deletions src/SerialLoops.Lib/Items/SystemTextureItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib.Util;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand All @@ -15,6 +17,9 @@ public class SystemTextureItem : Item
public int Width { get; set; }
public int Height { get; set; }

private const string COMMON_PALETTE = "#ff00f800,#fff00080,#ff80f000,#ff00f080,#ff00f800,#ff00f800,#ff00f800,#ff482828,#ff684038,#ff905850,#ffb88070,#ffc8a080,#ffd8b090,#ffe8c8a0,#fff8d8b0,#fff8e8c0,#fff00000,#fff02800,#fff04000,#fff05000,#fff06000,#fff07000,#fff08000,#fff09000,#fff0a000,#fff0b000,#fff0c000,#fff0d000,#fff0e000,#fff0f000,#ffe0f000,#ffd0f000,#ffc0f000,#ffb0f000,#ffa0f000,#ff90f000,#ff80f000,#ff68f000,#ff50f000,#ff30f000,#ff00f000,#ff00f040,#ff00f068,#ff00f090,#ff00f0b0,#ff00f0d0,#ff00f0f0,#ff00e0f0,#ff00d0f0,#ff00c0f0,#ff00b0f0,#ff00a0f0,#ff0090f0,#ff0080f0,#ff0070f0,#ff0060f0,#ff0050f0,#ff0040f0,#ff0030f0,#ff0020f0,#ff0000f0,#ff2000f0,#ff3000f0,#ff4000f0,#ff5000f0,#ff6000f0,#ff7000f0,#ff8000f0,#ff9000f0,#ffa000f0,#ffb000f0,#ffc000f0,#ffd000f0,#ffe000f0,#fff000f0,#fff000d0,#fff000b0,#fff00090,#fff00068,#fff00040,#fff8f8f8,#fff0f0f0,#ffe8e8e8,#ffe0e0e0,#ffd8d8d8,#ffd0d0d0,#ffc8c8c8,#ffc0c0c0,#ffb8b8b8,#ffb0b0b0,#ffa8a8a8,#ffa0a0a0,#ff989898,#ff909090,#ff888888,#ff808080,#ff787878,#ff707070,#ff686868,#ff606060,#ff585858,#ff505050,#ff484848,#ff404040,#ff383838,#ff303030,#ff282828,#ff202020,#ff181818,#ff101010,#ff080808,#ff000000,#ff281010,#ff502020,#ff783838,#ffa05050,#ffc86868,#fff08080,#fff8a0a0,#fff8c0c0,#fff8e0e0,#ff282810,#ff505020,#ff787838,#ffa0a050,#ffc8c868,#fff0f080,#fff8f8a0,#fff8f8c0,#fff8f8e0,#ff102810,#ff285028,#ff407840,#ff50a050,#ff68c868,#ff80f080,#ffa0f8a0,#ffc0f8c0,#ffe0f8e0,#ff102828,#ff205050,#ff387878,#ff50a0a0,#ff68c8c8,#ff80f0f0,#ffa0f8f8,#ffc0f8f8,#ffe0f8f8,#ff101028,#ff202050,#ff383878,#ff5050a0,#ff6868c8,#ff8080f0,#ffa0a0f8,#ffc0c0f8,#ffe0e0f8,#ff281028,#ff502050,#ff783878,#ffa050a0,#ffc868c8,#fff080f0,#fff8a0f8,#fff8c0f8,#fff8e0f8,#ff280010,#ff500020,#ff780038,#ffa00050,#ffc80068,#fff03898,#fff868b0,#fff898c8,#fff8c8e0,#ff281000,#ff502000,#ff783800,#ffa05000,#ffc86800,#fff09838,#fff8b068,#fff8c898,#fff8e0c8,#ff102800,#ff205000,#ff387800,#ff50a000,#ff68c800,#ff98f030,#ffb0f860,#ffc8f898,#ffe0f8c8,#ff002810,#ff005020,#ff007838,#ff00a050,#ff00c868,#ff30f098,#ff60f8b0,#ff98f8c8,#ffc8f8e0,#ff001028,#ff002050,#ff003878,#ff0050a0,#ff0068c8,#ff3098f0,#ff60b0f8,#ff98c8f8,#ffc8e0f8,#ff100028,#ff200050,#ff380078,#ff5000a0,#ff6800c8,#ff9830f0,#ffb060f8,#ffc898f8,#ffe0c8f8,#ff300000,#ff600000,#ff900000,#ffc00000,#fff03030,#fff86060,#ff303000,#ff606000,#ff909000,#ffc0c000,#fff0f030,#fff8f860,#ff003000,#ff006000,#ff009000,#ff00c000,#ff30f030,#ff60f860,#ff003030,#ff006060,#ff009090,#ff00c0c0,#ff30f0f0,#ff60f8f8,#ff000030,#ff000060,#ff000090,#ff0000c0,#ff3030f0,#ff6060f8,#ff300030,#ff600060,#ff900090,#ffc000c0,#fff030f0,#fff860f8";
private static SKColor TRANSPARENT = new(0, 248, 0);

public SystemTextureItem(SystemTexture sysTex, Project project, string name, int width = -1, int height = -1) : base(name, ItemType.System_Texture)
{
SysTex = sysTex;
Expand Down Expand Up @@ -63,15 +68,23 @@ public SKBitmap GetTexture()
}
}

public void SetTexture(SKBitmap bitmap, bool replacePalette)
public void SetTexture(SKBitmap bitmap, bool replacePalette, ILogger log)
{
if (replacePalette == false)
List<SKColor> replacedPalette = [];
if (replacePalette)
{
replacedPalette = Helpers.GetPaletteFromImage(bitmap, 255, log);
replacedPalette.Insert(0, SKColors.Transparent);
Grp.SetPalette(replacedPalette);
}
else if (Grp.Palette[0] == TRANSPARENT)
{
Grp.Palette[0] = SKColors.Transparent;
}

if (SysTex.Screen == SysTexScreen.BOTTOM_SCREEN)
{
Grp.SetImage(bitmap, replacePalette, transparentIndex: replacePalette ? 0 : -1);
Grp.SetImage(bitmap);
}
else
{
Expand All @@ -91,8 +104,9 @@ public void SetTexture(SKBitmap bitmap, bool replacePalette)
}
tileCanvas.Flush();

Grp.SetImage(tileBitmap, replacePalette, transparentIndex: replacePalette ? 0 : -1);
Grp.SetImage(tileBitmap);
}
Grp.Palette[0] = TRANSPARENT;
}

public void Write(Project project, ILogger log)
Expand All @@ -102,5 +116,10 @@ public void Write(Project project, ILogger log)
IO.WriteBinaryFile(Path.Combine("assets", "graphics", $"{Grp.Index:X3}.png"), grpStream.ToArray(), project, log);
IO.WriteStringFile(Path.Combine("assets", "graphics", $"{Grp.Index:X3}.gi"), Grp.GetGraphicInfoFile(), project, log);
}

public bool UsesCommonPalette()
{
return COMMON_PALETTE.Equals(string.Join(',', Grp.Palette.Select(c => c.ToString())), StringComparison.OrdinalIgnoreCase);
}
}
}
8 changes: 6 additions & 2 deletions src/SerialLoops/Editors/SystemTextureEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ public override Container GetEditorPanel()
Button replaceButton = new() { Text = Application.Instance.Localize(this, "Replace") };
replaceButton.Click += ReplaceButton_Click;

Button replaceWithPaletteButton = new() { Text = Application.Instance.Localize(this, "Replace with Palette") };
// If the common palette is used, we don't want to allow users to replace it
Button replaceWithPaletteButton = new() { Text = Application.Instance.Localize(this, "Replace with Palette"), Enabled = !_systemTexture.UsesCommonPalette() };
replaceWithPaletteButton.Click += ReplaceWithPaletteButton_Click;

Label usesCommonPaletteLabel = new() { Text = _systemTexture.UsesCommonPalette() ? Application.Instance.Localize(this, "This system texture uses a common palette, so palette replacement has been disabled") : string.Empty };

return new StackLayout
{
Orientation = Orientation.Vertical,
Spacing = 5,
Items =
{
new SKGuiImage(_systemTexture.GetTexture()),
usesCommonPaletteLabel,
new StackLayout
{
Orientation = Orientation.Horizontal,
Expand Down Expand Up @@ -97,7 +101,7 @@ private void ReplaceImage(bool replacePalette)
try
{
LoopyProgressTracker tracker = new(s => Application.Instance.Localize(null, s));
_ = new ProgressDialog(() => _systemTexture.SetTexture(systemTextureResizeDialog.FinalImage, replacePalette),
_ = new ProgressDialog(() => _systemTexture.SetTexture(systemTextureResizeDialog.FinalImage, replacePalette, _log),
() => Content = GetEditorPanel(), tracker, string.Format(Application.Instance.Localize(this, $"Replacing {0}..."), _systemTexture.DisplayName));
UpdateTabTitle(false);
}
Expand Down
10 changes: 10 additions & 0 deletions src/SerialLoops/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/SerialLoops/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,9 @@
<data name="The changes made will require Serial Loops to be restarted. Is that okay?" xml:space="preserve">
<value>The changes made will require Serial Loops to be restarted. Is that okay?</value>
</data>
<data name="This system texture uses a common palette, so palette replacement has been disabled" xml:space="preserve">
<value>This system texture uses a common palette, so palette replacement has been disabled</value>
</data>
<data name="Time (Frames)" xml:space="preserve">
<value>Time (Frames)</value>
</data>
Expand Down

0 comments on commit cd468e8

Please sign in to comment.