Skip to content

Commit 0f95bfd

Browse files
committed
add simple mix mode
1 parent c7ce229 commit 0f95bfd

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/rprocessing/RLangPApplet.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.renjin.sexp.SEXP;
1212

1313
import processing.core.PApplet;
14+
import rprocessing.util.Constant;
1415

1516
/**
1617
* RlangPApplet
@@ -34,15 +35,6 @@ private enum Mode {
3435
// definitions, which we then invoke during the run loop.
3536
private final Mode mode;
3637

37-
/** The name of processing's PApplet in R top context. */
38-
private static final String PROCESSING_VAR_NAME = "processing";
39-
40-
private static final String SETTINGS_NAME = "settings";
41-
42-
private static final String SETUP_NAME = "setup";
43-
44-
private static final String DRAW_NAME = "draw";
45-
4638
/** Program Code */
4739
private final String programText;
4840

@@ -79,6 +71,9 @@ public void prePassCode() {
7971
*/
8072
private Mode detectMode() {
8173
if (isActiveMode()) {
74+
if (isMixMode()) {
75+
return Mode.MIXED;
76+
}
8277
return Mode.ACTIVE;
8378
}
8479
return Mode.STATIC;
@@ -89,15 +84,15 @@ private Mode detectMode() {
8984
* Notice: DO NOT do it in constructor.
9085
*/
9186
public void AddPAppletToRContext() {
92-
this.renjinEngine.put(PROCESSING_VAR_NAME, this);
87+
this.renjinEngine.put(Constant.PROCESSING_VAR_NAME, this);
9388
}
9489

9590
/**
9691
* @see processing.core.PApplet#settings()
9792
*/
9893
@Override
9994
public void settings() {
100-
Object obj = this.renjinEngine.get(SETTINGS_NAME);
95+
Object obj = this.renjinEngine.get(Constant.SETTINGS_NAME);
10196
if (obj.getClass().equals(Closure.class)) {
10297
((Closure) obj).doApply(this.renjinEngine.getTopLevelContext());
10398
} else if (mode == Mode.STATIC) {
@@ -119,12 +114,12 @@ public void setup() {
119114
System.out.println(e);
120115
}
121116
} else if (this.mode == Mode.ACTIVE) {
122-
Object obj = this.renjinEngine.get(SETUP_NAME);
117+
Object obj = this.renjinEngine.get(Constant.SETUP_NAME);
123118
if (obj.getClass().equals(Closure.class)) {
124119
((Closure) obj).doApply(this.renjinEngine.getTopLevelContext());
125120
}
126121
} else {
127-
// TODO: implement MIX Mode.
122+
System.out.println("The program is in mix mode now.");
128123
}
129124
}
130125

@@ -134,7 +129,7 @@ public void setup() {
134129
*/
135130
@Override
136131
public void draw() {
137-
Object obj = this.renjinEngine.get(DRAW_NAME);
132+
Object obj = this.renjinEngine.get(Constant.DRAW_NAME);
138133
if (obj.getClass().equals(Closure.class)) {
139134
((Closure) obj).doApply(this.renjinEngine.getTopLevelContext());
140135
}
@@ -152,14 +147,30 @@ public void draw() {
152147
@SuppressWarnings("rawtypes")
153148
private boolean isActiveMode() {
154149
Class closureClass = Closure.class;
155-
if (isSameClass(this.renjinEngine.get(SETTINGS_NAME), closureClass)
156-
|| isSameClass(this.renjinEngine.get(SETUP_NAME), closureClass)
157-
|| isSameClass(this.renjinEngine.get(DRAW_NAME), closureClass)) {
150+
if (isSameClass(this.renjinEngine.get(Constant.SETTINGS_NAME), closureClass)
151+
|| isSameClass(this.renjinEngine.get(Constant.SETUP_NAME), closureClass)
152+
|| isSameClass(this.renjinEngine.get(Constant.DRAW_NAME), closureClass)) {
158153
return true;
159154
}
160155
return false;
161156
}
162157

158+
/**
159+
* Detect whether the program is in mix mode.
160+
* After: isActiveMode()
161+
*
162+
* @return
163+
*/
164+
@SuppressWarnings("rawtypes")
165+
private boolean isMixMode() {
166+
Class closureClass = Closure.class;
167+
if (isSameClass(this.renjinEngine.get(Constant.SIZE_NAME), closureClass)) {
168+
return true;
169+
}
170+
171+
return false;
172+
}
173+
163174
/**
164175
* Set Environment variables in R top context.
165176
*/

src/rprocessing/util/Constant.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package rprocessing.util;
2+
3+
/**
4+
*
5+
* @author github.com/gaocegege
6+
*/
7+
public class Constant {
8+
/** The name of processing's PApplet in R top context. */
9+
public static final String PROCESSING_VAR_NAME = "processing";
10+
11+
public static final String SETTINGS_NAME = "settings";
12+
13+
public static final String SETUP_NAME = "setup";
14+
15+
public static final String DRAW_NAME = "draw";
16+
17+
public static final String SIZE_NAME = "size";
18+
}

0 commit comments

Comments
 (0)