So I think my question is very simple but I lack the vocabulary to google it myself effectively, thing is, I’m creating a custom Button class and I need to call custom code when instantiating it. I tried using mehod()
but that only works if the function I’m trying to access is a “public one (?)” doesn’t work if the function is inside a class even if the funciton is declared as public. I also tried using ControlP5 but the result is the same.
Up until now I worked around this by calling a function outside the class that calls a function inside the class. But I feel this is not the correct way, it feels wrong.
This the current code for my button, I know is not great but It “works”.
public class Button{
private String name;
private int x;
private int y;
private int width;
private int height;
private boolean isHovered;
private boolean isPressed;
private boolean isClicked;
private boolean locked = true;
private String method;
private boolean currentMousePressed = false;
private boolean lastMousePressed = false;
private PImage[] images;
private int currentImage = 0;
private boolean usesImages = false;
private color color_search;
private color color_hovered;
private color color_pressed;
private Boolean usesColors = false;
private Boolean act_on_release = false;
private Boolean holdFunction = false;
private int holdTime = 1000;
private timer holdTimer = new timer(holdTime, false);
private String holdMethod = "";
private int holdTimePercentage = 0;
private PImage holdImage;
private Boolean usesHoldImage = false;
int textSize = 20;
public Button(String _name, int _x, int _y, int _width, int _height, String _method){
name = _name;
x = _x;
y = _y;
width = _width;
height = _height;
isHovered = false;
method = _method;
}
public Button(String _name, int _x, int _y, int _width, int _height, String _method, int _holdTime, String _holdMethod){
name = _name;
x = _x;
y = _y;
width = _width;
height = _height;
isHovered = false;
method = _method;
act_on_release = true;
holdFunction = true;
holdTime = _holdTime;
holdTimer.setTime(holdTime);
holdMethod = _holdMethod;
}
public String getName(){
return name;
}
public void setName(String _name){
name=_name;
}
public void setColors(color[] colors){
color_search = colors[0];
color_hovered = colors[1];
color_pressed = colors[2];
usesColors = true;
}
public color getColors(){
return color_search;
}
public void setTextSize(int _textSize){
textSize = _textSize;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public boolean getIsHovered(){
return isHovered;
}
public boolean getIsPressed(){
return isPressed;
}
public boolean getIsClicked(){
boolean out = isClicked;
if(isClicked){
deactivate();
}
return out;
}
private void deactivate(){
isClicked = false;
locked = true;
}
public void setActOnRelease(Boolean _act_on_release){
act_on_release = _act_on_release;
}
public void setHoldImage(PImage _holdImage){
holdImage = _holdImage;
usesHoldImage = true;
}
public void update(){
if(x < mouseX && mouseX < x + width && y < mouseY && mouseY < y + height){
isHovered = true;
}
else{
isHovered = false;
locked = false;
}
if(holdFunction){
holdTimer.update();
holdTimePercentage = (int)map(holdTimer.getElapsedTime(), 0, holdTimer.getTime(), 0, 100);
}
else{
holdTimer.reset();
}
//if the button is clicked, call the method
if(isHovered){
currentMousePressed = mousePressed;
isPressed = currentMousePressed;
//detect if the mouse is clicked
if(currentMousePressed != lastMousePressed){
lastMousePressed = currentMousePressed;
if(currentMousePressed){
if(holdFunction){
holdTimer.start();
}
}
if(!act_on_release){
if(currentMousePressed){
//the mouse is clicked
isClicked = true;
//call the method
try{
method(method);
deactivate();
}
catch(Exception e){
}
}
}
else{
if(!currentMousePressed){
//the mouse is clicked
if(!locked){
isClicked = true;
if(!holdFunction){
//call the method
try{
method(method);
}
catch(Exception e){
}
}
else{
if(!holdTimer.isFinished()){ //if the timer is not finished, execute the search method
try{
method(method);
}
catch(Exception e){
}
}
else{ //if the timer is finished, execute the hold method
try{
method(holdMethod);
}
catch(Exception e){
}
}
holdTimer.stop();
holdTimer.reset();
}
}
}
else{
//the mouse is not clicked
isClicked = false;
locked = false;
}
}
}
}
else{
isHovered = false;
isPressed = false;
isClicked = false;
locked = true;
}
}
public void draw(){
if(usesColors){
if(isPressed){
fill(color_pressed);
}
else if(isHovered){
fill(color_hovered);
}
else{
fill(color_search);
}
}
else{
if(isPressed){
fill(25, 34, 42);
}
else if(isHovered){
fill(69, 163, 188);
}
else{
fill(25, 34, 42);
}
}
stroke(white);
strokeWeight(1);
rect(x, y, width, height, 5);
fill(255);
textAlign(CENTER, CENTER);
textSize(textSize);
text(name, x + width/2, y + height/2 - textAscent()/4);
if(usesHoldImage){
drawHoldImage();
}
}
public void drawHoldImage(){
int startProgress = 30;
if(holdTimePercentage > startProgress){
float progressToDisplay=map(holdTimePercentage, startProgress, 100, 0, TWO_PI);
strokeWeight(10);
if(holdTimePercentage == 100){
stroke(green);
}
else{
stroke(yellow);
}
//noFill();
arc(500, 250, 200, 200, -HALF_PI, progressToDisplay-HALF_PI);
image(holdImage, 500-50, 250-50, 100, 100);
}
}
}
As I said before, this works only for public methods. The main usage will be inside other classes like this:
public class inventory{
Button searchButton;
Button addButton;
public inventory(){
int x = 20;
int y = 20;
searchButton = new Button("Search", x, y, 100, 50, "searchInventory_pub");
addButton = new Button("Add", x, y + 60, 100, 50, "addInventory_pub");
}
public void update(){
searchButton.update();
addButton.update();
searchButton.draw();
addButton.draw();
}
public void searchInventory(){
//whatever this does, not relevant
}
public void addInventory(){
//whatever this does, not relevant
}
}
And then public outer methods that call inside ones:
public void searchInventory_pub(){
inventory.searchInventory();
}
public void addInventory_pub(){
inventory.addInventory();
}
I feel the correct way of working with this would be to instantiate the Button like this inside the class and directly call the function from inside.
searchButton = new Button("Search", x, y, 100, 50, searchInventory());
addButton = new Button("Add", x, y + 60, 100, 50, addInventory());
What would be the clean way to do this?