Node Class Connections
in
Programming Questions
•
11 months ago
Hi to all.
In the following sketch I am trying to allow the user to drag a line from one circle and connect it with another circle (created as an object with a different class). After they are connected, no more connections are allowed (however the user can cancel this connection - drag the line from the connected object).
It works if I do it with only two items (one connection), but it does not work if I create more objects. Possibly there are some development methods that you can suggest to overcome this issue?
Many thanks
- InputItem[] inItem = new InputItem[4];
- OutItem[] outItem = new OutItem[4];
- int validate1, validate2, validateMouse1, validateMouse2 = 0;
- float pos1StartX, pos1StartY, pos1EndX, pos1EndY = -999;
- float pos2StartX, pos2StartY, pos2EndX, pos2EndY = -999;
- boolean clickedBall1, clickedBall2, selectBall1, connection = false;
- void setup() {
- size(800, 480);
- noStroke();
- smooth();
- for (int i = 0; i < 4; i++) {
- inItem[i] = new InputItem ();
- outItem[i] = new OutItem ();
- }
- }
- void draw() {
- background(30);
- drawLine();
- for (int i = 0; i < 4; i++) {
- inItem[i].update();
- inItem[i].display();
- outItem[i].update();
- outItem[i].display();
- }
- }
- void mouseReleased() {
- if (validateMouse1 == 1 && validate2 == 1) {
- connection = true;
- }
- if (!connection) {
- validate1 = 0;
- validate2 = 0;
- validateMouse1 = 0;
- pos1StartX = -999;
- pos1StartY = -999;
- pos1EndX = -999;
- pos1EndY = -999;
- }
- }
- void drawLine() {
- strokeWeight(2);
- pos1StartX = -999;
- pos1StartY = -999;
- pos1EndX = -999;
- pos1EndY = -999;
- if (inItem[0].mouseOverItem()) validate1 = 1;
- for (int i = 0; i < inItem.length; i++) {
- if (outItem[i].mouseOverItem()) {
- validate2 = 1;
- }
- else {
- validate2 = 0;
- }
- if (validate1 == 1 && mousePressed && !connection) {
- validateMouse1 = 1;
- stroke(255, 255, 0, 255);
- pos1StartX = inItem[i].location.x;
- pos1StartY = inItem[i].location.y;
- pos1EndX = mouseX;
- pos1EndY = mouseY;
- line(pos1StartX, pos1StartY, pos1EndX, pos1EndY);
- }
- else {
- stroke(255, 255, 0, 0);
- pos1StartX = -999;
- pos1StartY = -999;
- pos1EndX = -999;
- pos1EndY = -999;
- }
- if (connection) {
- validate1 = 0;
- validate2 = 0;
- pos1StartX = inItem[0].location.x;
- pos1StartY = inItem[0].location.y;
- pos2EndX = outItem[i].location.x;
- pos2EndY = outItem[i].location.y;
- stroke(255, 255, 255, 255);
- line(pos1StartX, pos1StartY, pos2EndX, pos2EndY);
- }
- else {
- pos1StartX = -999;
- pos1StartY = -999;
- pos1EndX = -999;
- pos1EndY = -999;
- stroke(255, 255, 255, 0);
- }
- if (connection && outItem[i].mouseOverItem() && mousePressed) {
- validate1 = 1;
- validate2 = 0;
- validateMouse1 = 0;
- connection = false;
- }
- }
- }
- class InputItem {
- int circleRadius = 50;
- PVector location, offsetLoc;
- PVector velocity, velocityStart, velocityStop;
- color colObject = color ((int) random (50), 0, (int) random (200));
- // Constructor
- InputItem () {
- location = new PVector((int) random(width), (int)random (height));
- offsetLoc = new PVector(circleRadius, 0);
- velocity = new PVector(random(-0.8, 0.8), random(-0.8, 0.8));
- velocityStart = new PVector(random(-0.8, 0.8), random(-0.8, 0.8));
- velocityStop = new PVector(0, 0);
- }
- void update() {
- location.add(velocity);
- if (location.x > width-circleRadius) {
- velocity.x = velocity.x * -1.;
- location.x = width-circleRadius;
- }
- else if (location.x < circleRadius) {
- velocity.x = velocity.x * -1.;
- location.x = circleRadius;
- }
- if (location.y > height-circleRadius) {
- velocity.y = velocity.y * -1.;
- location.y = height-circleRadius;
- }
- else if (location.y < circleRadius) {
- velocity.y = velocity.y * -1.;
- location.y = circleRadius;
- }
- }
- void display() {
- fill(colObject);
- noStroke();
- ellipse(location.x, location.y, circleRadius*1.5, circleRadius*1.5);
- }
- boolean mouseOverItem() {
- float disX = location.x -mouseX;
- float disY = location.y - mouseY;
- if (sqrt (sq (disX) + sq (disY)) < circleRadius) {
- return true;
- }
- else {
- return false;
- }
- }
- }
- class OutItem {
- int circleRadius = 50;
- PVector location, offsetLoc;
- PVector velocity, velocityStart, velocityStop;
- color colObject = color (200, (int) random (50), 0);
- // Constructor
- OutItem () {
- location = new PVector((int) random(width), (int)random (height));
- offsetLoc = new PVector(circleRadius, 0);
- velocity = new PVector(random(-0.8, 0.8), random(-0.8, 0.8));
- velocityStart = new PVector(random(-0.8, 0.8), random(-0.8, 0.8));
- velocityStop = new PVector(0, 0);
- }
- void update() {
- location.add(velocity);
- if (location.x > width-circleRadius) {
- velocity.x = velocity.x * -1.;
- location.x = width-circleRadius;
- }
- else if (location.x < circleRadius) {
- velocity.x = velocity.x * -1.;
- location.x = circleRadius;
- }
- if (location.y > height-circleRadius) {
- velocity.y = velocity.y * -1.;
- location.y = height-circleRadius;
- }
- else if (location.y < circleRadius) {
- velocity.y = velocity.y * -1.;
- location.y = circleRadius;
- }
- }
- void display() {
- fill(colObject);
- noStroke();
- ellipse(location.x, location.y, circleRadius*1.5, circleRadius*1.5);
- }
- boolean mouseOverItem() {
- float disX = location.x -mouseX;
- float disY = location.y - mouseY;
- if (sqrt (sq (disX) + sq (disY)) < circleRadius) {
- return true;
- }
- else {
- return false;
- }
- }
- }
1