sojamo / controlp5

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

saveProperties no save ColorWheel #107

Open manoloide opened 7 years ago

manoloide commented 7 years ago

I'm wanting to save ColorWheel content and it does not work for me, maybe it's my mistake. When I look inside the json the array is empty.

import controlP5.*;

ControlP5 cp5;
public color c = color(200);

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);

  cp5.addColorWheel("c", 20, 40, 150 );
  cp5.addButton("b1", 0, 20, 20, 80, 12).setLabel("save setA");
  cp5.addButton("b2", 0, 100, 20, 80, 12).setLabel("load setA");
}

void draw() {
  background(0);
}

void b1(float v) {
  cp5.saveProperties("test");
}

void b2(float v) {
  cp5.loadProperties("test");
}
GoldenQubicle commented 7 years ago

Not your mistake, color wheel is not included in save properties.

The range of controllers implementing save/load properties currently includes Slider, Knob, Numberbox, Toggle, Checkbox, RadioButton, Textlabel, Matrix,Range, textarea, ListBox, Dropdown, colorPicker.

To save colors from the colorwheel you could try using snapshots - though I've never got the hang of snapshots and just use getRGB() and serialize it to json.

c-mendoza commented 5 years ago

I had the same problem but I came up with a workaround so that ColorWheels are saved.

It involves:

  1. Adding a property to the ColorWheel when it is created
  2. After you call load() manually assigning the property to the value of the ColorWheel and to your variable.

Annoying, but it works.

import controlP5.*;

ControlP5 cp5;
public color c = color(200);

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);

  ColorWheel cw = cp5.addColorWheel("c", 20, 40, 150 );
 // 1. Modify the ColorWheel properties:
 // ColorWheels incorrectly set up their internal property value as ArrayValue. 
 // It should instead be a "value": 
  cw.registerProperty("value");
  cw.removeProperty("ArrayValue");
  cp5.addButton("b1", 0, 20, 20, 80, 12).setLabel("save setA");
  cp5.addButton("b2", 0, 100, 20, 80, 12).setLabel("load setA");
}

void draw() {
  background(0);
}

void b1(float v) {
  cp5.saveProperties("test");
}

void b2(float v) {
  cp5.loadProperties("test");
  // 2. Connect the loaded property with your ColorWheel and the backing variable:
  ColorWheel cw = (ColorWheel) gui.getController("c");
  cw.setRGB((int) cw.getValue());
  c = (int) cw.getValue();
}
jeremydouglass commented 5 years ago

ColorWheels incorrectly set up their internal property value as ArrayValue.

Good find. Do you mean that a pull request changing line 370 here is all it would take to fix the issue? Or might that break something else?

https://github.com/sojamo/controlp5/blob/1f7cb649865eb8657495b5cfeddd0dbe85d70cac/src/controlP5/ControlP5Legacy.java#L367-L372

c-mendoza commented 5 years ago

Good find. Do you mean that a pull request changing line 370 here is all it would take to fix the issue? Or might that break something else?

I just tested the change on a local copy of this repo and it works as its supposed to.

jeremydouglass commented 5 years ago

Interesting that it works. I asked because I'm not familiar with the cp5 codebase -- so not sure why some of those registerProperties are marked "value" and others "arrayValue", or why this is Legacy.

But if it works -- maybe submit a pull request?