|
| 1 | +namespace Loupedeck.GoXLRPlugin.Commands |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Drawing; |
| 6 | + using System.Linq; |
| 7 | + using System.Threading; |
| 8 | + |
| 9 | + using GoXLR.Server.Enums; |
| 10 | + using GoXLR.Server.Models; |
| 11 | + |
| 12 | + using Helpers; |
| 13 | + |
| 14 | + class SetRoutingCommand : PluginDynamicCommand |
| 15 | + { |
| 16 | + private readonly Dictionary<String, State> _states = new Dictionary<String, State>(); |
| 17 | + |
| 18 | + private GoXLRPlugin _plugin; |
| 19 | + |
| 20 | + public SetRoutingCommand() : base() |
| 21 | + { |
| 22 | + this.DisplayName = "Routing Toggle"; |
| 23 | + this.GroupName = ""; |
| 24 | + this.Description = "Select input and output to toggle."; |
| 25 | + |
| 26 | + this.MakeProfileAction("tree"); |
| 27 | + } |
| 28 | + |
| 29 | + protected override Boolean OnLoad() |
| 30 | + { |
| 31 | + this._plugin = (GoXLRPlugin)base.Plugin; |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + public void UpdateState(Routing routing, State state) |
| 36 | + { |
| 37 | + var actionParameter = $"{routing.Input}|{routing.Output}"; |
| 38 | + if (this._states.TryGetValue(actionParameter, out var oldState) && oldState == state) |
| 39 | + return; |
| 40 | + |
| 41 | + this._states[actionParameter] = state; |
| 42 | + |
| 43 | + this.ActionImageChanged(actionParameter); |
| 44 | + } |
| 45 | + |
| 46 | + protected override void RunCommand(String actionParameter) |
| 47 | + { |
| 48 | + if (!Routing.TryParseContext(actionParameter, out var routing)) |
| 49 | + return; |
| 50 | + |
| 51 | + var server = this._plugin.Server; |
| 52 | + server.SetRouting(RoutingAction.Toggle, routing, CancellationToken.None) |
| 53 | + .GetAwaiter() |
| 54 | + .GetResult(); |
| 55 | + } |
| 56 | + |
| 57 | + protected override PluginProfileActionData GetProfileActionData() |
| 58 | + { |
| 59 | + var tree = new PluginProfileActionTree("Routing Tree"); |
| 60 | + |
| 61 | + tree.AddLevel("Inputs"); |
| 62 | + tree.AddLevel("Outputs"); |
| 63 | + |
| 64 | + var routingTable = Routing.GetRoutingTable(); |
| 65 | + var inputs = routingTable |
| 66 | + .Select(routing => routing.Input) |
| 67 | + .Distinct(); |
| 68 | + |
| 69 | + foreach (var input in inputs) |
| 70 | + { |
| 71 | + var node = tree.Root.AddNode(input.ToString()); |
| 72 | + |
| 73 | + var outputs = routingTable |
| 74 | + .Where(routing => routing.Input == input) |
| 75 | + .Select(routing => routing.Output); |
| 76 | + |
| 77 | + foreach (var output in outputs) |
| 78 | + { |
| 79 | + var description = GoXLR.Server.Extensions.EnumExtensions.GetEnumDescription(output); |
| 80 | + |
| 81 | + node.AddItem($"{input}|{output}", output.ToString(), description); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return tree; |
| 86 | + } |
| 87 | + |
| 88 | + protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize) |
| 89 | + { |
| 90 | + using (var bitmapBuilder = new BitmapBuilder(imageSize)) |
| 91 | + { |
| 92 | + var background = ImageHelpers.GetImageBackground(imageSize, Color.Red); |
| 93 | + var bitmapImage = new BitmapImage(background); |
| 94 | + bitmapBuilder.SetBackgroundImage(bitmapImage); |
| 95 | + |
| 96 | + //base: Routing Toggle |
| 97 | + //this: Input|Output\r\n- -> Input|Output\r\nOn |
| 98 | + var text = this.GetCommandDisplayName(actionParameter, imageSize); |
| 99 | + bitmapBuilder.DrawText(text); |
| 100 | + |
| 101 | + return bitmapBuilder.ToImage(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + protected override String GetCommandDisplayName(String actionParameter, PluginImageSize imageSize) |
| 106 | + { |
| 107 | + //Does not work... |
| 108 | + if (actionParameter is null) |
| 109 | + return string.Empty; |
| 110 | + |
| 111 | + var text = actionParameter; |
| 112 | + if (actionParameter.IndexOf('|') != -1) |
| 113 | + { |
| 114 | + var split = actionParameter.Split('|'); |
| 115 | + text = $"{split[0]}\r\n{split[1]}"; |
| 116 | + } |
| 117 | + |
| 118 | + return this._states.TryGetValue(actionParameter, out var state) |
| 119 | + ? $"{text} \r\n {state}" |
| 120 | + : $"{text} \r\n -"; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments