Skip to content

Commit 7496e65

Browse files
hawkingreigaocegege
authored andcommitted
feat: move the function about mouse to BuiltinApplet (#126)
1 parent 7cb03da commit 7496e65

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

src/rprocessing/RLangPApplet.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ public class RLangPApplet extends BuiltinApplet {
5151
/** Program code */
5252
private final String programText;
5353

54-
/** Engine to interpret R code */
55-
private final RenjinScriptEngine renjinEngine;
56-
5754
private static final String CORE_TEXT =
5855
RScriptReader.readResourceAsText(Runner.class, "r/core.R");
5956

@@ -78,15 +75,6 @@ private static void log(String msg) {
7875
}
7976

8077
public RLangPApplet(final String programText, final Printer stdout) throws NotFoundException {
81-
// Create a script engine manager.
82-
ScriptEngineManager manager = new ScriptEngineManager();
83-
// Create a Renjin engine.
84-
ScriptEngine engine = manager.getEngineByName("Renjin");
85-
// Check if the engine has loaded correctly.
86-
if (engine == null) {
87-
throw new NotFoundException("Renjin Script Engine not found on the classpath.");
88-
}
89-
this.renjinEngine = (RenjinScriptEngine) engine;
9078
this.programText = programText;
9179
this.stdout = stdout;
9280
this.prePassCode();
@@ -322,39 +310,6 @@ private boolean isMixMode() {
322310
Class closureClass = Closure.class;
323311
return isSameClass(this.renjinEngine.get(Constant.SIZE_NAME), closureClass);
324312
}
325-
326-
/**
327-
*
328-
* @see processing.core.PApplet#focusGained()
329-
*/
330-
@Override
331-
public void focusGained() {
332-
super.focusGained();
333-
this.renjinEngine.put("focused",super.focused);
334-
}
335-
336-
/**
337-
*
338-
* @see processing.core.PApplet#focusLost()
339-
*/
340-
@Override
341-
public void focusLost() {
342-
super.focusLost();
343-
this.renjinEngine.put("focused",super.focused);
344-
}
345-
346-
@Override
347-
public void mouseMoved() {
348-
wrapMouseVariables();
349-
}
350-
351-
private void wrapMouseVariables() {
352-
this.renjinEngine.put("mouseX", mouseX);
353-
this.renjinEngine.put("mouseY", mouseY);
354-
this.renjinEngine.put("pmouseX", pmouseX);
355-
this.renjinEngine.put("pmouseY", pmouseY);
356-
//this.renjinEngine.put("mouseButton", mouseButton);
357-
}
358313

359314
/**
360315
* Set Environment variables in R top context.

src/rprocessing/applet/BuiltinApplet.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
import org.renjin.sexp.StringVector;
44

55
import processing.core.PApplet;
6+
import rprocessing.exception.NotFoundException;
7+
8+
import javax.script.ScriptEngine;
9+
import javax.script.ScriptEngineManager;
10+
import javax.script.ScriptException;
11+
12+
import org.renjin.parser.RParser;
13+
import org.renjin.script.RenjinScriptEngine;
14+
import org.renjin.sexp.Closure;
15+
import org.renjin.sexp.ExpressionVector;
16+
import org.renjin.sexp.FunctionCall;
17+
import org.renjin.sexp.SEXP;
18+
import org.renjin.sexp.Symbol;
619

720
/**
821
* BuiltinApplet is the type to refactor the function calls.
@@ -14,6 +27,20 @@ public class BuiltinApplet extends PApplet {
1427
/*
1528
* TODO: Check for the cast.
1629
*/
30+
/** Engine to interpret R code */
31+
protected final RenjinScriptEngine renjinEngine;
32+
33+
public BuiltinApplet() throws NotFoundException {
34+
// Create a script engine manager.
35+
ScriptEngineManager manager = new ScriptEngineManager();
36+
// Create a Renjin engine.
37+
ScriptEngine engine = manager.getEngineByName("Renjin");
38+
// Check if the engine has loaded correctly.
39+
if (engine == null) {
40+
throw new NotFoundException("Renjin Script Engine not found on the classpath.");
41+
}
42+
this.renjinEngine = (RenjinScriptEngine) engine;
43+
}
1744

1845
public void size(double width, double height) {
1946
super.size((int) width, (int) height);
@@ -34,4 +61,37 @@ public boolean getFocused() {
3461
public double getPI() {
3562
return PI;
3663
}
64+
65+
/**
66+
*
67+
* @see processing.core.PApplet#focusGained()
68+
*/
69+
@Override
70+
public void focusGained() {
71+
super.focusGained();
72+
this.renjinEngine.put("focused",super.focused);
73+
}
74+
75+
/**
76+
*
77+
* @see processing.core.PApplet#focusLost()
78+
*/
79+
@Override
80+
public void focusLost() {
81+
super.focusLost();
82+
this.renjinEngine.put("focused",super.focused);
83+
}
84+
85+
@Override
86+
public void mouseMoved() {
87+
wrapMouseVariables();
88+
}
89+
90+
protected void wrapMouseVariables() {
91+
this.renjinEngine.put("mouseX", mouseX);
92+
this.renjinEngine.put("mouseY", mouseY);
93+
this.renjinEngine.put("pmouseX", pmouseX);
94+
this.renjinEngine.put("pmouseY", pmouseY);
95+
//this.renjinEngine.put("mouseButton", mouseButton);
96+
}
3797
}

0 commit comments

Comments
 (0)