-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMain.cs
192 lines (166 loc) · 6.34 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using SuchByte.MacroDeck.ActionButton;
using SuchByte.MacroDeck.Events;
using SuchByte.MacroDeck.Folders;
using SuchByte.MacroDeck.GUI;
using SuchByte.MacroDeck.GUI.CustomControls;
using SuchByte.MacroDeck.Logging;
using SuchByte.MacroDeck.Plugins;
using SuchByte.MacroDeck.Profiles;
using SuchByte.MacroDeck.Variables;
using SuchByte.TwitchPlugin.Actions;
using SuchByte.TwitchPlugin.Language;
using SuchByte.TwitchPlugin.Models;
using SuchByte.TwitchPlugin.Views;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SuchByte.TwitchPlugin
{
public static class PluginInstance {
public static Main Main { get; set; }
}
public class Main : MacroDeckPlugin
{
private ContentSelectorButton _statusButton = new();
private readonly ToolTip _statusToolTip = new();
private MainWindow _mainWindow;
private readonly ChatCommandEvent _chatCommandEvent = new();
public Main()
{
PluginInstance.Main ??= this;
}
public override bool CanConfigure => true;
public override void Enable()
{
PluginLanguageManager.Initialize();
Actions = new List<PluginAction>()
{
new SetTitleGameAction(),
new ClearChatAction(),
new PlayAdAction(),
new SendChatMessageAction(),
new SetFollowerChatAction(),
new SetSlowChatAction(),
new SetEmoteChatAction(),
new SetSubscriberChatAction(),
new StreamMarkerAction(),
new MakeClipAction()
};
EventManager.RegisterEvent(_chatCommandEvent);
MacroDeck.MacroDeck.OnMainWindowLoad += MacroDeck_OnMainWindowLoad;
TwitchHelper.ConnectionStateChanged += TwitchHelper_ConnectionStateChanged;
if (MacroDeck.MacroDeck.MainWindow != null && !MacroDeck.MacroDeck.MainWindow.IsDisposed)
{
MacroDeck_OnMainWindowLoad(MacroDeck.MacroDeck.MainWindow, EventArgs.Empty);
}
Task.Run(async () => await Connect());
}
private static async Task Connect()
{
var twitchAccount = CredentialsHelper.GetTwitchAccount();
if (twitchAccount != null)
{
await TwitchHelper.Connect(twitchAccount);
}
}
private void TwitchHelper_ConnectionStateChanged(object sender, EventArgs e)
{
UpdateStatusIcon();
}
private void MacroDeck_OnMainWindowLoad(object sender, EventArgs e)
{
_mainWindow = sender as MainWindow;
_statusButton = new ContentSelectorButton
{
BackgroundImageLayout = ImageLayout.Stretch,
};
_statusButton.Click += StatusButton_Click;
_mainWindow?.contentButtonPanel.Controls.Add(_statusButton);
UpdateStatusIcon();
}
private void UpdateStatusIcon()
{
if (_mainWindow != null && !_mainWindow.IsDisposed && _statusButton != null && !_statusButton.IsDisposed)
{
_mainWindow.Invoke(() =>
{
_statusButton.BackgroundImage = TwitchHelper.IsConnected ? Properties.Resources.Twitch_Connected : Properties.Resources.Twitch_Disconnected;
_statusToolTip.SetToolTip(_statusButton, "Twitch " + (TwitchHelper.IsConnected ? " Connected" : "Disconnected"));
});
}
}
private async void StatusButton_Click(object sender, EventArgs e)
{
if (CredentialsHelper.GetTwitchAccount() == null)
{
OpenConfigurator();
return;
}
if (TwitchHelper.IsConnected)
{
TwitchHelper.Disconnect();
} else
{
await Connect();
}
}
public override void OpenConfigurator()
{
using var pluginConfig = new PluginConfigView();
pluginConfig.ShowDialog();
}
public void CommandIssued(object sender, EventArgs e)
{
_chatCommandEvent.Trigger(sender);
}
private class ChatCommandEvent : IMacroDeckEvent
{
public string Name => PluginLanguageManager.PluginStrings.EventName;
public EventHandler<MacroDeckEventArgs> OnEvent { get; set; }
public List<string> ParameterSuggestions
{
get
{
var commands = new List<string>();
var variable = PluginConfiguration.GetValue(PluginInstance.Main, "commandsList");
if (!string.IsNullOrWhiteSpace(variable))
commands.AddRange(variable.Split(';'));
return commands;
}
set { }
}
public void Trigger(object sender)
{
if (OnEvent == null)
{
return;
}
try
{
foreach (var macroDeckProfile in ProfileManager.Profiles)
{
foreach (var folder in macroDeckProfile.Folders)
{
if (folder.ActionButtons == null)
{
continue;
}
foreach (var actionButton in folder.ActionButtons.FindAll(actionButton => actionButton.EventListeners != null && actionButton.EventListeners.Find(x => x.EventToListen != null && x.EventToListen.Equals(Name)) != null))
{
var macroDeckEventArgs = new MacroDeckEventArgs
{
ActionButton = actionButton,
Parameter = (string)sender,
};
OnEvent(this, macroDeckEventArgs);
}
}
}
}
catch { }
}
}
}
}