moving ui code to mainwidget
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michele 2021-09-05 22:05:30 +01:00
parent dc7b2dd8f2
commit ef2ad6965b
4 changed files with 631 additions and 293 deletions

View File

@ -23,9 +23,13 @@ namespace Sledgemapper.Api.Controllers
[HttpPost]
[Route("{campaignName}")]
public async Task<bool> Post(string campaignName)
public async Task<ActionResult<bool>> Post(string campaignName)
{
var result = await _mediator.Send(new NewCampaignCommand(campaignName, UserId.ToString()));
if (!result)
{
return BadRequest();
}
return result;
}

View File

@ -38,7 +38,7 @@ namespace Sledgemapper.Api.Handlers
}
catch (Exception ex)
{
}
return false;

View File

@ -45,7 +45,6 @@ namespace Sledgemapper
private Texture2D _location;
private Texture2D _comment;
private readonly Session _sessionData;
private AuthenticateResponse _authResponse;
private RenderTarget2D rendertarget;
private MainWidget _mainWidget;
private bool _showCellNumbers;
@ -198,13 +197,15 @@ namespace Sledgemapper
MyraEnvironment.Game = this;
ResetRenderTarget();
_mainWidget = new MainWidget();
_mainWidget = new MainWidget(_communicationManager);
//_mainWidget = new MainWidget();
_mainWidget.MenuConnectSync.Selected += OnMenuConnectSyncSelected;
_mainWidget.MenuFileLoad.Selected += OnMenuFileLoadSelected;
_mainWidget.MenuFileSave.Selected += OnMenuFileSaveSelected;
_mainWidget.MenuFileSettings.Selected += OneMenuFileSettingsSelected;
_mainWidget.MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
//_mainWidget.MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
_mainWidget.MenuConnectNew.Selected += OnMenuConnectNewSelected;
_mainWidget.MenuConnectJoin.Selected += OnMenuConnectJoinSelected;
_mainWidget.MenuConnectUpload.Selected += OnMenuConnectUploadSelected;
@ -218,7 +219,7 @@ namespace Sledgemapper
_mainWidget.MenuMapOpen.Selected += OnMenuMapOpen;
_mainWidget.MenuMapNew.Selected += OnMenuMapNew;
_mainWidget.MenuConnectNew.Enabled = false;
_mainWidget.MenuConnectJoin.Enabled = false;
_mainWidget.MenuConnectSync.Enabled = false;
@ -283,7 +284,7 @@ namespace Sledgemapper
private async void OnButtonNewMapClicked(object sender, EventArgs e)
{
Container container = ((TextButton)sender).Parent;
Container container = ((TextButton)sender).Parent;
while (!(container is Window))
{
container = container.Parent;
@ -357,10 +358,10 @@ namespace Sledgemapper
var campaigns = await _communicationManager.Api.GetMaps(_state.CampaignName);
foreach (var campaign in campaigns)
{
var item = new UI.ListItem();
item.ItemName.Text = campaign.SessionName;
item.TouchUp += OnMapSelected;
content.StackCampaignsList.AddChild(item);
var item = new UI.ListItem();
item.ItemName.Text = campaign.SessionName;
item.TouchUp += OnMapSelected;
content.StackCampaignsList.AddChild(item);
}
//content.BtnNewCampaign.Text = "N";
@ -369,7 +370,7 @@ namespace Sledgemapper
content.BtnNewCampaign.Click += (s, e) =>
{
window.Close();
OnMenuCampaignNew(s, e);
OnMenuMapNew(s, e);
};
window.Content = content;
@ -394,10 +395,10 @@ namespace Sledgemapper
var players = await _communicationManager.Api.GetPlayers(_state.CampaignName);
foreach (var player in players)
{
var item = new UI.ListItem();
item.ItemName.Text = player.UserName;
var item = new UI.ListItem();
item.ItemName.Text = player.UserName;
content.StackCampaignsList.AddChild(item);
}
//content.BtnNewCampaign.Text = "N";
@ -518,7 +519,7 @@ namespace Sledgemapper
{
var item = new UI.ListItem();
item.ItemName.Text = campaign.Name;
item.TouchUp += OnCampaignSelected;
content.StackCampaignsList.AddChild(item);
}
@ -556,31 +557,31 @@ namespace Sledgemapper
private bool campaignSelected;
private void OnCampaignSelected(object sender, EventArgs e)
{
var item = sender as UI.ListItem;
var item = sender as UI.ListItem;
var localContent = GetParentContentInWindow<Widget>(item);
_state.CampaignName = item.ItemName.Text;
var list = item.Parent as Myra.Graphics2D.UI.Grid;
for (var i=0;i<list.ChildrenCount;i++)
{
var list = item.Parent as Myra.Graphics2D.UI.Grid;
for (var i = 0; i < list.ChildrenCount; i++)
{
var currentItem = list.GetChild(i) as HorizontalStackPanel;// UI.ListItem;
currentItem.Background= new SolidBrush("#D9D9D9FF");
}
item.Background=new SolidBrush(_settings.OverlayTintColor);
currentItem.Background = new SolidBrush("#D9D9D9FF");
}
item.Background = new SolidBrush(_settings.OverlayTintColor);
campaignSelected = true;
}
private void OnMapSelected(object sender, EventArgs e)
private void OnMapSelected(object sender, EventArgs e)
{
var item = sender as UI.ListItem;
var item = sender as UI.ListItem;
var localContent = GetParentContentInWindow<Widget>(item);
var list = item.Parent as Myra.Graphics2D.UI.Grid;
for (var i=0;i<list.ChildrenCount;i++)
{
var list = item.Parent as Myra.Graphics2D.UI.Grid;
for (var i = 0; i < list.ChildrenCount; i++)
{
var currentItem = list.GetChild(i) as HorizontalStackPanel;// UI.ListItem;
currentItem.Background= new SolidBrush("#D9D9D9FF");
}
item.Background=new SolidBrush(_settings.OverlayTintColor);
currentItem.Background = new SolidBrush("#D9D9D9FF");
}
item.Background = new SolidBrush(_settings.OverlayTintColor);
}
private void OneMenuFileSettingsSelected(object sender, EventArgs e)
@ -2134,7 +2135,7 @@ namespace Sledgemapper
private async void OnButtonJoinSessionClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<SessionWindow>(((TextButton)sender));
var isValid = ValidateTextbox(localContent.Content.TxtSession);
if (!isValid)
{
@ -2184,7 +2185,7 @@ namespace Sledgemapper
private async void OnButtonInvitePlayerClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<PlayerWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
var localContent = GetParentContentInWindow<PlayerWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
var isValid = ValidateTextbox(localContent.Content.TxtCampaign);
if (!isValid)
{
@ -2293,58 +2294,58 @@ namespace Sledgemapper
}
private async void OnButtonNewSessionClicked(object sender, EventArgs e)
{
var localContent = GetParentContentInWindow<SessionWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
//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;
}
// var isValid = ValidateTextbox(localContent.Content.TxtSession);
// if (!isValid)
// {
// return;
// }
if (_communicationManager.Connection.State != HubConnectionState.Connected)
{
_mainWidget.lblConnectionStatus.Text = "Connecting";
await _communicationManager.Connection.StartAsync();
UpdateConnectionState(_communicationManager.Connection);
}
// if (_communicationManager.Connection.State != HubConnectionState.Connected)
// {
// _mainWidget.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);
// var successful = false;
// try
// {
// var result = await _communicationManager.Api.NewSession(_state.CampaignName, localContent.Content.TxtSession.Text);
if (result)
{
_sessionData.SessionName = localContent.Content.TxtSession.Text;
_sessionData.MapEntityAdded -= OnMapEntityAdded;
_sessionData.MapEntityDeleted -= OnMapEntityDeleted;
_sessionData.MapEntityAdded += OnMapEntityAdded;
_sessionData.MapEntityDeleted += OnMapEntityDeleted;
}
successful = result;
var result2 = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.Content.TxtSession.Text);
_sessionData.SessionId = result2.SessionId;
_sessionData.SessionName = localContent.Content.TxtSession.Text;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
}
// if (result)
// {
// _sessionData.SessionName = localContent.Content.TxtSession.Text;
// _sessionData.MapEntityAdded -= OnMapEntityAdded;
// _sessionData.MapEntityDeleted -= OnMapEntityDeleted;
// _sessionData.MapEntityAdded += OnMapEntityAdded;
// _sessionData.MapEntityDeleted += OnMapEntityDeleted;
// }
// successful = result;
// var result2 = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.Content.TxtSession.Text);
// _sessionData.SessionId = result2.SessionId;
// _sessionData.SessionName = localContent.Content.TxtSession.Text;
// }
// catch (Exception ex)
// {
// ExceptionlessClient.Default.SubmitException(ex);
// }
if (successful)
{
_sessionData.SessionName = localContent.Content.TxtSession.Text;
_communicationManager.SessionData.Map = _sessionData.Map;
_communicationManager.SessionData.Overlays = _sessionData.Overlays;
_communicationManager.SessionData.Walls = _sessionData.Walls;
_mainWidget.lblSessionName.Text = _sessionData.SessionName;
_mainWidget.MenuConnectSync.Enabled = true;
_mainWidget.MenuConnectUpload.Enabled = true;
localContent.Window.Close();
}
}
// if (successful)
// {
// _sessionData.SessionName = localContent.Content.TxtSession.Text;
// _communicationManager.SessionData.Map = _sessionData.Map;
// _communicationManager.SessionData.Overlays = _sessionData.Overlays;
// _communicationManager.SessionData.Walls = _sessionData.Walls;
// _mainWidget.lblSessionName.Text = _sessionData.SessionName;
// _mainWidget.MenuConnectSync.Enabled = true;
// _mainWidget.MenuConnectUpload.Enabled = true;
// localContent.Window.Close();
// }
//}
private void UpdateConnectionState(HubConnection connection)
{
@ -2367,9 +2368,9 @@ namespace Sledgemapper
}
}
private (Window Window ,C Content) GetParentContentInWindow<C>(Widget widget) where C:Widget
{
Container container = widget.Parent;
private (Window Window, C Content) GetParentContentInWindow<C>(Widget widget) where C : Widget
{
Container container = widget.Parent;
while (!(container is Window))
{
container = container.Parent;
@ -2377,13 +2378,13 @@ private (Window Window ,C Content) GetParentContentInWindow<C>(Widget widget) wh
var localWindow = (Window)container;
var localContent = localWindow.Content as C;
return (localWindow,localContent);
}
return (localWindow, localContent);
}
private void OnButtonNoteOkClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
var localContent = GetParentContentInWindow<NoteWindow>(button) ;
var localContent = GetParentContentInWindow<NoteWindow>(button);
var note = new Note
{
X = _state.SelectedNote.X,
@ -2400,161 +2401,161 @@ private (Window Window ,C Content) GetParentContentInWindow<C>(Widget widget) wh
var content = GetParentContentInWindow<Widget>(button);
content.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;
}
//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;
// var localWindow = (Window)container;
// var localContent = localWindow.Content as LoginRegisterWindow;
if (!button.Enabled)
{
return;
}
// if (!button.Enabled)
// {
// return;
// }
var isValid = true;
isValid &= ValidateTextbox(localContent.TxtEmail);
isValid &= ValidateTextbox(localContent.TxtPassword);
// 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;
// if (!isValid)
// {
// localContent.LblLoginError.Text = "Username or password is not valid";
// localContent.LblLoginError.Visible = true;
return;
}
// 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";
}
// 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)
{
_mainWidget.MenuConnectNew.Enabled = true;
_mainWidget.MenuConnectJoin.Enabled = true;
_mainWidget.lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
localWindow.Close();
}
}
// if (successful)
// {
// _mainWidget.MenuConnectNew.Enabled = true;
// _mainWidget.MenuConnectJoin.Enabled = true;
// _mainWidget.lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
// localWindow.Close();
// }
//}
private async void OnButtonRegisterClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
//private async void OnButtonRegisterClick(object sender, EventArgs e)
//{
// var button = ((TextButton)sender);
var localContent = GetParentContentInWindow<LoginRegisterWindow>(button);// localWindow.Content as PlayerWindow;
// var localContent = GetParentContentInWindow<LoginRegisterWindow>(button);// localWindow.Content as PlayerWindow;
if (!button.Enabled)
{
return;
}
// 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);
// 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;
}
// 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 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)
{
_mainWidget.MenuConnectNew.Enabled = true;
_mainWidget.MenuConnectJoin.Enabled = true;
// 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)
// {
// _mainWidget.MenuConnectNew.Enabled = true;
// _mainWidget.MenuConnectJoin.Enabled = true;
_mainWidget.lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
// _mainWidget.lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
localContent.Window.Close();
}
}
// localContent.Window.Close();
// }
//}
private void OnContextMenuNewNoteClick(object sender, EventArgs e)
{
@ -2627,54 +2628,54 @@ private (Window Window ,C Content) GetParentContentInWindow<C>(Widget widget) wh
content.TxtSession.SetKeyboardFocus();
}
private void OnMenuConnectLoginSelected(object sender, EventArgs e)
{
Window window = new()
{
Title = "Login"
};
// private void OnMenuConnectLoginSelected(object sender, EventArgs e)
// {
// Window window = new()
// {
// Title = "Login"
// };
var content = new LoginRegisterWindow();
// var content = new LoginRegisterWindow();
#if DEBUG
content.TxtEmail.Text = "michele.scandura@outlook.com";
content.TxtPassword.Text = "slePharland!79";
#endif
//#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.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.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.BtnRegister.Click += OnButtonRegisterClick;
// content.BtnLogin.Click += OnButtonLoginClick;
window.Content = content;
window.ShowModal(_desktop);
content.TxtEmail.SetKeyboardFocus();
}
// window.Content = content;
// window.ShowModal(_desktop);
// content.TxtEmail.SetKeyboardFocus();
// }
private async void OnMenuConnectSyncSelected(object sender, EventArgs e)
{
@ -2702,26 +2703,26 @@ private (Window Window ,C Content) GetParentContentInWindow<C>(Widget widget) wh
await _communicationManager.Api.SaveSnapshot(_sessionData, _sessionData.SessionName);
}
private void OnMenuConnectNewSelected(object sender, EventArgs e)
{
if (!((MenuItem)sender).Enabled)
{
return;
}
//private void OnMenuConnectNewSelected(object sender, EventArgs e)
//{
// if (!((MenuItem)sender).Enabled)
// {
// return;
// }
Window window = new()
{
Title = "New mapping session"
};
// Window window = new()
// {
// Title = "New mapping session"
// };
var content = new SessionWindow();
content.BtnLogin.Text = "Join";
content.BtnLogin.Click += OnButtonNewSessionClicked;
window.Content = content;
// var content = new SessionWindow();
// content.BtnLogin.Text = "Join";
// content.BtnLogin.Click += OnButtonNewSessionClicked;
// window.Content = content;
window.ShowModal(_desktop);
content.TxtSession.SetKeyboardFocus();
}
// window.ShowModal(_desktop);
// content.TxtSession.SetKeyboardFocus();
//}
private void OnMenuFileSaveSelected(object sender, EventArgs e)
{

View File

@ -1,15 +1,36 @@
using Exceptionless;
using Microsoft.Xna.Framework;
using Myra.Graphics2D.Brushes;
using Myra.Graphics2D.UI;
using Sledgemapper.Shared.Entities;
using System;
using System.Diagnostics;
using System.Linq;
namespace Sledgemapper.UI
{
public partial class MainWidget
{
public void ClearSelection()
{
public partial class MainWidget
{
private AuthenticateResponse _authResponse;
private readonly CommunicationManager CommunicationManager;
public MainWidget(CommunicationManager communicationManager)
{
BuildUI();
CommunicationManager = communicationManager;
MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
}
public void ClearSelection()
{
ClearSelection(GridWalls);
ClearSelection(GridOverlays);
ClearSelection(Toolbar);
}
}
private void ClearSelection(Grid grid)
{
@ -19,12 +40,324 @@ namespace Sledgemapper.UI
}
}
private void ClearSelection(HorizontalStackPanel grid)
private void ClearSelection(HorizontalStackPanel grid)
{
foreach (var widget in grid.Widgets)
{
widget.Border = null;
}
}
}
private void OnMenuConnectLoginSelected(object sender, EventArgs e)
{
Window window = new()
{
Title = "Login"
};
var content = new LoginRegisterWindow();
#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;
window.Content = content;
window.ShowModal(Desktop);
content.TxtEmail.SetKeyboardFocus();
}
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)
{
if (!((MenuItem)sender).Enabled)
{
return;
}
Window window = new()
{
Title = "New mapping session"
};
var content = new SessionWindow();
content.BtnLogin.Text = "Join";
content.BtnLogin.Click += OnButtonNewSessionClicked;
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)
{
_mainWidget.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)
{
_sessionData.SessionName = localContent.Content.TxtSession.Text;
_sessionData.MapEntityAdded -= OnMapEntityAdded;
_sessionData.MapEntityDeleted -= OnMapEntityDeleted;
_sessionData.MapEntityAdded += OnMapEntityAdded;
_sessionData.MapEntityDeleted += OnMapEntityDeleted;
}
successful = result;
var result2 = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.Content.TxtSession.Text);
_sessionData.SessionId = result2.SessionId;
_sessionData.SessionName = localContent.Content.TxtSession.Text;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
}
if (successful)
{
_sessionData.SessionName = localContent.Content.TxtSession.Text;
_communicationManager.SessionData.Map = _sessionData.Map;
_communicationManager.SessionData.Overlays = _sessionData.Overlays;
_communicationManager.SessionData.Walls = _sessionData.Walls;
_mainWidget.lblSessionName.Text = _sessionData.SessionName;
_mainWidget.MenuConnectSync.Enabled = true;
_mainWidget.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);
}
}
}