如何使两个字段成为条件?

How to make a condition of two fields?

下午好,我需要添加几个条件来验证当前代码中的两个字段。

  1. 最小字段不能超过最大字段。最小 < 最大 / 5000 <4000禁止进入。
  2. 最大字段不能小于最小字段。 Max
  3. Min字段不能超过Max字段的间隔,可以少输入500个单位。 例如: Min 4500 and Max 5000, 4400/5000, 4250/5000, 4501/5000及以上禁止入场
  4. 最小值和最大值字段不能相等。 Min = Max 禁止输入。

代码:

import controlP5.*;

ControlP5 cp5;
Textfield O;
Textfield OO;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  O = cp5.addTextfield("MIN")
    .setPosition(20, 100)
    .setSize(200, 40);
  O.setInputFilter(ControlP5.INTEGER)
    .setFont(font)
    .setColor(color(255, 0, 0));

  OO = cp5.addTextfield("MAX")
    .setPosition(20, 170)
    .setSize(200, 40);
  OO.setInputFilter(ControlP5.INTEGER)
    .setFont(font);

  textFont(font);
}

void draw() {
  if (keyPressed && OO.isFocus()) {
    float n;
    try {
      n = Float.parseFloat(OO.getText().replace(',', '.'));
      if (!(n >= 1 && n <= 12000)) {
        throw new NumberFormatException(); // throw to catch below
      }
    } 
    catch (Exception e2) {
      String t;
      if (OO.getText().length() > 1) {
        t = OO.getText().substring(0, OO.getText().length() - 1);
      } else {
        t = "";
      }
      OO.setText(t);
    }
  }
  if (keyPressed && O.isFocus()) {
    float n;
    try {
      n = Float.parseFloat(O.getText().replace(',', '.'));
      if (!(n >= 1 && n <= 11500)) {
        throw new NumberFormatException(); // throw to catch below
      }
    } 
    catch (Exception e2) {
      String t;
      if (O.getText().length() > 1) {
        t = O.getText().substring(0, O.getText().length() - 1);
      } else {
        t = "";
      }
      O.setText(t);
    }
  }
  background(0);
  fill(255);
}

总的来说,您似乎在尝试让用户输入一系列有效值(其中最小值始终小于最大值)。已经有一个 ControlP5 控制器:Range

除了允许在一个范围内设置最小值和最大值之外,约束条件是保持最大值和最小值之间的差异至少为 500。

您可以将范围滑块手柄设置为 0 像素宽,实际上是禁用它们,这意味着您在开始时设置的范围(通过 setRangeValues)将保持不变:

import controlP5.*;

ControlP5 cp5;

Range range;

int rangeMinValue;
int rangeMaxValue;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  range = cp5.addRange("yourRange")
             // disable broadcasting since setRange and setRangeValues will trigger an event
             .setBroadcast(false) 
             .setFont(font)
             .setPosition(50,50)
             // notice the dimensions are proportional to the min/max range to avoid floating point values
             .setSize(500,40)
             // set minimum - maximum range here
             .setRange(4000,5000)
             // example: set initial (recommended) range values
             .setRangeValues(4000, 4500)
             // workaround to disable left/right handles contraining the range to 500
             .setHandleSize(0)
             // after the initialization we turn broadcast back on again
             .setBroadcast(true)
             ;
  
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
}

void controlEvent(ControlEvent event) {
  if(event.isFrom("yourRange")) {
    // min and max values are stored in an array.
    // access this array with controller().arrayValue().
    // min is at index 0, max is at index 1.
    rangeMinValue = int(event.getController().getArrayValue(0));
    rangeMaxValue = int(event.getController().getArrayValue(1));
    println("range:",rangeMinValue,"->",rangeMaxValue);
  }
  
}

一个限制是范围不能 > 500。如果这是一项要求,您仍然可以手动限制值(通过设置范围最小(低)/最大(高)值):

import controlP5.*;

// range constants
final int RANGE_MIN = 4000;
final int RANGE_MAX = 5000;
// the smallest allowed difference between min/max values
final int RANGE_MIN_DIFFERENCE = 500;
final int RANGE_MID = RANGE_MIN + ((RANGE_MAX - RANGE_MIN) / 2);

ControlP5 cp5;

Range range;

int rangeMinValue;
int rangeMaxValue;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  range = cp5.addRange("yourCustomRange")
             // disable broadcasting since setRange and setRangeValues will trigger an event
             .setBroadcast(false) 
             .setFont(font)
             .setPosition(50,50)
             // notice the dimensions are proportional to the min/max range to avoid floating point values
             .setSize(500,40)
             // set minimum - maximum range here
             .setRange(RANGE_MIN, RANGE_MAX)
             // example: set initial (recommended) range values
             .setRangeValues(RANGE_MIN, RANGE_MIN + RANGE_MIN_DIFFERENCE)
             // after the initialization we turn broadcast back on again
             .setBroadcast(true)
             ;
  
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
}

void controlEvent(ControlEvent event) {
  if(event.isFrom("yourCustomRange")) {
    // min and max values are stored in an array.
    // access this array with controller().arrayValue().
    // min is at index 0, max is at index 1.
    int rangeMinInt  = int(event.getController().getArrayValue(0));
    int rangeMaxInt  = int(event.getController().getArrayValue(1));
    // if the values are within the desired range, update global values
    if(rangeMaxInt - rangeMinInt >= RANGE_MIN_DIFFERENCE){
      rangeMinValue = rangeMinInt;
      rangeMaxValue = rangeMaxInt;
    }else{
      // otherwise check which side of the range should be constrained (right/max) or (left/min) to overwrite user input
      if(rangeMaxInt > RANGE_MID){
        range.setLowValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
      }else{
        range.setHighValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
      }
    }
    // values to use
    println("range:",rangeMinValue,"->",rangeMaxValue);
  }
  
}

如果这需要太多 space 您可以使用 Numberbox 与文本字段相比有一些优点:

  • 您无需担心字符串到整数的转换(它默认处理数字)
  • 您可以设置 min/max 个值

这是一个例子:

import controlP5.*;

ControlP5 cp5;

// range constants
final int RANGE_MIN = 4000;
final int RANGE_MAX = 5000;
// the smallest allowed difference between min/max values
final int RANGE_MIN_DIFFERENCE = 500;
final int RANGE_MID = RANGE_MIN + ((RANGE_MAX - RANGE_MIN) / 2);

int minValue;
int maxValue;

Numberbox inputMin;
Numberbox inputMax;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  inputMin = cp5.addNumberbox("minValue")
             .setPosition(100,100)
             .setSize(100,20)
             .setFont(font)
             .setScrollSensitivity(1.1)
             // set initial acceptable range
             .setMin(RANGE_MIN)
             .setMax(RANGE_MAX)
             // set default value
             .setValue(4000)
             ;
  
  inputMax = cp5.addNumberbox("maxValue")
             .setPosition(100,150)
             .setSize(100,20)
             .setFont(font)
             .setScrollSensitivity(1.1)
             // set initial acceptable range
             .setMin(RANGE_MIN)
             .setMax(RANGE_MAX)
             // set default value
             .setValue(RANGE_MID + 1)
             ;
  
  
  textFont(font);
}

void draw() {
  constrainRangeInputs();
  background(0);
  fill(255);
  text("minValue: " + minValue + "\n" +
       "maxValue: " + maxValue, 10, 15);
}

void constrainRangeInputs(){
  int rangeMinInt = (int)inputMin.getValue();
  int rangeMaxInt = (int)inputMax.getValue();
  // 
  if(abs(rangeMaxInt - rangeMinInt) < RANGE_MIN_DIFFERENCE){
    if(rangeMaxInt > RANGE_MID){
      inputMin.setValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
    }else{
      inputMax.setValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
    }
  }
}

将值限制为最小 500 差异的逻辑并非 100% 严格,可能还有一些我没有考虑的其他边缘情况。它更像是一种说明解决问题的方法的方法,因此您可以更好地这样做。

我建议通过 Processing > Examples > Contributed Libraries > ControlP5 和 运行 示例,特别是 controllers.您可以优先考虑那些听起来更接近您当前问题的元素,但值得获得这些选项的经验,以便您可以选择最适合您的问题的 controllers/UI 元素。

该示例可能不包括控制器具有的每个方法的用法,但是在底部有一个注释列表,您可以立即轻松 copy/paste/run。 另外当然你有 full documentation