diff --git a/src/BP-CoreLib/BP-CoreLib.csproj b/src/BP-CoreLib/BP-CoreLib.csproj index 9b19489..7e13d8a 100644 --- a/src/BP-CoreLib/BP-CoreLib.csproj +++ b/src/BP-CoreLib/BP-CoreLib.csproj @@ -227,7 +227,9 @@ + + diff --git a/src/BP-CoreLib/Interfaces/IActionLabel.cs b/src/BP-CoreLib/Interfaces/IActionLabel.cs new file mode 100644 index 0000000..f6b58d9 --- /dev/null +++ b/src/BP-CoreLib/Interfaces/IActionLabel.cs @@ -0,0 +1,12 @@ +using BrokeProtocol.Entities; +using System; +using System.Collections.Generic; + +namespace BPCoreLib.Interfaces +{ + public interface IActionLabel + { + string Name { get; } + Action Action { get; } + } +} \ No newline at end of file diff --git a/src/BP-CoreLib/OptionMenu/ActionLabel.cs b/src/BP-CoreLib/OptionMenu/ActionLabel.cs new file mode 100644 index 0000000..f2e2850 --- /dev/null +++ b/src/BP-CoreLib/OptionMenu/ActionLabel.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BPCoreLib.Interfaces; +using BrokeProtocol.API; +using BrokeProtocol.Collections; +using BrokeProtocol.Entities; +using BrokeProtocol.Required; + +namespace BPCoreLib.OptionMenu +{ + public class ActionLabel : IActionLabel + { + public string Name { get; } + public Action Action { get; } + + public ActionLabel(string name, Action action) + { + Name = name; + Action = action; + } + } + + internal class OptionMenu + { + public static Dictionary Menus = new Dictionary(); + + private readonly string _title; + public Dictionary Actions { get; private set; } + private readonly LabelID[] _options; + private readonly string _menuId; + + public OptionMenu(string title, IEnumerable labels, IEnumerable actions) + { + _title = title; + _options = labels.ToArray(); + _menuId = Guid.NewGuid().ToString(); + Menus.Add(_menuId, this); + Actions = new Dictionary(); + foreach (var action in actions) + { + Actions.Add(Guid.NewGuid().ToString(), action); + } + } + + internal void SendToPlayer(ShPlayer player) + { + var actions = new LabelID[Actions.Count]; + for (var i = 0; i < actions.Length; i++) + { + var action = Actions.ElementAt(i); + actions[i++] = new LabelID(action.Value.Name, action.Key); + } + player.svPlayer.SendOptionMenu(_title, player.ID, _menuId, _options, actions); + } + } + + public static class Extension + { + public static void SendOptionMenu(this ShPlayer player, string title, IEnumerable labels, IEnumerable actions) + { + (new OptionMenu(title, labels, actions)).SendToPlayer(player); + } + } + + public class OnAction : IScript + { + [Target(GameSourceEvent.PlayerOptionAction, ExecutionMode.Event)] + public void OnOptionAction(ShPlayer player, int targetID, string menuID, string optionID, string actionID) + { + if (!OptionMenu.Menus.ContainsKey(menuID)) + { + return; + } + + var menu = OptionMenu.Menus[menuID]; + + menu.Actions[actionID].Action.Invoke(player, optionID); + } + } +} \ No newline at end of file