BITFALL/Assets/Artists/Scripts/UX/UXLan.cs

80 lines
1.4 KiB
C#
Raw Normal View History

2023-12-15 00:08:02 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.UX;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITFALL.UX
{
public class UXLan : MonoBehaviour
{
[SerializeField] private UIDocument document;
[SerializeReference,SubclassSelector] private INetServer server;
[SerializeReference,SubclassSelector] private INetClient client;
[UXBindPath("lan-toggle")]
private Toggle _lanToggle;
[UXBindPath("connect-button")]
private Button _connectButton;
[UXBindPath("ip-textfield")]
private TextField _ipField;
private void Start()
{
BITKit.UX.UXUtils.Inject(this);
_lanToggle.SetValueWithoutNotify(server.IsRunningServer);
_lanToggle.RegisterValueChangedCallback(evt =>
{
try
{
if (evt.newValue)
{
server.StartServer();
}
else
{
server.StopServer();
}
}
catch (Exception e)
{
Alert.Print(new AlertMessage()
{
title = e.Message,
message = e.StackTrace
});
}
});
_connectButton.clicked += () =>
{
try
{
var field = _ipField.value;
if (string.IsNullOrEmpty(field))
{
client.Connect();
}
else
{
client.Connect(_ipField.value);
}
}
catch (Exception e)
{
Alert.Print(new AlertMessage()
{
title = e.Message,
message = e.StackTrace
});
}
};
}
}
}