@@ -37,6 +37,12 @@ private enum Mode {
3737 /** The name of processing's PApplet in R top context. */
3838 private static final String PROCESSING_VAR_NAME = "processing" ;
3939
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+
4046 /** Program Code */
4147 private final String programText ;
4248
@@ -46,8 +52,9 @@ private enum Mode {
4652 public RLangPApplet (final ScriptEngine renjinEngine , final String programText ) {
4753 this .renjinEngine = (RenjinScriptEngine ) renjinEngine ;
4854 this .programText = programText ;
49- this .mode = this .detectMode ();
5055 this .prePassCode ();
56+ // Detect the mode after pre-pass program code.
57+ this .mode = this .detectMode ();
5158 }
5259
5360 /**
@@ -67,9 +74,13 @@ public void prePassCode() {
6774 }
6875
6976 /**
70- * TODO: Detect the mode.
77+ * Detect the mode.
78+ * After: prePassCode()
7179 */
7280 private Mode detectMode () {
81+ if (isActiveMode ()) {
82+ return Mode .ACTIVE ;
83+ }
7384 return Mode .STATIC ;
7485 }
7586
@@ -82,13 +93,12 @@ public void AddPAppletToRContext() {
8293 }
8394
8495 /**
85- * TODO: Evaluate settings before the main program.
8696 * @see processing.core.PApplet#settings()
8797 */
8898 @ Override
8999 public void settings () {
90- Object obj = this .renjinEngine .get ("settings" );
91- if (obj .getClass () == Closure .class ) {
100+ Object obj = this .renjinEngine .get (SETTINGS_NAME );
101+ if (obj .getClass (). equals ( Closure .class ) ) {
92102 ((Closure ) obj ).doApply (this .renjinEngine .getTopLevelContext ());
93103 } else if (mode == Mode .STATIC ) {
94104 // TODO: Implement Static Mode.
@@ -101,18 +111,16 @@ public void settings() {
101111 */
102112 @ Override
103113 public void setup () {
114+ wrapProcessingVariables ();
104115 if (this .mode == Mode .STATIC ) {
105116 try {
106- wrapProcessingVariables ();
107117 this .renjinEngine .eval (this .programText );
108- // System.out.println(this.renjinEngine.getTopLevelContext().getEnvironment()
109- // .getVariable("settings"));
110118 } catch (ScriptException e ) {
111119 System .out .println (e );
112120 }
113121 } else if (this .mode == Mode .ACTIVE ) {
114- Object obj = this .renjinEngine .get ("setup" );
115- if (obj .getClass () == Closure .class ) {
122+ Object obj = this .renjinEngine .get (SETUP_NAME );
123+ if (obj .getClass (). equals ( Closure .class ) ) {
116124 ((Closure ) obj ).doApply (this .renjinEngine .getTopLevelContext ());
117125 }
118126 } else {
@@ -126,8 +134,8 @@ public void setup() {
126134 */
127135 @ Override
128136 public void draw () {
129- Object obj = this .renjinEngine .get ("draw" );
130- if (obj .getClass () == Closure .class ) {
137+ Object obj = this .renjinEngine .get (DRAW_NAME );
138+ if (obj .getClass (). equals ( Closure .class ) ) {
131139 ((Closure ) obj ).doApply (this .renjinEngine .getTopLevelContext ());
132140 }
133141 }
@@ -136,6 +144,22 @@ public void draw() {
136144 * Helper functions
137145 */
138146
147+ /**
148+ * Detect whether the program is in active mode.
149+ *
150+ * @return
151+ */
152+ @ SuppressWarnings ("rawtypes" )
153+ private boolean isActiveMode () {
154+ 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 )) {
158+ return true ;
159+ }
160+ return false ;
161+ }
162+
139163 /**
140164 * Set Environment variables in R top context.
141165 */
@@ -150,6 +174,18 @@ protected void wrapProcessingVariables() {
150174 this .renjinEngine .put ("frameRate" , frameRate );
151175 }
152176
177+ /**
178+ * Return whether the object has same class with clazz.
179+ *
180+ * @param obj
181+ * @param clazz
182+ * @return
183+ */
184+ @ SuppressWarnings ("rawtypes" )
185+ private static boolean isSameClass (Object obj , Class clazz ) {
186+ return obj .getClass ().equals (clazz );
187+ }
188+
153189 /*
154190 * Wrapper functions
155191 *
0 commit comments