boolean drawRequested = true; boolean startingUp = true; boolean showFileChooser = false; PFont defaultFont10; PFont defaultFont16; File file; color latticeColor = color(0,0,0,255); color textColor = color(0,0,0,255); color moduleMouseOverColor = color(255,230,230,255); color moduleMouseOverInvisible = color(255,230,230,40); color moduleColor = color(200,200,200,255); int moduleWidth = 60; int moduleHeight = 60; int roundedness = 5; int pad = 10; int visibleRows = 0; int visibleCols = 0; //LinkedList gridModules = new LinkedList(); final int MAX_ROWS = 10; final int MAX_COLS = 15; GridModule[][] allGridModules = new GridModule[MAX_ROWS][MAX_COLS]; // index: [row][col] Point mouseDownPoint = new Point(mouseX, mouseY); void setup(){ background(0,0,0); size(610, 407); frameRate(20); defaultFont10 = loadFont("LucidaSans-10.vlw"); defaultFont16 = loadFont("LucidaSans-16.vlw"); textFont(defaultFont10, 10); } void draw(){ if(startingUp){ // initial background paint seems to get missed.. // just a hack to make it pretty for now. delay(250); background(0,0,0); println("starting up... done"); initGridModules(); buildModuleList(); loadExampleModules(); // load Example startingUp = false; } if(drawRequested){ if(showFileChooser){ renderFileChooser(); }else{ renderAll(); } drawRequested = false; } } void loadExampleModules(){ allGridModules[1][0].visible = true; allGridModules[0][1].visible = true; allGridModules[2][1].visible = true; allGridModules[2][2].visible = true; allGridModules[3][1].visible = true; allGridModules[3][2].visible = true; allGridModules[1][3].visible = true; allGridModules[2][4].visible = true; allGridModules[0][5].visible = true; allGridModules[1][5].visible = true; } void mousePressed(){ if(showFileChooser){ if(overOK()){ // handle file selection showFileChooser = false; drawRequested = true; } if(overCancel()){ // handle Cancel showFileChooser = false; drawRequested = true; } return; } mouseDownPoint.x = mouseX; mouseDownPoint.y = mouseY; if(mouseButton == RIGHT){ for(int row=0; row Bounds x(" + b.minX + "," + b.maxX + ") y(" + b.minY + "," + b.maxY + ")"); int picWidth = b.maxX - b.minX; int picHeight = b.maxY - b.minY; println(" --> ideal picture would be: (" + picWidth + "," + picHeight + ")"); //showFileChooser = true; //drawRequested = true; } } } } } void renderFileChooser(){ file = new File("."); background(latticeColor); drawPath(); drawCancel(); drawOK(); if(file.isDirectory()){ println("file was a directory... listing files"); File[] files = file.listFiles(); for(int i=0; i width-225 && mouseX < width-150 && mouseY > height-50 && mouseY < height-25); } boolean overCancel(){ return (mouseX > width-125 && mouseX < width-25 && mouseY > height-50 && mouseY < height-25); } public class ImageFilter implements FilenameFilter{ public boolean accept(File f, String s){ if(f.getName().toLowerCase().endsWith(".jpg") || f.getName().toLowerCase().endsWith(".txt") || f.isDirectory()){ return true; } return false; } } void mouseReleased(){ if(showFileChooser){ return; } if(mouseDownPoint.distToXY(mouseX, mouseY) < 5.0 && mouseButton == LEFT){ // clicking occured without any dragging. for(int row=0; row 0){ // merge to right rect(x+gmWidth-roundedness, y, pad+2*roundedness, gmHeight); }else{ // merge to left rect(x-pad-roundedness, y, pad+2*roundedness, gmHeight); } }else{ if(rowDif > 0){ // merge down rect(x, y+gmHeight-roundedness, gmWidth, pad+2*roundedness); }else{ // merge up rect(x, y-pad-roundedness, gmWidth, pad+2*roundedness); } } } if(mouseIsOver){ fill(moduleMouseOverColor); }else{ fill(moduleColor); } roundedRect(x,y,gmWidth,gmHeight,roundedness); }else{ noStroke(); fill(latticeColor); rect(x, y, gmWidth+1, gmHeight+1); if(mouseIsOver && (x+gmWidth+pad) <= width && (y+gmHeight+pad) <= height){ fill(moduleMouseOverInvisible); roundedRect(x,y,gmWidth,gmHeight,roundedness); } } } public void handleMouseMove(int mx, int my){ if(pointInBounds(mx, my)){ mouseIsOver = true; drawModule(); }else{ if(mouseIsOver){ // mouse was over previously, but not anymore.. deselect it. mouseIsOver = false; drawModule(); } } } public void handleMouseClick(int mx, int my){ if(mouseIsOver){ visible = !visible; // remove all merges that point to this.. // and then clear this's merge list Iterator iter = mergedNeighbors.iterator(); while(iter.hasNext()){ GridModule gm = (GridModule)iter.next(); gm.removeMerge(this); boolean v = gm.visible; gm.visible = false; gm.drawModule(); gm.visible = v; gm.drawModule(); gm.drawModule(); } mergedNeighbors.clear(); clearLattice(); drawModule(); } } private void clearLattice(){ fill(latticeColor); rect(x-pad, y-pad, gmWidth+2*pad, pad); rect(x-pad, y-pad, pad, gmHeight+2*pad); rect(x+gmWidth, y-pad, pad, gmHeight+2*pad); rect(x-pad, y+gmHeight, gmWidth+2*pad, pad); } public void handleMouseDragged(int mx, int my){ if(visible && mouseIsOver){ // mouseIsOver only "true" for the element that the drag started on. :) for(int row=0; row 0); } public boolean pointInBounds(int px, int py){ return (px > x && px < x+gmWidth && py > y && py < y+gmHeight); } public void includeInBounds(Bounds b){ b.includePoint(x,y); b.includePoint(x+gmWidth, y+gmHeight); } } /** * Draw a rounded rectangle. oh, so smooth and sensual! :] * looks best with smooth() enabled. * Corner smoothness is given directly */ void roundedRect(int x, int y, int rWidth, int rHeight, int rounded){ noStroke(); rect(x+rounded, y, rWidth-2*rounded+1, rHeight); rect(x, y+rounded, rWidth, rHeight-2*rounded+1); //line(x+rounded, y, x+rWidth-rounded, y); //line(x+rounded, y+rHeight, x+rWidth-rounded, y+rHeight); //line(x, y+rounded, x, y+rHeight-rounded); //line(x+rWidth, y+rounded, x+rWidth, y+rHeight-rounded); arc(x+rounded, y+rounded, 2*rounded, 2*rounded, PI, 3.0*PI/2.0); arc(x+rounded, y+rHeight-rounded, 2*rounded, 2*rounded, PI/2.0, PI); arc(x+rWidth-rounded, y+rHeight-rounded, 2*rounded, 2*rounded, 0.0, PI/2.0); arc(x+rWidth-rounded, y+rounded, 2*rounded, 2*rounded, 3.0*PI/2.0, 0.0); } /** * Draw a rounded rectangle. oh, so smooth and sensual! :] * looks best with smooth() enabled. * Corner smoothness is a percent of the Math.min(rectWidth,rectHeight); */ void roundedRectPercent(int x, int y, int rWidth, int rHeight, int cornerPercent){ int rounded = (int)(Math.max(Math.min(cornerPercent/100.0,1.0),0.0)/2.0*Math.min(rWidth, rHeight)); roundedRect(x, y, rWidth, rHeight, rounded); } // kind of unfortunate.. need to fill in sets of 4 that form a square or // else a piece of the background lattice will show in the center. void fillSquares(){ //println(" ---- running fill squares --- "); for(int row=0; row