Andreas, that would be great!
Here's my modified form of your ControllerSprite.java, I added a new constructor, and a couple of methods (one of which allows setting total sprite states w/o using the new constructor, one to force into a given display state [this is used at display update time only, does not affect other activities], and one to count the total number of states a sprite has).
The only oddity is that I thought it would be nice to specify the count of states by their actual count, rather than index, so one would say there are 2 states, but lock into state #1 to get the second position.
Code:
package controlP5;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;
public class ControllerSprite {
// cc - andreas - I had to remove comment so I could post to BB
protected PImage sprite;
protected PImage display;
protected PImage mask;
protected int offsetX;
protected int offsetY;
protected int width;
protected int height;
protected int wh;
protected boolean isMask;
protected int _myState;
// cc - added to support forcing of state
// and count of total states
protected int _forceState = 0;
// default value (3 total states 0-2)
protected int _totalStates = 2;
public ControllerSprite(ControlP5 theControlP5,
PImage theImage,
int theWidth,
int theHeight) {
sprite = theImage;
width = theWidth;
height = theHeight;
wh = width*height;
_myState = 0;
display = new PImage(theWidth, theHeight);
display = theControlP5.papplet.createImage(theWidth, theHeight,PApplet.RGB);
update();
}
public ControllerSprite(ControlP5 theControlP5,
PImage theImage,
int theWidth,
int theHeight,
int theStates) {
sprite = theImage;
width = theWidth;
height = theHeight;
wh = width*height;
_myState = 0;
// cc - added to support a specified # of states
// specify total states, not index - e.g.:
// two total states arg should = 2, even though
// max state index is 1.
theStates--;
// cc - make sure we have at least one state
_totalStates = (theStates >= 0) ? theStates : 0;
display = new PImage(theWidth, theHeight);
display = theControlP5.papplet.createImage(theWidth, theHeight,PApplet.RGB);
update();
}
public void draw(
PApplet theApplet) {
theApplet.pushStyle();
theApplet.imageMode(PApplet.CORNER);
if(isMask) {
display.mask(mask);
}
theApplet.image(display, 0, 0);
theApplet.popStyle();
}
public void update() {
// cc - added to limit to a given # of
// states, also force state if forced state
// is greater than 0
int state = _forceState > 0 ? _forceState : _myState;
if( state > _totalStates )
state = _totalStates;
display.loadPixels();
System.arraycopy(sprite.pixels,wh * state,display.pixels,0,wh);
display.updatePixels();
}
// cc - added to support a specified # of states
// specify total states, not index - e.g.:
// two total states arg should = 2, even though
// max state index is 1.
public void setTotalStates(int count) {
// reduce index value
count--;
// cc - make sure we have at least one state
_totalStates = (count >= 0) ? count : 0;
}
// cc - added, to force state
public void setForceState( int state ) {
_forceState = state;
offsetY = height * _forceState;
update();
}
// cc - added to query count of states
public int getStateCount() {
return _totalStates;
}
public void setOffset(
int theX,
int theY) {
offsetX = theX;
offsetY = theY;
update();
}
public void setState(int theState) {
if(theState!=_myState) {
_myState = theState;
offsetY = height * _myState;
update();
}
}
public int getState() {
return _myState;
}
public int width() {
return width;
}
public int height() {
return height;
}
public void setMask(
PImage theImage) {
mask = theImage;
}
public void enableMask() {
isMask = true;
}
public void disableMask() {
isMask = true;
}
}
And here's an example of how I'm using it:
Code:
...
if( sprite_img.exists() ) {
ControllerSprite sprite = new ControllerSprite(panel,loadImage(img_path),w,h,2);
b.setSprite(sprite);
sprite_hm.put(b_name, sprite);
}
...
void show_panel(String grp_name, String spr_name) {
// look for a sprite that would represent the current button being
// displayed
ControllerSprite sprite = (ControllerSprite) get_sprite(current_spr);
// get the group that is currently being displayed
ControlGroup this_grp = (ControlGroup) get_group(current_grp);
if( ! grp_name.equals(current_grp) ) {
// let go of the clicked status
// for a given nav menu button, if
// currently clicked - only if the
// supplied sprite is valid
if( ! spr_name.equals("") ) {
// set previous sprite to no longer be
// forced into a state...
if( sprite != null )
sprite.setForceState(0);
// set new sprite (if available) as
// pressed.
sprite = (ControllerSprite) get_sprite(spr_name);
// force into 'pressed' state
if( sprite != null )
sprite.setForceState(1);
current_spr = spr_name;
}
// hide the previous group of controls
if( this_grp != null )
this_grp.hide();
// show the requested view
this_grp = (ControlGroup) get_group(grp_name);
if( this_grp != null )
this_grp.show();
// if provided, set the current sprite name argument
// locked into position #1 (mouse-over/clicked)
current_grp = grp_name;
}
}
Thanks again,
Chris