This commit is contained in:
Martin Haye 2015-06-28 09:27:02 -07:00
commit 7cf8cbeb0e
8 changed files with 318 additions and 70 deletions

View File

@ -9,6 +9,8 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
@ -23,7 +25,11 @@ 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.Mutation;
import org.badvision.outlaweditor.data.xml.Scope;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.UserType;
import org.badvision.outlaweditor.data.xml.Variable;
import org.badvision.outlaweditor.ui.ApplicationUIController;
import org.badvision.outlaweditor.ui.MythosScriptEditorController;
import org.w3c.dom.Document;
@ -36,13 +42,15 @@ import org.xml.sax.SAXException;
*/
public class MythosEditor {
Scope scope;
Script script;
Stage primaryStage;
MythosScriptEditorController controller;
public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
public MythosEditor(Script theScript) {
public MythosEditor(Script theScript, Scope theScope) {
script = theScript;
scope = theScope;
}
public void show() {
@ -104,7 +112,6 @@ public class MythosEditor {
xml = xml.replaceAll("'", "&apos;");
xml = xml.replace("?>", "?><xml>");
xml += "</xml>";
System.out.println("xml: " + xml);
return generateLoadScript(xml);
} catch (JAXBException ex) {
Logger.getLogger(MythosEditor.class.getName()).log(Level.SEVERE, null, ex);
@ -134,8 +141,73 @@ public class MythosEditor {
script.setName(name);
ApplicationUIController.getController().redrawScripts();
}
public List<UserType> getUserTypes() {
if (Application.gameData.getGlobal().getUserTypes() == null) {
return new ArrayList<>();
} else {
return Application.gameData.getGlobal().getUserTypes().getUserType();
}
}
public List<Script> getGlobalFunctions() {
return getFunctions(Application.gameData.getGlobal());
}
public List<Script> getLocalFunctions() {
return getFunctions(scope);
}
private List<Script> getFunctions(Scope scriptScope) {
if (scriptScope.getScripts() == null) {
return new ArrayList<>();
} else {
List<Script> scripts = scriptScope.getScripts().getScript();
List<Script> filteredList = scripts.stream().filter((Script s) -> {
return s.getName() != null;
}).collect(Collectors.toList());
return filteredList;
}
}
public List<Variable> getGlobalVariables() {
return getVariables(Application.gameData.getGlobal());
}
public List<Variable> getLocalVariables() {
return getVariables(scope);
}
public List getUserTypes() {
return new ArrayList();
private List<Variable> getVariables(Scope scriptScope) {
if (scriptScope.getVariables() == null) {
return new ArrayList<>();
} else {
return scriptScope.getVariables().getVariable();
}
}
public List<Variable> getVariablesByType(String type) {
Stream<Variable> allGlobals = getGlobalVariables().stream();
Stream<Variable> allLocals = getLocalVariables().stream();
return Stream.concat(allGlobals, allLocals).filter(
(Variable v) -> {
return v.getType().equals(type);
}).collect(Collectors.toList());
}
public List<String> getParametersForScript(Script script) {
List<String> allArgs = new ArrayList();
if (script.getBlock() != null && script.getBlock().getFieldOrMutationOrStatement() != null) {
script.getBlock().getFieldOrMutationOrStatement()
.stream().filter((o) -> (o instanceof Mutation))
.map((o) -> (Mutation) o).findFirst().ifPresent((m) -> {
m.getArg().stream().forEach((a) -> {
allArgs.add(a.getName());
});
});
}
return allArgs;
}
public void log(String message) {
System.out.println(message);
}
}

View File

@ -3,11 +3,8 @@ package org.badvision.outlaweditor.ui;
import java.net.URL;
import java.util.ListResourceBundle;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Set;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
@ -107,7 +104,7 @@ public class MythosScriptEditorController
editorView.getEngine().executeScript(loadScript);
}
});
editorView.getEngine().setPromptHandler((PromptData prompt) -> {
TextInputDialog dialog = new TextInputDialog(prompt.getDefaultValue());
dialog.setTitle("MythosScript Editor");

View File

@ -56,6 +56,7 @@ 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.Scope;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.ui.impl.ImageConversionWizardController;
@ -223,19 +224,19 @@ public class UIAction {
dialogStage.show();
}
public static Script createAndEditScript() {
public static Script createAndEditScript(Scope scope) {
Script script = new Script();
script.setName("New Script");
ApplicationUIController.getController().getVisibleEditor().addScript(script);
return editScript(script);
return editScript(script, scope);
}
public static Script editScript(Script script) {
public static Script editScript(Script script, Scope scope) {
if (script == null) {
System.err.println("Requested to edit a null script object, ignoring!");
return null;
}
MythosEditor editor = new MythosEditor(script);
MythosEditor editor = new MythosEditor(script, scope);
editor.show();
return script;
}

View File

@ -21,7 +21,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
@Override
protected void onScriptAddPressed(ActionEvent event) {
UIAction.createAndEditScript();
UIAction.createAndEditScript(Application.gameData.getGlobal());
}
@Override
@ -50,7 +50,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
Script script = TransferHelper.cloneObject(source, Script.class, "script");
script.setName(source.getName() + " CLONE");
getCurrentEditor().addScript(script);
editScript(script);
editScript(script, Application.gameData.getGlobal());
} catch (JAXBException ex) {
Logger.getLogger(MapEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex);
UIAction.alert("Error occured when attempting clone operation:\n" + ex.getMessage());
@ -86,7 +86,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
public void redrawGlobalScripts() {
DataUtilities.ensureGlobalExists();
globalScriptList.setOnEditStart((ListView.EditEvent<Script> event) -> {
UIAction.editScript(event.getSource().getItems().get(event.getIndex()));
UIAction.editScript(event.getSource().getItems().get(event.getIndex()), Application.gameData.getGlobal());
});
globalScriptList.setCellFactory(new Callback<ListView<Script>, ListCell<Script>>() {
@Override

View File

@ -28,7 +28,6 @@ import static org.badvision.outlaweditor.data.PropertyHelper.stringProp;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Scripts;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.ui.EntitySelectorCell;
import org.badvision.outlaweditor.ui.MapEditorTabController;
@ -148,7 +147,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
@Override
public void onMapScriptAddPressed(ActionEvent event) {
createAndEditScript();
createAndEditScript(getCurrentMap());
}
int errorCount = 0;
@ -185,7 +184,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
Script script = TransferHelper.cloneObject(source, Script.class, "script");
script.setName(source.getName() + " CLONE");
getCurrentEditor().addScript(script);
editScript(script);
editScript(script, getCurrentMap());
} catch (JAXBException ex) {
Logger.getLogger(MapEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex);
UIAction.alert("Error occured when attempting clone operation:\n" + ex.getMessage());
@ -393,7 +392,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
@Override
public void redrawMapScripts() {
mapScriptsList.setOnEditStart((ListView.EditEvent<Script> event) -> {
UIAction.editScript(event.getSource().getItems().get(event.getIndex()));
UIAction.editScript(event.getSource().getItems().get(event.getIndex()), getCurrentMap());
});
mapScriptsList.setCellFactory(new Callback<ListView<Script>, ListCell<Script>>() {
@Override

View File

@ -50,37 +50,79 @@
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="map">
<xs:sequence>
<xs:element name="chunk" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="row" maxOccurs="unbounded" type="xs:NMTOKENS"/>
</xs:sequence>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:element name="scripts" type="tns:scripts"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="width" type="xs:int"/>
<xs:attribute name="height" type="xs:int"/>
<xs:attribute name="category" type="xs:string"/>
<xs:attribute name="wrap" type="xs:boolean" default="false"/>
<xs:attribute name="startX" type="xs:int" default="0"/>
<xs:attribute name="startY" type="xs:int" default="0"/>
</xs:complexType>
<xs:complexType name="global">
<xs:sequence>
<xs:element name="scripts" type="tns:scripts"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="scripts">
<xs:sequence>
<xs:element name="script" type="tns:script" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="script" type="tns:script" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="variable">
<xs:attribute name="name" use="required" type="xs:NCName"/>
<xs:attribute name="type" use="required" type="xs:NMTOKEN"/>
<xs:attribute name="comment" type="xs:string"/>
</xs:complexType>
<xs:complexType name="variables">
<xs:sequence>
<xs:element name="variable" minOccurs="0" maxOccurs="unbounded" type="tns:variable"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="map">
<xs:complexContent>
<xs:extension base="tns:scope">
<xs:sequence>
<xs:element name="chunk" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="row" maxOccurs="unbounded" type="xs:NMTOKENS"/>
</xs:sequence>
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="width" type="xs:int"/>
<xs:attribute name="height" type="xs:int"/>
<xs:attribute name="category" type="xs:string"/>
<xs:attribute name="wrap" type="xs:boolean" default="false"/>
<xs:attribute name="startX" type="xs:int" default="0"/>
<xs:attribute name="startY" type="xs:int" default="0"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="global">
<xs:complexContent>
<xs:extension base="tns:scope">
<xs:sequence>
<xs:element name="userTypes" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="userType" minOccurs="0" maxOccurs="unbounded" type="tns:userType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="userType">
<xs:sequence>
<xs:element name="attribute" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" use="required" type="xs:NCName"/>
<xs:attribute name="type" use="required" type="xs:NMTOKEN"/>
<xs:attribute name="comment" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" use="required" type="xs:NCName"/>
<xs:attribute name="comment" use="required" type="xs:string"/>
</xs:complexType>
<xs:complexType name="scope">
<xs:sequence>
<xs:element name="scripts" type="tns:scripts"/>
<xs:element name="variables" type="tns:variables"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="block">
<xs:sequence>
<xs:element minOccurs="0" name="next" type="tns:next"/>

View File

@ -86,7 +86,7 @@
<script>
Mythos.initBlocks();
Blockly.inject(document.getElementById('blocklyDiv'),
Mythos.workspace = Blockly.inject(document.getElementById('blocklyDiv'),
{path: '../../', toolbox: document.getElementById('toolbox')});
</script>

View File

@ -24,38 +24,175 @@ if (typeof Mythos === "undefined") {
return Blockly.Xml.workspaceToDom(Blockly.mainWorkspace).innerHTML;
},
helpUrl: 'https://docs.google.com/document/d/1VXbiY4G533-cokjQevZFhwvqMMCL--17ziMAoFoeJ5M/edit#heading=h.yv9dmneqjr2b',
initCustomDefinitions: function() {
initCustomDefinitions: function () {
// Mythos.editor.log("Add user defined types");
Mythos.addUserDefinedTypes();
Mythos.addVariablesFromGlobalScope();
// Mythos.editor.log("Add custom variables");
Mythos.addCustomVariables();
// Mythos.editor.log("Add global functions");
Mythos.addFunctionsFromGlobalScope();
Mythos.addVariablesFromLocalScope();
// Mythos.editor.log("Add local functions");
Mythos.addFunctionsFromLocalScope();
// Mythos.editor.log("Reinitalizing toolbox");
Mythos.workspace.updateToolbox(document.getElementById('toolbox'));
// Mythos.editor.log("Done");
},
addUserDefinedTypes: function() {
var toolbarCategory = document.getElementById("customTypes");
for (var userType in Mythos.editor.getUserTypes()) {
var typeNode = document.createElement("block");
typeNode.setAttribute("type", "new_"+userType);
toolbarCategory.appendChild(typeNode);
Mythos.buildCustomType(userType);
each: function (list, func) {
if (list && list instanceof Array) {
for (var i = 0; i < list.length; i++) {
func(list[i]);
}
} else if (list) {
for (var i = 0; i < list.size(); i++) {
func(list.get(i));
}
}
},
buildCustomType: function(userType) {
//Blockly.Blocks['new_']
addUserDefinedTypes: function () {
var toolbarCategory = document.getElementById("customTypes");
Mythos.each(Mythos.editor.getUserTypes(), function (userType) {
var typeNode = document.createElement("block");
typeNode.setAttribute("type", "userType_" + userType.getName());
toolbarCategory.appendChild(typeNode);
var getNode = document.createElement("block");
getNode.setAttribute("type", "get_" + userType.getName());
toolbarCategory.appendChild(getNode);
var setNode = document.createElement("block");
setNode.setAttribute("type", "set_" + userType.getName());
toolbarCategory.appendChild(setNode);
Mythos.buildCustomTypeBlocks(userType);
});
},
addVariablesFromGlobalScope: function() {
buildCustomType: function (userType) {
Blockly.Blocks['userType_' + userType.getName()] = {
init: function () {
var typeConstructor = this;
typeConstructor.setColour(200);
typeConstructor.appendDummyInput()
.appendField("Create " + userType.getName());
Mythos.each(userType.getAttribute(), function (attribute) {
typeConstructor.appendValueInput(attribute.getName())
.setAlign(Blockly.ALIGN_RIGHT)
.setCheck(attribute.getType())
.appendField(attribute.getName());
});
typeConstructor.setPreviousStatement(true);
typeConstructor.setNextStatement(true);
typeConstructor.setOutput(true, userType.getName());
}
};
Blockly.Blocks['set_' + userType.getName()] = {
init: function () {
var typeSetter = this;
typeSetter.setColour(200);
typeSetter.appendValueInput("Set ")
.setAlign(Blockly.ALIGN_LEFT)
.setCheck(null)
.appendField(Mythos.getVariableDropdown(userType), "VAR")
.appendField(".")
.appendField(Mythos.getAttributeDropdown(userType), "ATTR");
typeSetter.setPreviousStatement(true);
typeSetter.setNextStatement(true);
typeSetter.setOutput(false);
}
};
Blockly.Blocks['get_' + userType.getName()] = {
init: function () {
var typeGetter = this;
typeGetter.setColour(200);
typeGetter.appendDummyInput()
.setAlign(Blockly.ALIGN_LEFT)
.setCheck(null)
.appendField(Mythos.getVariableDropdown(userType), "VAR")
.appendField(".")
.appendField(Mythos.getAttributeDropdown(userType), "ATTR");
typeGetter.setPreviousStatement(false);
typeGetter.setNextStatement(false);
typeGetter.setOutput(true, null);
}
};
},
addFunctionsFromGlobalScope: function() {
getVariableDropdown: function (userType) {
var variables = Mythos.editor.getVariablesByType(userType.getName());
var options = [];
Mythos.each(variables, function (variable) {
options.push([variable.getName(), variable.getName()]);
});
return Blockly.FieldDropdown(options);
},
getAttributeDropdown: function (userType) {
var options = [];
Mythos.each(userType.getAttribute(), function (attribute) {
options.push([attribute.getName(), attribute.getName()]);
});
return Blockly.FieldDropdown(options);
},
addFunctionsFromScope: function(target, prefix, functions) {
Mythos.each(functions, function (func) {
var scriptNode = document.createElement("block");
scriptNode.setAttribute("type", prefix + "_" + func.getName());
target.appendChild(scriptNode);
scriptNode = document.createElement("block");
scriptNode.setAttribute("type", prefix + "ignore_" + func.getName());
target.appendChild(scriptNode);
Blockly.Blocks[prefix + 'ignore_' + func.getName()] = {
init: function () {
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(250);
this.appendDummyInput()
.appendField(prefix + " " + func.getName());
var functionBlock = this;
Mythos.each(Mythos.editor.getParametersForScript(func), function (argName) {
functionBlock.appendValueInput(argName)
.setAlign(Blockly.ALIGN_RIGHT)
.setCheck(null)
.appendField(argName);
});
}
};
Blockly.Blocks[prefix + '_' + func.getName()] = {
init: function () {
this.setColour(250);
this.setPreviousStatement(false);
this.setNextStatement(false);
this.setOutput(true, null);
this.appendDummyInput()
.appendField(prefix + " " + func.getName());
var functionBlock = this;
Mythos.each(Mythos.editor.getParametersForScript(func), function (argName) {
functionBlock.appendValueInput(argName)
.setAlign(Blockly.ALIGN_RIGHT)
.setCheck(null)
.appendField(argName);
});
}
};
});
},
addFunctionsFromGlobalScope: function () {
var toolbarCategory = document.getElementById("globalFunctions");
Mythos.addFunctionsFromScope(toolbarCategory, "Global", Mythos.editor.getGlobalFunctions());
},
addVariablesFromLocalScope: function() {
},
addFunctionsFromLocalScope: function() {
addFunctionsFromLocalScope: function () {
var toolbarCategory = document.getElementById("localFunctions");
Mythos.addFunctionsFromScope(toolbarCategory, "Local", Mythos.editor.getLocalFunctions());
},
addCustomVariables: function () {
Blockly.Variables.allVariables_old = Blockly.Variables.allVariables;
Blockly.Variables.allVariables = function (workspace) {
var list = Blockly.Variables.allVariables_old(workspace);
Mythos.each(Mythos.editor.getVariablesByType("String"), function (variable) {
list.push(variable.getName());
});
Mythos.each(Mythos.editor.getVariablesByType("Number"), function (variable) {
list.push(variable.getName());
});
Mythos.each(Mythos.editor.getVariablesByType("Boolean"), function (variable) {
list.push(variable.getName());
});
return list;
};
},
initBlocks: function () {
Blockly.Blocks['flow_for'] = {
@ -162,7 +299,7 @@ if (typeof Mythos === "undefined") {
}
};
Blockly.Blocks['events_move_backward'] = {
init: function() {
init: function () {
this.setHelpUrl(Mythos.helpUrl);
this.setColour(54);
this.setPreviousStatement(true);
@ -382,4 +519,4 @@ if (typeof Mythos === "undefined") {
}
};
}
;
;