Skip to content

Commit 8c50e2c

Browse files
author
monkstone
committed
configure gui for JRubyArt
1 parent 4056dcb commit 8c50e2c

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

samples/JRubyArt/JRubyArt.pde

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import java.io.File;
2+
3+
Button enter, nojruby, check;
4+
String processingRoot = "enter your processing root here"; // edit this line in the sketch
5+
String done = "Done";
6+
String OS = System.getProperty("os.name").toLowerCase();
7+
String home, suggestion, separator, root, sketchbookPath;
8+
PFont font;
9+
float rectX, rectX2, rectX3, rectY; // Position of buttons
10+
float rectHeight = 30; // height of rect
11+
float rectWidth = 90; // width of rect
12+
int rectColor, rectColor2, rectColor3;
13+
int rectHighlight, rectHighlight2, rectHighlight3;
14+
int currentColor;
15+
int selectedColor, selectedColor2, selectedColor3;
16+
boolean acceptOver = false;
17+
boolean noJruby = false;
18+
boolean selected = false;
19+
boolean configCheck = false;
20+
String jruby = "true";
21+
22+
23+
void setup() {
24+
size(600, 200);
25+
home = System.getProperty("user.home");
26+
File f = new File(home);
27+
root = f.getParent();
28+
separator = System.getProperty("file.separator");
29+
font = createFont("Helvetica", 18);
30+
if (OS.contains("mac")) {
31+
suggestion = "/Applications/Processing.app/Contents/Resources/Java";
32+
sketchbookPath = home + separator + "Documents/Processing/sketchbook";
33+
} else if (OS.contains("windows")) {
34+
sketchbookPath = home + separator + "Documents" + separator + "sketchbook";
35+
suggestion = home + separator + "processing-3.0";
36+
} else {
37+
sketchbookPath = home + separator + "sketchbook";
38+
suggestion = home + separator + "processing-3.0";
39+
}
40+
rectColor = color(140);
41+
rectColor2 = color(140);
42+
rectColor3 = color(140);
43+
rectHighlight = color(100);
44+
rectHighlight2 = color(100);
45+
rectHighlight3 = color(100);
46+
selectedColor = color(0);
47+
selectedColor2 = color(0);
48+
selectedColor3 = color(0);
49+
rectX = rectWidth + 20;
50+
rectX2 = rectWidth + 150;
51+
rectX3 = rectWidth + 300;
52+
rectY = height * 0.8 - rectHeight / 4;
53+
enter = new Button(rectX2, rectY, rectWidth, rectHeight, "enter");
54+
nojruby = new Button(rectX, rectY, rectWidth, rectHeight, "nojruby");
55+
check = new Button(rectX3, rectY, rectWidth, rectHeight, "check");
56+
}
57+
58+
void draw() {
59+
background(200);
60+
fill(0, 0, 200);
61+
text("Suggestion:", 35, 28);
62+
text(suggestion, 35, 56);
63+
textFont(font, 18);
64+
fill(255, 0, 0);
65+
// this adds a blinking cursor after your text, at the expense of redrawing everything every frame
66+
text(processingRoot + (frameCount / 10 % 2 == 0 ? "_" : ""), 35, 100);
67+
fill(0, 0, 200);
68+
text("Select nojruby to use jruby-complete by default", 35, 140);
69+
update(mouseX, mouseY);
70+
71+
if (acceptOver) {
72+
enter.draw(rectHighlight);
73+
nojruby.draw(rectHighlight2);
74+
check.draw(rectHighlight3);
75+
} else {
76+
enter.draw(rectColor);
77+
nojruby.draw(rectColor2);
78+
check.draw(rectColor3);
79+
}
80+
}
81+
82+
void writeRoot() {
83+
rectColor = selectedColor;
84+
rectHighlight = selectedColor;
85+
String folder = home + separator + ".jruby_art";
86+
File file = new File(folder);
87+
if (!file.exists()) {
88+
if (file.mkdir()) {
89+
System.out.println("Directory is created!");
90+
} else {
91+
System.out.println("Failed to create directory!");
92+
}
93+
}
94+
String config = folder + separator + "config.yml";
95+
File yaml = new File(config);
96+
97+
if (!yaml.exists()) {
98+
PrintWriter writer = createWriter(config);
99+
writer.println(String.format("PROCESSING_ROOT: %s", processingRoot));
100+
writer.println(String.format("JRUBY: %s", jruby));
101+
writer.println(String.format("sketchbook_path: %s", sketchbookPath));
102+
processingRoot = done;
103+
}
104+
}
105+
106+
void keyReleased() {
107+
if (key != CODED) {
108+
switch (key) {
109+
case BACKSPACE:
110+
processingRoot = processingRoot.substring(0, max(0, processingRoot.length() - 1));
111+
break;
112+
case ENTER: // save the processing root to the config file
113+
case RETURN:
114+
writeRoot();
115+
break;
116+
case ESC:
117+
case DELETE:
118+
break;
119+
default:
120+
processingRoot += key;
121+
}
122+
}
123+
}
124+
125+
void update(float x, float y) {
126+
acceptOver = enter.overRect();
127+
noJruby = nojruby.overRect();
128+
}
129+
130+
131+
132+
void mouseClicked() {
133+
update(mouseX, mouseY);
134+
if (acceptOver) {
135+
rectColor = selectedColor;
136+
rectHighlight = selectedColor;
137+
writeRoot();
138+
} else if (noJruby) {
139+
rectColor2 = selectedColor2;
140+
rectHighlight2 = selectedColor2;
141+
jruby = "false";
142+
} else if (!configCheck) {
143+
rectColor3 = selectedColor3;
144+
rectHighlight3 = selectedColor3;
145+
configCheck = true;
146+
}
147+
}
148+
149+
class Button {
150+
151+
float x, y, w, h;
152+
String text;
153+
154+
Button(float x, float y, float w, float h, String text) {
155+
this.x = x;
156+
this.y = y;
157+
this.w = w;
158+
this.h = h;
159+
this.text = text;
160+
}
161+
162+
void draw(int col) {
163+
fill(col);
164+
rect(x, y, w, h, 20, 20, 20, 20);
165+
fill(255);
166+
text(text, x + 8, y + 20);
167+
}
168+
169+
boolean overRect() {
170+
return (mouseX >= x && mouseX <= x + w
171+
&& mouseY >= y && mouseY <= y + h);
172+
}
173+
}

0 commit comments

Comments
 (0)