Processing controlP5列表框选择活动颜色处理

Processing controlP5列表框选择活动颜色处理,processing,control-p5,Processing,Control P5,我希望选定的列表框项目显示为红色,并一直保持这种状态,直到我做出另一个选择。我该怎么做?此时,当我点击并按住鼠标键时,它保持红色,然后在我放开后恢复为原始背景色。这是方法.setColorActive()的函数吗?还是应该在单击后永久更改为我指定的颜色?我的代码如下。谢谢 list = controlp5.addListBox("LIST") .setPosition(130, 40) .setSize(100, 150) .setItemHeight(20)

我希望选定的列表框项目显示为红色,并一直保持这种状态,直到我做出另一个选择。我该怎么做?此时,当我点击并按住鼠标键时,它保持红色,然后在我放开后恢复为原始背景色。这是方法.setColorActive()的函数吗?还是应该在单击后永久更改为我指定的颜色?我的代码如下。谢谢

list = controlp5.addListBox("LIST")
      .setPosition(130, 40)
      .setSize(100, 150)
      .setItemHeight(20)
      .setBarHeight(20)
      .setColorBackground(color(40, 128))
      .setColorActive(color(255, 0, 0))

据我从源代码中所知,无论是否选择了控制器,都没有任何价值可言。但是,您可以使用controlEvent侦听器手动跟踪,并手动更改背景颜色,这是一个快速而棘手的解决方法:

import controlP5.*;

ControlP5 controlp5;
ListBox list;
int selectedIndex = -1;//no selection initially
int colourBG = 0xffff0000;
int colourSelected = 0xffff9900;

void setup(){
  size(400,400);
  controlp5 = new ControlP5(this);
  list = controlp5.addListBox("LIST")
      .setPosition(130, 40)
      .setSize(100, 150)
      .setItemHeight(20)
      .setBarHeight(20)
      .setColorBackground(colourBG)
      .setColorActive(colourSelected);

   for (int i=0;i<80;i++) {
    ListBoxItem lbi = list.addItem("item "+i, i);
    lbi.setColorBackground(colourBG);
  }
}
void draw(){
  background(255);
}
void controlEvent(ControlEvent e) {
  if(e.name().equals("LIST")){
    int currentIndex = (int)e.group().value();
    println("currentIndex:  "+currentIndex);
    if(selectedIndex >= 0){//if something was previously selected
      ListBoxItem previousItem = list.getItem(selectedIndex);//get the item
      previousItem.setColorBackground(colourBG);//and restore the original bg colours
    }
    selectedIndex = currentIndex;//update the selected index
    list.getItem(selectedIndex).setColorBackground(colourSelected);//and set the bg colour to be the active/'selected one'...until a new selection is made and resets this, like above

  }

}
导入控制p5.*;
控制p5控制p5;
列表框列表;
int selectedIndex=-1//最初没有选择
int COLORBG=0xffff0000;
int colorSelected=0xffff9900;
无效设置(){
尺寸(400400);
controlp5=新的controlp5(本);
list=controlp5.addListBox(“列表”)
.设置位置(130,40)
.设置大小(100150)
.setItemHeight(20)
.立根高度(20)
.setColorBackground(彩色背景)
.setColorActive(已选择颜色);
对于(int i=0;i=0){//如果以前选择了某个对象
ListBoxItem previousItem=list.getItem(selectedIndex);//获取项目
previousItem.setColorBackground(colorBG);//并恢复原始bg颜色
}
selectedIndex=currentIndex;//更新所选索引
list.getItem(selectedIndex).setColorBackground(colorSelected);//并将bg颜色设置为活动的/“选定颜色”…直到进行新的选择并重置此颜色,如上所述
}
}
因此,在上面的示例中,selectedIndex将以前/最近的选择存储为列表索引。然后,在controlEvent处理程序中使用此选项。如果之前进行了选择,则恢复正常背景色。然后继续将所选索引设置为最近选择的索引,并将背景颜色设置为活动颜色,使其在视觉上看起来处于选中状态


这是一种手动/黑客方法。较长的版本可能涉及扩展ListBox java类并在中添加此功能,或者编辑controlP5源代码,重新编译库并使用自定义版本。

您使用的是哪个版本的processing and controlP5?我使用的是processing version 1.5.1和controlP5 1.5.2。我使用的是旧版本,因为我的应用程序使用的是GLGraphics 1.0.0,我很难让它在新版本的处理上工作。我也这么认为。我也在做类似的事情,多亏了你的例子,我现在已经成功了。非常感谢。