use of org.terasology.rendering.nui.databinding.ReadOnlyBinding in project Terasology by MovingBlocks.
the class CreateGameScreen method initialise.
@Override
@SuppressWarnings("unchecked")
public void initialise() {
setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
UILabel gameTypeTitle = find("gameTypeTitle", UILabel.class);
if (gameTypeTitle != null) {
gameTypeTitle.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
if (loadingAsServer) {
return translationSystem.translate("${engine:menu#select-multiplayer-game-sub-title}");
} else {
return translationSystem.translate("${engine:menu#select-singleplayer-game-sub-title}");
}
}
});
}
final UIText worldName = find("worldName", UIText.class);
setGameName(worldName);
final UIText seed = find("seed", UIText.class);
if (seed != null) {
seed.setText(new FastRandom().nextString(32));
}
final UIDropdownScrollable<Module> gameplay = find("gameplay", UIDropdownScrollable.class);
gameplay.setOptions(getGameplayModules());
gameplay.setVisibleOptions(3);
gameplay.bindSelection(new Binding<Module>() {
Module selected;
@Override
public Module get() {
return selected;
}
@Override
public void set(Module value) {
setSelectedGameplayModule(value);
selected = value;
}
});
gameplay.setOptionRenderer(new StringTextRenderer<Module>() {
@Override
public String getString(Module value) {
return value.getMetadata().getDisplayName().value();
}
@Override
public void draw(Module value, Canvas canvas) {
canvas.getCurrentStyle().setTextColor(validateModuleDependencies(value.getId()) ? Color.WHITE : Color.RED);
super.draw(value, canvas);
canvas.getCurrentStyle().setTextColor(Color.WHITE);
}
});
UILabel gameplayDescription = find("gameplayDescription", UILabel.class);
gameplayDescription.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Module selectedModule = gameplay.getSelection();
if (selectedModule != null) {
return selectedModule.getMetadata().getDescription().value();
} else {
return "";
}
}
});
final UIDropdownScrollable<WorldGeneratorInfo> worldGenerator = find("worldGenerator", UIDropdownScrollable.class);
if (worldGenerator != null) {
worldGenerator.bindOptions(new ReadOnlyBinding<List<WorldGeneratorInfo>>() {
@Override
public List<WorldGeneratorInfo> get() {
// grab all the module names and their dependencies
// This grabs modules from `config.getDefaultModSelection()` which is updated in SelectModulesScreen
Set<Name> enabledModuleNames = getAllEnabledModuleNames().stream().collect(Collectors.toSet());
List<WorldGeneratorInfo> result = Lists.newArrayList();
for (WorldGeneratorInfo option : worldGeneratorManager.getWorldGenerators()) {
if (enabledModuleNames.contains(option.getUri().getModuleName())) {
result.add(option);
}
}
return result;
}
});
worldGenerator.setVisibleOptions(3);
worldGenerator.bindSelection(new Binding<WorldGeneratorInfo>() {
@Override
public WorldGeneratorInfo get() {
// get the default generator from the config. This is likely to have a user triggered selection.
WorldGeneratorInfo info = worldGeneratorManager.getWorldGeneratorInfo(config.getWorldGeneration().getDefaultGenerator());
if (info != null && getAllEnabledModuleNames().contains(info.getUri().getModuleName())) {
return info;
}
// just use the first available generator
for (WorldGeneratorInfo worldGenInfo : worldGeneratorManager.getWorldGenerators()) {
if (getAllEnabledModuleNames().contains(worldGenInfo.getUri().getModuleName())) {
set(worldGenInfo);
return worldGenInfo;
}
}
return null;
}
@Override
public void set(WorldGeneratorInfo value) {
if (value != null) {
config.getWorldGeneration().setDefaultGenerator(value.getUri());
}
}
});
worldGenerator.setOptionRenderer(new StringTextRenderer<WorldGeneratorInfo>() {
@Override
public String getString(WorldGeneratorInfo value) {
if (value != null) {
return value.getDisplayName();
}
return "";
}
});
final UIButton playButton = find("play", UIButton.class);
playButton.bindEnabled(new Binding<Boolean>() {
@Override
public Boolean get() {
return validateModuleDependencies(gameplay.getSelection().getId());
}
@Override
public void set(Boolean value) {
playButton.setEnabled(value);
}
});
}
WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
WidgetUtil.trySubscribe(this, "play", button -> {
if (worldGenerator.getSelection() == null) {
MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
if (errorMessagePopup != null) {
errorMessagePopup.setMessage("No World Generator Selected", "Select a world generator (you may need to activate a mod with a generator first).");
}
} else {
GameManifest gameManifest = new GameManifest();
gameManifest.setTitle(worldName.getText());
gameManifest.setSeed(seed.getText());
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
if (!result.isSuccess()) {
MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
if (errorMessagePopup != null) {
errorMessagePopup.setMessage("Invalid Module Selection", "Please review your module seleciton and try again");
}
return;
}
for (Module module : result.getModules()) {
gameManifest.addModule(module.getId(), module.getVersion());
}
// Time at dawn + little offset to spawn in a brighter env.
float timeOffset = 0.25f + 0.025f;
WorldInfo worldInfo = new WorldInfo(TerasologyConstants.MAIN_WORLD, gameManifest.getSeed(), (long) (WorldTime.DAY_LENGTH * timeOffset), worldGenerator.getSelection().getUri());
gameManifest.addWorld(worldInfo);
gameEngine.changeState(new StateLoading(gameManifest, (loadingAsServer) ? NetworkMode.DEDICATED_SERVER : NetworkMode.NONE));
}
});
UIButton previewSeed = find("previewSeed", UIButton.class);
ReadOnlyBinding<Boolean> worldGeneratorSelected = new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return worldGenerator != null && worldGenerator.getSelection() != null;
}
};
previewSeed.bindEnabled(worldGeneratorSelected);
PreviewWorldScreen screen = getManager().createScreen(PreviewWorldScreen.ASSET_URI, PreviewWorldScreen.class);
WidgetUtil.trySubscribe(this, "previewSeed", button -> {
if (screen != null) {
screen.bindSeed(BindHelper.bindBeanProperty("text", seed, String.class));
try {
screen.setEnvironment();
triggerForwardAnimation(screen);
} catch (Exception e) {
String msg = "Unable to load world for a 2D preview:\n" + e.toString();
getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", msg);
logger.error("Unable to load world for a 2D preview", e);
}
}
});
WidgetUtil.trySubscribe(this, "mods", w -> triggerForwardAnimation(SelectModulesScreen.ASSET_URI));
}
use of org.terasology.rendering.nui.databinding.ReadOnlyBinding in project Terasology by MovingBlocks.
the class SelectModulesScreen method initialise.
@Override
public void initialise() {
setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
remoteModuleRegistryUpdater = Executors.newSingleThreadExecutor().submit(moduleManager.getInstallManager().updateRemoteRegistry());
selectModulesConfig = config.getSelectModulesConfig();
resolver = new DependencyResolver(moduleManager.getRegistry());
modulesLookup = Maps.newHashMap();
sortedModules = Lists.newArrayList();
for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
Module latestVersion = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
if (!latestVersion.isOnClasspath()) {
ModuleSelectionInfo info = ModuleSelectionInfo.local(latestVersion);
modulesLookup.put(info.getMetadata().getId(), info);
sortedModules.add(info);
}
}
Collections.sort(sortedModules, moduleInfoComparator);
allSortedModules = new ArrayList<>(sortedModules);
final UIList<ModuleSelectionInfo> moduleList = find("moduleList", UIList.class);
if (moduleList != null) {
moduleList.setList(sortedModules);
moduleList.setItemRenderer(new AbstractItemRenderer<ModuleSelectionInfo>() {
public String getString(ModuleSelectionInfo value) {
return value.getMetadata().getDisplayName().toString();
}
@Override
public void draw(ModuleSelectionInfo value, Canvas canvas) {
if (isSelectedGameplayModule(value) && value.isValidToSelect()) {
canvas.setMode("gameplay");
} else if (value.isSelected() && value.isExplicitSelection()) {
canvas.setMode("enabled");
} else if (value.isSelected()) {
canvas.setMode("dependency");
} else if (!value.isPresent()) {
canvas.setMode("disabled");
} else if (!value.isValidToSelect()) {
canvas.setMode("invalid");
} else {
canvas.setMode("available");
}
canvas.drawText(getString(value), canvas.getRegion());
}
@Override
public Vector2i getPreferredSize(ModuleSelectionInfo value, Canvas canvas) {
String text = getString(value);
return new Vector2i(canvas.getCurrentStyle().getFont().getWidth(text), canvas.getCurrentStyle().getFont().getLineHeight());
}
});
// ItemActivateEventListener is triggered by double clicking
moduleList.subscribe((widget, item) -> {
if (item.isSelected() && moduleList.getSelection().isExplicitSelection()) {
deselect(item);
} else if (item.isValidToSelect()) {
select(item);
}
});
ResettableUIText moduleSearch = find("moduleSearch", ResettableUIText.class);
if (moduleSearch != null) {
moduleSearch.subscribe(new TextChangeEventListener() {
@Override
public void onTextChange(String oldText, String newText) {
filterText(newText);
}
});
}
final Binding<ModuleMetadata> moduleInfoBinding = new ReadOnlyBinding<ModuleMetadata>() {
@Override
public ModuleMetadata get() {
if (moduleList.getSelection() != null) {
return moduleList.getSelection().getMetadata();
}
return null;
}
};
UILabel name = find("name", UILabel.class);
if (name != null) {
name.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
if (moduleInfoBinding.get() != null) {
return moduleInfoBinding.get().getDisplayName().toString();
}
return "";
}
});
}
UILabel installedVersion = find("installedVersion", UILabel.class);
if (installedVersion != null) {
installedVersion.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo sel = moduleList.getSelection();
if (sel == null) {
return "";
}
return sel.isPresent() ? sel.getMetadata().getVersion().toString() : translationSystem.translate("${engine:menu#module-version-installed-none}");
}
});
}
UILabel onlineVersion = find("onlineVersion", UILabel.class);
if (onlineVersion != null) {
onlineVersion.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo sel = moduleList.getSelection();
if (sel == null) {
return "";
}
return (sel.getOnlineVersion() != null) ? sel.getOnlineVersion().getVersion().toString() : "none";
}
});
}
UILabel description = find("description", UILabel.class);
if (description != null) {
description.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleMetadata moduleMetadata = moduleInfoBinding.get();
if (moduleMetadata != null) {
String dependenciesNames;
List<DependencyInfo> dependencies = moduleMetadata.getDependencies();
if (dependencies != null && dependencies.size() > 0) {
dependenciesNames = translationSystem.translate("${engine:menu#module-dependencies-exist}") + ":" + '\n';
for (DependencyInfo dependency : dependencies) {
dependenciesNames += " " + dependency.getId().toString() + '\n';
}
} else {
dependenciesNames = translationSystem.translate("${engine:menu#module-dependencies-empty}") + ".";
}
return moduleMetadata.getDescription().toString() + '\n' + '\n' + dependenciesNames;
}
return "";
}
});
}
UILabel status = find("status", UILabel.class);
if (status != null) {
status.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo info = moduleList.getSelection();
if (info != null) {
if (isSelectedGameplayModule(info)) {
return translationSystem.translate("${engine:menu#module-status-activegameplay}");
} else if (info.isSelected() && info.isExplicitSelection()) {
return translationSystem.translate("${engine:menu#module-status-activated}");
} else if (info.isSelected()) {
return translationSystem.translate("${engine:menu#module-status-dependency}");
} else if (!info.isPresent()) {
return translationSystem.translate("${engine:menu#module-status-notpresent}");
} else if (info.isValidToSelect()) {
return translationSystem.translate("${engine:menu#module-status-available}");
} else {
return translationSystem.translate("${engine:menu#module-status-error}");
}
}
return "";
}
});
}
UIButton toggleActivate = find("toggleActivation", UIButton.class);
if (toggleActivate != null) {
toggleActivate.subscribe(button -> {
ModuleSelectionInfo info = moduleList.getSelection();
if (info != null) {
// Toggle
if (info.isSelected() && info.isExplicitSelection()) {
deselect(info);
} else if (info.isValidToSelect()) {
select(info);
}
}
});
toggleActivate.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
ModuleSelectionInfo info = moduleList.getSelection();
return info != null && info.isPresent() && !isSelectedGameplayModule(info) && (info.isSelected() || info.isValidToSelect());
}
});
toggleActivate.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
if (moduleList.getSelection() != null) {
if (moduleList.getSelection().isExplicitSelection()) {
return translationSystem.translate("${engine:menu#deactivate-module}");
} else {
return translationSystem.translate("${engine:menu#activate-module}");
}
}
// button should be disabled
return translationSystem.translate("${engine:menu#activate-module}");
}
});
}
UIButton downloadButton = find("download", UIButton.class);
if (downloadButton != null) {
downloadButton.subscribe(button -> {
if (moduleList.getSelection() != null) {
ModuleSelectionInfo info = moduleList.getSelection();
startDownloadingNewestModulesRequiredFor(info);
}
});
downloadButton.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
try {
return moduleList.getSelection().getOnlineVersion() != null;
} catch (NullPointerException e) {
return false;
}
}
});
downloadButton.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo info = moduleList.getSelection();
if (info != null && !info.isPresent()) {
return translationSystem.translate("${engine:menu#download-module}");
} else {
return translationSystem.translate("${engine:menu#update-module}");
}
}
});
}
UIButton disableAll = find("disableAll", UIButton.class);
if (disableAll != null) {
disableAll.subscribe(button -> sortedModules.stream().filter(info -> info.isSelected() && info.isExplicitSelection()).forEach(this::deselect));
}
localOnlyCheckbox = find("localOnly", UICheckbox.class);
localOnlyCheckbox.bindChecked(new Binding<Boolean>() {
@Override
public Boolean get() {
filterText(moduleSearch.getText());
prepareModuleList(selectModulesConfig.isChecked());
return selectModulesConfig.isChecked();
}
@Override
public void set(Boolean value) {
selectModulesConfig.setIsChecked(value);
filterText(moduleSearch.getText());
prepareModuleList(value);
}
});
}
WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
}
use of org.terasology.rendering.nui.databinding.ReadOnlyBinding in project Terasology by MovingBlocks.
the class JoinGameScreen method bindCustomButtons.
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 -> {
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();
popup.setServerInfo(info);
// editing invalidates the currently known info, so query it again
popup.onSuccess(item -> extInfo.put(item, infoService.requestInfo(item.getAddress(), item.getPort())));
});
}
UIButton removeButton = find("remove", UIButton.class);
if (removeButton != null) {
removeButton.bindEnabled(localSelectedServerOnly);
removeButton.subscribe(button -> {
ServerInfo info = visibleList.getSelection();
if (info != null) {
config.getNetwork().removeServerInfo(info);
extInfo.remove(info);
visibleList.setSelection(null);
}
});
}
UILabel downloadLabel = find("download", UILabel.class);
if (downloadLabel != null) {
downloadLabel.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
return translationSystem.translate(downloader.getStatus());
}
});
}
}
use of org.terasology.rendering.nui.databinding.ReadOnlyBinding in project Terasology by MovingBlocks.
the class JoinGameScreen method bindInfoLabels.
private void bindInfoLabels() {
final ReadOnlyBinding<ServerInfo> infoBinding = new ReadOnlyBinding<ServerInfo>() {
@Override
public ServerInfo get() {
return visibleList.getSelection();
}
};
UILabel name = find("name", UILabel.class);
if (name != null) {
name.bindText(BindHelper.bindBoundBeanProperty("name", infoBinding, ServerInfo.class, String.class));
}
UILabel owner = find("owner", UILabel.class);
if (owner != null) {
owner.bindText(BindHelper.bindBoundBeanProperty("owner", infoBinding, ServerInfo.class, String.class));
}
UILabel address = find("address", UILabel.class);
if (address != null) {
address.bindText(BindHelper.bindBoundBeanProperty("address", infoBinding, ServerInfo.class, String.class));
}
UILabel port = find("port", UILabel.class);
if (port != null) {
port.bindText(new IntToStringBinding(BindHelper.bindBoundBeanProperty("port", infoBinding, ServerInfo.class, int.class)));
}
UILabel onlinePlayers = find("onlinePlayers", UILabel.class);
onlinePlayers.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Future<ServerInfoMessage> info = extInfo.get(visibleList.getSelection());
if (info != null) {
if (info.isDone()) {
return getOnlinePlayersText(info);
} else {
return translationSystem.translate("${engine:menu#join-server-requested}");
}
}
return null;
}
});
UILabel modules = find("modules", UILabel.class);
modules.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Future<ServerInfoMessage> info = extInfo.get(visibleList.getSelection());
if (info != null) {
if (info.isDone()) {
return getModulesText(info);
} else {
return translationSystem.translate("${engine:menu#join-server-requested}");
}
}
return null;
}
});
UILabel worlds = find("worlds", UILabel.class);
worlds.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Future<ServerInfoMessage> info = extInfo.get(visibleList.getSelection());
if (info != null) {
if (info.isDone()) {
return getWorldText(info);
} else {
return translationSystem.translate("${engine:menu#join-server-requested}");
}
}
return null;
}
});
UIButton joinButton = find("join", UIButton.class);
if (joinButton != null) {
joinButton.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return infoBinding.get() != null;
}
});
joinButton.subscribe(button -> {
config.save();
ServerInfo item = infoBinding.get();
if (item != null) {
join(item.getAddress(), item.getPort());
}
});
}
UIButton refreshButton = find("refresh", UIButton.class);
if (refreshButton != null) {
refreshButton.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return visibleList.getSelection() != null;
}
});
refreshButton.subscribe(button -> {
refresh();
});
}
}
use of org.terasology.rendering.nui.databinding.ReadOnlyBinding in project Terasology by MovingBlocks.
the class BehaviorEditorScreen method initialise.
@Override
public void initialise() {
entityProperties = find("entity_properties", PropertyLayout.class);
behaviorEditor = find("tree", BehaviorEditor.class);
properties = find("properties", PropertyLayout.class);
selectTree = find("select_tree", UIDropdown.class);
selectEntity = find("select_entity", UIDropdown.class);
palette = find("palette", UIList.class);
behaviorEditor.initialize(context);
behaviorEditor.bindSelection(new Binding<RenderableNode>() {
private PropertyProvider provider = new PropertyProvider();
@Override
public RenderableNode get() {
return selectedNode;
}
@Override
public void set(RenderableNode value) {
onNodeSelected(value, provider);
}
});
Binding<List<BehaviorTree>> treeBinding = new ReadOnlyBinding<List<BehaviorTree>>() {
@Override
public List<BehaviorTree> get() {
return behaviorSystem.getTrees();
}
};
selectTree.bindOptions(treeBinding);
providerFactory.register("behaviorTrees", treeBinding);
selectTree.bindSelection(new Binding<BehaviorTree>() {
@Override
public BehaviorTree get() {
return behaviorEditor.getTree();
}
@Override
public void set(BehaviorTree value) {
onTreeSelected(value);
}
});
selectEntity.bindOptions(new ReadOnlyBinding<List<Interpreter>>() {
@Override
public List<Interpreter> get() {
return behaviorSystem.getInterpreters();
}
});
selectEntity.bindSelection(new Binding<Interpreter>() {
private PropertyProvider provider = new PropertyProvider();
@Override
public Interpreter get() {
return selectedInterpreter;
}
@Override
public void set(Interpreter value) {
onEntitySelected(value, provider);
}
});
palette.bindSelection(new Binding<BehaviorNodeComponent>() {
@Override
public BehaviorNodeComponent get() {
return null;
}
@Override
public void set(BehaviorNodeComponent value) {
onPaletteSelected(value);
}
});
palette.bindList(new ReadOnlyBinding<List<BehaviorNodeComponent>>() {
@Override
public List<BehaviorNodeComponent> get() {
return paletteItems;
}
});
palette.setItemRenderer(new ToStringTextRenderer<BehaviorNodeComponent>() {
@Override
public String getTooltip(BehaviorNodeComponent value) {
return value.description;
}
});
WidgetUtil.trySubscribe(this, "copy", button -> onCopyPressed());
WidgetUtil.trySubscribe(this, "layout", button -> onLayoutPressed());
WidgetUtil.trySubscribe(this, "new", button -> onNewPressed());
WidgetUtil.trySubscribe(this, "assign", button -> onAssignPressed());
WidgetUtil.trySubscribe(this, "remove", button -> onRemovePressed());
WidgetUtil.trySubscribe(this, "debug_run", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.run();
}
});
WidgetUtil.trySubscribe(this, "debug_pause", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.pause();
}
});
WidgetUtil.trySubscribe(this, "debug_reset", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.reset();
}
});
WidgetUtil.trySubscribe(this, "debug_step", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.tick(0.1f);
}
});
paletteItems = findPaletteItems();
}
Aggregations