Hello, I would like to detect a double mouse click from within a class. If a double click is detected, some event has to be executed. I would like the code to be modular, so the mouseClicked() or mousePressed() functions are not a viable option (I think).
More specific, I would like to set the value of the slider back to “startValue” if a double click is detected.
I would probably need to use an event handler, if anyone can show me how to do that using the minimum working example, that would be helpful. Any help is appreciated though. Thanks in advance!
See below for a MWE:
main.pde:
Slider slider1;
void setup(){
size(500, 500);
slider1 = new Slider(75, 75, 225, -50, 250, 75);
}
void draw(){
background(180);
slider1.reDraw(0, 0);
}
And a slider class Slider.pde:
class Slider{
// Default constructor
Slider(){
circleX = (startValue-minValue)*sliderLength/(maxValue-minValue)+x-sliderLength/2; // position of the circle
sliderUnit = "";
sliderName = "";
sliderNameColor = color(255);
sliderTextSize = 20;
lineWeight = 5;
circleHeight = 20;
sliderDarkLineColor = color(49,143,145);
sliderLightLineColor = color(79,193,195);
circleFillColor = color(20,45,55);
circleFillDisabledColor = color(60);
circleStrokeDisabledColor = color(110);
sliderNameColor = color(255);
circleStrokeColorEnabled = color(255);
circleStrokeColorFocused = color(255,0,0);
backgroundColor = color(180);
sliderState = "enabled";
amountOfDecimals = 1;
interrruptLength = 8;
}
// Constructor
Slider(int x, int y, int sliderLength, float minValue, float maxValue, float startValue){
this(); // call default constructor
this.x = x; // x coordinate of left of the slider
this.y = y; // y coordinate of left of the slider
this.sliderLength = sliderLength; // length of the slider in pixels
this.minValue = minValue; // value when slider is left
this.maxValue = maxValue; // value when slider is right
this.startValue = startValue; // position of slider at the beginning
currentValue = startValue; // current value of slider
}
// Set current values of slider and call draw slider function
void reDraw(int xOffset, int yOffset){
if (sliderState == "disabled"){
drawSlider("disabled", xOffset, yOffset);
}
else if(mouseX >= x + xOffset && mouseX <= x + xOffset + sliderLength && mouseY >= y + yOffset - circleHeight/2 && mouseY <= y + yOffset + circleHeight/2 && !mouseClickedFirst){
mousePositionFirst = true;
if(mousePressed && mousePositionFirst){
setCurrentValue();
}
drawSlider("focused", xOffset, yOffset);
}
else if((mouseX <= x + xOffset || mouseX >= x + xOffset +sliderLength || mouseY <= y + yOffset - circleHeight/2 || mouseY >= y + yOffset + circleHeight/2) && mousePressed && mousePositionFirst){
if(mousePressed && !mousePositionFirst){
mouseClickedFirst = true;
}
else if(mouseX >= x + xOffset && mouseX <= x + xOffset +sliderLength && mousePressed && mousePositionFirst){
setCurrentValue();
}
else if(mouseX <= x + xOffset && mousePressed && mousePositionFirst){
currentValue = minValue;
}
else if(mouseX >= x + xOffset + sliderLength && mousePressed && mousePositionFirst){
currentValue = maxValue;
}
else{
mouseClickedFirst = false;
mousePositionFirst = false;
}
drawSlider("focused", xOffset, yOffset);
}
else{
if(mousePressed && !mousePositionFirst){
mouseClickedFirst = true;
}
else if(mouseX >= x + xOffset && mouseX <= x + xOffset +sliderLength && mousePressed && mousePositionFirst){
setCurrentValue();
}
else if(mouseX <= x + xOffset && mousePressed && mousePositionFirst){
currentValue = minValue;
}
else if(mouseX >= x + xOffset + sliderLength && mousePressed && mousePositionFirst){
currentValue = maxValue;
}
else{
mouseClickedFirst = false;
mousePositionFirst = false;
}
drawSlider("enabled", xOffset, yOffset);
}
}
void drawSlider(String state, int xOffset, int yOffset){
stroke(sliderLightLineColor);
strokeWeight(lineWeight);
line(x + xOffset, y + yOffset, x + xOffset + sliderLength, y + yOffset);
stroke(sliderDarkLineColor);
line(getAbsoluteValue(minValue) + xOffset, y + yOffset, getAbsoluteValue(currentValue) + xOffset, y + yOffset);
drawCircle(state, xOffset, yOffset);
stroke(255);
textAlign(CENTER, CENTER);
fill(sliderNameColor);
textSize(sliderTextSize);
textAlign(RIGHT, BOTTOM);
text(nf(currentValue, 0, amountOfDecimals) + sliderUnit, x + xOffset + sliderLength, y + yOffset - circleHeight/2 - lineWeight/2);
fill(sliderNameColor);
textSize(sliderTextSize);
textAlign(LEFT, BOTTOM);
text(sliderName, x + xOffset, y + yOffset - circleHeight/2 - lineWeight/2);
}
void drawCircle(String state, int xOffset, int yOffset){
circleX = (currentValue-minValue)*sliderLength/(maxValue-minValue) + x + xOffset;
fill(backgroundColor);
stroke(backgroundColor);
circle(circleX, y + yOffset, circleHeight + interrruptLength);
if(state == "enabled"){
fill(circleFillColor);
stroke(circleStrokeColorEnabled);
}
else if(state == "disabled"){
fill(circleFillDisabledColor);
stroke(circleStrokeDisabledColor);
}
else{
fill(circleFillColor);
stroke(circleStrokeColorFocused);
}
circle(circleX, y + yOffset, circleHeight);
}
void setCurrentValue(){
if (sliderState == "enabled"){
currentValue = minValue + ((mouseX - (x + xOffset))/sliderLength)*(maxValue - minValue);
}
else{ }
}
float getAbsoluteValue(float sliderValue){
return sliderLength - sliderLength*(maxValue/(maxValue - minValue)) + sliderValue/(maxValue-minValue)*sliderLength + x;
}
// Class attributes
private int x, y, xOffset, yOffset, lineWeight, circleHeight, sliderTextSize, amountOfDecimals, interrruptLength;
private float sliderLength, minValue, maxValue, startValue, currentValue, circleX, circleY;
private boolean mousePositionFirst = false, mouseClickedFirst = false; // used for validation if mouse was clicked outside slider or not
private String sliderName, sliderUnit, sliderState; // name of slider, unit to be displayed and hovering state
private color sliderDarkLineColor, sliderLightLineColor, circleFillColor, circleStrokeColorEnabled, circleStrokeColorFocused, sliderNameColor, circleFillDisabledColor, circleStrokeDisabledColor, backgroundColor; // colors of the slider
}