Skip to content

Instantly share code, notes, and snippets.

@hanyuone
Last active January 13, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanyuone/11f4990d83a0831b96f5e65cbb5d3fe3 to your computer and use it in GitHub Desktop.
Save hanyuone/11f4990d83a0831b96f5e65cbb5d3fe3 to your computer and use it in GitHub Desktop.
Processing Stuff
import java.util.ArrayList;
class Button {
float[] coords;
float[] dimensions;
String text;
Runnable function;
float[] centre_coords;
Button(float[] co, float[] dim, String txt, Runnable fn) {
coords = co;
dimensions = dim;
text = txt;
function = fn;
}
void drawText() {
CentreText disp_text = new CentreText(text, 1,
new float[] {centre_coords[0], centre_coords[1]},
new float[] {dimensions[0], dimensions[1]});
disp_text.displayText();
}
void drawButton() {
centre_coords = new float[] {coords[0] + (dimensions[0] / 2),
coords[1] + (dimensions[1] / 2)};
rect(coords[0], coords[1], dimensions[0], dimensions[1]);
drawText();
}
void clicked() {
function.run();
}
}
class ButtonWindow {
ArrayList<float[]> window = new ArrayList<float[]>();
ArrayList<Runnable> window_commands = new ArrayList<Runnable>();
void addButton(Button btn) {
window.add(new float[] {btn.coords[0], btn.coords[1],
btn.coords[0] + btn.dimensions[0],
btn.coords[1] + btn.dimensions[1]});
window_commands.add(btn.function);
}
Runnable checkClick() {
for (int a = 0; a < window.size(); a++) {
if (window.get(a)[0] < mouseX && mouseX < window.get(a)[2] &&
window.get(a)[1] < mouseY && mouseY < window.get(a)[3]) {
return window_commands.get(a);
}
}
return new Runnable() {
public void run() {
}
};
}
}
class CentreText {
String text;
float font_size;
float[] coords;
float[] dimensions;
float text_width;
CentreText(String txt, int size, float[] co, float[] dim) {
coords = co;
text = txt;
font_size = size;
dimensions = dim;
textSize(font_size);
text_width = textWidth(text);
}
void changeFontSize() {
textSize(1);
font_size = (dimensions[0] - (dimensions[0] / 100)) / textWidth(text);
if (font_size > dimensions[1]) {
font_size = dimensions[1] - (dimensions[1] / 100);
}
textSize(font_size);
text_width = textWidth(text);
}
void displayText() {
changeFontSize();
fill(#000000);
text(text, coords[0] - (text_width / 2), coords[1] + (font_size / 3));
}
}
class ProgressBar {
float[] dimensions;
float[] coords;
int colour;
ProgressBar(float[] dim, float[] co, int col) {
dimensions = dim;
coords = co;
colour = col;
}
void fill_bar() {
fill(#ffffff);
rect(coords[0], coords[1], dimensions[0], dimensions[1]);
}
void update(float percent) {
float progress_width;
float x_axis;
String percent_str = Double.toString(Math.ceil(percent * 100)) + "%";
if (percent < 0.5) {
progress_width = dimensions[0] * (1 - percent);
x_axis = coords[0] + (dimensions[0] * (1 + percent) / 2);
} else {
progress_width = dimensions[0] * percent;
x_axis = coords[0] + (progress_width / 2);
}
fill_bar();
fill(colour);
rect(coords[0], coords[1], dimensions[0] * percent, dimensions[1]);
CentreText disp_percent = new CentreText(percent_str, 1,
new float[] {x_axis,
coords[1] + (dimensions[1] / 2)},
new float[] {progress_width, dimensions[1]});
disp_percent.displayText();
}
}
ButtonWindow window = new ButtonWindow();
Button start = new Button(new float[] {10, 10},
new float[] {100, 50}, "Start",
new Runnable() {
public void run() {
progress_inc = 1;
}
});
Button end = new Button(new float[] {120, 10},
new float[] {100, 50}, "End",
new Runnable() {
public void run() {
progress_inc = 0;
}
});
ProgressBar other = new ProgressBar(new float[] {200, 20},
new float[] {590, 10},
#ff00ff);
float progress = 0;
float progress_inc = 0;
boolean click = true;
long start_time = System.currentTimeMillis();
int start_count = 0;
void process_prog() {
progress = (progress + progress_inc) % 100;
other.update(progress / 100);
}
void setup() {
size(800, 800);
start.drawButton();
window.addButton(start);
end.drawButton();
window.addButton(end);
other.update(0);
}
void draw() {
if (mousePressed && click) {
window.checkClick().run();
click = false;
} else if (!mousePressed) {
click = true;
}
if ((System.currentTimeMillis() - start_time) / 100 > start_count) {
start_count += 1;
process_prog();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment