#region Using Directives using System ; using System.Collections.Generic ; using System.ComponentModel ; using System.Runtime.CompilerServices ; using System.Windows.Input; //using Fury.Game.NoesisUI; //using Noesis; //using Unity.Entities; using UnityEngine ; using EventHandler = System.EventHandler; #endregion namespace Fury.UI.Windows { public enum ConnectionMode : byte { Client, Host, }; public class StartMPGameViewModel : INotifyPropertyChanged { [SerializeField] public string PlayerName { get; set; } [SerializeField] public string IPString { get; set; } [SerializeField] public string PortString { get; set; } [SerializeField] public bool IsSpectator { get; set; } [SerializeField] public ConnectionMode ConnectionMode { get; set; } private bool _isHostSelected; private bool _isClientSelected; private bool _spectatorEnabled; public StartMPGameViewModel() { this.PlayerName = "Player"; this.IPString = "127.0.0.1"; this.PortString = "7777"; this.SpectatorEnabled = false; this.ConnectionMode = ConnectionMode.Host; CancelCommand = new DelegateCommand(SayCancelled); // Link the command to the method StartCommand = new DelegateCommand(StartGame); } public bool SpectatorEnabled { get => _spectatorEnabled; set { _spectatorEnabled = value; IsSpectator = value; //Debug.Log($"Spectator Mode = {_spectatorEnabled}"); } } public bool IsJoinSelected { get => _isClientSelected; set { _isClientSelected = value; //Debug.Log($"IsJoinSelected = {_isClientSelected}"); } } public bool IsHostSelected { get => _isHostSelected; set { _isHostSelected = value; //Debug.Log($"IsHostSelected = {_isHostSelected}"); } } // Bind "Command={Binding CancelCommand}" to a button in the XAML // Custom DelegateCommand class defined below: taken from https://www.noesisengine.com/docs/Gui.Core.CommandsTutorial.html#command-bindings public DelegateCommand CancelCommand { get; set; } public DelegateCommand StartCommand { get; set; } // TODO: hacky? private void StartGame(object parameter) { //if (IsHostSelected) //{ // World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged().HostButtonPressed(); //} //else if (IsJoinSelected) //{ // World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged().JoinButtonPressed(); //} //else { Debug.Log($"{parameter} Select either Host or Join!"); } } private void SayCancelled(object parameter) { Debug.Log($"{parameter} Cancelled!"); } #region DelegateCommand Implementation public class DelegateCommand : ICommand { public DelegateCommand(Action execute) { if (execute == null) { throw new ArgumentNullException("execute"); } _execute = execute; } public DelegateCommand(Func canExecute, Action execute) { if (canExecute == null) { throw new ArgumentNullException("canExecute"); } if (execute == null) { throw new ArgumentNullException("execute"); } _canExecute = canExecute; _execute = execute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) { CanExecuteChanged(this, System.EventArgs.Empty); } } private Func _canExecute; private Action _execute; } #endregion #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(propertyName); return true; } #endregion } }