Slider objects, only 1 working
in
Programming Questions
•
2 years ago
Hello,
I'm new to using objects and classes, and having some kind of logic problem with sliders. I hav 2 sliders, A and B. B works perfectly, but A doesn't reset when I let go. Can anyone see why and help me, please? Thanks!
Slider sliderA;
Slider sliderB;
int topRowY = 4;
int topRowY2 = 306;
int midRowY = 348;
int midRowY2 = 568;
int bottomRowY = 610;
int bottomRowY2 = 674;
int bracketHeight = 10;
int sliderPositionA;
int sliderPositionB;
void setup(){
rectMode(CORNER);
smooth();
size(1408, bottomRowY2+4);
background(255);
sliderA = new Slider(topRowY2 +bracketHeight + 2, 5, 1);
sliderB = new Slider(midRowY2 +bracketHeight + 2, 64, 5);
sliderPositionA = 4;
sliderPositionB = 4;
sliderA.displaySlider(sliderPositionA);
sliderB.displaySlider(sliderPositionB);
}
void draw(){
//sliderA.displaySlider(sliderPositionA);
//sliderB.displaySlider(sliderPositionB);
if (mouseY > topRowY2 && mouseX < midRowY){
sliderA.mouseOverCheck(sliderPositionA);
}
if (mouseY > midRowY2 && mouseY < bottomRowY){
sliderB.mouseOverCheck(sliderPositionB);
}
}
void mousePressed(){
sliderA.sliderPressed();
sliderB.sliderPressed();
}
void mouseDragged(){
sliderPositionA = sliderA.sliderDraggedPosition(sliderPositionA);
sliderPositionB = sliderB.sliderDraggedPosition(sliderPositionB);
sliderA.displaySlider(sliderPositionA);
sliderB.displaySlider(sliderPositionB);
}
void mouseReleased(){
sliderA.sliderReleasedLogic();
sliderB.sliderReleasedLogic();
}
class Slider {
//these are global variables for the class
int sliderY;
int daysInRow;
int daysInBracket;
int sliderX;
int arrowHeight = 16;
int arrowWidth = 10;
int sliderX2 = sliderX + arrowWidth;
int sliderY2 = sliderY + bracketHeight*2 + arrowHeight;
int sliderPosition;
int bracketWidth;
boolean sliderOver;
boolean sliderLocked;
//this is the constructor
Slider(int tempSliderY, int tempDaysInRow, int tempDaysInBracket) {
sliderY = tempSliderY;
daysInRow = tempDaysInRow;
daysInBracket = tempDaysInBracket;
}
//these are functions that live within slider
//draws the slider at the x position it is given in the parameter. x is middle of arrow/ bracket
void displaySlider(int tempSliderPosition) {
//makes bracket width of the
bracketWidth = (width/ daysInRow) * daysInBracket;
//draw rectangles to clear slider
fill(255);
noStroke();
rectMode(CORNER);
rect(0, sliderY - bracketHeight, 1408, sliderY + arrowHeight + bracketHeight);
//draw triangles
fill(0);
stroke(0);
beginShape();
vertex(tempSliderPosition - arrowWidth/2 + bracketWidth/2, sliderY);
vertex(tempSliderPosition + arrowWidth/2 + bracketWidth/2, sliderY);
vertex(tempSliderPosition - arrowWidth/2 + bracketWidth/2, sliderY + arrowHeight);
vertex(tempSliderPosition + arrowWidth/2 + bracketWidth/2, sliderY + arrowHeight);
endShape();
//draw top bracket
noFill();
beginShape();
vertex(4, sliderY - bracketHeight);
vertex(4, sliderY);
vertex(1404, sliderY);
vertex(1404, sliderY - bracketHeight);
endShape();
//drawMovingBracket
beginShape();
vertex(tempSliderPosition, sliderY + arrowHeight + bracketHeight);
vertex(tempSliderPosition, sliderY + arrowHeight);
vertex(tempSliderPosition + bracketWidth, sliderY + arrowHeight);
vertex(tempSliderPosition + bracketWidth, sliderY + arrowHeight + bracketHeight);
endShape();
}
//this checks if mouse is over bracket and says "yea, hey, I'm totally over bracket"
void mouseOverCheck(int tempSliderPosition){
sliderOver = false;
sliderOver = false;
if (mouseX > (tempSliderPosition - arrowWidth/2 + bracketWidth/2 - 4)
&& mouseX < (tempSliderPosition + arrowWidth/2 + bracketWidth/2 +4)
&& mouseY > (sliderY -4) && mouseY < (sliderY + arrowHeight +4)) {
sliderOver = true;
println(sliderOver);
}
else {
sliderOver = false;
println(sliderOver);
}
/*
if (sliderOver = true){
cursor(HAND);
}
else if (sliderOver = false){
cursor(CROSS);
}
*/
}
void sliderPressed(){
if (sliderOver) {
sliderLocked = true;
}
else {
sliderLocked = false;
}
}
int sliderDraggedPosition(int tempSliderPosition){
if (sliderLocked){
mouseX = constrain(mouseX, 4, width - bracketWidth/2 - 4);
int position = mouseX;
//println(position);
return position;
}
else {
return tempSliderPosition;
}
}
void sliderReleasedLogic(){
sliderLocked = false;
}
}
1