我使用 controlP5 模块获得了以下处理代码:
import controlP5.*;
ControlP5 cp5;
void setup() {
size(180,240);
smooth();
noStroke();
cp5 = new ControlP5(this);
cp5.addSlider("CCV_Control")
.setPosition(40,40)
.setNumberOfTickMarks(11)
.setSliderMode(Slider.FLEXIBLE)
.showTickMarks(false)
.snapToTickMarks(true)
.setSize(100,20)
.setRange(0,100)
.setValue(20)
.setLabel("CCV Percent Open");
cp5.getController("CCV_Control").getCaptionLabel().align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE).setPaddingY(2);
cp5.addKnob("Water_Flow")
.setRange(0,70)
.setValue(20)
.setPosition(40,100)
.setRadius(50)
.setNumberOfTickMarks(28)
.snapToTickMarks(true)
.setLabel("Facility Water");
}
void draw() {
fill(140);
rect(20,20,140,200);
}
void Water_Flow(int theValue) {
float ccvPercentage = 0;
if (theValue <= 2) {
cp5.getController("Water_Flow").setColorBackground(color(200, 0, 0));
cp5.getController("CCV_Control").setValue(0);
} else {
cp5.getController("Water_Flow").setColorBackground(color(0, 31, 63));
ccvPercentage = (theValue*110)/70;
cp5.getController("CCV_Control").setValue(ccvPercentage);
}
}
void CCV_Control(int theValue) {
float facilityWaterFlow = 0;
if (theValue <= 2) {
//cp5.getController("Water_Flow").setValue(0);
} else {
facilityWaterFlow = (theValue*70)/100;
//cp5.getController("Water_Flow").setValue(facilityWaterFlow);
}
println("Water Flow: "+facilityWaterFlow+ " is CCV percent: "+theValue);
}
它工作正常,但我需要这两个控件在两个方向上相互绑定。但是,当我取消注释“getController(“Water_Flow”)”行时,日志中充满了错误。有没有更简单的方法将它们绑在一起?
这是根据 apodidae 的响应更新的代码:
void Water_Flow(int theValue) {
float ccvPercentage = 0;
if (theValue <= 2) {
cp5.getController("Water_Flow").setColorBackground(color(200, 0, 0));
} else {
cp5.getController("Water_Flow").setColorBackground(color(0, 31, 63));
ccvPercentage = (theValue*110)/70;
}
// temporarily turn off broadcasting for controller "CCV_Control"
boolean broadcast = cp5.getController("Water_Flow").isBroadcast();
cp5.getController("CCV_Control").setBroadcast(false);
cp5.getController("CCV_Control").setValue(ccvPercentage);
// change broadcast back to initial value
cp5.getController("CCV_Control").setBroadcast(broadcast);
}
void CCV_Control(int theValue) {
float facilityWaterFlow = 0;
color knobBackground = color(0, 31, 63);
if (theValue <= 2) {
knobBackground = color(200, 0, 0);
} else {
facilityWaterFlow = (theValue*70)/100;
}
// temporarily turn off broadcasting for controller "Water_Flow"
boolean broadcast = cp5.getController("Water_Flow").isBroadcast();
cp5.getController("Water_Flow").setBroadcast(false);
cp5.getController("Water_Flow").setValue(facilityWaterFlow);
cp5.getController("Water_Flow").setColorBackground(knobBackground);
// change broadcast back to initial value
cp5.getController("Water_Flow").setBroadcast(broadcast);
println("Water Flow: "+facilityWaterFlow+ " is CCV percent: "+theValue);
}