import com.sun.awt.AWTUtilities; import java.awt.GraphicsDevice.*; // PC only import java.awt.Shape; import java.awt.geom.*; import javax.swing.*; public void init(){ frame.removeNotify(); frame.setUndecorated(true); super.init(); } import de.looksgood.ani.*; import gifAnimation.*; import java.util.ArrayList; //filename-A_0_1_0_1_.gif static int EINS = 1; static int ZWEI = 2; static int DREI = 3; static int L = 4; static int R = 5; String path = "i/non-free/gif/"; int bandlength = 500; // min 5 int tileWidth = 200; int tileHeight = 200; ArrayList allTiles; ArrayList lSrF, lFrS, lSrS, lFrF, lSrN, lFrN, lNrS, lNrF, lNrN; ArrayList lS, lF, rS, rF, lN, rN; ArrayList currentList; int currentListIndex; int leftIndex, centerIndex, rightIndex = -1; int dir; int xoffset = 0; int yoffset = 0; boolean invert = false; boolean blockInput = false; boolean clearblack, clearwhite, showSpecial = false; //banditTile errorTile = new banditTile("i/", "error.gif", this); banditTile[] band = new banditTile[bandlength]; int bandindex = bandlength/2; Gif anim; PImage xxleft, xleft, left, center, right, xright, xxright, centerup, centerdown; Ani aniLeft, aniRight, aniUp, aniDown; int fps = 50; int tautoaction = 3 * fps; int thandaction = 10 * fps; int tcount = 0; boolean handaction = false; int lastrand, rand; float aniupdownHand = 0.4; float aniupdownAuto = 0.7; float anileftrightHand = 0.4; float anileftrightAuto = 1.2; //=========================================================================== //=== SETUP ================================================================ //=========================================================================== void setup() { size(5*tileWidth, tileHeight); smooth(); frameRate(50); frame.setAlwaysOnTop(true); frame.setLocation(-100,0); Ani.init(this); generateLists(); // aniLeft = new Ani(this, 0.4, "xoffset", +tileWidth, Ani.QUAD_IN_OUT, "onStart:onAniStart, onEnd:shiftLeft"); // aniRight = new Ani(this, 0.4, "xoffset", -tileWidth, Ani.QUAD_IN_OUT, "onStart:onAniStart, onEnd:shiftRight"); aniUp = new Ani(this, 0.5, "yoffset", +tileHeight, Ani.BACK_IN_OUT, "onStart:onAniStart, onEnd:shiftUp"); aniDown = new Ani(this, 0.5, "yoffset", -tileHeight, Ani.BACK_IN_OUT, "onStart:onAniStart, onEnd:shiftDown"); aniLeft = new Ani(this, 1.2, "xoffset", +tileWidth, Ani.BACK_IN_OUT, "onStart:onAniStart, onEnd:shiftLeft"); aniRight = new Ani(this, 1.2, "xoffset", -tileWidth, Ani.BACK_IN_OUT, "onStart:onAniStart, onEnd:shiftRight"); //Collections.shuffle(Arrays.asList(rightList)); int startindex = (int)random(allTiles.size()); band[bandindex] = (banditTile) allTiles.get(startindex); planHorizontal(); planVertical(); shiftRight(); shiftLeft(); shiftUp(); shiftDown(); } //=========================================================================== //=== DRAW ================================================================ //=========================================================================== void draw() { //tint(0, 153, 204); if (frameCount == 10) { frame.setLocation(20,150); } background(255); if(!clearblack && !clearwhite) { if(pressed && dir != 0) { if(dir == R) { xoffset -= 15; if(xoffset < -tileWidth) shiftRight(); } else if(dir == L) { xoffset += 15; if(xoffset > tileWidth) shiftLeft(); } } if(xoffset != 0) { if(!aniLeft.isPlaying() && !aniRight.isPlaying() && !pressed) { xoffset = 0; } image(xxleft, -(tileWidth*1)+xoffset, 0, tileWidth, tileHeight); image(xxright, (tileWidth*5)+xoffset, 0, tileWidth, tileHeight); } if(yoffset != 0) { if(!aniUp.isPlaying() && !aniDown.isPlaying() && !pressed) { yoffset = 0; } image(centerup, (tileWidth*2), -tileHeight+yoffset, tileWidth, tileHeight); image(centerdown, (tileWidth*2), tileHeight+yoffset, tileWidth, tileHeight); } image(xleft, -(tileWidth*0)+xoffset, 0, tileWidth, tileHeight); image(left, (tileWidth*1)+xoffset, 0, tileWidth, tileHeight); image(center, (tileWidth*2)+xoffset, 0+yoffset, tileWidth, tileHeight); image(right, (tileWidth*3)+xoffset, 0, tileWidth, tileHeight); image(xright, (tileWidth*4)+xoffset, 0, tileWidth, tileHeight); if(invert) { filter(INVERT); } if(showSpecial) { if(frameCount%4 == 0) { filter(INVERT); } if(!keyPressed) showSpecial = false; } tcount++; if(tcount > tautoaction && !handaction) { lastrand = rand; rand = int(random(0, 50)); if(rand < 25) { aniLeft.setDuration(anileftrightAuto); aniLeft.start(); } else { planVertical(); if(lastrand >= 25) { rand = lastrand; } if(rand > 37) { aniUp.setDuration(aniupdownAuto); aniUp.start(); } else { aniDown.setDuration(aniupdownAuto); aniDown.start(); } } tcount = 0; } else if(handaction && tcount > thandaction) { handaction = false; } } else { // clearblack OR clearwhite if(clearblack) { background(0); if(!keyPressed) clearblack = false; } else { background(255); if(!keyPressed) clearwhite = false; } } } //=========================================================================== //=== PLANNING ============================================================ //=========================================================================== void planHorizontal() { // done once for (int k = bandindex; k <= bandlength-1; k++) { if(band[k] != null) { continue; } else { band[k] = newNB(band[k-1], R); } } for (int k = bandindex; k >= 0; k--) { if(band[k] != null) { continue; } else { band[k] = newNB(band[k+1], L); } } } void planVertical() { currentList = band[bandindex].getList(); currentListIndex = band[bandindex].getListIndex(); shiftVertical(currentList, currentListIndex); println("planVertical: " +currentListIndex +"/" +currentList.size()); } //=========================================================================== //=== Re-reference tiles after shiftani ==================================== //=========================================================================== void shiftRight() { //++ bandindex = (bandindex < bandlength-1) ? bandindex+1 : 0; //println("bandindex: " +bandindex); shiftHorizontal(); } void shiftLeft() { //-- bandindex = (bandindex >= 1) ? bandindex-1 : bandlength-1; //println("bandindex: " +bandindex); shiftHorizontal(); } void shiftHorizontal() { if(bandindex < 3) { if(bandindex == 2) { xxleft = band[bandlength-1].getImage(); // xleft = band[bandindex-2].getImage(); left = band[bandindex-1].getImage(); center = band[bandindex].getImage(); right = band[bandindex+1].getImage(); xright = band[bandindex+2].getImage(); xxright = band[bandindex+3].getImage(); } else if(bandindex == 1) { xxleft = band[bandlength-2].getImage(); // xleft = band[bandlength-1].getImage(); // left = band[bandindex-1].getImage(); center = band[bandindex].getImage(); right = band[bandindex+1].getImage(); xright = band[bandindex+2].getImage(); xxright = band[bandindex+3].getImage(); } else { // == 0 xxleft = band[bandlength-3].getImage(); // xleft = band[bandlength-2].getImage(); // left = band[bandlength-1].getImage(); // center = band[bandindex].getImage(); right = band[bandindex+1].getImage(); xright = band[bandindex+2].getImage(); xxright = band[bandindex+3].getImage(); } } else if((bandindex+3) >= bandlength) { if(bandindex == bandlength-3) { xxleft = band[bandindex-3].getImage(); xleft = band[bandindex-2].getImage(); left = band[bandindex-1].getImage(); center = band[bandindex].getImage(); right = band[bandindex+1].getImage(); xright = band[bandindex+2].getImage(); // xxright = band[0].getImage(); // } else if(bandindex == bandlength-2) { xxleft = band[bandindex-3].getImage(); xleft = band[bandindex-2].getImage(); left = band[bandindex-1].getImage(); center = band[bandindex].getImage(); right = band[bandindex+1].getImage(); xright = band[0].getImage(); // xxright = band[1].getImage(); // } else { // == lastElem xxleft = band[bandindex-3].getImage(); xleft = band[bandindex-2].getImage(); left = band[bandindex-1].getImage(); center = band[bandindex].getImage(); right = band[0].getImage(); // xright = band[1].getImage(); // xxright = band[2].getImage(); // } } else { xxleft = band[bandindex-3].getImage(); xleft = band[bandindex-2].getImage(); left = band[bandindex-1].getImage(); center = band[bandindex].getImage(); right = band[bandindex+1].getImage(); xright = band[bandindex+2].getImage(); xxright = band[bandindex+3].getImage(); } xoffset = 0; blockInput = false; } void shiftUp() { if(currentListIndex == currentList.size()-1) { currentListIndex = 0; } else { currentListIndex++; } shiftVertical(currentList, currentListIndex); } void shiftDown() { if(currentListIndex == 0) { currentListIndex = currentList.size()-1; } else { currentListIndex--; } shiftVertical(currentList, currentListIndex); } void shiftVertical(ArrayList li, int listIndex) { band[bandindex] = (banditTile)li.get(listIndex); centerup = ((banditTile)li.get(((listIndex+1)==li.size()) ? 0 : listIndex+1)).getImage(); center = band[bandindex].getImage(); centerdown = ((banditTile)li.get(((listIndex-1)==-1) ? li.size()-1 : listIndex-1)).getImage(); yoffset = 0; blockInput = false; } //=========================================================================== //=== CALLBACK ============================================================ //=========================================================================== // called onStart of diameterAni animation void onAniStart() { blockInput = true; } //=========================================================================== //=== USER INPUT ========================================================== //=========================================================================== float start; float timeout = 250; boolean pressed = false; void keyPressed() { if(keyCode == 107) { //NUMPAD_PLUS clearblack = true; } else if(keyCode == 109) { //NUMPAD_MINUS clearwhite = true; } else if(key == ENTER || key == RETURN) { showSpecial = true; } else { if(!blockInput) { if(!pressed) { start = millis(); pressed = true; dir = 0; } if((millis() - start) > timeout) { //println("pressed"); //if (key == CODED) { if (keyCode == LEFT) { //xoffset += 30; dir = L; //if(xoffset > tileWidth) shiftLeft(); //println(xoffset); //shiftLeft(); //xoffset = 0; //aniLeft.start(); } else if(keyCode == RIGHT) { //xoffset -= 30; dir = R; //if(xoffset < -tileWidth) shiftRight(); //println(xoffset); //shiftRight(); //xoffset = 0; //aniRight.start(); } /* else if(keyCode == UP) { yoffset = 0; planVertical(); aniUp.start(); } else if(keyCode == DOWN) { yoffset = 0; planVertical(); aniDown.start(); } */ //} } } } } void keyReleased() { if(!blockInput) { if(pressed && (millis() - start) <= timeout) { //if (key == CODED) { if (keyCode == LEFT || keyCode == KeyEvent.VK_NUMPAD4) { xoffset = 0; aniLeft.setDuration(anileftrightHand); aniLeft.start(); } else if(keyCode == RIGHT || keyCode == KeyEvent.VK_NUMPAD6) { xoffset = 0; aniRight.setDuration(anileftrightHand); aniRight.start(); } else if(keyCode == UP || keyCode == KeyEvent.VK_NUMPAD8) { yoffset = 0; aniUp.setDuration(aniupdownHand); planVertical(); aniUp.start(); } else if(keyCode == DOWN || keyCode == KeyEvent.VK_NUMPAD2) { yoffset = 0; aniDown.setDuration(aniupdownHand); planVertical(); aniDown.start(); } else if(keyCode == 107) { //NUMPAD_PLUS clearblack = false; } else if(keyCode == 109) { //NUMPAD_MINUS clearwhite = false; } else if(key == ENTERĀ || key == RETURN) { showSpecial = false; } //else { // println(keyCode); //} //} } else { } dir = 0; pressed = false; start = 0; } if(key == 'x' || keyCode == KeyEvent.VK_NUMPAD5) { invert = !invert; } tcount = 0; handaction = true; } //=========================================================================== //=== LIST / TILE UTIL ==================================================== //=========================================================================== // find a suitable neighbor for the given tile on L or R side banditTile newNB(banditTile tile, int side) { banditTile nb = null; try { if(side == L) { if(tile.connectsLeft()) { if(tile.slimLeft()) { nb = (banditTile)rS.get((int) random(rS.size())); } else { nb = (banditTile)rF.get((int) random(rF.size())); } } else { nb = (banditTile)rN.get((int) random(rN.size())); } } else if(side == R) { if(tile.connectsRight()) { if(tile.slimRight()) { nb = (banditTile)lS.get((int) random(lS.size())); } else { nb = (banditTile)lF.get((int) random(lF.size())); } } else { nb = (banditTile)lN.get((int) random(lN.size())); } } } catch(IndexOutOfBoundsException e) { println("leereListe"); //nb = errorTile; } return nb; } void generateLists() { String[] allTileNames = listFileNames(sketchPath("") +path); println(allTileNames); allTiles = new ArrayList(); for (int k = 0; k < allTileNames.length; k++) { String file = allTileNames[k]; if(file.indexOf(".gif") != -1) { allTiles.add(new banditTile(path, file, this)); } } allTileNames = null; //============================================================ lS = new ArrayList(); lF = new ArrayList(); rS = new ArrayList(); rF = new ArrayList(); lN = new ArrayList(); rN = new ArrayList(); lSrF = new ArrayList(); lSrS = new ArrayList(); lSrN = new ArrayList(); lFrF = new ArrayList(); lFrS = new ArrayList(); lFrN = new ArrayList(); lNrF = new ArrayList(); lNrS = new ArrayList(); lNrN = new ArrayList(); for (int i = 0; i < allTiles.size(); i++) { banditTile t = (banditTile)allTiles.get(i); if(t.connectsLeft()) { if(t.slimLeft()) { lS.add(t); } else if(t.fatLeft()) { lF.add(t); } } else { lN.add(t); } if(t.connectsRight()) { if(t.slimRight()) { rS.add(t); } else if(t.fatRight()) { rF.add(t); } } else { rN.add(t); } } //============================================================ generateSubLists(lS, lSrF, lSrS, lSrN); generateSubLists(lF, lFrF, lFrS, lFrN); generateSubLists(lN, lNrF, lNrS, lNrN); } void generateSubLists(ArrayList sourceList, ArrayList list1, ArrayList list2, ArrayList list3) { for (int i = 0; i < sourceList.size(); i++) { banditTile t = (banditTile)sourceList.get(i); if(t.connectsRight()) { if(t.fatRight()) { list1.add(t); t.setList(list1); t.setListIndex(list1.size()-1); } else if(t.slimRight()) { list2.add(t); t.setList(list2); t.setListIndex(list2.size()-1); } } else { list3.add(t); t.setList(list3); t.setListIndex(list3.size()-1); } } } //=========================================================================== //=== GENERAL UTIL ======================================================== //=========================================================================== // This function returns all the files in a directory as an array of Strings String[] listFileNames(String dir) { File file = new File(dir); if (file.isDirectory()) { String names[] = file.list(); return names; } else { // If it's not a directory return null; } } void printArrayList(ArrayList l) { println("----------------------------------"); for (int i = 0; i < l.size(); i++) { println( ((banditTile) l.get(i)).getFilename()); } println("----------------------------------"); }