Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ProcessingShapeModeler by Jack Davenport
- * 2019
- */
- ArrayList<PVector> positions = new ArrayList<PVector>();
- PImage backgroundImage;
- // settings
- boolean joinWithFirst = true;
- boolean fillShape = true;
- boolean strokeShape = true;
- boolean outputTexCoords = true;
- float zoom = 1f;
- int selectedVertex = -1;
- boolean deleteFlag = false;
- boolean hideGui = false;
- // pan vars
- float panX = 0f, panY = 0f;
- float offX = 0f, offY = 0f;
- boolean shiftPressed = false;
- // handle vars
- boolean xHandled = false;
- boolean yHandled = false;
- float dmx, dmy, dpx, dpy;
- void settings() {
- size(1024, 640);
- }
- void setup() {
- }
- void draw() {
- float oriX = width / 2 + panX;
- float oriY = height / 2 + panY;
- // background colour
- background(200);
- pushMatrix();
- {
- translate(oriX, oriY);
- scale(zoom);
- if (backgroundImage != null) {
- imageMode(CENTER);
- image(backgroundImage, offX, offY);
- }
- strokeWeight(1/zoom);
- stroke(150);
- for (float x = -100f; x <= 100f; x += 10f) {
- line(x, -100f, x, 100f);
- }
- for (float y = -100f; y <= 100f; y += 10f) {
- line(-100f, y, 100f, y);
- }
- }
- popMatrix();
- // draw shape
- strokeWeight(1/zoom);
- if (strokeShape) {
- stroke(0);
- } else {
- noStroke();
- }
- if (fillShape) {
- fill(128, 128, 128, 128);
- } else {
- noFill();
- }
- if (!positions.isEmpty()) {
- pushMatrix();
- {
- translate(oriX, oriY);
- scale(zoom);
- // draw shapes
- beginShape();
- for (PVector pos : positions) {
- vertex(pos.x, pos.y);
- }
- if (joinWithFirst) {
- PVector first = positions.get(0);
- vertex(first.x, first.y);
- }
- endShape();
- // show vertices
- strokeWeight(6/zoom);
- boolean flag = false;
- for (int i = 0; i < positions.size(); i++) {
- PVector pos = positions.get(i);
- if (selectedVertex == i) {
- stroke(255, 255, 0);
- } else if (!flag && dist(oriX+pos.x*zoom, oriY+pos.y*zoom, mouseX, mouseY) < 6) {
- stroke(0, 255, 0);
- flag = true;
- } else {
- stroke(0);
- }
- point(pos.x, pos.y);
- }
- }
- popMatrix();
- }
- // show origin point
- strokeWeight(5);
- stroke(255, 0, 0);
- point(oriX, oriY);
- // show position controls
- if (selectedVertex != -1) {
- PVector pos = positions.get(selectedVertex);
- pushMatrix();
- {
- translate(oriX, oriY);
- translate(pos.x*zoom, pos.y*zoom);
- float mx = mouseX - (oriX + pos.x * zoom);
- float my = mouseY - (oriY + pos.y * zoom);
- strokeWeight(1);
- // x-axis
- stroke(255, 0, 0);
- fill(255, 0, 0);
- if (xHandled || (mx > 40 && mx < 50 && my > -10 && my < 10)) {
- stroke(255, 255, 0);
- fill(255, 255, 0);
- }
- line(6, 0, 40, 0);
- triangle(40, 10, 40, -10, 50, 0);
- // y-axis
- stroke(0, 255, 0);
- fill(0, 255, 0);
- if (yHandled || (mx > -10 && mx < 10 && my > 40 && my < 50)) {
- stroke(255, 255, 0);
- fill(255, 255, 0);
- }
- line(0, 6, 0, 40);
- triangle(10, 40, -10, 40, 0, 50);
- }
- popMatrix();
- // do dragging
- if (xHandled) {
- float dx = mouseX - dmx;
- pos.x = dpx + dx / zoom;
- }
- if (yHandled) {
- float dy = mouseY - dmy;
- pos.y = dpy + dy / zoom;
- }
- }
- // deletion
- if (deleteFlag && selectedVertex != -1) {
- positions.remove(selectedVertex);
- selectedVertex = -1;
- deleteFlag = false;
- }
- // show gui
- if (!hideGui) {
- fill(0);
- textSize(20);
- textAlign(LEFT, TOP);
- text("Vertex count: " + (positions.size() + (joinWithFirst && !positions.isEmpty() ? 1 : 0)), 10, 10);
- text("Join with first vertex? " + (joinWithFirst ? "yes" : "no"), 10, 30);
- text("Fill shape (editor only)? " + (fillShape ? "yes" : "no"), 10, 50);
- text("Stroke shape (editor only)? " + (strokeShape ? "yes" : "no"), 10, 70);
- if (backgroundImage != null) {
- text("Output texture coordinates? " + (outputTexCoords ? "yes" : "no"), 10, 90);
- }
- textAlign(LEFT, BOTTOM);
- text("Controls", 10, height-250);
- text("P: print shape to console (shift to save to file)", 10, height-230);
- text("U: undo last vertex", 10, height-210);
- text("J: toggle join with first", 10, height-190);
- text("B: select background image", 10, height-170);
- text("N: clear background image", 10, height-150);
- text("F: toggle fill", 10, height-130);
- text("S: toggle stroke", 10, height-110);
- text("T: toggle output texture coordinates", 10, height-90);
- text("H: hide/show GUI", 10, height-70);
- text("Del/Backspace: Delete selected vertex", 10, height-50);
- text("Arrow Keys: Pan camera", 10, height-30);
- text("Shift + Arrow Keys: Pan texture", 10, height-10);
- }
- }
- void mousePressed() {
- float oriX = width/2 + panX;
- float oriY = height/2 + panY;
- int idx = -1;
- for (int i = 0; i < positions.size(); i++) {
- PVector pos = positions.get(i);
- if (dist(oriX+pos.x*zoom, oriY+pos.y*zoom, mouseX, mouseY) < 6) {
- idx = i;
- break;
- }
- }
- if (idx != -1) {
- selectedVertex = idx;
- return;
- }
- if (selectedVertex == -1) {
- float relX = (mouseX - oriX) / zoom;
- float relY = (mouseY - oriY) / zoom;
- positions.add(0, new PVector(relX, relY));
- } else {
- PVector pos = positions.get(selectedVertex);
- float mx = mouseX - (oriX + pos.x * zoom);
- float my = mouseY - (oriY + pos.y * zoom);
- // x handle
- if (mx > 40 && mx < 50 && my > -10 && my < 10) {
- xHandled = true;
- dmx = mouseX;
- dmy = mouseY;
- dpx = pos.x;
- dpy = pos.y;
- }
- // y handle
- else if (mx > -10 && mx < 10 && my > 40 && my < 50) {
- yHandled = true;
- dmx = mouseX;
- dmy = mouseY;
- dpx = pos.x;
- dpy = pos.y;
- } else {
- selectedVertex = -1;
- }
- }
- }
- void keyPressed() {
- if (key == 'p') {
- printShape(false);
- } else if(key == 'P') {
- printShape(true);
- } else if (key == 'u' || key == 'U') {
- if (positions.isEmpty()) {
- return;
- }
- positions.remove(0);
- } else if (key == 'j' || key == 'J') {
- joinWithFirst = !joinWithFirst;
- } else if (key == 'b' || key == 'B') {
- selectInput("Select a background image", "onBackgroundSelected");
- } else if (key == 'n' || key == 'N') {
- backgroundImage = null;
- } else if (key == 'f' || key == 'F') {
- fillShape = !fillShape;
- } else if (key == 's' || key == 'S') {
- strokeShape = !strokeShape;
- } else if (keyCode == BACKSPACE || keyCode == DELETE) {
- if (selectedVertex != -1) {
- deleteFlag = true;
- }
- } else if ((key == 't' || key == 'T') && backgroundImage != null) {
- outputTexCoords = !outputTexCoords;
- } else if (keyCode == LEFT) {
- if (shiftPressed) offX += 2f;
- else panX += 2f;
- } else if (keyCode == RIGHT) {
- if (shiftPressed) offX -= 2f;
- else panX -= 2f;
- } else if (keyCode == UP) {
- if (shiftPressed) offY += 2f;
- else panY += 2f;
- } else if (keyCode == DOWN) {
- if (shiftPressed) offY -= 2f;
- else panY -= 2f;
- } else if (keyCode == SHIFT) {
- shiftPressed = true;
- } else if(key == 'h' || key == 'H') {
- hideGui = !hideGui;
- }
- }
- void keyReleased() {
- if (keyCode == SHIFT) {
- shiftPressed = false;
- }
- }
- void mouseWheel(MouseEvent e) {
- zoom += (float)e.getCount() * 0.1f;
- zoom = min(max(zoom, 0.1f), 10f);
- }
- void mouseReleased() {
- if (xHandled) {
- xHandled = false;
- }
- if (yHandled) {
- yHandled = false;
- }
- }
- void printShape(boolean file) {
- String out = "";
- if (!positions.isEmpty()) {
- boolean includeUVs = backgroundImage != null && outputTexCoords;
- float texWidth = includeUVs ? backgroundImage.width * 0.5f : 0f;
- float texHeight = includeUVs ? backgroundImage.height * 0.5f : 0f;
- out += "/* -------- CREATED WITH ProcessingShapeModeler ------- */\n";
- if (includeUVs) {
- out += "/* WARNING: this shape uses textures, and requires P2D to render correctly! */\n";
- out += "PImage tex = ... // provide your own image\n";
- }
- out += "PShape shape = createShape();\n";
- out += "shape.beginShape();\n";
- if (includeUVs) {
- out += "shape.texture(tex);\n";
- out += "shape.textureMode(NORMAL);\n";
- }
- for (PVector pos : positions) {
- out += printVertex(pos, includeUVs, texWidth, texHeight);
- }
- if (joinWithFirst) {
- out += printVertex(positions.get(0), includeUVs, texWidth, texHeight);
- }
- out += "shape.endShape();\n";
- } else {
- out += "/* ERROR: please create some vertices first! */\n";
- }
- if(file) {
- PrintWriter writer = createWriter("shape.pde");
- writer.print(out);
- writer.flush();
- writer.close();
- println("// saved to shape.pde");
- } else {
- println(out);
- }
- }
- String printVertex(PVector vector, boolean includeUVs, float texWidth, float texHeight) {
- String out = "";
- out += "shape.vertex(" + vector.x + ", " + vector.y;
- if (includeUVs) {
- float u = ((vector.x - offX) / texWidth) * 0.5f + 0.5f;
- float v = ((vector.y - offY) / texHeight) * 0.5f + 0.5f;
- out += ", " + u + ", " + v;
- }
- return out + ");\n";
- }
- void onBackgroundSelected(File backgroundFile) {
- try {
- backgroundImage = loadImage(backgroundFile.getAbsolutePath());
- }
- catch(Exception e) {
- println("ERROR: invalid background image file: " + backgroundFile.getAbsolutePath());
- backgroundImage = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement