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

Open
wants to merge 16 commits into
base: Avalonia
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions src/SerialLoops.Lib/Factories/ConfigFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text.Json;
using DynamicData.Binding;
using HaruhiChokuretsuLib.Util;

namespace SerialLoops.Lib.Factories;
Expand Down
3 changes: 1 addition & 2 deletions src/SerialLoops.Lib/Items/ItemDescription.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using HaruhiChokuretsuLib.Archive.Event;
using ReactiveUI;
Expand Down
59 changes: 53 additions & 6 deletions src/SerialLoops.Lib/Items/ScriptItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Event;
Expand Down Expand Up @@ -39,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 @@ -47,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 @@ -57,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 @@ -210,7 +211,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 @@ -691,7 +692,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 @@ -791,6 +792,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 @@ -1007,6 +1030,30 @@ public static (SKBitmap PreviewImage, string ErrorImage) GeneratePreviewImage(Sc
canvas.DrawBitmap(topicFlyout, 256 - topicFlyout.Width, 320);
}

// 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 Expand Up @@ -1068,4 +1115,4 @@ public override void Refresh(Project project, ILogger log)
Graph.AddVertexRange(Event.ScriptSections);
CalculateGraphEdges(GetScriptCommandTree(project, log), log);
}
}
}
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,
}
}
}
9 changes: 3 additions & 6 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 Expand Up @@ -389,7 +386,7 @@ private static List<ScriptParameter> GetScriptParameters(ScriptCommandInvocation
}
break;
case CommandVerb.SCENE_GOTO:
case CommandVerb.SCENE_GOTO2:
case CommandVerb.SCENE_GOTO_CHESS:
if (i == 0)
{
parameters.Add(new ConditionalScriptParameter(localize("Scene"), eventFile.ConditionalsSection.Objects[parameter]));
Expand Down Expand Up @@ -750,4 +747,4 @@ public ScriptItemCommand Clone()
};
}

}
}
4 changes: 3 additions & 1 deletion src/SerialLoops.Lib/Script/ScriptPreview.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using HaruhiChokuretsuLib.Archive.Event;
using SerialLoops.Lib.Items;
using SerialLoops.Lib.Script.Parameters;

Expand All @@ -19,5 +20,6 @@ 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 string ErrorImage { get; set; }
}
}
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.2" />
<PackageReference Include="HarfBuzzSharp.NativeAssets.macOS" Version="7.3.0.2" />
<PackageReference Include="HaruhiChokuretsuLib" Version="0.43.0" />
<PackageReference Include="HaruhiChokuretsuLib" Version="0.43.1" />
<PackageReference Include="NAudio.Vorbis" Version="1.5.0" />
<PackageReference Include="NitroPacker.Core" Version="2.5.1" />
<PackageReference Include="NLayer" Version="1.16.0" />
Expand Down
6 changes: 3 additions & 3 deletions src/SerialLoops.Lib/Util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static string GetOriginalString(this string line, Project project)
public static void CollectGarbage(this EventFile evt)
{
// Collect conditional garbage
IEnumerable<string> conditionalContainingCommands = new[] { CommandVerb.VGOTO, CommandVerb.SCENE_GOTO, CommandVerb.SCENE_GOTO2 }.Select(c => c.ToString());
IEnumerable<string> conditionalContainingCommands = new[] { CommandVerb.VGOTO, CommandVerb.SCENE_GOTO, CommandVerb.SCENE_GOTO_CHESS }.Select(c => c.ToString());
List<UsedIndex> conditionalUsedIndices = [];
foreach (ScriptCommandInvocation conditionalCommand in evt.ScriptSections.SelectMany(s => s.Objects).Where(c => conditionalContainingCommands.Contains(c.Command.Mnemonic)))
{
Expand Down Expand Up @@ -123,7 +123,7 @@ public static void CollectGarbage(this EventFile evt)
if (dialogueUsedIndices.All(idx => idx.Index != i))
{
evt.DialogueSection.Objects.RemoveAt(i);
evt.DialogueLines.RemoveAt(i);
evt.DialogueLines.RemoveAt(i--);
for (int j = 0; j < dialogueUsedIndices.Count; j++)
{
if (dialogueUsedIndices[j].Index >= i)
Expand Down Expand Up @@ -294,4 +294,4 @@ public override SKColor Read(ref Utf8JsonReader reader, Type typeToConvert, Json
}

public override void Write(Utf8JsonWriter writer, SKColor value, JsonSerializerOptions options) => writer.WriteStringValue($"{value.Alpha:X2}{value.Red:X2}{value.Green:X2}{value.Blue:X2}");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using NAudio.Wave;

// Adapted from https://github.com/naudio/NAudio.WaveFormRenderer/blob/master/WaveFormRendererLib/MaxPeakProvider.cs
// Copyright (c) 2021 NAudio
Expand Down
6 changes: 3 additions & 3 deletions src/SerialLoops/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:mintoolbarthemes="using:MiniToolbar.Avalonia.Themes"
xmlns:dialoghost="using:DialogHostAvalonia"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
Expand All @@ -24,7 +24,7 @@
<NativeMenuItem Header="{x:Static assets:Strings.View__Logs}" Click="Logs_Click"/>
</NativeMenu>
</NativeMenu.Menu>

<Application.Styles>
<FluentTheme />
<dialoghost:DialogHostStyles />
Expand Down Expand Up @@ -70,5 +70,5 @@
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>

</Application>
Loading
Loading