Skip to content

Commit

Permalink
Lua-powered buttons (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis committed Jan 2, 2021
1 parent 3c0619d commit 9f34518
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 29 deletions.
12 changes: 12 additions & 0 deletions Backend/Plugins/LuaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private void StartLua()
Lua.RegisterFunction("set_temp_state", Api, typeof(LuaApi).GetMethod("SetTempState", new[] { typeof(string), typeof(string), typeof(int) }));
Lua.RegisterFunction("get_state", Api, typeof(LuaApi).GetMethod("GetState", new[] { typeof(string) }));
Lua.RegisterFunction("event_to_json", Api, typeof(LuaApi).GetMethod("EventToJson", new[] { typeof(IEvent) }));
Lua.RegisterFunction("create_button", Api, typeof(LuaApi).GetMethod("CreateButton", new[] { typeof(string) }));
Lua.RegisterFunction("delete_button", Api, typeof(LuaApi).GetMethod("DeleteButton", new[] { typeof(string) }));

var ScriptPath = Path.GetDirectoryName(FilePath).Replace("\\", "\\\\");
Lua.DoString($"package.path = \"{ScriptPath}\\\\?.lua;\" .. package.path;");
Expand Down Expand Up @@ -195,6 +197,16 @@ public string EventToJson(IEvent @event)
return EventSerdeService.Serialize(@event);
}

public void CreateButton(string text)
{
EventBus.PublishEvent(EventFactory.CreateUICommandCreateButton(text));
}

public void DeleteButton(string text)
{
EventBus.PublishEvent(EventFactory.CreateUICommandDeleteButton(text));
}

private void HandleDelayedExecution(IDictionary<string, DelayedExecution> functions)
{
string? deleteKey = null;
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- PluginState to InternalPluginState
- CommandTwitchSendMessage to TwitchCommandSendMessage
- CommandWriteToConsole to UICommandWriteToConsole
- Buttons: Make simple buttons in UI using Lua (create_button("text")) and remove them again (delete_button("text"))

## [0.2.0](https://github.com/dennis/slipstream/releases/tag/v0.2.0) (2020-12-30)
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.1.0...v0.2.0)
Expand Down
66 changes: 41 additions & 25 deletions Frontend/MainWindow.Designer.cs

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

46 changes: 42 additions & 4 deletions Frontend/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using Slipstream.Shared;
using Slipstream.Shared.Events.UI;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -18,6 +19,7 @@ public partial class MainWindow : Form
private IEventBusSubscription? EventBusSubscription;
private readonly BlockingCollection<string> PendingMessages = new BlockingCollection<string>();
private readonly IDictionary<string, ToolStripMenuItem> MenuPluginItems = new Dictionary<string, ToolStripMenuItem>();
private readonly IDictionary<string, Button> LuaButtons = new Dictionary<string, Button>();
private readonly ApplicationConfiguration ApplicationConfiguration;
private readonly string CleanTitle;

Expand Down Expand Up @@ -84,10 +86,9 @@ private void EventListenerMain()
Debug.Assert(EventBusSubscription != null);

EventHandler.OnInternalPluginState += (s, e) => EventHandler_OnInternalPluginState(e.Event);
EventHandler.OnUICommandWriteToConsole += (s, e) =>
{
PendingMessages.Add($"{DateTime.Now:s} {e.Event.Message}");
};
EventHandler.OnUICommandWriteToConsole += (s, e) => PendingMessages.Add($"{DateTime.Now:s} {e.Event.Message}");
EventHandler.OnUICommandCreateButton += (s, e) => EventHandler_OnUICommandCreateButton(e.Event);
EventHandler.OnUICommandDeleteButton += (s, e) => EventHandler_OnUICommandDeleteButton(e.Event);

// Request full state of all known plugins, so we get any that might be started before "us"
EventBus.PublishEvent(EventFactory.CreateInternalCommandPluginStates());
Expand All @@ -98,6 +99,43 @@ private void EventListenerMain()
}
}

private void EventHandler_OnUICommandCreateButton(UICommandCreateButton @event)
{
ExecuteSecure(() =>
{
if (LuaButtons.ContainsKey(@event.Text))
return;
var b = new Button
{
Text = @event.Text
};
b.Click += (s, e) =>
{
var b = s as Button;
if(b != null)
EventBus.PublishEvent(EventFactory.CreateUIButtonTriggered(b.Text));
};
LuaButtons.Add(@event.Text, b);
ButtonFlowLayoutPanel.Controls.Add(b);
});
}

private void EventHandler_OnUICommandDeleteButton(UICommandDeleteButton @event)
{
ExecuteSecure(() =>
{
if (!LuaButtons.ContainsKey(@event.Text))
return;
ButtonFlowLayoutPanel.Controls.Remove(LuaButtons[@event.Text]);
LuaButtons.Remove(@event.Text);
});
}

private void EventHandler_OnInternalPluginState(Shared.Events.Internal.InternalPluginState e)
{
if (e.PluginStatus != "Unregistered" && !MenuPluginItems.ContainsKey(e.Id))
Expand Down
24 changes: 24 additions & 0 deletions Shared/EventFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,29 @@ public UICommandWriteToConsole CreateUICommandWriteToConsole(string message)
Message = message
};
}

public UICommandCreateButton CreateUICommandCreateButton(string text)
{
return new UICommandCreateButton
{
Text = text,
};
}

public UICommandDeleteButton CreateUICommandDeleteButton(string text)
{
return new UICommandDeleteButton
{
Text = text,
};
}

public UIButtonTriggered CreateUIButtonTriggered(string text)
{
return new UIButtonTriggered
{
Text = text
};
}
}
}
30 changes: 30 additions & 0 deletions Shared/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public EventHandlerArgs(T e)
#region Events: UI
public delegate void OnUICommandWriteToConsoleHandler(EventHandler source, EventHandlerArgs<Shared.Events.UI.UICommandWriteToConsole> e);
public event OnUICommandWriteToConsoleHandler? OnUICommandWriteToConsole;

public delegate void OnUICommandCreateButtonHandler(EventHandler source, EventHandlerArgs<Shared.Events.UI.UICommandCreateButton> e);
public event OnUICommandCreateButtonHandler? OnUICommandCreateButton;

public delegate void OnUICommandDeleteButtonHandler(EventHandler source, EventHandlerArgs<Shared.Events.UI.UICommandDeleteButton> e);
public event OnUICommandDeleteButtonHandler? OnUICommandDeleteButton;

public delegate void OnUIButtonTriggeredHandler(EventHandler source, EventHandlerArgs<Shared.Events.UI.UIButtonTriggered> e);
public event OnUIButtonTriggeredHandler? OnUIButtonTriggered;
#endregion

#region Events: Audio
Expand Down Expand Up @@ -257,6 +266,27 @@ public void HandleEvent(IEvent? ev)
OnUICommandWriteToConsole.Invoke(this, new EventHandlerArgs<Shared.Events.UI.UICommandWriteToConsole>(tev));
break;

case Shared.Events.UI.UICommandCreateButton tev:
if (OnUICommandCreateButton == null)
OnDefault?.Invoke(this, new EventHandlerArgs<IEvent>(tev));
else
OnUICommandCreateButton.Invoke(this, new EventHandlerArgs<Shared.Events.UI.UICommandCreateButton>(tev));
break;

case Shared.Events.UI.UICommandDeleteButton tev:
if (OnUICommandDeleteButton == null)
OnDefault?.Invoke(this, new EventHandlerArgs<IEvent>(tev));
else
OnUICommandDeleteButton.Invoke(this, new EventHandlerArgs<Shared.Events.UI.UICommandDeleteButton>(tev));
break;

case Shared.Events.UI.UIButtonTriggered tev:
if (OnUIButtonTriggered == null)
OnDefault?.Invoke(this, new EventHandlerArgs<IEvent>(tev));
else
OnUIButtonTriggered.Invoke(this, new EventHandlerArgs<Shared.Events.UI.UIButtonTriggered>(tev));
break;

// Setting

case Shared.Events.Setting.FileMonitorSettings tev:
Expand Down
9 changes: 9 additions & 0 deletions Shared/Events/UI/UIButtonTriggered.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Slipstream.Shared.Events.UI
{
public class UIButtonTriggered : IEvent
{
public string EventType => "UIButtonTriggered";
public bool ExcludeFromTxrx => false;
public string Text { get; set; } = "INVALID-NAME";
}
}
9 changes: 9 additions & 0 deletions Shared/Events/UI/UICommandCreateButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Slipstream.Shared.Events.UI
{
public class UICommandCreateButton : IEvent
{
public string EventType => "UICommandCreateButton";
public bool ExcludeFromTxrx => true;
public string Text { get; set; } = "";
}
}
9 changes: 9 additions & 0 deletions Shared/Events/UI/UICommandDeleteButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Slipstream.Shared.Events.UI
{
public class UICommandDeleteButton : IEvent
{
public string EventType => "UICommandDeleteButton";
public bool ExcludeFromTxrx => true;
public string Text { get; set; } = "";
}
}
3 changes: 3 additions & 0 deletions Shared/IEventFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,8 @@ string fogLevel
TwitchReceivedCommand CreateTwitchReceivedCommand(string from, string message, bool moderator, bool subscriber, bool vip, bool broadcaster);

UICommandWriteToConsole CreateUICommandWriteToConsole(string message);
UICommandCreateButton CreateUICommandCreateButton(string text);
UICommandDeleteButton CreateUICommandDeleteButton(string text);
UIButtonTriggered CreateUIButtonTriggered(string text);
}
}
3 changes: 3 additions & 0 deletions Slipstream.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@
<Compile Include="Shared\Events\Internal\InternalInitialized.cs" />
<Compile Include="Shared\Events\Internal\InternalCommandPluginStates.cs" />
<Compile Include="Shared\Events\Setting\TxrxSettings.cs" />
<Compile Include="Shared\Events\UI\UIButtonTriggered.cs" />
<Compile Include="Shared\Events\UI\UICommandDeleteButton.cs" />
<Compile Include="Shared\Events\UI\UICommandCreateButton.cs" />
<Compile Include="Shared\IApplicationConfiguration.cs" />
<Compile Include="Shared\ApplicationVersionService.cs" />
<Compile Include="Shared\Events\IRacing\IRacingPitstopReport.cs" />
Expand Down

0 comments on commit 9f34518

Please sign in to comment.