/** *
This processing script was designed along my quest to build a mini space battles game. * Its called changeFocus, and the idea behind it is to be able to distguish the selection of * different objects on the screen, since mouse actions like dragging need this kind of knowledge.
*The way I've made it work is to have a stack of objects, with object having its own checkFocus() method. * When a mouse action (mousePressed(), mouseReleased()) is triggered in the main code a loop is run to call * all the checkFocus() methods of all the objects on the scene and to assign or reassign mouse priority. *
*So in this example I have different types of objects, squares, circles, diamonds and images all which can be dragged around * (so essentially the same code) but with their own object classes. This is to show that any type of object can be used.
*To enable different classes of object to be used in the same array, and access similarly named functions I had to create an * interface class called focusObject. This interface class contains methods which are required by all classes that implement it, in * this program shapeSquare, shapeCircle, shapeDiamond and focusImage. By typecasting objects with the name of the interface I could * access the shared methods in while loops without having to check the exact object type.
*The stack isn't perfect since you can hover and focus several objects at any time. What I really need in its place is a collection * class that contains the objectStack[] array but also tracks which object has focus, and preferrably allow me to set the visual depth * of each object (e.g. the focused object jumps to the top of the page above all other elements). Something for the future any how.
*/ Object[] objectStack = new Object[10]; color black, grey, white, red, green, blue; PImage greenFace, redHat, background; void setup() { // Set up colours colorMode(HSB, 255, 255, 255); black = color(0, 0, 0); grey = color(0, 0, 153); white = color(0, 0, 255); red = color(0, 200, 240); blue = color(170, 200, 240); green = color(85, 200, 200); // Load images background = loadImage("background.gif"); greenFace = loadImage("green_face.gif"); redHat = loadImage("red_hat.gif"); // Set applet size and framerate size(300, 300); background(grey); image(background, 0, 0); framerate(25); // Create shapes int i = 0; objectStack[i++] = new focusImage(240, 140, greenFace); objectStack[i++] = new focusImage(60, 200, redHat); objectStack[i++] = new shapeSquare(60, 140, 30, 30, red); objectStack[i++] = new shapeSquare(65, 145, 30, 30, blue); objectStack[i++] = new shapeCircle(120, 140, 20, green); objectStack[i++] = new shapeDiamond(180, 140, 30, 20, blue); } void draw() { background(grey); image(background, 0, 0); for(int i=0; i