如何创建两个字段的条件?

huangapple go评论57阅读模式
英文:

How to make a condition of two fields?

问题

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

  1. 最小字段不得超过最大字段。Min < Max / 5000
    输入时禁止 < 4000。
  2. 最大字段不得小于最小字段。Max < Min / 4000 < 5000
    输入时禁止。
  3. 最小字段不应超过最大字段的间隔,可以少500个单位。
    例如:
    Min 4500 和 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);
}

如何创建两个字段的条件?
如何创建两个字段的条件?
如何创建两个字段的条件?

英文:

Good afternoon, I need to add several conditions to validate two fields in the current code.

  1. The Min field must not exceed the Max field. Min <Max / 5000
    <4000 prohibited when entering.
  2. The Max field must not be less than the Min field. Max <Min / 4000 <5000 prohibited when entering.
  3. The Min field should not exceed the interval from the Max field, 500 units less can be entered.
    For instance:
    Min 4500 and Max 5000, 4400/5000, 4250/5000, 4501/5000 are prohibited to enter and above.
  4. The Min and Max fields must not be equal. Min = Max is prohibited when entering.

如何创建两个字段的条件?
如何创建两个字段的条件?如何创建两个字段的条件?

code:

import controlP5.*;
ControlP5 cp5;
Textfield O;
Textfield OO;
void setup() {
size(700, 400);
PFont font = createFont(&quot;arial&quot;, 18);
cp5 = new ControlP5(this);
O = cp5.addTextfield(&quot;MIN&quot;)
.setPosition(20, 100)
.setSize(200, 40);
O.setInputFilter(ControlP5.INTEGER)
.setFont(font)
.setColor(color(255, 0, 0));
OO = cp5.addTextfield(&quot;MAX&quot;)
.setPosition(20, 170)
.setSize(200, 40);
OO.setInputFilter(ControlP5.INTEGER)
.setFont(font);
textFont(font);
}
void draw() {
if (keyPressed &amp;&amp; OO.isFocus()) {
float n;
try {
n = Float.parseFloat(OO.getText().replace(&#39;,&#39;, &#39;.&#39;));
if (!(n &gt;= 1 &amp;&amp; n &lt;= 12000)) {
throw new NumberFormatException(); // throw to catch below
}
} 
catch (Exception e2) {
String t;
if (OO.getText().length() &gt; 1) {
t = OO.getText().substring(0, OO.getText().length() - 1);
} else {
t = &quot;&quot;;
}
OO.setText(t);
}
}
if (keyPressed &amp;&amp; O.isFocus()) {
float n;
try {
n = Float.parseFloat(O.getText().replace(&#39;,&#39;, &#39;.&#39;));
if (!(n &gt;= 1 &amp;&amp; n &lt;= 11500)) {
throw new NumberFormatException(); // throw to catch below
}
} 
catch (Exception e2) {
String t;
if (O.getText().length() &gt; 1) {
t = O.getText().substring(0, O.getText().length() - 1);
} else {
t = &quot;&quot;;
}
O.setText(t);
}
}
background(0);
fill(255);
}

答案1

得分: 2

总的来说,你似乎想让用户输入一系列有效范围的值(其中最小值始终小于最大值)。已经有一个ControlP5控制器可以实现这一功能:Range

除了允许在范围内设置最小值和最大值之外,还有一个限制,即最大值和最小值之间的差值至少为500。

你可以通过使范围滑块的手柄宽度为0像素来实现,从而将其禁用,这意味着你在开始时设置的范围(通过setRangeValues)将会保持不变:

// 代码示例

另一个限制是范围不能大于500。如果这是一个要求,你仍然可以手动限制值(通过设置范围的最小值/最大值):

// 代码示例

如果上述示例占用了太多空间,你可以使用Numberbox,与文本字段相比,它具有一些优势:

  • 你不需要担心字符串到整数的转换(它默认处理数字)
  • 你可以设置最小/最大值

以下是一个示例:

// 代码示例

这个逻辑可以将值约束到至少500的差值,不是百分之百的严格,可能有一些我没有考虑到的边缘情况。这更多是为了说明解决问题的方式,以便你更好地应对问题。

我建议你查看Processing > Examples > Contributed Libraries > ControlP5,并运行示例,特别是controllers部分。你可以优先考虑与你当前问题更接近的示例,但值得对这些选项有更多了解,以便能够选择最适合解决问题的控制器/用户界面元素。

这个示例可能没有包含控制器的每个方法的使用,但底部有一个注释列表,你可以轻松地复制/粘贴/运行。

另外,当然还有完整的文档可供参考。

英文:

Overall it sounds like you're trying to have the user enter a range of valid values (where the minimum is always smaller than the maximum). There's already a ControlP5 controller for that: Range

Other than allowing to set a min and max value within a range is the constraint to keep a difference between max and min value of at least 500.

You could get away with making the Range slider handles 0px wide, essentially disabling them which means the range you set at the start (via setRangeValues) will be maintained:

import controlP5.*;
ControlP5 cp5;
Range range;
int rangeMinValue;
int rangeMaxValue;
void setup() {
size(700, 400);
PFont font = createFont(&quot;arial&quot;, 18);
cp5 = new ControlP5(this);
range = cp5.addRange(&quot;yourRange&quot;)
// 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(&quot;yourRange&quot;)) {
// 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(&quot;range:&quot;,rangeMinValue,&quot;-&gt;&quot;,rangeMaxValue);
}
}

如何创建两个字段的条件?

The one limitation is that ranges can't be > 500. If that's a requirment, you can still manually constrain the values (by setting the range min(low)/max(high) values):

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(&quot;arial&quot;, 18);
cp5 = new ControlP5(this);
range = cp5.addRange(&quot;yourCustomRange&quot;)
// 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(&quot;yourCustomRange&quot;)) {
// 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 &gt;= 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 &gt; RANGE_MID){
range.setLowValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
}else{
range.setHighValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
}
}
// values to use
println(&quot;range:&quot;,rangeMinValue,&quot;-&gt;&quot;,rangeMaxValue);
}
}

如何创建两个字段的条件?

If that takes too much space you can use Numberbox which compared to the text field has a few advantages:

  • you don't need to worry about string to integer conversion (it handles numbers by default)
  • you can set min/max values

Here's an example:

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(&quot;arial&quot;, 18);
cp5 = new ControlP5(this);
inputMin = cp5.addNumberbox(&quot;minValue&quot;)
.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(&quot;maxValue&quot;)
.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(&quot;minValue: &quot; + minValue + &quot;\n&quot; +
&quot;maxValue: &quot; + maxValue, 10, 15);
}
void constrainRangeInputs(){
int rangeMinInt = (int)inputMin.getValue();
int rangeMaxInt = (int)inputMax.getValue();
// 
if(abs(rangeMaxInt - rangeMinInt) &lt; RANGE_MIN_DIFFERENCE){
if(rangeMaxInt &gt; RANGE_MID){
inputMin.setValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
}else{
inputMax.setValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
}
}
}

如何创建两个字段的条件?

The logic constrain values to a minimum 500 difference is not 100% tight, there may some other edge cases I haven't considered. It's more of a way to illustrate ways of solving the problem so you're better equipt to do so.

I would recommend going through Processing > Examples > Contributed Libraries > ControlP5 and running the examples, in particular the controllers. You can prioritise the ones that sound closer to your current problem, but it's worth getting experience with the options so you can choose the best controllers/UI element to fit your problem.

The example may not include usage of every method the controller has, however there's a comment list at the bottom you could easily copy/paste/run immediately.
Additionally of course you have the full documentation

huangapple
  • 本文由 发表于 2020年10月7日 18:02:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定