2013-11-23 39 views
1

我想要一個選定的列表框項目以紅色顯示,並保持這種方式,直到我做出另一個選擇。我怎樣才能做到這一點?此刻,當我點擊並按住鼠標鍵時,它會保持紅色,然後在放開後恢復原始背景顏色。這是方法.setColorActive()?的函數,還是應該永久更改爲點擊後指定的顏色?我的代碼如下。謝謝。controlP5列表框選擇活動顏色處理

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

你正在使用什麼版本的處理和controlp5? –

+0

我正在使用處理版本1.5.1和controlp5 1.5.2。我正在使用舊版本,因爲我的應用程序正在使用GLGraphics 1.0.0,並且無法使它在更新版本的處理中工作。 – omegaFlame

回答

2

據我從源代碼可以看出,沒有價值來跟蹤控制器被選中與否。但是,您可以手動跟蹤使用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 

    } 

} 

所以在上面的例子中,的selectedIndex存儲先前/最近選擇的列表索引。這然後在controlEvent處理程序中使用。如果之前進行了選擇,請恢復正常的背景顏色。然後繼續將選定的索引設置爲最近選擇的項目,並將背景色設置爲活動項目,這樣看起來就像是選中了。

這是一個手動/哈克的方法。較長的版本將涉及擴展ListBox java類並添加此功能,或者編輯controlP5源代碼,重新編譯庫並使用定製版本。

+0

我這麼認爲。我正在做類似的工作,感謝你的例子,我現在已經開始工作了。非常感激。 – omegaFlame