Skip to content

Commit 13f4a06

Browse files
committed
Big update: JavaScript compiler (and ScriptEngine)
- Tons of new default blocks. (ADD, MUL, EQUALS...) - JavaScript-Viewer - JsBeautifier - Sprites update - Removed SpriteChooserTest class - ScriptGrabber => ScriptToCode - ByobBlock bug is STILL not fixed The JavaScript-ScriptEngine is slower than the default ScriptEngine. :( ZeroLuck git-svn-id: http://svn.code.sf.net/p/jblocks/code-0/trunk@104 9daefd43-015a-43da-9b33-cb4dc19dcf47
1 parent 1025207 commit 13f4a06

28 files changed

+1125
-231
lines changed

org/jblocks/JBlocks.java

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import java.awt.Color;
44
import java.awt.Component;
55
import java.awt.Container;
6+
import java.awt.Dimension;
67
import java.awt.image.BufferedImage;
78
import java.io.IOException;
89
import java.io.InputStream;
9-
import java.util.ArrayList;
1010
import java.util.Collections;
1111
import java.util.HashMap;
12-
import java.util.List;
1312
import java.util.Map;
1413

1514
import javax.imageio.ImageIO;
@@ -24,17 +23,17 @@
2423
import org.jblocks.editor.JBlockEditor;
2524
import org.jblocks.editor.JBlockEditor.Category;
2625
import org.jblocks.editor.JScriptPane;
27-
import org.jblocks.editor.ScriptGrabber;
2826
import org.jblocks.gui.JBlocksPane;
2927
import org.jblocks.gui.JBubble;
3028
import org.jblocks.gui.JDragPane;
3129
import org.jblocks.scriptengine.Block;
3230
import org.jblocks.scriptengine.Block.Default;
33-
import org.jblocks.scriptengine.IScript;
3431
import org.jblocks.scriptengine.IScriptEngine;
3532
import org.jblocks.scriptengine.IScriptEngine.ScriptEngineListener;
3633
import org.jblocks.scriptengine.IScriptThread;
3734
import org.jblocks.scriptengine.NativeBlock;
35+
import org.jblocks.stage.ImageSprite;
36+
import org.jblocks.stage.SpriteData;
3837

3938
/**
4039
* The context of JBlocks: <br />
@@ -111,6 +110,21 @@ public void started(IScriptThread t) {
111110

112111
scriptEngine.addListener(defaultListener);
113112
initDefaultBlocks();
113+
initDefaultSprites();
114+
}
115+
116+
/**
117+
* Initialises the default sprites. <br />
118+
*/
119+
private void initDefaultSprites() {
120+
Dimension size = gui.getStage().getStageSize();
121+
122+
ImageSprite test = new ImageSprite();
123+
test.setLocation(size.width / 2, size.height / 2);
124+
test.addCostume("Test", org.jblocks.JBlocks.getImage("splash.png"));
125+
126+
SpriteData data = new SpriteData("Logo", test);
127+
gui.addSprite(data);
114128
}
115129

116130
/**
@@ -125,28 +139,38 @@ public Object evaluate(Object ctx, Object... param) {
125139
return null;
126140
}
127141
}));
128-
installBlock(BlockModel.createModel("boolean", "Operators", "true", new NativeBlock(0, 200 + 2) {
129142

130-
@Override
131-
public Object evaluate(Object ctx, Object... param) {
132-
return true;
133-
}
134-
}));
135-
installBlock(BlockModel.createModel("boolean", "Operators", "false", new NativeBlock(0, 200 + 3) {
136-
137-
@Override
138-
public Object evaluate(Object ctx, Object... param) {
139-
return false;
140-
}
141-
}));
142143
installBlock(BlockModel.createModel("cap", "Control", "return %{t}", scriptEngine.getDefaultBlock(Default.RETURN)));
143144
installBlock(BlockModel.createModel("command", "Control", "while %{b}%{br}%{s}", scriptEngine.getDefaultBlock(Default.WHILE)));
144145
installBlock(BlockModel.createModel("command", "Control", "if %{b}%{br}%{s}", scriptEngine.getDefaultBlock(Default.IF)));
145146
installBlock(BlockModel.createModel("command", "Control", "if %{b}%{br}%{s}%{br}else%{br}%{s}", scriptEngine.getDefaultBlock(Default.IF_ELSE)));
146147
installBlock(BlockModel.createModel("command", "Control", "repeat %{t}%{br}%{s}", scriptEngine.getDefaultBlock(Default.FOR)));
147-
148148
installBlock(BlockModel.createModel("reporter", "Variables", "%{v}", scriptEngine.getDefaultBlock(Default.READ_GLOBAL_VARIABLE)));
149149
installBlock(BlockModel.createModel("command", "Variables", "set %{v} to %{t}", scriptEngine.getDefaultBlock(Default.WRITE_GLOBAL_VARIABLE)));
150+
151+
152+
installBlock(BlockModel.createModel("reporter", "Operators", "%{t}+%{t}",
153+
scriptEngine.getDefaultBlock(Default.ADD)));
154+
installBlock(BlockModel.createModel("reporter", "Operators", "%{t}-%{t}",
155+
scriptEngine.getDefaultBlock(Default.SUB)));
156+
installBlock(BlockModel.createModel("reporter", "Operators", "%{t}*%{t}",
157+
scriptEngine.getDefaultBlock(Default.MUL)));
158+
installBlock(BlockModel.createModel("reporter", "Operators", "%{t}/%{t}",
159+
scriptEngine.getDefaultBlock(Default.DIV)));
160+
installBlock(BlockModel.createModel("boolean", "Operators", "true", scriptEngine.getDefaultBlock(Default.TRUE)));
161+
installBlock(BlockModel.createModel("boolean", "Operators", "false", scriptEngine.getDefaultBlock(Default.FALSE)));
162+
installBlock(BlockModel.createModel("reporter", "Operators", "%{t}mod%{t}",
163+
scriptEngine.getDefaultBlock(Default.MOD)));
164+
installBlock(BlockModel.createModel("boolean", "Operators", "%{t}<%{t}",
165+
scriptEngine.getDefaultBlock(Default.SMALLER)));
166+
installBlock(BlockModel.createModel("boolean", "Operators", "%{t}>%{t}",
167+
scriptEngine.getDefaultBlock(Default.BIGGER)));
168+
installBlock(BlockModel.createModel("boolean", "Operators", "%{t}=%{t}",
169+
scriptEngine.getDefaultBlock(Default.EQUALS)));
170+
installBlock(BlockModel.createModel("boolean", "Operators", "%{b}and%{b}",
171+
scriptEngine.getDefaultBlock(Default.AND)));
172+
installBlock(BlockModel.createModel("boolean", "Operators", "%{b}or%{b}",
173+
scriptEngine.getDefaultBlock(Default.OR)));
150174
}
151175

152176
/**
@@ -321,15 +345,15 @@ public void stopScripts() {
321345
* Starts all "green flag" hat blocks. <br />
322346
*/
323347
public synchronized void startScripts() {
324-
// TODO: Run all JScriptPanes (=> JSpriteChooser)
325-
326-
JScriptPane pane = gui.getEditor().getScriptPane();
327-
for (Component c : pane.getComponents()) {
328-
if (c instanceof AbstrBlock) {
329-
AbstrBlock block = (AbstrBlock) c;
330-
BlockModel model = block.getModel();
331-
if (model != null && model.getID() == GREEN_FLAG_PRESSED_ID) {
332-
block.tryToExecute();
348+
for (SpriteData sprite : gui.getSprites().values()) {
349+
JScriptPane pane = sprite.getScriptPane();
350+
for (Component c : pane.getComponents()) {
351+
if (c instanceof AbstrBlock) {
352+
AbstrBlock block = (AbstrBlock) c;
353+
BlockModel model = block.getModel();
354+
if (model != null && model.getID() == GREEN_FLAG_PRESSED_ID) {
355+
block.tryToExecute();
356+
}
333357
}
334358
}
335359
}

org/jblocks/JBlocksApplet.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class JBlocksApplet extends JApplet {
2020
@Override
2121
public void init() {
2222
Repainter.install();
23-
// TODO run JBlocks
2423

2524
String frame = getParameter("frame");
2625
if (frame != null && frame.equalsIgnoreCase("true")) {

org/jblocks/JBlocksLauncher.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class JBlocksLauncher implements Runnable {
2727
*/
2828
public static void main(String[] args) {
2929
Repainter.install();
30-
// TODO run JBlocks
3130
SwingUtils.run(new JBlocksLauncher());
3231
}
3332

org/jblocks/blockstore/JBlockStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public Component getListCellRendererComponent(JList list, Object value,
355355
}
356356

357357
public static void openBlockStore(JDesktopPane desktop, Icon icon) {
358-
JInternalFrame frm = SwingUtils.showInternalFrame(desktop, new JBlockStore(), "ZeroLuck's Block-Store", new Dimension(600, 400));
358+
JInternalFrame frm = SwingUtils.showInternalFrame(desktop, new JBlockStore(), "Block-Store (by ZeroLuck)", new Dimension(600, 400));
359359
if (icon != null) {
360360
frm.setFrameIcon(icon);
361361
}

org/jblocks/blockstore/JSimpleComboBox.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.awt.BorderLayout;
44
import java.awt.Component;
5-
import java.awt.Cursor;
65
import java.awt.Dimension;
76
import java.awt.event.MouseAdapter;
87
import java.awt.event.MouseEvent;

org/jblocks/byob/JByobEditor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.jblocks.editor.JScriptVariableInput;
3838
import org.jblocks.editor.Puzzle;
3939
import org.jblocks.editor.PuzzleAdapter;
40-
import org.jblocks.editor.ScriptGrabber;
40+
import org.jblocks.editor.ScriptToCode;
4141
import org.jblocks.scriptengine.Block;
4242
import org.jblocks.scriptengine.ByobBlock;
4343
import org.jblocks.utils.SwingUtils;
@@ -342,7 +342,7 @@ public void cancel() {
342342
@Override
343343
public void finished(final BlockModel model, final AbstrBlock script) {
344344
try {
345-
final Block[] code = ScriptGrabber.getCodeFromScript(script);
345+
final Block[] code = ScriptToCode.getCodeFromScript(script);
346346
JBlocks ctx = JBlocks.getContextForComponent(edt);
347347
model.setCode(new ByobBlock(BlockFactory.countParameters(model.getSyntax()), model.getID(), code));
348348
ctx.installBlock(model);

org/jblocks/cyob/JCodePane.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.jblocks.cyob;
22

3+
import java.util.Map;
34
import javax.swing.JScrollPane;
4-
import javax.swing.text.ViewFactory;
55
import java.awt.BorderLayout;
66
import javax.swing.JPanel;
77
import javax.swing.undo.UndoManager;
@@ -20,12 +20,12 @@
2020
*
2121
* @author ZeroLuck
2222
*/
23-
class JCodePane extends JPanel {
23+
public class JCodePane extends JPanel {
2424

25-
private final JTextPane textPane;
25+
public static final Map<String, MutableAttributeSet> javaKeywords;
2626

27-
public JCodePane() {
28-
final HashMap<String, MutableAttributeSet> javaKeywords = new HashMap<String, MutableAttributeSet>(16);
27+
static {
28+
javaKeywords = new HashMap<String, MutableAttributeSet>(100);
2929
javaKeywords.put("public", DEFAULT_KEYWORD);
3030
javaKeywords.put("private", DEFAULT_KEYWORD);
3131
javaKeywords.put("abstract", DEFAULT_KEYWORD);
@@ -87,11 +87,19 @@ public JCodePane() {
8787
javaKeywords.put("void", DEFAULT_KEYWORD);
8888
javaKeywords.put("volatile", DEFAULT_KEYWORD);
8989
javaKeywords.put("while", DEFAULT_KEYWORD);
90+
}
91+
private final JTextPane textPane;
92+
93+
public JCodePane() {
94+
this(javaKeywords, true);
95+
}
96+
97+
public JCodePane(final Map<String, MutableAttributeSet> keywords, boolean edit) {
9098
EditorKit editorKit = new StyledEditorKit() {
9199

92100
@Override
93101
public Document createDefaultDocument() {
94-
MultiSyntaxDocument doc = new MultiSyntaxDocument(javaKeywords);
102+
MultiSyntaxDocument doc = new MultiSyntaxDocument(keywords);
95103
doc.setTabs(4);
96104
doc.addUndoableEditListener(new UndoListener());
97105
return doc;
@@ -100,10 +108,11 @@ public Document createDefaultDocument() {
100108
textPane = new JTextPane();
101109
textPane.setEditorKitForContentType("text/java", editorKit);
102110
textPane.setContentType("text/java");
111+
textPane.setEditable(edit);
103112

104113
JScrollPane scroll = new JScrollPane(textPane);
105114
scroll.setRowHeaderView(new TextLineNumber(textPane));
106-
115+
107116
setLayout(new BorderLayout());
108117
add(scroll, BorderLayout.CENTER);
109118
}

org/jblocks/cyob/MultiSyntaxDocument.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ class MultiSyntaxDocument extends DefaultStyledDocument {
5050
private MutableAttributeSet normal = DEFAULT_NORMAL;
5151
private MutableAttributeSet comment = DEFAULT_COMMENT;
5252
private MutableAttributeSet quote = DEFAULT_STRING;
53-
private HashMap<String, MutableAttributeSet> keywords;
53+
private Map<String, MutableAttributeSet> keywords;
5454
private int fontSize = DEFAULT_FONT_SIZE;
5555
private String fontName = DEFAULT_FONT_FAMILY;
5656

5757
@SuppressWarnings("LeakingThisInConstructor")
58-
public MultiSyntaxDocument(final HashMap<String, MutableAttributeSet> keywords) {
58+
public MultiSyntaxDocument(final Map<String, MutableAttributeSet> keywords) {
5959
doc = this;
6060
rootElement = doc.getDefaultRootElement();
6161
putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");

org/jblocks/editor/AbstrBlock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ public void tryToExecute() {
430430
try {
431431
IScriptEngine eng = context.getScriptEngine();
432432
if (!(this instanceof Puzzle)) {
433-
Block b = ScriptGrabber.getCodeFromBlock(this);
433+
Block b = ScriptToCode.getCodeFromBlock(this);
434434
setHighlight(true);
435435
context.addHighlight(eng.execute(eng.compile(new Block[]{b})), new AbstrBlock[]{this});
436436
} else {
437-
Block[] b = ScriptGrabber.getCodeFromScript(this);
437+
Block[] b = ScriptToCode.getCodeFromScript(this);
438438
AbstrBlock[] blocks = JBlockSequence.getPuzzlePieces((Puzzle) this, PuzzleAdapter.TYPE_DOWN);
439439
for (AbstrBlock toHighlight : blocks) {
440440
toHighlight.setHighlight(true);

org/jblocks/editor/BlockIO.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* The output format is XML.
3131
* </p>
3232
*
33-
* @see ScriptGrabber
33+
* @see ScriptToCode
3434
* @author ZeroLuck
3535
*/
3636
public class BlockIO {
@@ -321,16 +321,16 @@ public static AbstrBlock[] createScript(JBlocks ctx, Block[] code) {
321321
* Converts "GUI blocks" to code. <br />
322322
* The opposite of this method is {@link #createScript(org.jblocks.JBlocks, org.jblocks.scriptengine.Block[]) }. <br />
323323
*
324-
* @see ScriptGrabber
324+
* @see ScriptToCode
325325
* @param ctx the context of JBlocks
326326
* @param block the block (or the puzzle) which should be converted
327327
* @return the converted script
328328
*/
329329
public static Block[] createCode(JBlocks ctx, AbstrBlock block) {
330330
if (block instanceof Puzzle) {
331-
return ScriptGrabber.getCodeFromScript(block);
331+
return ScriptToCode.getCodeFromScript(block);
332332
} else {
333-
return new Block[]{ScriptGrabber.getCodeFromBlock(block)};
333+
return new Block[]{ScriptToCode.getCodeFromBlock(block)};
334334
}
335335
}
336336
}

org/jblocks/editor/JPopupBlockMenu.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
import javax.swing.JScrollPane;
2525
import javax.swing.JTextArea;
2626
import org.jblocks.JBlocks;
27+
import org.jblocks.cyob.JCodePane;
2728
import org.jblocks.gui.JDragPane;
29+
import org.jblocks.scriptengine.Block;
2830
import org.jblocks.scriptengine.NativeBlock;
31+
import org.jblocks.scriptengine.js.impl.JsBeautifier;
32+
import org.jblocks.scriptengine.js.impl.JsScriptEngine;
2933
import org.jblocks.utils.StreamUtils;
3034
import org.jblocks.utils.SwingUtils;
3135

@@ -80,6 +84,7 @@ public JPopupBlockMenu(AbstrBlock b) {
8084

8185
this.addSeparator();
8286
this.add("save script").addActionListener(this);
87+
this.add("view JavaScript").addActionListener(this);
8388
}
8489

8590
private void deleteBlock() {
@@ -115,14 +120,49 @@ private JFileChooser showFileChooser(String title) {
115120
return ch;
116121
}
117122

123+
private void viewJavaScript() {
124+
try {
125+
final JsScriptEngine tmpEngine = new JsScriptEngine(false); // <- not for executing, just compiling
126+
final Block[] blocks;
127+
if (parent instanceof Puzzle) {
128+
blocks = ScriptToCode.getCodeFromScript(parent);
129+
} else {
130+
blocks = new Block[]{ScriptToCode.getCodeFromBlock(parent)};
131+
}
132+
final String code = JsBeautifier.work(tmpEngine.compileToJavaScriptCode(blocks));
133+
final JInternalFrame frm = new JInternalFrame("JavaScript-Viewer (by ZeroLuck)");
134+
135+
final JPanel pane = new JPanel(new BorderLayout());
136+
final JCodePane codePane = new JCodePane(JCodePane.javaKeywords, false);
137+
final JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
138+
final JButton cancel = new JButton("Cancel");
139+
cancel.addActionListener(new ActionListener() {
140+
141+
@Override
142+
public void actionPerformed(ActionEvent ae) {
143+
frm.dispose();
144+
}
145+
});
146+
codePane.setText(code);
147+
bottom.add(cancel);
148+
pane.add(codePane, BorderLayout.CENTER);
149+
pane.add(bottom, BorderLayout.SOUTH);
150+
151+
SwingUtils.showInternalFrame(JBlocks.getContextForComponent(parent).getDesktop(), frm, pane, new Dimension(620, 360));
152+
} catch (Exception ex) {
153+
JOptionPane.showInternalMessageDialog(JBlocks.getContextForComponent(parent).getDesktop(),
154+
"Couldn't compile to JavaScript: \n" + ex, "Error", JOptionPane.ERROR_MESSAGE);
155+
}
156+
}
157+
118158
private void shareBlock() {
119159
final BlockModel model = parent.getModel();
120160
final JPanel p = new JPanel(new BorderLayout());
121161
final JTextArea description = new JTextArea(
122-
"I am a " + (model.getCode() instanceof NativeBlock ? "native" : "BYOB") + " " +model.getType() + " block "
162+
"I am a " + (model.getCode() instanceof NativeBlock ? "native" : "BYOB") + " " + model.getType() + " block "
123163
+ "made by " + System.getProperty("user.name") + ".\n");
124-
description.setFont(new Font(Font.MONOSPACED,Font.PLAIN, description.getFont().getSize()));
125-
164+
description.setFont(new Font(Font.MONOSPACED, Font.PLAIN, description.getFont().getSize()));
165+
126166
p.add(new JScrollPane(description), BorderLayout.CENTER);
127167
JPanel south = new JPanel(new FlowLayout(FlowLayout.RIGHT));
128168
JButton uploadButton = new JButton("Upload");
@@ -276,6 +316,8 @@ public void actionPerformed(ActionEvent evt) {
276316
savePicture();
277317
} else if (command.equals("save script")) {
278318
saveScript();
319+
} else if (command.equals("view JavaScript")) {
320+
viewJavaScript();
279321
}
280322
}
281323
}

0 commit comments

Comments
 (0)