sw01
Full Member
Offline
Posts: 175
Re: Multiple Draggable Objects
Reply #6 - Jun 7th , 2009, 10:34pm
I'm not sure why it's not working. One thing different is that I have it in one tab. but here is the full code:Draggable draggable; NullDraggableObject nullDraggableObject; ArrayList draggables; void setup() { size(400, 400); smooth(); nullDraggableObject = new NullDraggableObject(); draggables = new ArrayList(); for(int i = 0; i < 10; i++) draggables.add(new DraggableObject(random(width), random(height/2))); for(int i = 0; i < 10; i++) draggables.add(new DraggableImage(random(width), random(height/2), loadImage("someImage.jpg"))); } void draw() { background(0); noFill(); stroke(255); strokeWeight(3); draggable = nullDraggableObject; for(int i = 0; i < draggables.size(); i++) { Draggable d = (Draggable)draggables.get(i); d.draw(); if(d.isBeingMouseHovered()) draggable = d; } } void mousePressed() { draggable.mousePressed(); } void mouseDragged() { draggable.mouseDragged(); } void mouseReleased() { draggable.mouseReleased(); } interface Draggable{ boolean isBeingMouseHovered(); boolean inside(float ix, float iy); void draw(); void mousePressed(); void mouseDragged(); void mouseReleased(); } class NullDraggableObject implements Draggable{ boolean isBeingMouseHovered(){ return false;} boolean inside(float ix, float iy){return false;} void draw(){} void mousePressed(){} void mouseDragged(){} void mouseReleased(){} } class DraggableObject implements Draggable { float x, y; float radius; boolean drag; float dragX, dragY; DraggableObject(float _x, float _y) { x = _x; y = _y; radius = 30; drag = false; dragX = 0; dragY = 0; } boolean isBeingMouseHovered() { return inside(mouseX, mouseY); } boolean inside(float ix, float iy) { return (dist(x, y, ix, iy) < radius); } void draw() { ellipseMode(CENTER); ellipse(x, y, 2*radius, 2*radius); } void mousePressed() { drag = inside(mouseX, mouseY); if (drag) { dragX = mouseX - x; dragY = mouseY - y; } } void mouseDragged() { if (drag) { x = mouseX - dragX; y = mouseY - dragY; } } void mouseReleased() { drag = false; } } class DraggableImage implements Draggable { float x, y; float radius; boolean drag; float dragX, dragY; PImage anImage; DraggableImage(float _x, float _y, PImage _anImage) { anImage = _anImage; x = _x; y = _y; //radius = 30; drag = false; dragX = 0; dragY = 0; } boolean isBeingMouseHovered() { return inside(mouseX, mouseY); } boolean inside(float ix, float iy) { boolean answer = true; if(ix < x) answer = false; if(ix > (x + anImage.width)) answer = false; if(iy < y) answer = false; if(iy > (y + anImage.height)) answer = false; return answer; } void draw() { image(anImage, x, y); } void mousePressed() { drag = inside(mouseX, mouseY); if (drag) { dragX = mouseX - x; dragY = mouseY - y; } } void mouseDragged() { if (drag) { x = mouseX - dragX; y = mouseY - dragY; } } void mouseReleased() { drag = false; } }