mirror of
https://github.com/badvision/lawless-legends.git
synced 2024-08-25 12:28:55 +00:00
Global variables can be created and used in scripts now
This commit is contained in:
parent
cf55e06347
commit
bc0e0b1217
@ -25,6 +25,7 @@ import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.badvision.outlaweditor.data.xml.Block;
|
||||
import org.badvision.outlaweditor.data.xml.Global;
|
||||
import org.badvision.outlaweditor.data.xml.Mutation;
|
||||
import org.badvision.outlaweditor.data.xml.Scope;
|
||||
import org.badvision.outlaweditor.data.xml.Script;
|
||||
@ -143,15 +144,16 @@ public class MythosEditor {
|
||||
}
|
||||
|
||||
public List<UserType> getUserTypes() {
|
||||
if (Application.gameData.getGlobal().getUserTypes() == null) {
|
||||
Global global = (Global) getGlobalScope();
|
||||
if (global.getUserTypes() == null) {
|
||||
return new ArrayList<>();
|
||||
} else {
|
||||
return Application.gameData.getGlobal().getUserTypes().getUserType();
|
||||
return global.getUserTypes().getUserType();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Script> getGlobalFunctions() {
|
||||
return getFunctions(Application.gameData.getGlobal());
|
||||
return getFunctions(getGlobalScope());
|
||||
}
|
||||
public List<Script> getLocalFunctions() {
|
||||
return getFunctions(scope);
|
||||
@ -169,7 +171,15 @@ public class MythosEditor {
|
||||
}
|
||||
|
||||
public List<Variable> getGlobalVariables() {
|
||||
return getVariables(Application.gameData.getGlobal());
|
||||
return getVariables(getGlobalScope());
|
||||
}
|
||||
|
||||
public static Scope getGlobalScope() {
|
||||
return Application.gameData.getGlobal();
|
||||
}
|
||||
|
||||
private boolean isGlobalScope() {
|
||||
return scope.equals(getGlobalScope());
|
||||
}
|
||||
|
||||
public List<Variable> getLocalVariables() {
|
||||
@ -185,7 +195,10 @@ public class MythosEditor {
|
||||
}
|
||||
|
||||
public List<Variable> getVariablesByType(String type) {
|
||||
Stream<Variable> allGlobals = getGlobalVariables().stream();
|
||||
Stream<Variable> allGlobals = Stream.empty();
|
||||
if (!isGlobalScope()) {
|
||||
allGlobals = getGlobalVariables().stream();
|
||||
}
|
||||
Stream<Variable> allLocals = getLocalVariables().stream();
|
||||
return Stream.concat(allGlobals, allLocals).filter(
|
||||
(Variable v) -> {
|
||||
|
@ -3,6 +3,7 @@ package org.badvision.outlaweditor.data;
|
||||
import org.badvision.outlaweditor.Application;
|
||||
import org.badvision.outlaweditor.data.xml.Global;
|
||||
import org.badvision.outlaweditor.data.xml.Scripts;
|
||||
import org.badvision.outlaweditor.data.xml.Variables;
|
||||
|
||||
public class DataUtilities {
|
||||
public static void sortScripts(Scripts s) {
|
||||
@ -24,4 +25,18 @@ public class DataUtilities {
|
||||
Application.gameData.setGlobal(new Global());
|
||||
}
|
||||
}
|
||||
|
||||
public static void sortVariables(Variables vars) {
|
||||
if (vars == null || vars.getVariable()== null) {
|
||||
return;
|
||||
}
|
||||
vars.getVariable().sort((a, b) -> {
|
||||
if (a.getName().equalsIgnoreCase("init")) {
|
||||
return -1;
|
||||
} else if (b.getName().equalsIgnoreCase("init")) {
|
||||
return 1;
|
||||
}
|
||||
return a.getName().compareTo(b.getName());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import javafx.fxml.FXML;
|
||||
import javafx.scene.control.ListView;
|
||||
import org.badvision.outlaweditor.GlobalEditor;
|
||||
import org.badvision.outlaweditor.data.xml.Script;
|
||||
import org.badvision.outlaweditor.data.xml.UserType;
|
||||
import org.badvision.outlaweditor.data.xml.Variable;
|
||||
|
||||
public abstract class GlobalEditorTabController {
|
||||
private final GlobalEditor currentEditor = new GlobalEditor();
|
||||
@ -15,9 +17,9 @@ public abstract class GlobalEditorTabController {
|
||||
@FXML
|
||||
protected ListView<Script> globalScriptList;
|
||||
@FXML
|
||||
protected ListView<?> dataTypeList;
|
||||
protected ListView<UserType> dataTypeList;
|
||||
@FXML
|
||||
protected ListView<?> variableList;
|
||||
protected ListView<Variable> variableList;
|
||||
|
||||
@FXML
|
||||
abstract protected void onScriptAddPressed(ActionEvent event);
|
||||
@ -39,7 +41,15 @@ public abstract class GlobalEditorTabController {
|
||||
abstract protected void onVariableClonePressed(ActionEvent event);
|
||||
|
||||
abstract public void redrawGlobalScripts();
|
||||
|
||||
abstract public void redrawGlobalVariables();
|
||||
|
||||
abstract public void redrawGlobalDataTypes();
|
||||
|
||||
public void initialize() {
|
||||
redrawGlobalScripts();
|
||||
redrawGlobalVariables();
|
||||
redrawGlobalDataTypes();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.badvision.outlaweditor.ui;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javafx.application.Platform;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.ButtonType;
|
||||
import javafx.scene.control.Control;
|
||||
import javafx.scene.control.Dialog;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author blurry
|
||||
*/
|
||||
public class ModalEditor {
|
||||
|
||||
public static interface EditControl<V> {
|
||||
|
||||
public Control getControl();
|
||||
|
||||
public V getValue();
|
||||
|
||||
public void setValue(V value);
|
||||
}
|
||||
|
||||
public static class TextControl implements EditControl<String> {
|
||||
|
||||
TextField control = new TextField();
|
||||
|
||||
@Override
|
||||
public Control getControl() {
|
||||
return control;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return control.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String value) {
|
||||
control.setText(value);
|
||||
}
|
||||
}
|
||||
|
||||
public PropertyDescriptor getPropertyDescriptor(PropertyDescriptor[] descriptors, String propertyName) {
|
||||
for (PropertyDescriptor descriptor : descriptors) {
|
||||
if (descriptor.getName().equalsIgnoreCase(propertyName)) {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> Optional<T> editObject(T sourceObject, Map<String, EditControl> obj, Class<T> clazz, String title, String header) throws IntrospectionException {
|
||||
BeanInfo info = Introspector.getBeanInfo(clazz);
|
||||
|
||||
Dialog dialog = new Dialog();
|
||||
dialog.setTitle(title);
|
||||
dialog.setHeaderText(header);
|
||||
|
||||
// dialog.setGraphic(...);
|
||||
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
|
||||
|
||||
GridPane grid = new GridPane();
|
||||
grid.setHgap(10);
|
||||
grid.setVgap(10);
|
||||
grid.setPadding(new Insets(20, 150, 10, 10));
|
||||
|
||||
final AtomicInteger row = new AtomicInteger(0);
|
||||
obj.forEach((String property, EditControl control) -> {
|
||||
PropertyDescriptor descriptor = getPropertyDescriptor(info.getPropertyDescriptors(), property);
|
||||
if (row.get() == 0) {
|
||||
Platform.runLater(() -> control.getControl().requestFocus());
|
||||
}
|
||||
grid.add(new Label(uppercaseFirst(descriptor.getDisplayName())), 0, row.get());
|
||||
grid.add(control.getControl(), 1, row.getAndAdd(1));
|
||||
try {
|
||||
control.setValue(descriptor.getReadMethod().invoke(sourceObject));
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
||||
Logger.getLogger(ModalEditor.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
|
||||
dialog.getDialogPane().setContent(grid);
|
||||
|
||||
dialog.setResultConverter(dialogButton -> {
|
||||
if (dialogButton == ButtonType.OK) {
|
||||
obj.forEach((String property, EditControl control) -> {
|
||||
PropertyDescriptor descriptor = getPropertyDescriptor(info.getPropertyDescriptors(), property);
|
||||
try {
|
||||
descriptor.getWriteMethod().invoke(sourceObject, control.getValue());
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
||||
Logger.getLogger(ModalEditor.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
return sourceObject;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
return dialog.showAndWait();
|
||||
}
|
||||
|
||||
private String uppercaseFirst(String str) {
|
||||
StringBuilder b = new StringBuilder(str);
|
||||
int i = 0;
|
||||
do {
|
||||
b.replace(i, i + 1, b.substring(i, i + 1).toUpperCase());
|
||||
i = b.indexOf(" ", i) + 1;
|
||||
} while (i > 0 && i < b.length());
|
||||
return b.toString();
|
||||
}
|
||||
}
|
@ -4,10 +4,14 @@
|
||||
*/
|
||||
package org.badvision.outlaweditor.ui;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
@ -56,9 +60,12 @@ import org.badvision.outlaweditor.data.DataUtilities;
|
||||
import org.badvision.outlaweditor.data.TileUtils;
|
||||
import org.badvision.outlaweditor.data.TilesetUtils;
|
||||
import org.badvision.outlaweditor.data.xml.GameData;
|
||||
import org.badvision.outlaweditor.data.xml.Global;
|
||||
import org.badvision.outlaweditor.data.xml.Scope;
|
||||
import org.badvision.outlaweditor.data.xml.Script;
|
||||
import org.badvision.outlaweditor.data.xml.Tile;
|
||||
import org.badvision.outlaweditor.data.xml.Variable;
|
||||
import org.badvision.outlaweditor.data.xml.Variables;
|
||||
import org.badvision.outlaweditor.ui.impl.ImageConversionWizardController;
|
||||
|
||||
/**
|
||||
@ -241,6 +248,39 @@ public class UIAction {
|
||||
return script;
|
||||
}
|
||||
|
||||
public static void createAndEditVariable(Scope scope) throws IntrospectionException {
|
||||
Variable newVariable = new Variable();
|
||||
newVariable.setName("changeme");
|
||||
newVariable.setType("String");
|
||||
newVariable.setComment("");
|
||||
Optional<Variable> var = editAndGetVariable(newVariable);
|
||||
if (var.isPresent()) {
|
||||
if (scope.getVariables() == null) {
|
||||
scope.setVariables(new Variables());
|
||||
}
|
||||
scope.getVariables().getVariable().add(var.get());
|
||||
}
|
||||
}
|
||||
|
||||
public static void editVariable(Variable var, Global global) {
|
||||
try {
|
||||
editAndGetVariable(var);
|
||||
} catch (IntrospectionException ex) {
|
||||
Logger.getLogger(UIAction.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<Variable> editAndGetVariable(Variable v) throws IntrospectionException {
|
||||
ModalEditor editor = new ModalEditor();
|
||||
Map<String, ModalEditor.EditControl> controls = new LinkedHashMap<>();
|
||||
|
||||
controls.put("name", new ModalEditor.TextControl());
|
||||
controls.put("type", new ModalEditor.TextControl());
|
||||
controls.put("comment", new ModalEditor.TextControl());
|
||||
|
||||
return editor.editObject(v, controls, Variable.class, "Variable", "Edit and press OK, or Cancel to abort");
|
||||
}
|
||||
|
||||
public static ImageConversionWizardController openImageConversionModal(Image image, ImageDitherEngine ditherEngine, int targetWidth, int targetHeight, ImageConversionPostAction postAction) {
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(UIAction.class.getResource("/imageConversionWizard.fxml"));
|
||||
try {
|
||||
|
@ -1,10 +1,13 @@
|
||||
package org.badvision.outlaweditor.ui.impl;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.ListCell;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.Tooltip;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.FontWeight;
|
||||
import javafx.util.Callback;
|
||||
@ -13,12 +16,19 @@ import org.badvision.outlaweditor.Application;
|
||||
import org.badvision.outlaweditor.TransferHelper;
|
||||
import org.badvision.outlaweditor.data.DataUtilities;
|
||||
import org.badvision.outlaweditor.data.xml.Script;
|
||||
import org.badvision.outlaweditor.data.xml.Variable;
|
||||
import org.badvision.outlaweditor.ui.GlobalEditorTabController;
|
||||
import org.badvision.outlaweditor.ui.UIAction;
|
||||
import static org.badvision.outlaweditor.ui.UIAction.editScript;
|
||||
|
||||
public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
super.initialize();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onScriptAddPressed(ActionEvent event) {
|
||||
UIAction.createAndEditScript(Application.gameData.getGlobal());
|
||||
@ -72,14 +82,51 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
|
||||
|
||||
@Override
|
||||
protected void onVariableAddPressed(ActionEvent event) {
|
||||
try {
|
||||
UIAction.createAndEditVariable(Application.gameData.getGlobal());
|
||||
redrawGlobalVariables();
|
||||
} catch (IntrospectionException ex) {
|
||||
Logger.getLogger(GlobalEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onVariableDeletePressed(ActionEvent event) {
|
||||
Variable var = variableList.getSelectionModel().getSelectedItem();
|
||||
if (var != null) {
|
||||
UIAction.confirm(
|
||||
"Are you sure you want to delete the variable "
|
||||
+ var.getName()
|
||||
+ "? There is no undo for this!",
|
||||
() -> {
|
||||
Application.gameData.getGlobal().getVariables().getVariable().remove(var);
|
||||
redrawGlobalVariables();
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onVariableClonePressed(ActionEvent event) {
|
||||
Variable source = variableList.getSelectionModel().getSelectedItem();
|
||||
if (source == null) {
|
||||
String message = "First select a variable and then press Clone";
|
||||
UIAction.alert(message);
|
||||
} else {
|
||||
try {
|
||||
Variable variable = TransferHelper.cloneObject(source, Variable.class, "variable");
|
||||
variable.setName(source.getName() + " CLONE");
|
||||
Optional<Variable> newVar = UIAction.editAndGetVariable(variable);
|
||||
if (newVar.isPresent()) {
|
||||
Application.gameData.getGlobal().getVariables().getVariable().add(newVar.get());
|
||||
redrawGlobalVariables();
|
||||
}
|
||||
} catch (JAXBException ex) {
|
||||
Logger.getLogger(MapEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||||
UIAction.alert("Error occured when attempting clone operation:\n" + ex.getMessage());
|
||||
} catch (IntrospectionException ex) {
|
||||
Logger.getLogger(GlobalEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -99,17 +146,8 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
|
||||
if (empty || item == null) {
|
||||
setText("");
|
||||
} else {
|
||||
// ImageView visibleIcon = getVisibleIcon(item);
|
||||
// visibleIcon.setOnMouseClicked((e) -> {
|
||||
// toggleVisibility(visibleIcon, item);
|
||||
// mapScriptsList.getSelectionModel().clearSelection();
|
||||
// });
|
||||
// setGraphic(visibleIcon);
|
||||
// getCurrentEditor().getCurrentMap().getScriptColor(item).ifPresent(this::setTextFill);
|
||||
setText(item.getName());
|
||||
setFont(Font.font(null, FontWeight.BOLD, 12.0));
|
||||
// scriptDragDrop.registerDragSupport(this, item);
|
||||
// visibleIcon.setMouseTransparent(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -124,4 +162,44 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redrawGlobalVariables() {
|
||||
DataUtilities.ensureGlobalExists();
|
||||
variableList.setOnEditStart((ListView.EditEvent<Variable> event) -> {
|
||||
UIAction.editVariable(event.getSource().getItems().get(event.getIndex()), Application.gameData.getGlobal());
|
||||
});
|
||||
variableList.setCellFactory(new Callback<ListView<Variable>, ListCell<Variable>>() {
|
||||
@Override
|
||||
public ListCell<Variable> call(ListView<Variable> param) {
|
||||
final ListCell<Variable> cell = new ListCell<Variable>() {
|
||||
@Override
|
||||
protected void updateItem(Variable item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
if (empty || item == null) {
|
||||
setText("");
|
||||
} else {
|
||||
setText(item.getName());
|
||||
if (item.getComment() != null && !(item.getComment().isEmpty())) {
|
||||
setTooltip(new Tooltip(item.getComment()));
|
||||
}
|
||||
setFont(Font.font(null, FontWeight.BOLD, 12.0));
|
||||
}
|
||||
}
|
||||
};
|
||||
return cell;
|
||||
}
|
||||
});
|
||||
if (variableList.getItems() != null && Application.gameData.getGlobal().getVariables()!= null) {
|
||||
DataUtilities.sortVariables(Application.gameData.getGlobal().getVariables());
|
||||
variableList.getItems().setAll(Application.gameData.getGlobal().getVariables().getVariable());
|
||||
} else {
|
||||
variableList.getItems().clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redrawGlobalDataTypes() {
|
||||
DataUtilities.ensureGlobalExists();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -183,13 +183,19 @@ if (typeof Mythos === "undefined") {
|
||||
Blockly.Variables.allVariables = function (workspace) {
|
||||
var list = Blockly.Variables.allVariables_old(workspace);
|
||||
Mythos.each(Mythos.editor.getVariablesByType("String"), function (variable) {
|
||||
if (list.indexOf(variable.getName()) < 0) {
|
||||
list.push(variable.getName());
|
||||
}
|
||||
});
|
||||
Mythos.each(Mythos.editor.getVariablesByType("Number"), function (variable) {
|
||||
if (list.indexOf(variable.getName()) < 0) {
|
||||
list.push(variable.getName());
|
||||
}
|
||||
});
|
||||
Mythos.each(Mythos.editor.getVariablesByType("Boolean"), function (variable) {
|
||||
if (list.indexOf(variable.getName()) < 0) {
|
||||
list.push(variable.getName());
|
||||
}
|
||||
});
|
||||
return list;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user