controlp5 icon indicating copy to clipboard operation
controlp5 copied to clipboard

Slider bug when saving serialized properties

Open kroeder opened this issue 9 years ago • 0 comments

There's a bug when saving slider properties as serialized file After loading a serialized file all Label-Alignments are broken (see screenshot)

This only happens with sliders (they are initialized as Center, Center)

2016-07-23 1

Code Example

  cp5 = new ControlP5(this);
  cp5.getProperties().setFormat(ControlP5.SERIALIZED);
  cp5.setFont(createFont("Monospace", 12));

  Slider beatDelaySlider = cp5.addSlider("beatDelay").setSize(395, 20).setPosition(10, 134).setRange(10, 1000);
  beatDelaySlider.getCaptionLabel().set("Beat Delay (ms)").align(ControlP5.CENTER, ControlP5.CENTER);
  beatDelaySlider.setValue(200);

  cp5.saveProperties("test");
  cp5.loadProperties("test");

I implemented a workaround

Hashtable<String, int[]> uiAlignments = new Hashtable<String, int[]>(); 

/*
 * This function stores all alignments on app initialization
 */
void saveSliderLabelAlign() 
{
  for(Slider s:cp5.getAll(Slider.class)) {
    uiAlignments.put(s.getName(), s.getCaptionLabel().getAlign());
  }
}

/*
 * This function reloads all label alignments (taken from saveSliderLabelAlign())
 */
void resetSliderLabelAlign() 
{
  Set<String> keys = uiAlignments.keySet();
  for(String key: keys){
    int[] alignments = uiAlignments.get(key);
    cp5.getController(key).getCaptionLabel().align(alignments[0], alignments[1]);
  }
}

You have to call the save function after everything was initialized and then call the resetSliderLabelAlign function everytime you call loadProperties

kroeder avatar Jul 24 '16 12:07 kroeder