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 : PanelContainer { private defer_manager _Deferer = null; private defer_manager Deferer { get { _Deferer ??= GetNode("/root/DeferManager"); return _Deferer; } } public string Channel => GetNode("%ChannelNameEdit").Text; [Signal] public delegate void BeforeOkEventHandler(); [Signal] public delegate void ImportSelectedEventHandler(string filename); [Signal] public delegate void ExportSelectedEventHandler(string filename, ExportSettings settings); // Called when the node enters the scene tree for the first time. public override void _Ready() { DisplayServer.WindowSetMinSize( DisplayServer.WindowGetMinSize().Union(Size)); GetNode("/root/TwitchChatWatcher") .Connect(TwitchChatWatcher.SignalName.SocketConnected, new Callable(this, nameof(SocketConnected)), (uint)ConnectFlags.Deferred); GetNode("/root/TwitchChatWatcher") .Connect(TwitchChatWatcher.SignalName.SocketDisconnected, new Callable(this, nameof(SocketDisconnected)), (uint)ConnectFlags.Deferred); } // 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 ShowPopup() { this.GetParentOfType().MenuOpenDisableInteraction(); Show(); } public void ClosePopup() { Hide(); this.GetParentOfType().MenuClosedEnableInteraction(); } public void _on_cancel_button_pressed() { ClosePopup(); } public void _on_ok_button_pressed() { GetNode("%ConfigStretchContainer").Apply(); ClosePopup(); } public void _on_connect_button_pressed() { var tcw = GetNode("/root/TwitchChatWatcher"); //string chName = GetNode("%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; } public void _on_join_channel_button_pressed() { var channel = Channel; if (string.IsNullOrWhiteSpace(channel)) return; var tcw = GetNode("/root/TwitchChatWatcher"); _ = Task.Run(() => tcw.JoinChannel(channel)); } private void FinishConnection(Task t) { if (t.IsCompletedSuccessfully) { CallDeferred(nameof(SuccessfulConnection)); } else { GD.PrintErr(t.Exception); CallDeferred(nameof(UnlockPopup)); } } private void UnlockPopup() { ProcessMode = ProcessModeEnum.Inherit; } private void SuccessfulConnection() { var tcw = GetNode("/root/TwitchChatWatcher"); if (tcw.State != System.Net.WebSockets.WebSocketState.Open) throw new Exception("Websocket closed"); var settings = GetNode("/root/Settings"); settings.AllowModerators = GetNode("%CheckBoxModerator").ButtonPressed; settings.Command = GetNode("%CommandEdit").Text; settings.SetUserLists(GetNode("%WhiteListEdit").Text.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries), GetNode("%BlackListEdit").Text.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)); UnlockPopup(); } private void SocketConnected() { GetNode("%ConnectButton").Disabled = true; } private void SocketDisconnected() { GetNode("%ConnectButton").Disabled = false; } private void ImportFileSelected(string filename) { EmitSignal(SignalName.ImportSelected, filename); } private void ExportFileSelected(string filename) { ExportSettings es = new() { ScaleImages = GetNode("%ExportCompressPicturesButton").ButtonPressed, }; EmitSignal(SignalName.ExportSelected, filename, Variant.From(es)); } }