package shapes; import processing.core.PApplet; import toxi.color.TColor; import toxi.geom.Vec3D; import mekstension.Mekstension; public class ShapeManager { public final static int OBJ_SET_POSITION = 2; public final static int OBJ_SET_ROTATION = 3; public final static int OBJ_SET_SCALE = 4; public final static int OBJ_SET_DEPTH = 5; public final static int OBJ_SET_MODX_FREQ = 6; public final static int OBJ_SET_MODX_AMP = 7; public final static int OBJ_SET_MODY_FREQ = 8; public final static int OBJ_SET_MODY_AMP = 9; public final static int OBJ_SET_SEGMENTS = 10; public final static int GLOBAL_SET_DEPTH = 99; public final static int GLOBAL_SET_ANGLE = 100; public final static int GLOBAL_SET_FOCAL_LENGTH = 101; public final static int GLOBAL_SET_SEGMENTS = 108; public final static int GLOBAL_CYCLE_FG = 102; public final static int GLOBAL_CYCLE_BG = 103; public final static int GLOBAL_CYCLE_STROKE = 104; public final static int GLOBAL_SET_FG_ALPHA = 105; public final static int GLOBAL_SET_BG_ALPHA = 106; public final static int GLOBAL_SET_STROKE_ALPHA = 107; public final static int GLOBAL_SET_COLORCYCLE_SPEED = 110; private int editmode = OBJ_SET_POSITION; private Mekstension p; private ShapeList shapelist; public Shape currentShape; public int globalSegments = 2; public float globalAngle = 0f, globalFocalLength = 0.5f; public TColor globalFG, globalBG, globalStroke; public boolean cycleFG, cycleBG, cycleStroke; public float colorCycleSpeed = 0.05f; /** * Constructor * @param p_ Mekstension application, extended from PApplet * @see PApplet */ public ShapeManager(Mekstension p_) { this.p = p_; shapelist = new ShapeList(); currentShape = null; globalFG = new TColor( TColor.newRandom() ); globalBG = new TColor( TColor.newRandom() ); globalStroke = new TColor( TColor.newRandom() ); globalFG.setBrightness(1); globalBG.setBrightness(1); globalStroke.setBrightness(1); addShape(); } /** * * @param mx current mouse x * @param my current mouse y * @param pmx previous mouse x * @param pmy previous mouse y */ public void mouseEdit(int mx, int my, int pmx, int pmy) { float vX = mx - pmx; float vY = my - pmy; switch( editmode ) { case ShapeManager.GLOBAL_SET_ANGLE : for( int i = 0; i < shapelist.size(); i++ ) { globalAngle += vX / 200f; shapelist.get(i).setAngle(globalAngle); } break; case ShapeManager.GLOBAL_SET_FOCAL_LENGTH : for( int i = 0; i < shapelist.size(); i++ ) { globalFocalLength += vX / 100f; shapelist.get(i).setZScale(globalFocalLength); } break; case ShapeManager.GLOBAL_SET_DEPTH : for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).setDepth(shapelist.get(i).destDepth += vX); break; case ShapeManager.GLOBAL_SET_SEGMENTS : int segments = vX > 0 ? 1 : -1; globalSegments = segments; for( int i = 0; i < shapelist.size(); i++ ) { shapelist.get(i).setSegments(shapelist.get(i).numSegments += segments); globalSegments = shapelist.get(i).numSegments; } break; case ShapeManager.GLOBAL_CYCLE_FG : globalFG.rotateRYB( vX / 100f ); globalFG.saturate( vY / 100f); for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).fg = globalFG; break; case ShapeManager.GLOBAL_CYCLE_BG: globalBG.rotateRYB( vX / 100f ); globalBG.saturate( vY / 100f ); for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).bg = globalBG; break; case ShapeManager.GLOBAL_CYCLE_STROKE: globalStroke.rotateRYB( vX / 100f ); globalStroke.saturate( vY / 100f); for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).stroke = globalStroke; break; case ShapeManager.GLOBAL_SET_FG_ALPHA : float new_fga = globalFG.alpha(); new_fga += (vX / 600f); globalFG.alpha = PApplet.constrain(new_fga, 0, 1); for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).fg = globalFG; break; case ShapeManager.GLOBAL_SET_BG_ALPHA: float new_bga = globalBG.alpha(); new_bga += (vX / 600f); globalBG.alpha = PApplet.constrain(new_bga, 0, 1); for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).bg = globalBG; break; case ShapeManager.GLOBAL_SET_STROKE_ALPHA: float new_stra = globalStroke.alpha(); new_stra += (vX / 600f); globalStroke.alpha = PApplet.constrain(new_stra, 0, 1); for( int i = 0; i < shapelist.size(); i++ ) shapelist.get(i).stroke = globalStroke; break; case ShapeManager.GLOBAL_SET_COLORCYCLE_SPEED : colorCycleSpeed += (vX / 5000f); break; case ShapeManager.OBJ_SET_SCALE : currentShape.setScale(currentShape.destScale += (my - pmy)); break; case ShapeManager.OBJ_SET_DEPTH : currentShape.setDepth(currentShape.destDepth += (my - pmy)); break; case ShapeManager.OBJ_SET_ROTATION : currentShape.setRot(currentShape.destRot += ((mx - pmx) / 100f)); break; case ShapeManager.OBJ_SET_POSITION : Vec3D pos = currentShape.getPos(); currentShape.setPos(pos.x += ((mx - pmx)), pos.y += ((my - pmy)), 0); break; case ShapeManager.OBJ_SET_MODX_AMP : float modx = currentShape.destxAmp += (vX / 10f); currentShape.setModAmpX(modx); break; case ShapeManager.OBJ_SET_MODX_FREQ : float freqx = currentShape.destModXFreq += (vX / 600f); currentShape.setModFreqX(freqx); break; case ShapeManager.OBJ_SET_MODY_AMP : float mody = currentShape.destyAmp += (vY / 10f); currentShape.setModAmpY(mody); break; case ShapeManager.OBJ_SET_MODY_FREQ : float freqy = currentShape.destModYFreq += (vY / 600f); currentShape.setModFreqY(freqy); break; case ShapeManager.OBJ_SET_SEGMENTS : int segs = vX > 0 ? 1 : -1; currentShape.setSegments(currentShape.numSegments += segs); break; default : break; } } /** * toggle foreground audo color cycle */ public void toggleFGCycle() { cycleFG = cycleFG ? false : true; } /** * toggle background audo color cycle */ public void toggleBGCycle() { cycleBG = cycleBG ? false : true; } /** * toggle stroke audo color cycle */ public void toggleStrokeCycle() { cycleStroke = cycleStroke ? false : true; } /** * Set edit mode * @param mode */ public void setEditMode(int mode) { editmode = mode; } /** * Create new shape and to shapelist ArrayList */ public void addShape() { Shape newShape = new Shape(p, 4, "Shape " + shapelist.size(), (shapelist.size()+1)); newShape.fg = globalFG; newShape.bg = globalBG; newShape.stroke = globalStroke; newShape.angle = globalAngle; newShape.setZScale( globalFocalLength ); // newShape.setSegments( globalSegments ); newShape.setAngle(globalAngle); shapelist.add(newShape); nextShape(); } /** * update loop */ public void update() { if(cycleFG || cycleBG || cycleStroke) { for( int i = 0; i < shapelist.size(); i++ ) { if(cycleFG) shapelist.get(i).fg.rotateRYB(colorCycleSpeed); if(cycleBG) shapelist.get(i).bg.rotateRYB(colorCycleSpeed); if(cycleStroke) shapelist.get(i).stroke.rotateRYB(colorCycleSpeed); } } } /** * step to the next shape and assign it to the current shape */ public void nextShape() { currentShape = shapelist.next(); } /** * get shapelist */ public ShapeList getShapelist() { return shapelist; } }