Skip to content

Commit

Permalink
Merge PR #1992 by @msteiger - join game screen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cervator committed Oct 31, 2015
2 parents badae13 + d17dbb7 commit c07995b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
15 changes: 6 additions & 9 deletions engine/src/main/java/org/terasology/config/NetworkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package org.terasology.config;

import com.google.common.collect.Lists;

import org.terasology.engine.TerasologyConstants;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author Immortius
Expand Down Expand Up @@ -64,20 +65,16 @@ public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}

public void add(ServerInfo serverInfo) {
public void addServerInfo(ServerInfo serverInfo) {
servers.add(serverInfo);
}

public void remove(ServerInfo info) {
public void removeServerInfo(ServerInfo info) {
servers.remove(info);
}

public List<ServerInfo> getServers() {
return servers;
}

public void setServers(List<ServerInfo> servers) {
this.servers = servers;
public List<ServerInfo> getServerInfos() {
return Collections.unmodifiableList(servers);
}

public String getMasterServer() {
Expand Down
4 changes: 1 addition & 3 deletions engine/src/main/java/org/terasology/config/ServerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.google.common.base.Preconditions;

import java.util.Optional;


/**
* @author Immortius
Expand All @@ -29,7 +27,7 @@ public class ServerInfo {
private String address;
private String owner;
private int port;
private boolean active;
private boolean active = true;

ServerInfo() {
// for serialization purposes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class AddServerPopup extends CoreScreenLayer {

public static final ResourceUrn ASSET_URI = new ResourceUrn("engine:addServerPopup!instance");

@In
private Config config;
private UIText nameText;
private UIText ownerText;
private UIText addressText;
Expand Down Expand Up @@ -72,8 +70,6 @@ public void onActivated(UIWidget button) {
// create new
serverInfo = new ServerInfo(name, address, port);
serverInfo.setOwner(owner);

config.getNetwork().add(serverInfo);
} else {
// update existing
serverInfo.setName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.terasology.rendering.nui.UIWidget;
import org.terasology.rendering.nui.WidgetUtil;
import org.terasology.rendering.nui.databinding.BindHelper;
import org.terasology.rendering.nui.databinding.DefaultBinding;
import org.terasology.rendering.nui.databinding.IntToStringBinding;
import org.terasology.rendering.nui.databinding.ReadOnlyBinding;
import org.terasology.rendering.nui.itemRendering.StringTextRenderer;
Expand Down Expand Up @@ -86,9 +85,10 @@ public class JoinGameScreen extends CoreScreenLayer {
private UIList<ServerInfo> visibleList;

private List<ServerInfo> listedServers = new ArrayList<>();
private List<ServerInfo> customServers = new ArrayList<>();

private boolean updateComplete = false;
private Predicate<ServerInfo> activeServersOnly = server -> server.isActive();

private boolean updateComplete;

@Override
public void initialise() {
Expand All @@ -99,12 +99,14 @@ public void initialise() {

UIList<ServerInfo> customServerList = find("customServerList", UIList.class);
if (customServerList != null) {
configureServerList(customServerList, customServers);
customServerList.setList(config.getNetwork().getServerInfos());
configureServerList(customServerList);
}

UIList<ServerInfo> onlineServerList = find("onlineServerList", UIList.class);
if (onlineServerList != null) {
configureServerList(onlineServerList, listedServers);
onlineServerList.setList(listedServers);
configureServerList(onlineServerList);
}

ActivateEventListener activateCustom = e -> {
Expand Down Expand Up @@ -154,13 +156,8 @@ public void update(float delta) {
updateComplete = true;
}

Predicate<ServerInfo> onlyActive = server -> server.isActive();

customServers.clear();
customServers.addAll(Collections2.filter(config.getNetwork().getServers(), onlyActive));

listedServers.clear();
listedServers.addAll(Collections2.filter(downloader.getServers(), onlyActive));
listedServers.addAll(Collections2.filter(downloader.getServers(), activeServersOnly));
}
}

Expand Down Expand Up @@ -200,9 +197,8 @@ public JoinStatus call() throws InterruptedException {

}

private void configureServerList(final UIList<ServerInfo> serverList, List<ServerInfo> servers) {
private void configureServerList(final UIList<ServerInfo> serverList) {

serverList.bindList(new DefaultBinding<List<ServerInfo>>(servers));
serverList.subscribe(new ItemActivateEventListener<ServerInfo>() {
@Override
public void onItemActivated(UIWidget widget, ServerInfo item) {
Expand Down Expand Up @@ -314,17 +310,29 @@ public void onActivated(UIWidget button) {

private void bindCustomButtons() {

UIList<?> customServerList = find("customServerList", UIList.class);
ReadOnlyBinding<Boolean> localSelectedServerOnly = new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return customServerList.getSelection() != null;
}
};

UIButton add = find("add", UIButton.class);
if (add != null) {
add.subscribe(button -> {
AddServerPopup popup = getManager().pushScreen(AddServerPopup.ASSET_URI, AddServerPopup.class);
// select the entry if added successfully
popup.onSuccess(item -> visibleList.setSelection(item));
popup.onSuccess(item -> {
config.getNetwork().addServerInfo(item);
visibleList.setSelection(item);
});
});
}

UIButton edit = find("edit", UIButton.class);
if (edit != null) {
edit.bindEnabled(localSelectedServerOnly);
edit.subscribe(button -> {
AddServerPopup popup = getManager().pushScreen(AddServerPopup.ASSET_URI, AddServerPopup.class);
ServerInfo info = visibleList.getSelection();
Expand All @@ -337,10 +345,11 @@ private void bindCustomButtons() {

UIButton removeButton = find("remove", UIButton.class);
if (removeButton != null) {
removeButton.bindEnabled(localSelectedServerOnly);
removeButton.subscribe(button -> {
ServerInfo info = visibleList.getSelection();
if (info != null) {
config.getNetwork().remove(info);
config.getNetwork().removeServerInfo(info);
extInfo.remove(info);
visibleList.setSelection(null);
}
Expand Down

0 comments on commit c07995b

Please sign in to comment.