Files
TierMakerGodot/settings_popup.cs

76 lines
2.3 KiB
C#

using Godot;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
public partial class settings_popup : PopupPanel
{
[Signal]
public delegate void BeforeOkEventHandler();
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Transient = true;
Exclusive = true;
}
private readonly ConcurrentQueue<Action> ActionQueue = new();
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
// //perform 1 action every frame
// if (ActionQueue.TryDequeue(out Action a))
// {
// GD.Print("Found action");
// a?.Invoke();
// }
}
public void _on_cancel_button_pressed()
{
GD.Print("Cancel pressed");
Hide();
}
public void _on_ok_button_pressed()
{
GD.Print("OK pressed");
var tcw = GetNode<TwitchChatWatcher>("/root/TwitchChatWatcher");
string chName = GetNode<LineEdit>("%ChannelNameEdit").Text;
Task.Run(tcw.ConnectAsync).ContinueWith(t => tcw.Authenticate(null, null))
.ContinueWith(t => tcw.RequestTags())
.ContinueWith(t => tcw.JoinChannel(chName))
.ContinueWith(FinishConnection);
ProcessMode = ProcessModeEnum.Disabled;
}
private void FinishConnection(Task t)
{
GD.Print(t.Status);
if (t.IsCompletedSuccessfully)
{
CallDeferred(nameof(SuccessfulConnection));
}
else
{
GD.PrintErr(t.Exception);
ActionQueue.Enqueue(() => ProcessMode = ProcessModeEnum.Inherit);
}
}
private void SuccessfulConnection()
{
GD.Print("Running completion task");
var tcw = GetNode<TwitchChatWatcher>("/root/TwitchChatWatcher");
if (tcw.State != System.Net.WebSockets.WebSocketState.Open)
throw new Exception("Websocket closed");
var settings = GetNode<Settings>("/root/Settings");
settings.AllowModerators = GetNode<CheckBox>("%CheckBoxModerator").ButtonPressed;
settings.Command = GetNode<LineEdit>("%CommandEdit").Text;
settings.SetUserLists(GetNode<TextEdit>("%WhiteListEdit").Text.Split('\n',
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries),
GetNode<TextEdit>("%BlackListEdit").Text.Split('\n',
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
ProcessMode = ProcessModeEnum.Inherit;
Hide();
}
}