ControlP5 controller dynamically added when mouse clicked from class

edited August 2016 in Library Questions

Hi Everyone, I am on beginner level in processing and have been writing a code using ControlP5, yet having issues how to add a button in a class triggered by mouse click. Found this sourcehttps://forum.processing.org/one/topic/having-a-controlp5-controller-inside-a-class.html, which nearly answers my question, without mouseClick. I don't use processing in another environment (so eliminated static+PApplet parts completely). Was also checking the ControlP5 examples, but none of them use ControlEvent in class (or I couldn't find it with my beginner level). I have a solution, but outside of class. The final aim is to collect values assigned to the same objects coming from different controllers. And later on, want to compare those objects based on values. Right now, I only have one controller (textfield, but will create more) "assigned" to objects. Hope I make sense. Any help/recommendation/advice very much appreciated. Thank you!!! first part

import controlP5.*;
Object obj=new Object();
ControlP5 cp5;

int c=0; //object counter
int a=0; //attribute counter

PImage img;
ArrayList<Object> objSet=new ArrayList();
ArrayList<Textfield> t =new ArrayList();
ArrayList s=new ArrayList();
ArrayList temp=new ArrayList();

void setup() {
  size(640, 600);
  img= loadImage("room1.jpg");
  img.resize(320, 300);
  cp5=new ControlP5(this);
}

void draw() {
  background(0);
  image(img, 0, 0);
  //println(objSet.size());
  for (int i=0; i<objSet.size(); i++) {
    Object obj1=(Object) objSet.get(i);   
    obj1.display();
  }
}

void mousePressed() {
  if (mouseX<320 && mouseY<300) {
    obj.press();
    a=0;
  }
}

void mouseReleased() {
  if (mouseX<320 && mouseY<300) {
    obj.release();
    objSet.add(obj);
    t.add(cp5.addTextfield("object"+c, 330, 10+c*100, 200, 20)
      .setId(c));
    c+=1;
  }
}

void controlEvent (ControlEvent theEvent) {
  println("got an event from controller id:" +theEvent.getId()+":"+ theEvent.getStringValue());
  temp.add(theEvent.getId()); //collected Id
  s.add(theEvent.getId(), theEvent.getStringValue()); //textField into arrayList
  int tempC=theEvent.getId(); //actual Id
  checker(s,temp,tempC);
  println(s);
}
//you have to fill in with data first, but responds to modification
ArrayList checker(ArrayList toFilter, ArrayList helperA, int helperN) { //removes false values
  int counter=0;
  for (int i=0; i<helperA.size(); i++) {
    if ((int)helperA.get(i)-helperN==0) {
      counter+=1;
      if (counter%2==0) {
        toFilter.remove(helperN+1);
        helperA.remove(i);
        counter=0;
      }
    }
  }
  return toFilter;
}

and the class itself: class Object {

  boolean done=false;
  //ArrayList<Object> objSet=new ArrayList();
  public ArrayList mouseV=new ArrayList();
  ArrayList<String> attributeN=new ArrayList();
  ArrayList attributeV=new ArrayList();

  Object () {
  }    

  void press() {    //make first part of rect
    mouseV.add(new PVector(mouseX, mouseY));
    done=false;
  }

  void release() { //finish rect
    mouseV.add(new PVector(mouseX, mouseY));
    done=true;
  }


  PShape display() {
    PVector centroid= new PVector();
    PShape rect=new PShape();
    if (done==true) {
      noFill();
      stroke(0, 255, 0);
      for (int i=1; i<mouseV.size()+1; i+=2) { 
        PVector v1= (PVector)mouseV.get(i-1);
        PVector v2= (PVector)mouseV.get(i);
        rect= createShape(RECT, v1.x, v1.y, v2.x-v1.x, v2.y-v1.y);
        shape(rect, 0, 0);
        centroid.x=(v1.x+v2.x)/2;
        centroid.y=(v1.y+v2.y)/2;
      }
    }
    return rect;
  }
}

Answers

  • You could show and hide your controllers. For example, in setup you declare:

    cp5 = new ControlP5(this);

    and in draw you could do either:

        cp5.show();
        cp5.hide();
    

    Any controlP5 widgets associated to this object will be shown or hiden all together. I hope this helps,

    Kf

  • edited August 2016

    Thank you, but they are unfortunately not associated to class objects. My solution is a little cheat simply because I don't know how to do the right way. I mean in your solution they are associated to cp5, but I want to associate them to my custom class objects as well, when I hit mouse button. Hope I make sense.

  • Maybe you can post some code?

  • edited August 2016

    Sure, sure. Here I added a textfield and 2 numboxes in class, hopefully attached them to object class, but I got stuck when I add mouse interaction (line 38 and line 45). Thank you in advance!!! first part:

        import controlP5.*;
        ControlP5 cp5;
        Object obj;
        ArrayList<Object> objSet=new ArrayList();    
        int c=0;    
    
        void setup() {
          size(640, 600);
          cp5 = new ControlP5( this );      
        }
    
        void draw() {
          background(0);
          for (int i=0; i<objSet.size(); i++) {
            Object obj1=(Object) objSet.get(i);   
            obj1.display();
          }
        }
    
        void mousePressed() {
          if (mouseX<320 && mouseY<300) {
            obj=new Object(c);
            obj.press();
          }
        }
    
        void mouseReleased() {
          if (mouseX<320 && mouseY<300) {
            obj.release();
            objSet.add(obj);
           c+=1;
          }
        }
    `
    

    class itself, issue is highlighted:

    ` class Object{
    
          boolean done=false;
          public ArrayList mouseV=new ArrayList(); //mouse position
          String attributeN=new String(); //object name
          PVector location=new PVector(); //centroid of object
          float size; 
          String face; 
          String position; 
          int ID; //object counter
          int innerID=0; //attribute counter
          Textfield t;
          Numberbox attr1;
          Numberbox attr2;
    
    
          Object (int id) { //object identifier
            ID=id;
    
          }    
    
          void press() {    
            //println("P");
            mouseV.add(new PVector(mouseX, mouseY)); //starting vertex of rectangle
            done=false;
          }
    
          void release() { 
            innerID+=1;
            mouseV.add(new PVector(mouseX, mouseY)); //furthest vertex of rectangle
            done=true;
            t=cp5.addTextfield("object"+ID,330, 10+ID*110, 200, 20) //doesn't react?????
            .plugTo(this,"object"+ID) //plug to same object?
            .setId(ID); //different ID
            attr1=cp5.addNumberbox("attribute1-"+ID+innerID,330,35+10+ID*110,200,20)
            .plugTo(this,"attribute1"+Float.toString(ID)+innerID)
    
             //.mouseEvent(if mouse is over the same ID rectangle, attr1== area of rectangle; THIS IS FURTHER DEVELOPMENT, area code is missing now)
    
            .setId(innerID); //different ID
            innerID+=1;
            attr2=cp5.addNumberbox("attribute2-"+ID+innerID,330,70+10+ID*110,200,20)
            .plugTo(this,"attribute2-"+Float.toString(ID)+innerID)
    
            //.mouseEvent(if mouse is over the same ID rectangle, change the number of numbox-attr2)
    
            .setId(innerID); //different ID  
          }
    
         void controlEvent(ControlEvent theEvent) { //double check to see if it works
           println("got an event from controller id:" +theEvent.getId()+":"+ theEvent.getValue());
           println("got an event from controller id:" +theEvent.getId()+"::"+ theEvent.getStringValue());
         }
    
          PShape display() { //make rectangle
            PVector centroid= new PVector();
            PShape rect=new PShape();
            textSize(15);    
            if (done==true) {
              //color (0,255,0);
              noFill();
              stroke(0, 255, 0);
              for (int i=1; i<mouseV.size()+1; i+=2) {
                PVector v1= (PVector)mouseV.get(i-1);
                PVector v2= (PVector)mouseV.get(i);        
                rect= createShape(RECT, v1.x, v1.y, v2.x-v1.x, v2.y-v1.y);
                shape(rect, 0, 0);
                centroid.x=(v1.x+v2.x)/2;
                centroid.y=(v1.y+v2.y)/2;
                fill(0, 255, 0);
                text(ID,v1.x,v1.y-5);
              }
            }
            return rect;
          }
        }
    `
    
Sign In or Register to comment.