Skip to content

Commit

Permalink
More changes towards sprite editor...
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed Dec 17, 2023
1 parent 02146b1 commit 031b188
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 43 deletions.
20 changes: 13 additions & 7 deletions src/Murder.Editor/CustomComponents/EventListenerEditorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ protected override bool DrawAllMembersWithTable(ref object target, bool _)
ImmutableArray<SpriteEventInfo> events = listener.Events;

// ======
// Initialize events, if necessary.
// Initialize according to the sprite events.
// ======
if (listener.Events.Length == 0 && eventNames is not null && eventNames.Count > 0)
if (eventNames is not null && eventNames.Count > 0)
{
HashSet<string> trackedEvents = events.Select(e => e.Id).ToHashSet();

var builder = ImmutableArray.CreateBuilder<SpriteEventInfo>();
foreach (string e in eventNames)
{
if (trackedEvents.Contains(e))
{
continue;
}

builder.Add(new(e));
}

events = builder.ToImmutable();
fileChanged = true;
events = events.AddRange(builder.ToImmutable());
}

// ======
// Add new event.
// ======
Expand Down Expand Up @@ -90,9 +96,9 @@ protected override bool DrawAllMembersWithTable(ref object target, bool _)
flags: ImGuiTableFlags.NoBordersInBody,
(-1, ImGuiTableColumnFlags.WidthFixed), (-1, ImGuiTableColumnFlags.WidthFixed), (-1, ImGuiTableColumnFlags.WidthStretch), (-1, ImGuiTableColumnFlags.WidthFixed));

for (int i = 0; i < listener.Events.Length; ++i)
for (int i = 0; i < events.Length; ++i)
{
SpriteEventInfo info = listener.Events[i];
SpriteEventInfo info = events[i];

ImGui.TableNextRow();
ImGui.TableNextColumn();
Expand Down
95 changes: 59 additions & 36 deletions src/Murder.Editor/CustomEditors/SpriteEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ internal class SpriteEditor : CustomEditor

private float _viewportSize = 500;

private int? _targetFrameForPopup = null;
private string _message = string.Empty;

public override void OpenEditor(ImGuiRenderer imGuiRenderer, RenderContext renderContext, object target, bool overwrite)
{
_sprite = (SpriteAsset)target;
Expand Down Expand Up @@ -174,19 +177,11 @@ private bool AddTestSounds(SpriteInformation info)
{
GameLogger.Verify(_sprite is not null);

if (!info.SoundTests.TryGetValue(info.SelectedAnimation, out var sounds))
{
return false;
}

var builder = ImmutableDictionary.CreateBuilder<string, SpriteEventInfo>();

Animation animation = _sprite.Animations[info.SelectedAnimation];
foreach ((int frame, SoundEventId? sound) in sounds)
foreach ((string message, SoundEventId? sound) in info.SoundTests)
{
if (sound is null || sound.Value.IsGuidEmpty ||
!animation.Events.TryGetValue(frame, out string? message) ||
message is null)
if (sound is null || sound.Value.IsGuidEmpty)
{
continue;
}
Expand All @@ -210,7 +205,7 @@ private void SelectAnimation(SpriteInformation info, string animation)

info.Stage.AddOrReplaceComponentOnEntity(
info.HelperId,
new AnimationOverloadComponent(animation, loop: true, ignoreFacing: true, startTime: info.Hook.Time));
new AnimationOverloadComponent(animation, loop: true, ignoreFacing: true, startTime: 0));
}

private void DrawFirstColumn(SpriteInformation info)
Expand Down Expand Up @@ -254,8 +249,6 @@ private void DrawFirstColumn(SpriteInformation info)
}
}

private string _message = string.Empty;

private void DrawMessages(SpriteInformation info)
{
GameLogger.Verify(_sprite is not null);
Expand All @@ -264,7 +257,8 @@ private void DrawMessages(SpriteInformation info)
flags: ImGuiTableFlags.NoBordersInBody,
(-1, ImGuiTableColumnFlags.WidthFixed),
(-1, ImGuiTableColumnFlags.WidthFixed),
(-1, ImGuiTableColumnFlags.WidthStretch));
(-1, ImGuiTableColumnFlags.WidthStretch),
(53, ImGuiTableColumnFlags.WidthFixed));

Animation animation = _sprite.Animations[info.SelectedAnimation];
for (int i = 0; i < animation.FrameCount; ++i)
Expand All @@ -277,45 +271,49 @@ private void DrawMessages(SpriteInformation info)
ImGui.TableNextRow();
ImGui.TableNextColumn();

if (ImGuiHelpers.DeleteButton($"delete_event_listener_{i}"))
{
DeleteMessage(info.SelectedAnimation, frame: i);
}

ImGui.SameLine();

ImGuiHelpers.SelectedButton($"{i}");

ImGui.TableNextColumn();
ImGui.Text(message);

ImGui.TableNextColumn();

ImGui.PushID($"sound_test_{i}");

EditorMember? member = ReflectionHelper.TryGetFieldForEditor(typeof(SpriteInformation), nameof(SpriteInformation.SoundTests));
member = member?.CreateFrom(typeof(SoundEventId?), "SoundTest");
member = member?.CreateFrom(typeof(SoundEventId?), name: "SoundTest"); // Make a fake member so the editor is happy drawing this.

SoundEventId? sound = null;

if (!info.SoundTests.TryGetValue(info.SelectedAnimation, out var soundsForThisAnimation))
{
soundsForThisAnimation = new();
info.SoundTests[info.SelectedAnimation] = soundsForThisAnimation;
}
info.SoundTests.TryGetValue(message, out SoundEventId? sound);

soundsForThisAnimation.TryGetValue(i, out sound);
ImGui.PushID($"sound_test_{i}");

if (member is not null &&
CustomField.DrawValue(member, sound, out SoundEventId? result))
{
soundsForThisAnimation[i] = result;
info.SoundTests[message] = result;
AddTestSounds(info);
}

ImGui.PopID();

ImGuiHelpers.HelpTooltip("This value is just for testing, it must be set on the prefab that uses this sprite.");

ImGui.PopID();
ImGui.TableNextColumn();

if (ImGuiHelpers.IconButton('\uf303', $"rename_event_listener_{i}"))
{
_message = message;
ImGui.OpenPopup($"rename_message_to_frame_{i}");
}

DrawRenamePopup(info, i);

ImGui.SameLine();

if (ImGuiHelpers.DeleteButton($"delete_event_listener_{i}"))
{
DeleteMessage(info.SelectedAnimation, frame: i);
}

ImGui.SameLine();
}
}

Expand Down Expand Up @@ -444,7 +442,32 @@ private void DrawTimeline(SpriteInformation info)
ImGui.EndChild();
}

private int? _targetFrameForPopup = null;
private void DrawRenamePopup(SpriteInformation info, int frame)
{
if (ImGui.BeginPopup($"rename_message_to_frame_{frame}"))
{
ImGui.PushItemWidth(170);

ImGui.Text($"Name for this message:");
ImGui.InputText("##add_new_message_input", ref _message, 128);

if (string.IsNullOrEmpty(_message))
{
ImGuiHelpers.SelectedButton("Ok!");
}
else if (ImGui.Button("Ok!") || Game.Input.Pressed(MurderInputButtons.Submit))
{
AddMessage(info.SelectedAnimation, frame, _message);

_message = string.Empty;

ImGui.CloseCurrentPopup();
}

ImGui.PopItemWidth();
ImGui.EndPopup();
}
}

private void DrawAddMessageOnRightClick(SpriteInformation info, int frame, string? message)
{
Expand Down Expand Up @@ -574,7 +597,7 @@ protected record SpriteInformation(Stage Stage)
[Tooltip("This will create a sound to test in this editor. The actual sound must be added to the entity!")]
[Default("Add sound to test")]
[JsonIgnore] // This won't be serializable.
public Dictionary<string, Dictionary<int, SoundEventId?>> SoundTests = new();
public Dictionary<string, SoundEventId?> SoundTests = new();
}
}
}

0 comments on commit 031b188

Please sign in to comment.