Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more commands to the script editor on Avalonia #421

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/SerialLoops.Lib/Items/ChessPuzzleItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HaruhiChokuretsuLib.Archive;
using HaruhiChokuretsuLib.Archive;
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Graphics;
using HaruhiChokuretsuLib.Util;
Expand Down
56 changes: 52 additions & 4 deletions src/SerialLoops.Lib/Items/ScriptItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ScriptItem(EventFile evt, EventTable evtTbl, Func<string, string> localiz

public Dictionary<ScriptSection, List<ScriptItemCommand>> GetScriptCommandTree(Project project, ILogger log)
{
ScriptCommandInvocation currentCommand = null;
try
{
Dictionary<ScriptSection, List<ScriptItemCommand>> commands = [];
Expand All @@ -46,6 +47,7 @@ public Dictionary<ScriptSection, List<ScriptItemCommand>> GetScriptCommandTree(P
commands.Add(section, []);
foreach (ScriptCommandInvocation command in section.Objects)
{
currentCommand = command;
commands[section].Add(ScriptItemCommand.FromInvocation(command, section,
commands[section].Count, Event, project, _localize, log));
}
Expand All @@ -56,8 +58,8 @@ public Dictionary<ScriptSection, List<ScriptItemCommand>> GetScriptCommandTree(P
catch (Exception ex)
{
log.LogException(
string.Format(project.Localize("Error getting script command tree for script {0} ({1})"),
DisplayName, Name), ex);
string.Format(project.Localize("Error getting script command tree for script {0} ({1}): {2} {3}"),
DisplayName, Name, currentCommand?.Command.Mnemonic ?? "NULL_COMMAND", string.Join(", ", currentCommand?.Parameters ?? [])), ex);
return null;
}
}
Expand Down Expand Up @@ -207,7 +209,7 @@ public ScriptPreview GetScriptPreview(Dictionary<ScriptSection, List<ScriptItemC
if (commands is null)
{
log.LogWarning($"No path found to current command.");
preview.ErrorImage = "SerialLoops.Graphics.ScriptPreviewError.png";
preview.ErrorImage = "avares://SerialLoops/Assets/Graphics/ScriptPreviewError.png";
return preview;
}

Expand Down Expand Up @@ -702,7 +704,7 @@ public ScriptPreview GetScriptPreview(Dictionary<ScriptSection, List<ScriptItemC
};
}

if (spriteShakeParam.ShakeEffect != SpriteShakeScriptParameter.SpriteShakeEffect.NONE &&
if (spriteShakeParam.ShakeEffect != SpriteShakeScriptParameter.SpriteShakeEffect.NO_SHAKE &&
sprites.ContainsKey(character))
{
switch (spriteShakeParam.ShakeEffect)
Expand Down Expand Up @@ -796,6 +798,28 @@ public ScriptPreview GetScriptPreview(Dictionary<ScriptSection, List<ScriptItemC
((TopicScriptParameter)currentCommand.Parameters[0]).TopicId);
}

// Draw SELECT choices
if (currentCommand.Verb == CommandVerb.SELECT)
{
preview.CurrentChocies = [];
if (((OptionScriptParameter)currentCommand.Parameters[0]).Option.Id > 0)
{
preview.CurrentChocies.Add(((OptionScriptParameter)currentCommand.Parameters[0]).Option.Text);
}
if (((OptionScriptParameter)currentCommand.Parameters[1]).Option.Id > 0)
{
preview.CurrentChocies.Add(((OptionScriptParameter)currentCommand.Parameters[1]).Option.Text);
}
if (((OptionScriptParameter)currentCommand.Parameters[2]).Option.Id > 0)
{
preview.CurrentChocies.Add(((OptionScriptParameter)currentCommand.Parameters[2]).Option.Text);
}
if (((OptionScriptParameter)currentCommand.Parameters[3]).Option.Id > 0)
{
preview.CurrentChocies.Add(((OptionScriptParameter)currentCommand.Parameters[3]).Option.Text);
}
}

return preview;
}

Expand Down Expand Up @@ -1021,6 +1045,30 @@ public static (SKBitmap PreviewImage, string ErrorImage) GeneratePreviewImage(Sc
canvas.DrawBitmap(topicFlyout, 256 - topicFlyout.Width, verticalOffset + 128);
}

// Draw select choices
if (preview.CurrentChocies?.Count > 0)
{
List<SKBitmap> choiceGraphics = [];
foreach (string choice in preview.CurrentChocies)
{
SKBitmap choiceGraphic = new(218, 18);
SKCanvas choiceCanvas = new(choiceGraphic);
choiceCanvas.DrawRect(1, 1, 216, 16, new() { Color = new(146, 146, 146) });
choiceCanvas.DrawRect(2, 2, 214, 14, new() { Color = new(69, 69, 69) });
int choiceWidth = project.LangCode.Equals("ja") ? choice.Length * 14 : choice.Sum(c => project.FontReplacement.ReverseLookup(c).Offset);
choiceCanvas.DrawHaroohieText(choice, DialogueScriptParameter.Paint00, project, (218 - choiceWidth) / 2, 2);
choiceCanvas.Flush();
choiceGraphics.Add(choiceGraphic);
}

int graphicY = (192 - (choiceGraphics.Count * 18 + (choiceGraphics.Count - 1) * 8)) / 2 + 184;
foreach (SKBitmap choiceGraphic in choiceGraphics)
{
canvas.DrawBitmap(choiceGraphic, 19, graphicY);
graphicY += 26;
}
}

canvas.Flush();
return (previewBitmap, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public BgmModeScriptParameter(string name, short mode) : base(name, ParameterTyp

public enum BgmMode : short
{
START = 2,
STOP = 4,
Start = 2,
Stop = 4,
}

public override BgmModeScriptParameter Clone(Project project, EventFile eventFile)
{
return new(Name, (short)Mode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public override ColorMonochromeScriptParameter Clone(Project project, EventFile

public enum ColorMonochrome : short
{
CUSTOM = 0,
CUSTOM_COLOR = 0,
BLACK = 1,
WHITE = 2,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override PaletteEffectScriptParameter Clone(Project project, EventFile ev

public enum PaletteEffect : short
{
DEFAULT = 216,
DEFAULT_PALETTE = 216,
INVERTED = 217,
GRAYSCALE = 218,
SEPIA = 219,
Expand Down Expand Up @@ -89,4 +89,4 @@ public static SKPaint GetPaletteEffectPaint(PaletteEffect effect)
0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
]),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public override SpriteShakeScriptParameter Clone(Project project, EventFile even

public enum SpriteShakeEffect : short
{
NONE = 0,
NO_SHAKE = 0,
SHAKE_CENTER = 1,
BOUNCE_HORIZONTAL_CENTER = 2,
BOUNCE_HORIZONTAL_CENTER_WITH_SMALL_SHAKES = 3,
SHAKE_RIGHT = 4,
SHAKE_LEFT = 5,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public override TextEntranceEffectScriptParameter Clone(Project project, EventFi

public enum TextEntranceEffect : short
{
NORMAL = 0,
NORMAL_TEXT_ENTRANCE = 0,
SHRINK_IN = 1,
TERMINAL_TYPING = 2,
}
}
}
5 changes: 1 addition & 4 deletions src/SerialLoops.Lib/Script/ScriptItemCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ public List<ScriptItemCommand> WalkCommandGraph(Dictionary<ScriptSection, List<S
{
List<ScriptItemCommand> commands = [];

Func<ScriptSectionEdge, double> weightFunction = new((ScriptSectionEdge edge) =>
{
return 1;
});
Func<ScriptSectionEdge, double> weightFunction = new((ScriptSectionEdge edge) => 1);

if (Section != commandTree.Keys.First())
{
Expand Down
1 change: 1 addition & 0 deletions src/SerialLoops.Lib/Script/ScriptPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ScriptPreview
public List<PositionedSprite> Sprites { get; set; } = [];
public ScriptItemCommand LastDialogueCommand { get; set; }
public TopicItem Topic { get; set; }
public List<string> CurrentChocies { get; set; }
public bool ChessMode { get; set; }
public ChessPuzzleItem ChessPuzzle { get; set; }
public string ErrorImage { get; set; }
Expand Down
29 changes: 29 additions & 0 deletions src/SerialLoops.Lib/Util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ public static void CollectGarbage(this EventFile evt)
}
}
}

// Collect dialogue garbage
IEnumerable<string> dialogueContainingCommands = new[] { CommandVerb.DIALOGUE, CommandVerb.PIN_MNL }.Select(c => c.ToString());
List<UsedIndex> dialogueUsedIndices = [];
foreach (ScriptCommandInvocation dialogueCommand in evt.ScriptSections.SelectMany(s => s.Objects)
.Where((c => dialogueContainingCommands.Contains(c.Command.Mnemonic))))
{
dialogueUsedIndices.Add(new() { Command = dialogueCommand, Index = dialogueCommand.Parameters[0] });
}

if (dialogueUsedIndices.DistinctBy(i => i.Index).Count() < evt.DialogueSection.Objects.Count)
{
for (short i = 0; i < evt.DialogueSection.Objects.Count; i++)
{
if (dialogueUsedIndices.All(idx => idx.Index != i))
{
evt.DialogueSection.Objects.RemoveAt(i);
evt.DialogueLines.RemoveAt(i--);
for (int j = 0; j < dialogueUsedIndices.Count; j++)
{
if (dialogueUsedIndices[j].Index >= i)
{
dialogueUsedIndices[j].Command.Parameters[0]--;
dialogueUsedIndices[j].Index--;
}
}
}
}
}
}

private class UsedIndex
Expand Down
Loading