RMafia/MafiaClient/MainWindow.axaml.cs

316 lines
11 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DiscordRPC;
using MafiaCommon;
using Button = Avalonia.Controls.Button;
namespace MafiaClient
{
public class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
_mainWindow = this;
App.Instance.GetRpcClient().SetPresence(new RichPresence()
{
State = "В главном меню"
});
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private static MainWindow _mainWindow;
public static MainWindow Instance => _mainWindow;
public void HideAll()
{
this.Find<Grid>("MainMenu").IsVisible = false;
this.Find<Grid>("SettingsGui").IsVisible = false;
this.Find<Grid>("IpInput").IsVisible = false;
this.Find<Grid>("GameQueue").IsVisible = false;
this.Find<Grid>("DisconnectScreen").IsVisible = false;
this.Find<Grid>("GameVotingPassive").IsVisible = false;
this.Find<Grid>("GameVotingActive").IsVisible = false;
this.Find<Grid>("GameEndScreen").IsVisible = false;
this.Find<Grid>("GameDay").IsVisible = false;
}
private void IpInputEnable(bool enable)
{
this.FindControl<Button>("IpInputGuiBack").IsEnabled = enable;
this.FindControl<Button>("IpInputGuiConnect").IsEnabled = enable;
this.FindControl<TextBox>("IpBox").IsEnabled = enable;
}
private void Exit_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void Play_OnClick(object sender, RoutedEventArgs e)
{
IpInputEnable(true);
HideAll();
this.Find<Grid>("IpInput").IsVisible = true;
App.Instance.GetRpcClient().SetPresence(new RichPresence()
{
State = "Выбирает сервер"
});
}
private void ConnectToServer_OnClick(object sender, RoutedEventArgs e)
{
String ip = this.FindControl<TextBox>("IpBox").Text ?? "";
App.Instance.GetRpcClient().SetPresence(new RichPresence()
{
State = "Присоединяется к серверу"
});
IpInputEnable(false);
Thread thread = new Thread(() =>
{
App.Instance.Connection = new ServerConnection(ip);
});
thread.Start();
}
private void Back_OnClick(object sender, RoutedEventArgs e)
{
HideAll();
this.Find<Grid>("MainMenu").IsVisible = true;
App.Instance.GetRpcClient().SetPresence(new RichPresence()
{
State = "В главном меню"
});
}
private void SettingsBack_OnClick(object sender, RoutedEventArgs e)
{
Settings.Config().Nick = this.FindControl <TextBox> ("NickBox").Text;
Settings.Save();
HideAll();
this.Find<Grid>("MainMenu").IsVisible = true;
App.Instance.GetRpcClient().SetPresence(new RichPresence()
{
State = "В главном меню"
});
}
private void Settings_OnClick(object sender, RoutedEventArgs e)
{
HideAll();
this.Find<Grid>("SettingsGui").IsVisible = true;
this.FindControl<TextBox>("NickBox").Text = Settings.Config().Nick;
}
public void ShowGameQueueScreen()
{
HideAll();
this.Find<Grid>("GameQueue").IsVisible = true;
}
public void ShowDisconnectScreen(string reason)
{
this.Find<TextBlock>("ReasonText").Text = reason;
HideAll();
this.Find<Grid>("DisconnectScreen").IsVisible = true;
}
private void GameQueue_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
GameQueueChatSend_OnClick(null, null);
}
}
private void GameQueueChatSend_OnClick(object sender, RoutedEventArgs e)
{
if (this.FindControl<TextBox>("QueueChatInput").Text != null)
{
if (this.FindControl<TextBox>("QueueChatInput").Text.Trim() != "")
{
App.Instance.Connection.SendMessage(ChatType.Queue,
this.FindControl<TextBox>("QueueChatInput").Text);
this.FindControl<TextBox>("QueueChatInput").Clear();
}
}
}
public void ShowVotingPassive(string name)
{
HideAll();
this.Find<Grid>("GameVotingPassive").IsVisible = true;
this.Find<TextBlock>("GameVotingPassiveRole").Text = name;
}
public void ShowVotingActive(string name)
{
HideAll();
this.Find<Grid>("GameVotingActive").IsVisible = true;
this.Find<TextBlock>("GameVotingPassiveRole").Text = name;
}
private void GameVotingActiveChatSend_OnClick(object sender, RoutedEventArgs e)
{
if (this.FindControl<TextBox>("GameVotingActiveChatInput").Text != null)
{
if (this.FindControl<TextBox>("GameVotingActiveChatInput").Text.Trim() != "")
{
App.Instance.Connection.SendMessage(ChatType.Active,
this.FindControl<TextBox>("GameVotingActiveChatInput").Text);
this.FindControl<TextBox>("GameVotingActiveChatInput").Clear();
}
}
}
private void GameVotingActive_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
GameVotingActiveChatSend_OnClick(null, null);
}
}
public void ShowGameEnd(bool isWon, List<PlayerRole> playerRoles)
{
this.Find<TextBlock>("GameEndScreenWon").Text = isWon ? "Ты победил!" : "Ты проиграл!";
string text = "";
foreach (PlayerRole playerRole in playerRoles)
{
string role = "Роль";
switch (playerRole.Role)
{
case Role.Citizen:
role = "Мирный житель";
break;
case Role.Mafia:
role = "Мафия";
break;
case Role.Don:
role = "Дон";
break;
}
text += playerRole.Name + " - " + role + Environment.NewLine;
}
this.Find<TextBlock>("GameEndScreenRoles").Text = text;
HideAll();
this.Find<Grid>("GameEndScreen").IsVisible = true;
}
private void GameEndScreenBack_OnClick(object sender, RoutedEventArgs e)
{
HideAll();
this.Find<Grid>("GameQueue").IsVisible = true;
}
private void GameQueueLeave_OnClick(object sender, RoutedEventArgs e)
{
App.Instance.Connection.Disconnect();
HideAll();
this.Find<Grid>("MainMenu").IsVisible = true;
App.Instance.GetRpcClient().SetPresence(new RichPresence()
{
State = "В главном меню"
});
}
private void GameVotingActiveSelect_OnMouseDoubleClick(object sender, RoutedEventArgs e)
{
ListBoxItem selected = (ListBoxItem)(this.FindControl<ListBox>("GameVotingActiveSelect").SelectedItem);
App.Instance.Connection.Vote(Convert.ToInt32(((string)selected.Content).Split(")")[0]));
}
private void GameDayWait_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
GameDayChatSend_OnClick(null, null);
}
}
private void GameDayChatSend_OnClick(object sender, RoutedEventArgs e)
{
if (this.FindControl<TextBox>("GameDayChatInput").Text != null)
{
if (this.FindControl<TextBox>("GameDayChatInput").Text.Trim() != "")
{
App.Instance.Connection.SendMessage(ChatType.Day,
this.FindControl<TextBox>("GameDayChatInput").Text);
this.FindControl<TextBox>("GameDayChatInput").Clear();
}
}
}
private void GameDaySelect_OnMouseDoubleClick(object sender, RoutedEventArgs e)
{
if (this.FindControl<ListBox>("GameDaySelect").SelectedItem != null)
{
ListBoxItem selected = (ListBoxItem) (this.FindControl<ListBox>("GameDaySelect").SelectedItem);
App.Instance.Connection.Vote(Convert.ToInt32(((string) selected.Content).Split(")")[0]));
}
}
public void ShowDay(bool voting, Role role, List<Player> players)
{
this.FindControl<ListBox>("GameDaySelect").IsEnabled = voting;
if (players.Count != 0)
{
this.FindControl<ListBox>("GameDaySelect").Items = null;
List<ListBoxItem> items = new List<ListBoxItem>();
foreach (Player player in players)
{
items.Add(new ListBoxItem {Content = player.Id + ") " + player.Name});
Console.WriteLine(player.Id + ") " + player.Name);
}
this.FindControl<ListBox>("GameDaySelect").Items = items;
}
if (role == Role.Died)
{
this.Find<TextBlock>("GameDayHotBar").Text = "Коли умер, жди";
}
else
{
this.Find<TextBlock>("GameDayHotBar").Text = voting ? "Голосуй и молись" : "Обсуждай и думай";
}
this.Find<Grid>("GameDay").IsVisible = true;
}
private void Window_OnClosing(object? sender, CancelEventArgs e)
{
App.Instance.GetRpcClient().Dispose();
}
private void AvaloniaObject_OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property.Name == "TransformedBounds")
{
this.Find<TextBlock>("Logo").FontSize = Math.Min(this.Width / 17, this.Height / 10);
}
}
}
}