82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using Godot;
|
|
using System;
|
|
using Network;
|
|
using Util;
|
|
|
|
public class MainMenu : Panel, PeerListener {
|
|
|
|
private Net Net;
|
|
|
|
private LineEdit IPAddressEdit;
|
|
private LineEdit PortEdit;
|
|
|
|
private Label StatusText;
|
|
|
|
public override void _Ready() {
|
|
Net = (Net) GetNode("/root/Net");
|
|
var IPAddressNode = GetNode("IPAddress");
|
|
var PortNode = GetNode("Port");
|
|
var StatusNode = GetNode("StatusText");
|
|
if (IPAddressNode is LineEdit) {
|
|
IPAddressEdit = (LineEdit) IPAddressNode;
|
|
}
|
|
if (PortNode is LineEdit) {
|
|
PortEdit = (LineEdit) PortNode;
|
|
}
|
|
if (StatusNode is Label) {
|
|
StatusText = (Label) StatusNode;
|
|
}
|
|
}
|
|
|
|
private void OnLaunchServer() {
|
|
GD.print("Launch Server!");
|
|
string address;
|
|
int port;
|
|
SetAddressAndPort(out address, out port, "*", 7070);
|
|
Net.StartServer(address, port);
|
|
}
|
|
|
|
|
|
private void OnLaunchClient() {
|
|
GD.print("Launch Client!");
|
|
string address;
|
|
int port;
|
|
SetAddressAndPort(out address, out port, "localhost", 7070);
|
|
var Client = Net.StartClient(address, port);
|
|
if (Client != null) {
|
|
Client.AddPeerListener(this);
|
|
}
|
|
StatusText.Text = "Connecting..";
|
|
}
|
|
|
|
private void SetAddressAndPort(out string address, out int port, string defaultAddress, int defaultPort) {
|
|
if (IPAddressEdit == null || IPAddressEdit.GetText() == "") {
|
|
address = defaultAddress;
|
|
} else {
|
|
address = IPAddressEdit.GetText();
|
|
}
|
|
port = defaultPort;
|
|
if (PortEdit != null && PortEdit.GetText() != "") {
|
|
int temp_port;
|
|
Int32.TryParse(PortEdit.GetText(), out temp_port);
|
|
if (temp_port > 80) {
|
|
port = temp_port;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnStart() {
|
|
}
|
|
|
|
public void OnStop() {
|
|
}
|
|
|
|
public void OnConnect(Connection conn) {
|
|
StatusText.Text = "Connecting..";
|
|
}
|
|
|
|
public void OnDisconnect(Connection conn, DisconnectReason reason) {
|
|
StatusText.Text = "Disconnected, reason: " + reason.ToString();
|
|
}
|
|
}
|