Skip to content

Commit

Permalink
Improve event listener editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed Jul 22, 2023
1 parent 10d1de0 commit afdb7b5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
19 changes: 13 additions & 6 deletions src/Murder.Editor/CustomComponents/EventListenerEditorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override bool DrawAllMembersWithTable(ref object target)
// ======
if (animations is not null)
{
SelectAnimation(listener, animations);
SelectAnimation(animations);
}

// ======
Expand Down Expand Up @@ -140,25 +140,32 @@ protected override bool DrawAllMembersWithTable(ref object target)

private static int _lastAnimationSelected = 0;

private void SelectAnimation(EventListenerEditorComponent listener, string[] animations)
private void SelectAnimation(string[] animations)
{
ImGui.SameLine();
ImGui.Text("\uf03d");

ImGui.SameLine();
if (_lastAnimationSelected >= animations.Length)
if (_lastAnimationSelected >= animations.Length)
{
_lastAnimationSelected = 0;
}

ImGui.Combo("##select_animation", ref _lastAnimationSelected, animations, animations.Length);
ImGui.SameLine();

if (ImGuiHelpers.IconButton('\uf04b', "##select_animation_for_event"))
{
StageHelpers.AddComponentsOnSelectedEntityForWorldOnly(
new AnimationOverloadComponent(animations[_lastAnimationSelected], loop: false, ignoreFacing: true),
listener.ToEventListener());
new AnimationOverloadComponent(animations[_lastAnimationSelected], loop: false, ignoreFacing: true));

StageHelpers.ToggleSystem(typeof(EventListenerComponent), true);
}

ImGui.SameLine();
if (ImGuiHelpers.IconButton('\uf04c', "##unselect_animation_for_event"))
{
StageHelpers.ToggleSystem(typeof(EventListenerComponent), false);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Murder.Editor/CustomEditors/AssetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,5 +838,12 @@ public void AddComponentsToWorldOnly(IEntity entityInstance, IComponent[] compon
Stages[_asset.Guid].AddComponentForInstance(entityInstance.Guid, c);
}
}

public void ToggleSystem(Type t, bool enable)
{
GameLogger.Verify(_asset is not null && Stages.ContainsKey(_asset.Guid));

Stages[_asset.Guid].ToggleSystem(t, enable);
}
}
}
2 changes: 1 addition & 1 deletion src/Murder.Editor/CustomEditors/CharacterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void InitializeStage(Stage stage, ScriptInformation info)

// Activate dialog system here:
stage.ActivateSystemsWith(enable: true, typeof(DialogueEditorAttribute));
stage.DeactivateSystem(typeof(EntitiesSelectorSystem));
stage.ToggleSystem(typeof(EntitiesSelectorSystem), false);

if (_script.Situations.Length != 0)
{
Expand Down
11 changes: 9 additions & 2 deletions src/Murder.Editor/Stage/Stage_Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ public IList<IEntity> FindEntitiesWith(params Type[] components)
private readonly Dictionary<Type, ImmutableArray<Type>> _attributeToSystems = new();
private readonly Dictionary<Type, bool?> _activeAttributeToSystems = new();

internal void DeactivateSystem(Type system)
internal void ToggleSystem(Type system, bool enable)
{
_world.DeactivateSystem(system);
if (enable)
{
_world.ActivateSystem(system);
}
else
{
_world.DeactivateSystem(system);
}
}

internal bool ActivateSystemsWith(bool enable, Type attribute)
Expand Down
17 changes: 16 additions & 1 deletion src/Murder.Editor/Utilities/StageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ internal static class StageHelpers
continue;
}

DefaultEditorSystemAttribute attribute = (DefaultEditorSystemAttribute)t.GetCustomAttribute(typeof(DefaultEditorSystemAttribute))!;
bool isActive = attribute.StartActive;

if (Activator.CreateInstance(t) is ISystem system)
{
systems.Add((system, true));
systems.Add((system, isActive));
systemsAdded.Add(t);
}
else
Expand Down Expand Up @@ -290,6 +293,18 @@ public static Type[] FetchComponentsWithAttribute<T>(bool cache = true) where T
return null;
}

public static bool ToggleSystem(Type t, bool enable)
{
if (Architect.Instance.ActiveScene is not EditorScene editor || editor.EditorShown is not AssetEditor assetEditor)
{
return false;
}

assetEditor.ToggleSystem(t, enable);

return true;
}

public static bool AddComponentsOnSelectedEntityForWorldOnly(params IComponent[] components)
{
if (Architect.Instance.ActiveScene is not EditorScene editor || editor.EditorShown is not AssetEditor assetEditor)
Expand Down
2 changes: 1 addition & 1 deletion src/Murder/Systems/Effects/EventListenerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Murder.Systems.Effects
{
[DefaultEditorSystem]
[DefaultEditorSystem(startActive: false)]
[Filter(typeof(EventListenerComponent))]
[Messager(typeof(AnimationEventMessage))]
internal class EventListenerSystem : IMessagerSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ namespace Murder.Attributes
/// </summary>
public class DefaultEditorSystemAttribute : Attribute
{
public readonly bool StartActive = true;

public DefaultEditorSystemAttribute() { }

public DefaultEditorSystemAttribute(bool startActive) => StartActive = startActive;
}
}

0 comments on commit afdb7b5

Please sign in to comment.