1

I am working on my university project and came across an issue. I am creating a slider in Processing that changes value to 0-100. And I want to it to snap into values of 10 (10,20,30...) using the button I created. Here's how it looks:

a left arrow square gray button, a slider with a white handle and gray background and a right arrow square gray button on a black background. the slider background has 9 black vertical bars, almost half the height of the slider which represent slider ticks to snap to

I am sorry if my code looks messy, I am a beginner. Here is my code:

int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0; 
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0; 
float syOffset = 0.0;

void setup() {
  size(550,300);
}

void draw() {
  background(0);
  fill (100);
  rect (75, 25, 400, 50); //the slider track
  
  if(mousePressed) {
  if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
    {x=mouseX;}
    }
    
  float value = map(x, 75, 475, 0, 100);

  fill(255);
  rect (x, 20, 3, 60); //the slider thumb, positioned at x}
  fill (100);

  if(mousePressed){
  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize){
      if(x <=75){
    x=76;}
    fill(255);
      if(x >=475){
      x= x-40;
      }else if (x >= 435){
      x = x-40;
      }  
      }
      }
  
  rect(bx, by, boxSize, boxSize);
  fill(10);
  triangle(30, 50, 50, 40, 50, 60);
  
  fill(100);
  
  if(mousePressed){
  if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
      mouseY > sby-sboxSize && mouseY < sby+sboxSize){
        
  if(x >= 475){
    x=474;}      
        
    fill(255);
    x++;
      }
  }
  
  
  rect(483,25,50,50);
  fill(10);
  triangle(520, 50, 500, 40, 500, 60);
  
  println(value);
  

  line (115, 60, 115, 90);
  line (155, 60, 155, 90);
  line (195, 60, 195, 90);
  line (235, 60, 235, 90);
  line (275, 60, 275, 90);
  line (315, 60, 315, 90);
  line (355, 60, 355, 90);
  line (395, 60, 395, 90);
  line (435, 60, 435, 90);
}

I started trying to figure out how to make it go from 100 to 90 to 80 and so on. But I came across a problem wherein if I press the button, it goes to 80 instead of it stopping to 90 first. I am assuming this is cause by my else if (x >= 435){x = x-40; statement. But I can't seem to figure out a way to fix this issue.

1 Answers1

1

Use the mousePressed() and move the slider to the next line when the button is pressed:

void mousePressed() {
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
        mouseY > by-boxSize && mouseY < by+boxSize) {  
        if (x > 75) {
            x = ((x+39 - 75) / 40) * 40 + 75;
            x -= 40;
        }
    }
    if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
        mouseY > sby-sboxSize && mouseY < sby+sboxSize) {
        if (x < 475) {
            x = ((x - 75) / 40) * 40 + 75;
            x += 40;
        }
    }
}

Complete example:

int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0; 
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0; 
float syOffset = 0.0;

void setup() {
  size(550,300);
}

void mousePressed() {
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
        mouseY > by-boxSize && mouseY < by+boxSize) {  
        if (x > 75) {
            x = ((x+39 - 75) / 40) * 40 + 75;
            x -= 40;
        }
    }
    if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
        mouseY > sby-sboxSize && mouseY < sby+sboxSize) {
        if (x < 475) {
            x = ((x - 75) / 40) * 40 + 75;
            x += 40;
        }
    }
}


void draw() {
  background(0);
  fill (100);
  rect (75, 25, 400, 50); //the slider track
  
  if(mousePressed) {
  if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
    {x=mouseX;}
    }
    
  float value = map(x, 75, 475, 0, 100);

  fill(255);
  rect (x, 20, 3, 60); //the slider thumb, positioned at x}
  fill (100);

  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize) {
      fill(255);
  }
  
  rect(bx, by, boxSize, boxSize);
  fill(10);
  triangle(30, 50, 50, 40, 50, 60);
  
  fill(100);
  if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
      mouseY > sby-sboxSize && mouseY < sby+sboxSize){
      fill(255);
  }
  
  
  rect(483,25,50,50);
  fill(10);
  triangle(520, 50, 500, 40, 500, 60);
  
  println(value);
  

  line (115, 60, 115, 90);
  line (155, 60, 155, 90);
  line (195, 60, 195, 90);
  line (235, 60, 235, 90);
  line (275, 60, 275, 90);
  line (315, 60, 315, 90);
  line (355, 60, 355, 90);
  line (395, 60, 395, 90);
  line (435, 60, 435, 90);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174