more refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michele Scandura 2021-09-09 10:53:32 +01:00
parent f4c2ad79f2
commit 34863a9984
8 changed files with 207 additions and 333 deletions

View file

@ -14,6 +14,7 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using TinyMessenger;
namespace Sledgemapper.UI
{
@ -23,10 +24,12 @@ namespace Sledgemapper.UI
private readonly CommunicationManager CommunicationManager;
private State _state;
private Settings _settings;
private GameWindow _window;
private readonly TinyMessengerHub _messenger;
private readonly CachedContent _cachedContent;
public MainWidget(CommunicationManager communicationManager, State state, Settings settings, CachedContent cachedContent, GameWindow window)
private readonly GameWindow Window;
public MainWidget(CommunicationManager communicationManager, State state, Settings settings, CachedContent cachedContent, TinyMessengerHub messenger, GameWindow window)
{
BuildUI();
@ -34,7 +37,8 @@ namespace Sledgemapper.UI
_state = state;
_settings = settings;
_cachedContent = cachedContent;
_window = window;
_messenger=messenger;
MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
MenuConnectSync.Selected += OnMenuConnectSyncSelected;
MenuFileLoad.Selected += OnMenuFileLoadSelected;
@ -62,9 +66,36 @@ namespace Sledgemapper.UI
BtnToolbarLine.Click += OnBtnToolbarLinClicked;
BtnToolbarRoom.Click += OnBtnToolbarRoomClicked;
BtnToolbarDelete.Click += OnBtnToolbarDeleteClicked;
_messenger.Subscribe<LoginSuccesfulMessage>(OnLoginSuccesfulMessage);
_messenger.Subscribe<SignalrConnectionUpdateMessage>(OnSignalrConnectionUpdateMessage);
_messenger.Subscribe<MapOpenedMessage>(OnMapOpenedMessage);
}
private void OnMapOpenedMessage(MapOpenedMessage obj)
{
lblSessionName.Text = CommunicationManager.SessionData.SessionName;
MenuConnectSync.Enabled = true;
MenuConnectUpload.Enabled = true;
CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded;
CommunicationManager.SessionData.MapEntityDeleted -= OnMapEntityDeleted;
CommunicationManager.SessionData.MapEntityAdded += OnMapEntityAdded;
CommunicationManager.SessionData.MapEntityDeleted += OnMapEntityDeleted;
}
private async void OnSignalrConnectionUpdateMessage(SignalrConnectionUpdateMessage obj)
{
lblConnectionStatus.Text = "Connecting";
await CommunicationManager.Connection.StartAsync();
UpdateConnectionState(CommunicationManager.Connection);
}
private void OnLoginSuccesfulMessage(LoginSuccesfulMessage obj)
{
MenuConnectNew.Enabled = true;
MenuConnectJoin.Enabled = true;
lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
}
public void ClearSelection()
{
@ -96,204 +127,11 @@ namespace Sledgemapper.UI
Title = "Login"
};
var content = new LoginRegisterWindow(CommunicationManager, this, window);
// #if DEBUG
// content.TxtEmail.Text = "michele.scandura@outlook.com";
// content.TxtPassword.Text = "slePharland!79";
// #endif
// content.RdoLogin.IsPressed = true;
// content.RdoLogin.Click += (s, e) =>
// {
// content.TxtFirstname.Visible = false;
// content.TxtLastname.Visible = false;
// content.TxtInitials.Visible = false;
// content.LblFirstname.Visible = false;
// content.LblLastname.Visible = false;
// content.LblInitials.Visible = false;
// content.BtnLogin.Visible = true;
// content.BtnRegister.Visible = false;
// window.Title = "Login";
// };
// content.RdoRegister.Click += (s, e) =>
// {
// content.TxtFirstname.Visible = true;
// content.TxtLastname.Visible = true;
// content.TxtInitials.Visible = true;
// content.LblFirstname.Visible = true;
// content.LblLastname.Visible = true;
// content.LblInitials.Visible = true;
// content.BtnLogin.Visible = false;
// content.BtnRegister.Visible = true;
// window.Title = "Register";
// };
// content.BtnRegister.Click += OnButtonRegisterClick;
// content.BtnLogin.Click += OnButtonLoginClick;
// content.TxtEmail.SetKeyboardFocus();
var content = new LoginRegisterWindow(CommunicationManager, window,_messenger);
window.Content = content;
window.ShowModal(Desktop);
}
// private async void OnButtonRegisterClick(object sender, EventArgs e)
// {
// var button = ((TextButton)sender);
// var localContent = GetParentContentInWindow<LoginRegisterWindow>(button);// localWindow.Content as PlayerWindow;
// if (!button.Enabled)
// {
// return;
// }
// var isValid = true;
// isValid &= ValidateTextbox(localContent.Content.TxtEmail);
// isValid &= ValidateTextbox(localContent.Content.TxtPassword);
// isValid &= ValidateTextbox(localContent.Content.TxtFirstname);
// isValid &= ValidateTextbox(localContent.Content.TxtLastname);
// isValid &= ValidateTextbox(localContent.Content.TxtInitials);
// if (!isValid)
// {
// localContent.Content.LblLoginError.Text = "Please complete all the fields";
// localContent.Content.LblLoginError.Visible = true;
// return;
// }
// var successful = false;
// try
// {
// button.Text = "Wait...";
// localContent.Content.LblLoginError.Text = "";
// localContent.Content.LblLoginError.Visible = false;
// var result = await CommunicationManager.Register(new RegisterModel
// {
// Username = localContent.Content.TxtEmail.Text,
// Email = localContent.Content.TxtEmail.Text,
// Password = localContent.Content.TxtPassword.Text,
// FirstName = localContent.Content.TxtFirstname.Text,
// LastName = localContent.Content.TxtLastname.Text,
// Initials = localContent.Content.TxtInitials.Text
// });
// if (result.Result)
// {
// _authResponse = await CommunicationManager.Login(new AuthenticateModel
// {
// Username = localContent.Content.TxtEmail.Text,
// Password = localContent.Content.TxtPassword.Text
// });
// successful = true;
// }
// else
// {
// localContent.Content.LblLoginError.Text = result.Errors.FirstOrDefault();
// localContent.Content.LblLoginError.Visible = true;
// }
// }
// catch (Refit.ApiException refitException)
// {
// ExceptionlessClient.Default.SubmitException(refitException);
// localContent.Content.LblLoginError.Text = refitException.Content;
// localContent.Content.LblLoginError.Visible = true;
// }
// catch (Exception ex)
// {
// ExceptionlessClient.Default.SubmitException(ex);
// localContent.Content.LblLoginError.Text = "Can't connect to the server";
// localContent.Content.LblLoginError.Visible = true;
// Debug.Write(ex);
// }
// finally
// {
// button.Enabled = true;
// button.Text = "Register";
// }
// if (successful)
// {
// MenuConnectNew.Enabled = true;
// MenuConnectJoin.Enabled = true;
// lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
// localContent.Window.Close();
// }
// }
// private async void OnButtonLoginClick(object sender, EventArgs e)
// {
// var button = ((TextButton)sender);
// Container container = button.Parent;
// while (!(container is Window))
// {
// container = container.Parent;
// }
// var localWindow = (Window)container;
// var localContent = localWindow.Content as LoginRegisterWindow;
// if (!button.Enabled)
// {
// return;
// }
// var isValid = true;
// isValid &= ValidateTextbox(localContent.TxtEmail);
// isValid &= ValidateTextbox(localContent.TxtPassword);
// if (!isValid)
// {
// localContent.LblLoginError.Text = "Username or password is not valid";
// localContent.LblLoginError.Visible = true;
// return;
// }
// var successful = false;
// try
// {
// button.Text = "Wait...";
// localContent.LblLoginError.Text = "";
// localContent.LblLoginError.Visible = false;
// _authResponse = await CommunicationManager.Login(new AuthenticateModel
// {
// Username = localContent.TxtEmail.Text,
// Email = localContent.TxtEmail.Text,
// Password = localContent.TxtPassword.Text
// });
// successful = _authResponse != null;
// }
// catch (Refit.ApiException refitException)
// {
// ExceptionlessClient.Default.SubmitException(refitException);
// localContent.LblLoginError.Text = refitException.Content;
// localContent.LblLoginError.Visible = true;
// }
// catch (Exception ex)
// {
// ExceptionlessClient.Default.SubmitException(ex);
// localContent.LblLoginError.Text = "Can't connect to the server";
// localContent.LblLoginError.Visible = true;
// Debug.Write(ex);
// }
// finally
// {
// button.Enabled = true;
// button.Text = "Login";
// }
// if (successful)
// {
// MenuConnectNew.Enabled = true;
// MenuConnectJoin.Enabled = true;
// lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
// localWindow.Close();
// }
// }
private void OnMenuConnectNewSelected(object sender, EventArgs e)
{
@ -307,96 +145,10 @@ namespace Sledgemapper.UI
Title = "New mapping session"
};
var content = new SessionWindow();
content.BtnLogin.Text = "Join";
content.BtnLogin.Click += OnButtonNewSessionClicked;
var content = new SessionWindow(CommunicationManager,_messenger,window, _state);
window.Content = content;
window.ShowModal(Desktop);
content.TxtSession.SetKeyboardFocus();
}
private async void OnButtonNewSessionClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<SessionWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
var isValid = ValidateTextbox(localContent.Content.TxtSession);
if (!isValid)
{
return;
}
if (CommunicationManager.Connection.State != HubConnectionState.Connected)
{
lblConnectionStatus.Text = "Connecting";
await CommunicationManager.Connection.StartAsync();
UpdateConnectionState(CommunicationManager.Connection);
}
var successful = false;
try
{
var result = await CommunicationManager.Api.NewSession(_state.CampaignName, localContent.Content.TxtSession.Text);
if (result)
{
CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded;
CommunicationManager.SessionData.MapEntityDeleted -= OnMapEntityDeleted;
CommunicationManager.SessionData.MapEntityAdded += OnMapEntityAdded;
CommunicationManager.SessionData.MapEntityDeleted += OnMapEntityDeleted;
}
successful = result;
var result2 = await CommunicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.Content.TxtSession.Text);
CommunicationManager.SessionData.SessionId = result2.SessionId;
CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
}
if (successful)
{
CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
CommunicationManager.SessionData.Map = CommunicationManager.SessionData.Map;
CommunicationManager.SessionData.Overlays = CommunicationManager.SessionData.Overlays;
CommunicationManager.SessionData.Walls = CommunicationManager.SessionData.Walls;
lblSessionName.Text = CommunicationManager.SessionData.SessionName;
MenuConnectSync.Enabled = true;
MenuConnectUpload.Enabled = true;
localContent.Window.Close();
}
}
//TODO refactor
private bool ValidateTextbox(TextBox textBox)
{
var valid = !string.IsNullOrWhiteSpace(textBox.Text);
if (!valid)
{
textBox.Background = new SolidBrush(Color.Red);
}
else
{
textBox.Background = new SolidBrush(new Color(51, 51, 51)); ;
}
return valid;
}
//TODO Refactor
private (Window Window, C Content) GetParentContentInWindow<C>(Widget widget) where C : Widget
{
Container container = widget.Parent;
while (!(container is Window))
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as C;
return (localWindow, localContent);
}
private void UpdateConnectionState(HubConnection connection)
@ -585,7 +337,7 @@ namespace Sledgemapper.UI
private void CenterOnTile(int x, int y)
{
var center = new Point((_window.ClientBounds.Width + 200) / 2 - _state.TileSize / 2, _window.ClientBounds.Height / 2 - _state.TileSize / 2);
var center = new Point((Window.ClientBounds.Width + 200) / 2 - _state.TileSize / 2, Window.ClientBounds.Height / 2 - _state.TileSize / 2);
var dx = center.X - x * _state.TileSize - _state.ViewportCenter.X;
var dy = center.Y - y * _state.TileSize - _state.ViewportCenter.Y;
_state.ViewportCenter = new Vector3(_state.ViewportCenter.X + dx, _state.ViewportCenter.Y + dy, _state.ViewportCenter.Z);
@ -672,7 +424,7 @@ namespace Sledgemapper.UI
private void OnCampaignSelected(object sender, EventArgs e)
{
var item = sender as UI.ListItem;
var localContent = GetParentContentInWindow<Widget>(item);
var localContent = item.GetParentContentInWindow<Widget>();
_state.CampaignName = item.ItemName.Text;
var list = item.Parent as Myra.Graphics2D.UI.Grid;
for (var i = 0; i < list.ChildrenCount; i++)
@ -687,7 +439,7 @@ namespace Sledgemapper.UI
private void OnMapSelected(object sender, EventArgs e)
{
var item = sender as UI.ListItem;
var localContent = GetParentContentInWindow<Widget>(item);
var localContent = item.GetParentContentInWindow<Widget>();
var list = item.Parent as Myra.Graphics2D.UI.Grid;
for (var i = 0; i < list.ChildrenCount; i++)
@ -790,9 +542,9 @@ namespace Sledgemapper.UI
private async void OnButtonJoinSessionClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<SessionWindow>(((TextButton)sender));
var localContent = ((TextButton)sender).GetParentContentInWindow<SessionWindow>();
var isValid = ValidateTextbox(localContent.Content.TxtSession);
var isValid = localContent.Content.TxtSession.ValidateTextbox();
if (!isValid)
{
return;
@ -841,8 +593,8 @@ namespace Sledgemapper.UI
private async void OnButtonInvitePlayerClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<PlayerWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
var isValid = ValidateTextbox(localContent.Content.TxtCampaign);
var localContent = ((TextButton)sender).GetParentContentInWindow<PlayerWindow>();// localWindow.Content as PlayerWindow;
var isValid = localContent.Content.TxtCampaign.ValidateTextbox();
if (!isValid)
{
return;
@ -896,9 +648,9 @@ namespace Sledgemapper.UI
private async void OnButtonNewCampaignClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<CampaignWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
var localContent = ((TextButton)sender).GetParentContentInWindow<CampaignWindow>();// localWindow.Content as PlayerWindow;
var isValid = ValidateTextbox(localContent.Content.TxtCampaign);
var isValid = localContent.Content.TxtCampaign.ValidateTextbox();
if (!isValid)
{
return;
@ -1005,7 +757,7 @@ namespace Sledgemapper.UI
}
var localWindow = (Window)container;
var localContent = localWindow.Content as MapWindow;
var isValid = ValidateTextbox(localContent.TxtCampaign);
var isValid = localContent.TxtCampaign.ValidateTextbox();
if (!isValid)
{
return;
@ -1078,7 +830,7 @@ namespace Sledgemapper.UI
private void OnButtonNoteOkClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
var localContent = GetParentContentInWindow<NoteWindow>(button);
var localContent = button.GetParentContentInWindow<NoteWindow>();
var note = new Note
{
X = _state.SelectedNote.X,
@ -1092,7 +844,7 @@ namespace Sledgemapper.UI
private void OnButtonNoteCancelClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
var content = GetParentContentInWindow<Widget>(button);
var content = button.GetParentContentInWindow<Widget>();
content.Window.Close();
}