Additional working fixes to blockly integration, the marshalling/unmarshalling of blockly script code is starting to function!

This commit is contained in:
Brendan Robert 2014-04-03 23:19:59 -05:00
parent b9b12c9ead
commit 160821f077
11 changed files with 144 additions and 72 deletions

View File

@ -1,7 +1,6 @@
package org.badvision.outlaweditor;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
@ -14,18 +13,30 @@ import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
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.Script;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Mythos Scripting Editor
*
* @author blurry
*/
public class MythosEditor {
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) {
script = theScript;
}
@ -53,8 +64,13 @@ public class MythosEditor {
}
});
primaryStage.show();
controller.onLoad(new Runnable() {
@Override
public void run() {
loadScript();
}
});
}
public void close() {
primaryStage.close();
@ -63,13 +79,19 @@ public class MythosEditor {
public void applyChanges() {
try {
String xml = String.valueOf(controller.editorView.getEngine().executeScript("Blockly.Xml.workspaceToDom(Blockly.mainWorkspace).outerHTML"));
JAXBContext context = JAXBContext.newInstance(Block.class);
Block scriptBlock = (Block) context.createUnmarshaller().unmarshal(new StringReader(xml));
script.setBlock(scriptBlock);
} catch (JAXBException ex) {
xml = xml.replace("<xml>", "");
xml = xml.replace("</xml>", "");
JAXBContext context = JAXBContext.newInstance("org.badvision.outlaweditor.data.xml");
Unmarshaller unmarshaller = context.createUnmarshaller();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
JAXBElement<Block> b = unmarshaller.unmarshal(doc, Block.class);
script.setBlock(b.getValue());
} catch (JAXBException | ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(MythosEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void loadScript() {
@ -89,13 +111,14 @@ public class MythosEditor {
}
public void loadScript(String xml) {
xml = xml.replaceAll("\"","\\\"");
String loadScript = "var xml = Blockly.Xml.textToDom("+xml+");";
loadScript += "Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);";
xml = XML_HEADER + xml;
xml = xml.replaceAll("'", "\\'");
xml = xml.replaceAll("\n", "");
String loadScript = "Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, Blockly.Xml.textToDom('" + xml + "'));";
controller.editorView.getEngine().executeScript(loadScript);
}
private String createDefaultScript() {
return "<block/>";
return "<xml><block type=\"procedures_defreturn\" id=\"1\" inline=\"false\" x=\"5\" y=\"5\"><mutation></mutation><field name=\"NAME\">New function</field></block></xml>";
}
}

View File

@ -1,7 +1,12 @@
package org.badvision.outlaweditor;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@ -13,6 +18,15 @@ public class MythosScriptEditorController
implements Initializable {
public static final String MYTHOS_EDITOR = "/mythos/mythos-editor/html/editor.html";
private final List<Runnable> onLoadEvents = new ArrayList<>();
boolean loaded = false;
public synchronized void onLoad(Runnable runnable) {
if (loaded) {
runnable.run();
} else {
onLoadEvents.add(runnable);
}
}
@FXML // fx:id="editorView"
WebView editorView; // Value injected by FXMLLoader
@ -73,6 +87,19 @@ public class MythosScriptEditorController
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
editorView.getEngine().getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
loaded = true;
for (Runnable r : onLoadEvents) {
r.run();
}
onLoadEvents.clear();
}
}
});
assert editorView != null : "fx:id=\"editorView\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
assert menuItemAbortChanges != null : "fx:id=\"menuItemAbortChanges\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
assert menuItemAboutBlockly != null : "fx:id=\"menuItemAboutBlockly\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";

View File

@ -31,6 +31,8 @@ import javax.swing.JOptionPane;
import javax.xml.bind.JAXB;
import org.badvision.outlaweditor.data.TilesetUtils;
import org.badvision.outlaweditor.data.xml.GameData;
import org.badvision.outlaweditor.data.xml.IntervalScript;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
/**
@ -203,6 +205,10 @@ public class UIAction {
script.setName("New Script");
MythosEditor editor = new MythosEditor(script);
editor.show();
if (Application.instance.controller.currentMap.getScripts() == null) {
Application.instance.controller.currentMap.setScripts(new Map.Scripts());
}
Application.instance.controller.currentMap.getScripts().getScript().add(script);
return script;
}
}

View File

@ -12,7 +12,9 @@ import org.badvision.outlaweditor.data.xml.*;
* @author brobert
*/
public class TileUtils {
static Map<String, Map<Platform, WritableImage>> display;
static {
clear();
}
@ -51,7 +53,13 @@ public class TileUtils {
public static void redrawTile(Tile t) {
Map<Platform, WritableImage> displays = getDisplay(t);
for (PlatformData d : t.getDisplayData()) {
Platform p = Platform.valueOf(d.getPlatform());
Platform p;
try {
p = Platform.valueOf(d.getPlatform());
} catch (IllegalArgumentException e) {
System.err.println("Unable to find any platform support for '" + d.getPlatform() + "'");
continue;
}
displays.put(p, p.tileRenderer.redrawSprite(d.getValue(), displays.get(p)));
}
DataProducer.notifyObservers(t);

View File

@ -2,15 +2,11 @@
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="500.0" prefWidth="800.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="org.badvision.outlaweditor.ApplicationUIControllerImpl">
<AnchorPane id="AnchorPane" prefHeight="575.0" prefWidth="1000.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.badvision.outlaweditor.ApplicationUIControllerImpl">
<children>
<VBox prefHeight="500.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
@ -73,7 +69,7 @@
<Button mnemonicParsing="false" onAction="#onTileDeletePressed" text="Delete" />
<MenuButton mnemonicParsing="false" text="Tools">
<items>
<Menu mnemonicParsing="false" text="Pattern" fx:id="tilePatternMenu" />
<Menu fx:id="tilePatternMenu" mnemonicParsing="false" text="Pattern" />
<Menu mnemonicParsing="false" text="Draw mode">
<items>
<MenuItem mnemonicParsing="false" onAction="#tileBitMode" text="Bit Toggle" />
@ -90,13 +86,13 @@
<children>
<AnchorPane id="imageDetailsPane" prefHeight="200.0" prefWidth="200.0">
<children>
<Label layoutX="4.0" layoutY="14.0" text="Name" />
<TextField id="" fx:id="tileNameField" layoutX="53.0" layoutY="11.0" prefWidth="147.0" />
<Label layoutX="4.0" layoutY="36.0" text="ID" />
<TextField fx:id="tileIdField" layoutX="53.0" layoutY="33.0" prefWidth="147.0" />
<Label layoutX="4.0" layoutY="58.0" text="Category" />
<TextField fx:id="tileCategoryField" layoutX="74.0" layoutY="55.0" prefWidth="126.0" />
<CheckBox fx:id="tileObstructionField" layoutX="4.0" layoutY="78.0" mnemonicParsing="false" text="Physical Obstruction" />
<Label layoutX="5.0" layoutY="5.0" prefHeight="29.0" prefWidth="37.0" text="Name" />
<TextField id="" fx:id="tileNameField" layoutX="54.0" layoutY="5.0" prefWidth="147.0" />
<Label layoutX="5.0" layoutY="33.0" prefHeight="29.0" prefWidth="46.0" text="ID" />
<TextField fx:id="tileIdField" layoutX="54.0" layoutY="36.0" prefWidth="147.0" />
<Label layoutX="5.0" layoutY="72.0" text="Category" />
<TextField fx:id="tileCategoryField" layoutX="64.0" layoutY="67.0" prefHeight="26.0" prefWidth="137.0" />
<CheckBox fx:id="tileObstructionField" layoutX="6.0" layoutY="98.0" mnemonicParsing="false" text="Physical Obstruction" />
</children>
</AnchorPane>
<AnchorPane fx:id="tileEditorAnchorPane" prefHeight="387.0" prefWidth="477.0000999999975" HBox.hgrow="ALWAYS" />
@ -133,7 +129,7 @@
<Button mnemonicParsing="false" onAction="#onMapPreviewPressed" text="Preview" />
<MenuButton mnemonicParsing="false" text="Tools">
<items>
<Menu mnemonicParsing="false" text="Change tile" fx:id="mapSelectTile" />
<Menu fx:id="mapSelectTile" mnemonicParsing="false" text="Change tile" />
<Menu mnemonicParsing="false" text="Draw mode">
<items>
<MenuItem mnemonicParsing="false" onAction="#mapDraw1" text="Radius 1" />
@ -220,7 +216,7 @@
<Button mnemonicParsing="false" onAction="#onImageDeletePressed" text="Delete" />
<MenuButton mnemonicParsing="false" text="Tools">
<items>
<Menu mnemonicParsing="false" text="Pattern" fx:id="imagePatternMenu" />
<Menu fx:id="imagePatternMenu" mnemonicParsing="false" text="Pattern" />
<Menu mnemonicParsing="false" text="Draw mode">
<items>
<MenuItem mnemonicParsing="false" onAction="#imageBitMode" text="Bit Toggle mode" />

View File

@ -1,14 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="org.badvision.outlaweditor.MythosScriptEditorController">
<AnchorPane id="AnchorPane" prefHeight="748.0" prefWidth="1024.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="org.badvision.outlaweditor.MythosScriptEditorController">
<!-- <stylesheets>
<URL value="@/styles/mythosscripteditor.css"/>
</stylesheets>-->
@ -17,20 +14,20 @@
<menus>
<Menu mnemonicParsing="false" text="MythosScript">
<items>
<MenuItem mnemonicParsing="false" onAction="#onApplyChangesSelected" text="Apply changes" fx:id="menuItemApplyChanges" />
<MenuItem mnemonicParsing="false" onAction="#onAbortChangesSelected" text="Abort changes" fx:id="menuItemAbortChanges" />
<MenuItem fx:id="menuItemApplyChanges" mnemonicParsing="false" onAction="#onApplyChangesSelected" text="Apply changes" />
<MenuItem fx:id="menuItemAbortChanges" mnemonicParsing="false" onAction="#onAbortChangesSelected" text="Abort changes" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" onAction="#onUndoSelected" text="Undo" fx:id="menuItemUndo" />
<MenuItem mnemonicParsing="false" onAction="#onRedoSelected" text="Redo" fx:id="menuItemRedo" />
<MenuItem fx:id="menuItemUndo" mnemonicParsing="false" onAction="#onUndoSelected" text="Undo" />
<MenuItem fx:id="menuItemRedo" mnemonicParsing="false" onAction="#onRedoSelected" text="Redo" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#onMythosHelpSelected" text="Mythos Script Help" fx:id="menuItemMythosHelp" />
<MenuItem mnemonicParsing="false" onAction="#onAboutBlocklySelected" text="About Blockly" fx:id="menuItemAboutBlockly" />
<MenuItem fx:id="menuItemMythosHelp" mnemonicParsing="false" onAction="#onMythosHelpSelected" text="Mythos Script Help" />
<MenuItem fx:id="menuItemAboutBlockly" mnemonicParsing="false" onAction="#onAboutBlocklySelected" text="About Blockly" />
</items>
</Menu>
</menus>

View File

@ -64,14 +64,9 @@
</xs:element>
<xs:element name="scripts">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="enter" type="tns:script" minOccurs="0"/>
<xs:element name="exit" type="tns:script" minOccurs="0"/>
<xs:element name="stepOn" type="tns:locationScript" minOccurs="0"/>
<xs:element name="stepOff" type="tns:locationScript" minOccurs="0" />
<xs:element name="interact" type="tns:locationScript" minOccurs="0"/>
<xs:element name="interval" type="tns:intervalScript" minOccurs="0"/>
</xs:choice>
<xs:sequence>
<xs:element name="script" type="tns:script" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
@ -94,6 +89,9 @@
</xs:sequence>
<xs:attribute name="inline" type="xs:boolean"/>
<xs:attribute name="type" use="required" type="xs:NCName"/>
<xs:attribute name="id" type="xs:integer"/>
<xs:attribute name="uri" type="xs:string"/>
<xs:attribute name="local" type="xs:string"/>
<xs:attribute name="x" type="xs:integer"/>
<xs:attribute name="y" type="xs:integer"/>
</xs:complexType>

View File

@ -19,7 +19,7 @@
</style>
</head>
<body>
<div id="blocklyDiv" style="height: 550px; width: 800px;"></div>
<div id="blocklyDiv" style="height: 700px; width: 996px;"></div>
<xml id="toolbox" style="display: none">
<category name="Variables">

View File

@ -2,7 +2,7 @@
<gameData xmlns="outlaw">
<map>
<scripts>
<enter>
<script>
<name>name</name>
<description>description</description>
<block type="variables_set" inline="true" x="245" y="27">
@ -47,7 +47,7 @@
</block>
</next>
</block>
</enter>
</script>
</scripts>
</map>
</gameData>

View File

@ -2,7 +2,7 @@
<gameData xmlns="outlaw">
<map>
<scripts>
<enter>
<script>
<name>name</name>
<description>description</description>
<block type="procedures_defreturn" inline="false" x="213" y="27">
@ -112,7 +112,26 @@
<block type="logic_null"></block>
</value>
</block>
</enter>
</script>
<script>
<name>name</name>
<description>description</description>
<block type="procedures_defreturn" id="2" inline="false" x="3" y="5">
<mutation></mutation>
<field name="NAME">New function2</field>
<statement name="STACK">
<block type="text_moveto" id="20">
<field name="x">0</field>
<field name="y">0</field>
<next>
<block type="math_change" id="32" inline="true">
<field name="VAR">item</field>
</block>
</next>
</block>
</statement>
</block>
</script>
</scripts>
</map>
</gameData>

View File

@ -5,9 +5,7 @@ import java.io.IOException;
import java.io.StringWriter;
import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import org.badvision.outlaweditor.data.xml.Block;
import org.badvision.outlaweditor.data.xml.GameData;
import org.badvision.outlaweditor.data.xml.Map;
@ -53,7 +51,7 @@ public class TestMythosEditor {
script.setDescription("description");
script.setBlock(theBlock);
map.setScripts(new Scripts());
map.getScripts().getEnterOrExitOrStepOn().add(new JAXBElement<Script>(new QName("outlaw", "enter"), Script.class, script));
map.getScripts().getScript().add(script);
m.marshal(d, testWriter);
String testOutput = testWriter.getBuffer().toString();
assertNotNull(testOutput);
@ -92,9 +90,9 @@ public class TestMythosEditor {
assertEquals(1, gd.getMap().size());
Scripts s = gd.getMap().get(0).getScripts();
assertNotNull(s);
assertNotNull(s.getEnterOrExitOrStepOn());
assertEquals(1, s.getEnterOrExitOrStepOn().size());
Script scr = (Script) s.getEnterOrExitOrStepOn().get(0).getValue();
assertNotNull(s.getScript());
assertTrue(s.getScript().size() > 0);
Script scr = (Script) s.getScript().get(0);
return scr.getBlock();
}
}