GodotTicTacToe/scripts/MainMenu.cs

57 lines
1.5 KiB
C#
Raw Normal View History

2017-11-21 18:33:48 +01:00
using Godot;
using System;
using Network;
2017-11-21 18:33:48 +01:00
public class MainMenu : Panel {
private Net Net;
2017-11-21 23:51:27 +01:00
private LineEdit IPAddressEdit;
private LineEdit PortEdit;
2017-11-21 18:33:48 +01:00
public override void _Ready() {
Net = (Net) GetNode("/root/Net");
2017-11-21 23:51:27 +01:00
var IPAddressNode = GetNode("IPAddress");
var PortNode = GetNode("Port");
if (IPAddressNode is LineEdit) {
IPAddressEdit = (LineEdit) IPAddressNode;
}
if (PortNode is LineEdit) {
PortEdit = (LineEdit) PortNode;
}
2017-11-21 18:33:48 +01:00
}
private void OnLaunchServer() {
GD.print("Launch Server!");
2017-11-21 23:51:27 +01:00
string address;
int port;
SetAddressAndPort(out address, out port, "*", 7070);
Net.StartServer(address, port);
2017-11-21 18:33:48 +01:00
}
private void OnLaunchClient() {
GD.print("Launch Client!");
2017-11-21 23:51:27 +01:00
string address;
int port;
SetAddressAndPort(out address, out port, "localhost", 7070);
Net.StartClient(address, port);
}
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;
}
}
2017-11-21 18:33:48 +01:00
}
}