I’m creating a slider component for my game with graphics in Slick2D. Almost everything is working as supposed but there’s a problem, to properly control the slider box. As you can see below in the code, I’m increasing or decreasing the value, instead of changing the sliderBoxX by the position of the mouseX. So tell me should I start all over again or is it still possible to make this code working in this code order, to make sliderBoxX and component values change by mouse X coordinate?
As a matter of a problem, is the order of the code where I compute and check things wrong?
Code is inside update method:
this.minusBoxX = this.x;
this.minusBoxY = this.y;
this.minusBoxWidth = this.height;
this.minusBoxHeight = this.height;
this.plusBoxX = this.x + this.width - this.height;
this.plusBoxY = this.y;
this.plusBoxWidth = this.height;
this.plusBoxHeight = this.height;
this.sliderBoxWidth = this.height / 2;
this.sliderBoxHeight = this.height / 2;
this.minSliderX = this.minusBoxX + this.minusBoxWidth;
this.maxSliderX = this.plusBoxX;
this.lineWidth = Math.abs(minSliderX - maxSliderX) - this.sliderBoxWidth;
this.percentage = this.value / this.maxValue;
this.sliderBoxX = this.minSliderX + (this.lineWidth * this.percentage);
this.sliderBoxY = this.y + ((this.y + this.height) - this.y) / 2
- (this.sliderBoxHeight / 2);
float minSliderBoxX = this.sliderBoxX;
float minSliderBoxY = this.sliderBoxY;
float maxSliderBoxX = this.sliderBoxX + this.sliderBoxWidth;
float maxSliderBoxY = this.sliderBoxY + this.sliderBoxHeight;
if ((mx > minSliderBoxX && mx < maxSliderBoxX) && (my > minSliderBoxY && my < maxSliderBoxY)) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
if (this.percentage * 100 >= 0 && this.percentage * 100 <= 100) {
this.wasSliderBoxDown = true;
}
}
}
if (this.wasSliderBoxDown) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
if (this.percentage * 100 >= 0 && this.percentage * 100 <= 100) {
// this.sliderBoxX = mx; something like this
if (mx > maxSliderBoxX) {
this.value += 0.1f * 100.0f; // this works, but is not correct
}
if (mx < minSliderBoxX) {
this.value -= 0.1f * 100.0f; // this works, but is not correct
}
}
} else {
this.wasSliderBoxDown = false;
}
}
Edit:
Ok, I’m changing mind. I will make first the slider line made of a component width, and not like I did between minus and plus boxes. But now the questions is, what should I change current value or a slider box position from the mx and then compute percentage? And what checks should I provide, like should I check the percentage, value or current mx positon for bounds?