Get id while hovering in scrollable list

PerPer
edited March 2016 in Library Questions

Is there a way of getting the individual id of scrollable list in controlP5 while hovering? In the sketch below you get the id by clicking. Possible to get it by hovering?

import controlP5.*;
import java.util.*;

//String info;

ControlP5 cp5;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);
  List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
  cp5.addScrollableList("dropdown")
    .setPosition(100, 100)
    .setSize(200, 100)
    .setBarHeight(20)
    .setItemHeight(20)
    .addItems(l)
    .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
    .onMove(new CallbackListener() { 
    public void controlEvent(CallbackEvent theEvent) {
      float value = theEvent.getController().getValue();
      println("the value is " + value);
    }
  }
  );
}

void draw() {
  background(240);
  // HOVER
  boolean over = cp5.get(ScrollableList.class, "dropdown").isMouseOver();
  // println("OVER " + over);
}

void dropdown(int n) {
  println("id:" + n);
}

Answers

  • PerPer
    edited March 2016

    bump

    not possible to get the value im rolling over somehow by Callbacklistener like this or am i on the wrong path:

        .onMove(new CallbackListener() { 
        public void controlEvent(CallbackEvent theEvent) {
          float value = theEvent.getController().getValue();
          println("the value is " + value);
        }
    

    @sojamo ?

  • Look in the... documentation maybe??

  • PerPer
    edited March 2016

    yes I have but cannot figure it out. I done it with buttons through a stringlist though but cannot manage rollover with a ScrollableList

    import controlP5.*;
    
    StringList lista;
    
    ControlP5 cp5;
    
    void setup() {
      size(400, 400);
      cp5 = new ControlP5(this);
    
      lista = new StringList(); 
    
      lista.append("Button 1");
      lista.append("Button 2");
    
      for (int i = 0; i < lista.size(); i++) {
        cp5.addButton(lista.get(i))
          .setValue(i)
          .setPosition(100, 100+ (i*40) )
          .setSize(200, 30)
          .onEnter(new CallbackListener() { 
          public void controlEvent(CallbackEvent theEvent) {
            float value = theEvent.getController().getValue();
            println("the value is " + value);
          }
        }
        );
      }
    }
    
    void draw() {
      background(240);
    }
    
  • i googled for this quite a bit, but couldn't find any solutions to this. is there someone who managed..?

  • I looked up the source code for this (libraries/controlP5/src/controlP5/ScrollableList.java), and saw that "itemHover", the variable storing the required info, is declared protected. Because of this, subclassing lets us access the value. A quick and dirty, working solution exposes said variable:

    public class MyScrollableList extends ScrollableList {
      // the constructor is required, since there is no constructor in 
      // ScrollableList that allows for implicit calling 
      MyScrollableList (ControlP5 controlp5, String name) {
        super(controlp5, name);
      }
    
      // returns -1 if nothing is selected
      int getHover() {
        return itemHover;
      }
    }
    
    // ...
    
    MyScrollableList myScrollableList = null
    myScrollableList = new MyScrollableList (cp5, "myScrollableList");
    myScrollableList.setPosition(10, 10)
                    .setSize(280, 300)
                    .setItemHeight(15);
    
    // ...
    
    int hoveredItem = myScrollableList .getHover();
    if (hoveredItem >= 0) {
      Map<String , Object> item =  myScrollableList getItem(hoveredItem);
      println(item.get("name").toString());
    }
    

    Hope this helps!

Sign In or Register to comment.