sojamo / controlp5

A gui library for processing.org
GNU Lesser General Public License v2.1
490 stars 142 forks source link

Trigger callback after load #53

Closed MatteoFantasy closed 8 years ago

MatteoFantasy commented 8 years ago

Hi, I'm currently implementing ControlP5 into my apps and I like a lot the idea of the load/save functions. One thing I don't understand though is why the callback functions of the controllers are not called when I load new settings. Since the new value is different I would like the callbacks to be triggered automatically. Is there an option to do so ? Thanks for your help !

sojamo commented 8 years ago

Hi, the ControlP5propertiesCustom example works fine for me and updates variables and function calls accordingly and as expected when loading a setting. When adding a callback to a controller:

cp5.addSlider("slider")
  .setRange(0,200)
  .setValue(128)
  .setPosition(20,100)
  .setSize(20,100)
  .addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      if (theEvent.getAction()==ControlP5.ACTION_BROADCAST) {
        println("update received for", theEvent.getController().getName());
      }
    }
  }
  );

the callback event is triggered when loading a setting that contains a data entry for that particular Controller.

MatteoFantasy commented 8 years ago

Hello, I managed to make it work with a toggle (the slider also works), but it doesn't work with text fields for example. I wrote the following minimal sketch to demonstrate : when loading a previously saved value, the text in the field is changed but neither the usual callback (linked with the name) nor the callback I added are called.

import controlP5.*;

ControlP5 cp5;
Textfield portInput;

void setup() {

  size(640, 480, P2D);
  cp5 = new ControlP5(this);
  portInput = cp5.addTextfield("changeInputPort")
     .setPosition(10,40)
     .setSize(40,20)
     .setAutoClear(false)
     .setCaptionLabel("")
     .setInputFilter(ControlP5.INTEGER)
     .addCallback(new CallbackListener() {
        public void controlEvent(CallbackEvent theEvent) {
          if (theEvent.getAction()==ControlP5.ACTION_BROADCAST) {
            println("TEST");
          }
        }
     }
     );
}

public void changeInputPort(String s) {
  println("input : "+s);
}

void keyPressed() {
  if(key == 's'){
   saveSettings("settings"); 
  } else if (key == 'l'){
     loadSettings("settings"); 
  }
}

void saveSettings(String file){
  println("Saving to : "+file);
  cp5.saveProperties(file);
}

void loadSettings(String file){
  println("Loading from : "+file);
  cp5.loadProperties(file);
}
sojamo commented 8 years ago

Textfield indeed is problematic here. a manual keyboard return or calling Textfield.submit() is necessary to trigger a broadcast event. A workaround would be to call submit() on a textfield after properties have been loaded.

void loadSettings(String file){
  println("Loading from : "+file);
  cp5.loadProperties(file);
  // manually call submit() to trigger a broadcast for 
  // Textfield portInput when loading settings.
  portInput.submit(); 
}
sojamo commented 8 years ago

I will leave this to the work-around example in my previous post.