1100 lines
No EOL
40 KiB
C#
1100 lines
No EOL
40 KiB
C#
using Exceptionless;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Content;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Myra.Graphics2D.Brushes;
|
|
using Myra.Graphics2D.TextureAtlases;
|
|
using Myra.Graphics2D.UI;
|
|
using Myra.Graphics2D.UI.File;
|
|
using Myra.Graphics2D.UI.Properties;
|
|
using Newtonsoft.Json;
|
|
using Sledgemapper.Shared.Entities;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace Sledgemapper.UI
|
|
{
|
|
public partial class MainWidget
|
|
{
|
|
private AuthenticateResponse _authResponse;
|
|
private readonly CommunicationManager CommunicationManager;
|
|
private State _state;
|
|
private Settings _settings;
|
|
private GameWindow _window;
|
|
|
|
private readonly CachedContent _cachedContent;
|
|
public MainWidget(CommunicationManager communicationManager, State state, Settings settings, CachedContent cachedContent, GameWindow window)
|
|
{
|
|
BuildUI();
|
|
|
|
CommunicationManager = communicationManager;
|
|
_state = state;
|
|
_settings = settings;
|
|
_cachedContent = cachedContent;
|
|
_window = window;
|
|
MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
|
|
MenuConnectSync.Selected += OnMenuConnectSyncSelected;
|
|
MenuFileLoad.Selected += OnMenuFileLoadSelected;
|
|
MenuFileSave.Selected += OnMenuFileSaveSelected;
|
|
MenuFileSettings.Selected += OneMenuFileSettingsSelected;
|
|
//MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
|
|
//MenuConnectNew.Selected += OnMenuConnectNewSelected;
|
|
MenuConnectJoin.Selected += OnMenuConnectJoinSelected;
|
|
MenuConnectUpload.Selected += OnMenuConnectUploadSelected;
|
|
MenuViewCenterOnSelection.Selected += OnMenuViewCenterOnSelectionSelected;
|
|
MenuViewShowCellNUmbers.Selected += OnMenuViewShowCellNUmbersSelected;
|
|
MenuViewShowNotes.Selected += OnMenuViewNotesSelected;
|
|
|
|
MenuCampaingNew.Selected += OnMenuCampaignNew;
|
|
MenuCampaignOpen.Selected += OnMenuCampaignOpen;
|
|
MenuCampaignPlayers.Selected += OnMenuCampaignPlayersSelected;
|
|
|
|
MenuMapOpen.Selected += OnMenuMapOpen;
|
|
MenuMapNew.Selected += OnMenuMapNew;
|
|
|
|
MenuConnectNew.Enabled = false;
|
|
MenuConnectJoin.Enabled = false;
|
|
MenuConnectSync.Enabled = false;
|
|
MenuConnectUpload.Enabled = false;
|
|
BtnToolbarLine.Click += OnBtnToolbarLinClicked;
|
|
BtnToolbarRoom.Click += OnBtnToolbarRoomClicked;
|
|
BtnToolbarDelete.Click += OnBtnToolbarDeleteClicked;
|
|
}
|
|
|
|
|
|
|
|
public void ClearSelection()
|
|
{
|
|
ClearSelection(GridWalls);
|
|
ClearSelection(GridOverlays);
|
|
ClearSelection(Toolbar);
|
|
}
|
|
|
|
private void ClearSelection(Grid grid)
|
|
{
|
|
foreach (var widget in grid.Widgets)
|
|
{
|
|
widget.Border = null;
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
switch (connection.State)
|
|
{
|
|
case HubConnectionState.Connected:
|
|
lblConnectionStatus.Text = "Connected";
|
|
break;
|
|
case HubConnectionState.Connecting:
|
|
lblConnectionStatus.Text = "Connecting";
|
|
break;
|
|
case HubConnectionState.Disconnected:
|
|
lblConnectionStatus.Text = "Disconnected";
|
|
|
|
break;
|
|
case HubConnectionState.Reconnecting:
|
|
lblConnectionStatus.Text = "Reconnecting";
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void OnMapEntityAdded(object sender, MapEntityAddedEventArgs e)
|
|
{
|
|
CommunicationManager.Enqueue(e.MapEntity, TileAction.Add);
|
|
}
|
|
|
|
private void OnMapEntityDeleted(object sender, MapEntityDeletedEventArgs e)
|
|
{
|
|
CommunicationManager.Enqueue(e.MapEntity, TileAction.Delete);
|
|
}
|
|
|
|
private async void OnMenuConnectSyncSelected(object sender, EventArgs e)
|
|
{
|
|
if (!((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var serverMap = await CommunicationManager.Api.Session(CommunicationManager.SessionData.SessionName);
|
|
CommunicationManager.SessionData.Overlays = serverMap.Overlays;
|
|
CommunicationManager.SessionData.Map = serverMap.Map;
|
|
CommunicationManager.SessionData.Walls = serverMap.Walls;
|
|
CommunicationManager.SessionData.Notes = serverMap.Notes;
|
|
CommunicationManager.SessionData.Lines = serverMap.Lines;
|
|
CommunicationManager.SessionData.Rooms = serverMap.Rooms;
|
|
}
|
|
|
|
private async void OnMenuConnectUploadSelected(object sender, EventArgs e)
|
|
{
|
|
if (!((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await CommunicationManager.Api.SaveSnapshot(CommunicationManager.SessionData, CommunicationManager.SessionData.SessionName);
|
|
}
|
|
|
|
private void OnMenuFileSaveSelected(object sender, EventArgs e)
|
|
{
|
|
FileDialog dialog = new(FileDialogMode.SaveFile)
|
|
{
|
|
Filter = "*.map"
|
|
};
|
|
|
|
dialog.Closed += (s, a) =>
|
|
{
|
|
if (!dialog.Result)
|
|
{
|
|
return;
|
|
}
|
|
|
|
using StreamWriter file = File.CreateText(dialog.FilePath);
|
|
JsonSerializer serializer = new();
|
|
serializer.Serialize(file, CommunicationManager.SessionData);
|
|
};
|
|
|
|
dialog.ShowModal(Desktop);
|
|
}
|
|
|
|
private void OnMenuFileLoadSelected(object sender, EventArgs e)
|
|
{
|
|
var dialog = new FileDialog(FileDialogMode.OpenFile)
|
|
{
|
|
Filter = "*.map"
|
|
};
|
|
|
|
dialog.Closed += (s, a) =>
|
|
{
|
|
if (!dialog.Result)
|
|
{
|
|
return;
|
|
}
|
|
using StreamReader file = File.OpenText(dialog.FilePath);
|
|
JsonSerializer serializer = new();
|
|
var loadData = (Session)serializer.Deserialize(file, typeof(Session));
|
|
CommunicationManager.SessionData.Map = loadData.Map;
|
|
CommunicationManager.SessionData.Overlays = loadData.Overlays;
|
|
CommunicationManager.SessionData.Walls = loadData.Walls;
|
|
CommunicationManager.SessionData.Rooms = loadData.Rooms;
|
|
CommunicationManager.SessionData.Lines = loadData.Lines;
|
|
};
|
|
|
|
dialog.ShowModal(Desktop);
|
|
}
|
|
|
|
private void OneMenuFileSettingsSelected(object sender, EventArgs e)
|
|
{
|
|
var propertyGrid = new PropertyGrid
|
|
{
|
|
Object = _settings,
|
|
Width = 350
|
|
};
|
|
|
|
var _windowEditor = new Window
|
|
{
|
|
Title = "Object Editor",
|
|
Content = propertyGrid
|
|
};
|
|
_windowEditor.ShowModal(Desktop);
|
|
}
|
|
|
|
private void OnMenuConnectJoinSelected(object sender, EventArgs e)
|
|
{
|
|
if (!((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Window window = new()
|
|
{
|
|
Title = "Join mapping session"
|
|
};
|
|
|
|
var content = new SessionWindow();
|
|
content.BtnLogin.Text = "Join";
|
|
content.BtnLogin.Click += OnButtonJoinSessionClicked;
|
|
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
content.TxtSession.SetKeyboardFocus();
|
|
}
|
|
|
|
private void OnMenuViewShowCellNUmbersSelected(object sender, EventArgs e)
|
|
{
|
|
_state.ShowCellNumbers = !_state.ShowCellNumbers;
|
|
}
|
|
|
|
private void OnMenuViewCenterOnSelectionSelected(object sender, EventArgs e)
|
|
{
|
|
CenterOnSelectedTile();
|
|
}
|
|
|
|
private void OnMenuViewNotesSelected(object sender, EventArgs e)
|
|
{
|
|
Window window = new()
|
|
{
|
|
Title = "Notes"
|
|
};
|
|
|
|
var content = new NoteList();
|
|
|
|
for (var i = 0; i < CommunicationManager.SessionData.Notes.Values.Count; i++)
|
|
{
|
|
var note = CommunicationManager.SessionData.Notes.Values.ElementAt(i);
|
|
var item = new NoteListItem();
|
|
item.LblNoteText.Text = $"{note.ToString()} - {note.Text}";
|
|
item.BtnNoteCenter.Image = new TextureRegion(_cachedContent.Location);
|
|
item.BtnNoteView.Image = new TextureRegion(_cachedContent.Eye);
|
|
item.BtnNoteCenter.Click += (s, e) => { CenterOnTile(note.X, note.Y); };
|
|
item.BtnNoteView.Click += (s, e) => { EditNote(note); window.Close(); };
|
|
content.StackNotesList.AddChild(item);
|
|
}
|
|
|
|
window.Content = content;
|
|
window.ShowModal(Desktop);
|
|
}
|
|
|
|
private void CenterOnSelectedTile()
|
|
{
|
|
CenterOnTile(_state.SelectedTile.X, _state.SelectedTile.Y);
|
|
}
|
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
private void OnMenuCampaignNew(object sender, EventArgs e)
|
|
{
|
|
if (sender is MenuItem && !((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Window window = new()
|
|
{
|
|
Title = "New campaign"
|
|
};
|
|
|
|
var content = new CampaignWindow();
|
|
//content.BtnNewCampaign.Text = "N";
|
|
content.BtnNewCampaign.Click += OnButtonNewCampaignClicked;
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
content.TxtCampaign.SetKeyboardFocus();
|
|
}
|
|
|
|
private async void OnMenuCampaignOpen(object sender, EventArgs e)
|
|
{
|
|
if (!((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Window window = new()
|
|
{
|
|
Title = "Campaigns"
|
|
};
|
|
|
|
var content = new CampaignList();
|
|
var campaigns = await CommunicationManager.Api.GetCampaigns();
|
|
foreach (var campaign in campaigns)
|
|
{
|
|
var item = new UI.ListItem();
|
|
item.ItemName.Text = campaign.Name;
|
|
|
|
item.TouchUp += OnCampaignSelected;
|
|
content.StackCampaignsList.AddChild(item);
|
|
}
|
|
//content.BtnNewCampaign.Text = "N";
|
|
|
|
|
|
|
|
content.BtnNewCampaign.Click += (s, e) =>
|
|
{
|
|
window.Close();
|
|
OnMenuCampaignNew(s, e);
|
|
};
|
|
|
|
content.BtnLoadCampaign.Click += (s, e) =>
|
|
{
|
|
if (campaignSelected)
|
|
{
|
|
campaignSelected = false;
|
|
window.Close();
|
|
}
|
|
};
|
|
|
|
content.BtnCancelCampaign.Click += (s, e) =>
|
|
{
|
|
|
|
window.Close();
|
|
};
|
|
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
// content.TxtCampaign.SetKeyboardFocus();
|
|
}
|
|
|
|
private bool campaignSelected;
|
|
|
|
|
|
private void OnCampaignSelected(object sender, EventArgs e)
|
|
{
|
|
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 currentItem = list.GetChild(i) as HorizontalStackPanel;// UI.ListItem;
|
|
currentItem.Background = new SolidBrush("#D9D9D9FF");
|
|
}
|
|
item.Background = new SolidBrush(_settings.OverlayTintColor);
|
|
campaignSelected = true;
|
|
}
|
|
|
|
private void OnMapSelected(object sender, EventArgs e)
|
|
{
|
|
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 currentItem = list.GetChild(i) as HorizontalStackPanel;// UI.ListItem;
|
|
currentItem.Background = new SolidBrush("#D9D9D9FF");
|
|
}
|
|
item.Background = new SolidBrush(_settings.OverlayTintColor);
|
|
}
|
|
|
|
private async void OnMenuCampaignPlayersSelected(object sender, EventArgs e)
|
|
{
|
|
if (!((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Window window = new()
|
|
{
|
|
Title = "Players"
|
|
};
|
|
|
|
var content = new PlayerList();
|
|
var players = await CommunicationManager.Api.GetPlayers(_state.CampaignName);
|
|
foreach (var player in players)
|
|
{
|
|
var item = new UI.ListItem();
|
|
item.ItemName.Text = player.UserName;
|
|
|
|
|
|
content.StackCampaignsList.AddChild(item);
|
|
}
|
|
//content.BtnNewCampaign.Text = "N";
|
|
|
|
|
|
|
|
content.BtnInvitePlayer.Click += (s, e) =>
|
|
{
|
|
window.Close();
|
|
ShowAddPLayerWindow();
|
|
};
|
|
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
// content.TxtCampaign.SetKeyboardFocus();
|
|
}
|
|
private async void OnMenuMapOpen(object sender, EventArgs e)
|
|
{
|
|
if (!((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Window window = new()
|
|
{
|
|
Title = "Maps"
|
|
};
|
|
|
|
var content = new MapList();
|
|
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);
|
|
}
|
|
//content.BtnNewCampaign.Text = "N";
|
|
|
|
|
|
|
|
content.BtnNewCampaign.Click += (s, e) =>
|
|
{
|
|
window.Close();
|
|
OnMenuMapNew(s, e);
|
|
};
|
|
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
// content.TxtCampaign.SetKeyboardFocus();
|
|
}
|
|
|
|
private void ShowAddPLayerWindow()
|
|
{
|
|
Window window = new()
|
|
{
|
|
Title = "Invite player"
|
|
};
|
|
|
|
var content = new PlayerWindow();
|
|
//content.BtnNewCampaign.Text = "N";
|
|
content.BtnNewCampaign.Click += OnButtonInvitePlayerClicked;
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
content.TxtCampaign.SetKeyboardFocus();
|
|
}
|
|
|
|
private async void OnButtonJoinSessionClicked(object sender, EventArgs e)
|
|
{
|
|
var localContent = GetParentContentInWindow<SessionWindow>(((TextButton)sender));
|
|
|
|
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.Connection?.InvokeAsync<Session>("JoinSession", localContent.Content.TxtSession.Text);
|
|
if (result != null)
|
|
{
|
|
CommunicationManager.SessionData.Map = result.Map;
|
|
CommunicationManager.SessionData.Walls = result.Walls;
|
|
CommunicationManager.SessionData.Overlays = result.Overlays;
|
|
|
|
CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded;
|
|
CommunicationManager.SessionData.MapEntityDeleted -= OnMapEntityDeleted;
|
|
CommunicationManager.SessionData.MapEntityAdded += OnMapEntityAdded;
|
|
CommunicationManager.SessionData.MapEntityDeleted += OnMapEntityDeleted;
|
|
CommunicationManager.SessionData.SessionId = result.SessionId;
|
|
CommunicationManager.SessionData.SessionName = result.SessionName;
|
|
}
|
|
successful = result != null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
if (successful)
|
|
{
|
|
CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
|
|
lblSessionName.Text = CommunicationManager.SessionData.SessionName;
|
|
MenuConnectSync.Enabled = true;
|
|
MenuConnectUpload.Enabled = true;
|
|
localContent.Window.Close();
|
|
}
|
|
}
|
|
|
|
private async void OnButtonInvitePlayerClicked(object sender, EventArgs e)
|
|
{
|
|
var localContent = GetParentContentInWindow<PlayerWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
|
|
var isValid = ValidateTextbox(localContent.Content.TxtCampaign);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//if (CommunicationManager.Connection.State != HubConnectionState.Connected)
|
|
//{
|
|
// lblConnectionStatus.Text = "Connecting";
|
|
// await CommunicationManager.Connection.StartAsync();
|
|
// UpdateConnectionState(CommunicationManager.Connection);
|
|
//}
|
|
|
|
var successful = false;
|
|
try
|
|
{
|
|
await CommunicationManager.Api.InvitePlayer(_state.CampaignName, localContent.Content.TxtCampaign.Text);
|
|
|
|
//if (result)
|
|
//{
|
|
// CommunicationManager.SessionData.SessionName = localContent.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.TxtSession.Text);
|
|
//CommunicationManager.SessionData.SessionId = result2.SessionId;
|
|
//CommunicationManager.SessionData.SessionName = localContent.TxtSession.Text;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionlessClient.Default.SubmitException(ex);
|
|
}
|
|
|
|
//if (successful)
|
|
//{
|
|
// //CommunicationManager.SessionData.SessionName = localContent.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;
|
|
// localWindow.Close();
|
|
//}
|
|
|
|
localContent.Window.Close();
|
|
|
|
}
|
|
|
|
private async void OnButtonNewCampaignClicked(object sender, EventArgs e)
|
|
{
|
|
var localContent = GetParentContentInWindow<CampaignWindow>(((TextButton)sender));// localWindow.Content as PlayerWindow;
|
|
|
|
var isValid = ValidateTextbox(localContent.Content.TxtCampaign);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//if (CommunicationManager.Connection.State != HubConnectionState.Connected)
|
|
//{
|
|
// lblConnectionStatus.Text = "Connecting";
|
|
// await CommunicationManager.Connection.StartAsync();
|
|
// UpdateConnectionState(CommunicationManager.Connection);
|
|
//}
|
|
|
|
var successful = false;
|
|
try
|
|
{
|
|
await CommunicationManager.Api.NewCampaign(localContent.Content.TxtCampaign.Text);
|
|
|
|
//if (result)
|
|
//{
|
|
// CommunicationManager.SessionData.SessionName = localContent.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.TxtSession.Text);
|
|
//CommunicationManager.SessionData.SessionId = result2.SessionId;
|
|
//CommunicationManager.SessionData.SessionName = localContent.TxtSession.Text;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionlessClient.Default.SubmitException(ex);
|
|
}
|
|
|
|
//if (successful)
|
|
//{
|
|
// //CommunicationManager.SessionData.SessionName = localContent.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;
|
|
// localWindow.Close();
|
|
//}
|
|
|
|
localContent.Window.Close();
|
|
|
|
}
|
|
private void OnBtnToolbarRoomClicked(object sender, EventArgs e)
|
|
{
|
|
_state.InsertMode = InsertMode.NewRoom;
|
|
ClearSelection();
|
|
((ImageTextButton)sender).Border = new SolidBrush(Color.Red);
|
|
((ImageTextButton)sender).BorderThickness = new Myra.Graphics2D.Thickness(2);
|
|
|
|
}
|
|
|
|
private void OnBtnToolbarLinClicked(object sender, EventArgs e)
|
|
{
|
|
_state.InsertMode = InsertMode.NewLine;
|
|
ClearSelection();
|
|
((ImageTextButton)sender).Border = new SolidBrush(Color.Red);
|
|
((ImageTextButton)sender).BorderThickness = new Myra.Graphics2D.Thickness(2);
|
|
}
|
|
|
|
private void OnBtnToolbarDeleteClicked(object sender, EventArgs e)
|
|
{
|
|
_state.InsertMode = InsertMode.NewDelete;
|
|
|
|
ClearSelection();
|
|
((ImageTextButton)sender).Border = new SolidBrush(Color.Red);
|
|
((ImageTextButton)sender).BorderThickness = new Myra.Graphics2D.Thickness(2);
|
|
}
|
|
|
|
private void OnMenuMapNew(object sender, EventArgs e)
|
|
{
|
|
if (sender is MenuItem && !((MenuItem)sender).Enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Window window = new()
|
|
{
|
|
Title = "New map"
|
|
};
|
|
|
|
var content = new MapWindow();
|
|
//content.BtnNewCampaign.Text = "N";
|
|
content.BtnNewCampaign.Click += OnButtonNewMapClicked;
|
|
window.Content = content;
|
|
|
|
window.ShowModal(Desktop);
|
|
content.TxtCampaign.SetKeyboardFocus();
|
|
}
|
|
|
|
private async void OnButtonNewMapClicked(object sender, EventArgs e)
|
|
{
|
|
Container container = ((TextButton)sender).Parent;
|
|
while (!(container is Window))
|
|
{
|
|
container = container.Parent;
|
|
}
|
|
var localWindow = (Window)container;
|
|
var localContent = localWindow.Content as MapWindow;
|
|
var isValid = ValidateTextbox(localContent.TxtCampaign);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//if (CommunicationManager.Connection.State != HubConnectionState.Connected)
|
|
//{
|
|
// lblConnectionStatus.Text = "Connecting";
|
|
// await CommunicationManager.Connection.StartAsync();
|
|
// UpdateConnectionState(CommunicationManager.Connection);
|
|
//}
|
|
|
|
var successful = false;
|
|
try
|
|
{
|
|
await CommunicationManager.Api.NewSession(_state.CampaignName, localContent.TxtCampaign.Text);
|
|
|
|
//if (result)
|
|
//{
|
|
// CommunicationManager.SessionData.SessionName = localContent.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.TxtSession.Text);
|
|
//CommunicationManager.SessionData.SessionId = result2.SessionId;
|
|
//CommunicationManager.SessionData.SessionName = localContent.TxtSession.Text;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionlessClient.Default.SubmitException(ex);
|
|
}
|
|
|
|
//if (successful)
|
|
//{
|
|
// //CommunicationManager.SessionData.SessionName = localContent.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;
|
|
// localWindow.Close();
|
|
//}
|
|
|
|
localWindow.Close();
|
|
}
|
|
|
|
//TODO Refactor
|
|
private void EditNote(Note note)
|
|
{
|
|
_state.SelectedNote = new Note { X = note.X, Y = note.Y, Text = note.Text };
|
|
var noteWindow = new NoteWindow();
|
|
|
|
Window window = new()
|
|
{
|
|
Title = $" Note on {note.X}:{note.Y}"
|
|
};
|
|
noteWindow.NoteText.Text = note.Text;
|
|
noteWindow.BtnOk.Click += OnButtonNoteOkClick;
|
|
noteWindow.BtnCancel.Click += OnButtonNoteCancelClick;
|
|
|
|
window.Content = noteWindow;
|
|
window.ShowModal(Desktop);
|
|
noteWindow.NoteText.SetKeyboardFocus();
|
|
}
|
|
|
|
private void OnButtonNoteOkClick(object sender, EventArgs e)
|
|
{
|
|
var button = ((TextButton)sender);
|
|
var localContent = GetParentContentInWindow<NoteWindow>(button);
|
|
var note = new Note
|
|
{
|
|
X = _state.SelectedNote.X,
|
|
Y = _state.SelectedNote.Y,
|
|
Text = localContent.Content.NoteText.Text
|
|
};
|
|
CommunicationManager.SessionData.NewNote(note);
|
|
localContent.Window.Close();
|
|
}
|
|
|
|
private void OnButtonNoteCancelClick(object sender, EventArgs e)
|
|
{
|
|
var button = ((TextButton)sender);
|
|
var content = GetParentContentInWindow<Widget>(button);
|
|
content.Window.Close();
|
|
}
|
|
|
|
}
|
|
} |