Skip to content

Commit

Permalink
new example: UtilityMethods, #288
Browse files Browse the repository at this point in the history
  • Loading branch information
KrabCode committed Feb 10, 2024
1 parent cfd7522 commit d387bdf
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import com.krab.lazy.*;

LazyGui gui;

void setup() {
size(800, 800, P2D);
gui = new LazyGui(this);
}

void draw() {
background(gui.colorPicker("background", color(36)).hex);
translate(width/2, height/2);

gui.pushFolder("rect");
pushMatrix();
transform();
PVector size = gui.plotXY("size", 300, 100);
style();
rect(0, 0, size.x, size.y);
popMatrix();
gui.popFolder();

gui.pushFolder("text");
pushMatrix();
transform();
font();
text(gui.text("text", "hello"), 0, 0);
popMatrix();
gui.popFolder();
}

void transform() {
PVector pos = gui.plotXY("pos");
translate(pos.x, pos.y);
rotate(gui.slider("rotate"));
}

void style() {
gui.pushFolder("style");
strokeWeight(gui.slider("weight", 4));
stroke(gui.colorPicker("stroke", color(0)).hex);
fill(gui.colorPicker("fill", color(200)).hex);
String rectMode = gui.radio("rect mode", new String[]{"center", "corner"});
if("center".equals(rectMode)){
rectMode(CENTER);
}else{
rectMode(CORNER);
}
gui.popFolder();
}

// font() related fields
HashMap<String, PFont> fontCache = new HashMap<String, PFont>();
HashMap<String, Integer> xAligns;
HashMap<String, Integer> yAligns;

void font() {
gui.pushFolder("font");
fill(gui.colorPicker("fill", color(0)).hex);
int size = gui.sliderInt("size", 64);
if (xAligns == null || yAligns == null) {
xAligns = new HashMap<String, Integer>();
xAligns.put("left", LEFT);
xAligns.put("center", CENTER);
xAligns.put("right", RIGHT);
yAligns = new HashMap<String, Integer>();
yAligns.put("top", TOP);
yAligns.put("center", CENTER);
yAligns.put("bottom", BOTTOM);
}
String xAlignSelection = gui.radio("align x", xAligns.keySet().toArray(new String[0]), "center");
String yAlignSelection = gui.radio("align y", yAligns.keySet().toArray(new String[0]), "center");
textAlign(xAligns.get(xAlignSelection), yAligns.get(yAlignSelection));
String fontName = gui.text("font name", "Arial");
if (gui.button("list fonts")) {
String[] fonts = PFont.list();
for (String font : fonts) {
println(font);
}
}
String fontKey = fontName + " | " + size;
if (!fontCache.containsKey(fontKey)) {
PFont loadedFont = createFont(fontName, size);
fontCache.put(fontKey, loadedFont);
}
PFont cachedFont = fontCache.get(fontKey);
textFont(cachedFont);
gui.popFolder();
}

0 comments on commit d387bdf

Please sign in to comment.