Implemented LAN discovery

This commit is contained in:
Anuken
2018-01-01 22:13:20 -05:00
parent 24248df4af
commit 8a38160124
9 changed files with 115 additions and 36 deletions

View File

@@ -187,6 +187,11 @@ public class Control extends Module{
Settings.defaults("save-" + i + "-name", "untitled");
Settings.defaults("save-" + i + "-data", "empty");
}
Settings.defaultList(
"ip", "localhost",
"port", Vars.port+""
);
Settings.loadAll("io.anuke.moment");

View File

@@ -178,29 +178,6 @@ public class UI extends SceneModule{
editor = new MapEditor();
editorDialog = new MapEditorDialog(editor);
}
join = new FloatingDialog("$text.joingame.title");
join.content().add("$text.joingame.ip").left();
Mindustry.platforms.addDialog(join.content().addField("localhost", text -> lastip = text).size(180f, 54f).get());
join.content().row();
join.content().add("$text.server.port").left();
Mindustry.platforms.addDialog(join.content()
.addField(Vars.port + "", new DigitsOnlyFilter(), text -> lastport = Strings.parseInt(text))
.size(180f, 54f).get());
join.buttons().defaults().size(140f, 60f).pad(4f);
join.buttons().addButton("$text.cancel", join::hide);
join.buttons().addButton("$text.ok", () -> {
showLoading("$text.connecting");
Timers.runTask(2f, () -> {
try{
Net.connect(lastip, lastport);
}catch (IOException e) {
showError(Bundles.format("text.connectfail", Strings.parseException(e, false)));
hideLoading();
}
});
}).disabled(b -> lastip.isEmpty() || lastport == Integer.MIN_VALUE);
settingserror = new Dialog("Warning", "dialog");
settingserror.content().add("[crimson]Failed to access local storage.\nSettings will not be saved.");
@@ -210,6 +187,8 @@ public class UI extends SceneModule{
gameerror = new Dialog("$text.error.crashtitle", "dialog");
gameerror.content().labelWrap("$text.error.crashmessage").width(600f).pad(10f);
gameerror.buttons().addButton("#text.ok", gameerror::hide).size(200f, 50);
join = new JoinDialog();
discord = new Dialog("Discord", "dialog");
discord.content().margin(12f);

View File

@@ -55,7 +55,7 @@ public class Net{
/**Starts discovering servers on a different thread. Does not work with GWT.
* Callback is run on the main libGDX thread.*/
public void discoverServers(Consumer<Array<Address>> cons){
public static void discoverServers(Consumer<Array<Address>> cons){
executor.submit(() -> {
Array<Address> arr = clientProvider.discover();
Gdx.app.postRunnable(() -> {

View File

@@ -1,12 +1,103 @@
package io.anuke.mindustry.ui;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.net.Address;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.core.Settings;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.scene.style.Drawable;
import io.anuke.ucore.scene.ui.Dialog;
import io.anuke.ucore.scene.ui.TextButton;
import io.anuke.ucore.scene.ui.TextField.TextFieldFilter.DigitsOnlyFilter;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Strings;
import java.io.IOException;
public class JoinDialog extends FloatingDialog {
Dialog join;
Table hosts = new Table();
float w = 400;
public JoinDialog(){
super("$text.joingame");
addCloseButton();
join = new FloatingDialog("$text.joingame.title");
join.content().add("$text.joingame.ip").left();
Mindustry.platforms.addDialog(join.content().addField(Settings.getString("ip"),text ->{
Settings.putString("ip", text);
Settings.save();
}).size(180f, 54f).get());
join.content().row();
join.content().add("$text.server.port").left();
Mindustry.platforms.addDialog(join.content()
.addField(Settings.getString("port"), new DigitsOnlyFilter(), text ->{
Settings.putString("port", text);
Settings.save();
})
.size(180f, 54f).get());
join.buttons().defaults().size(140f, 60f).pad(4f);
join.buttons().addButton("$text.cancel", join::hide);
join.buttons().addButton("$text.ok", () ->
connect(Settings.getString("port"), Integer.parseInt(Settings.getString("port")))
).disabled(b -> Settings.getString("ip").isEmpty() || Integer.parseInt(Settings.getString("port")) == Integer.MIN_VALUE);
setup();
shown(() -> {
hosts.clear();
hosts.background("button");
hosts.label(() -> "[accent]" + Bundles.get("text.hosts.discovering") + new String(new char[(int)(Timers.time() / 10) % 4]).replace("\0", ".")).pad(10f);
Net.discoverServers(list -> {
addHosts(list);
});
});
}
void setup(){
hosts.background("button");
content().clear();
content().add(hosts).width(w).pad(0);
content().row();
content().addButton("$text.joingame.byip", "clear", join::show).width(w).height(80f);
}
void addHosts(Array<Address> array){
hosts.clear();
if(array.size == 0){
hosts.add("$text.hosts.none").pad(20f);
}else {
for (Address a : array) {
TextButton button = hosts.addButton("[accent]"+a.name, "clear", () -> {
connect(a.address, Vars.port);
}).width(w).height(80f).pad(4f).get();
button.left();
button.row();
button.add("[lightgray]" + a.address + " / " + Vars.port).pad(4).left();
hosts.row();
hosts.background((Drawable) null);
}
}
}
void connect(String ip, int port){
Vars.ui.showLoading("$text.connecting");
Timers.runTask(2f, () -> {
try{
Net.connect(ip, port);
}catch (IOException e) {
Vars.ui.showError(Bundles.format("text.connectfail", Strings.parseException(e, false)));
Vars.ui.hideLoading();
}
});
}
}

View File

@@ -44,17 +44,15 @@ public class MenuDialog extends FloatingDialog{
ui.showPrefs();
});
//if(!Vars.gwt){
content().row();
content().addButton("$text.savegame", () -> {
save.show();
});
content().row();
content().addButton("$text.savegame", () -> {
save.show();
});
content().row();
content().addButton("$text.loadgame", () -> {
load.show();
});
//}
content().row();
content().addButton("$text.loadgame", () -> {
load.show();
});
content().row();

View File

@@ -39,7 +39,6 @@ public class MenuFragment implements Fragment{
row();
if(!gwt){
add(new MenuButton("$text.editor", group, ui::showEditor));
row();
}