Sorry for late response, I was doing some other work.
I was afraid that you misunderstood me, my english…
But anyway, this is how the things are.
The first snippet, is written in actionPerform method at the end, and that method is inside PSlider class.
public void performAction(GameContainer gc, int mx, int my) throws SlickException {
Input input = gc.getInput();
this.sliderBoxWidth = Math.round(this.height / 2);
this.sliderBoxHeight = Math.round(this.height / 2);
this.sliderBoxY = Math.round(this.y + ((this.y + this.height) - this.y) / 2
- (this.sliderBoxHeight / 2));
this.lineWidth = Math.round(this.width - this.sliderBoxWidth);
this.lineHeight = 1;
this.lineX = Math.round(this.x + (this.sliderBoxWidth / 2));
this.lineY = Math.round(this.y + ((this.y + this.height) - this.y) / 2);
if (this.sliderBoxX < this.lineX - Math.round(this.sliderBoxWidth / 2)) {
this.sliderBoxX = this.lineX - Math.round(this.sliderBoxWidth / 2);
if (this.value != this.minValue) {
this.value = this.minValue;
this.newValue = this.value;
}
}
else if (this.sliderBoxX > this.lineX + this.lineWidth + Math.round(this.sliderBoxWidth / 2)) {
this.sliderBoxX = this.lineX + this.lineWidth + Math.round(this.sliderBoxWidth / 2);
if (this.value != this.maxValue) {
this.value = this.maxValue;
this.newValue = this.value;
}
}
float mxRelative = mx - this.lineX;
if (my >= this.y && my <= this.y + this.height) {
if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
if ((mxRelative >= 0) && (mxRelative <= this.lineWidth)) {
this.percentage = (float) (mxRelative / this.lineWidth) * 100;
this.value = this.minValue + ((this.maxValue - this.minValue) * percentage) / 100;
this.sliderBoxX = mxRelative + this.lineX - Math.round(sliderBoxWidth / 2);
this.newValue = this.value;
}
}
}
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)) {
wasSliderBoxDown = true;
}
}
if (wasSliderBoxDown) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
if (mxRelative >= 0 && mxRelative <= this.lineWidth) {
this.percentage = (float) (mxRelative / this.lineWidth) * 100;
this.value = this.minValue + ((this.maxValue - this.minValue) * percentage) / 100;
this.sliderBoxX = mxRelative + this.lineX - Math.round(sliderBoxWidth / 2);
this.newValue = this.value;
}
} else {
wasSliderBoxDown = false;
}
}
if (this.newValue != this.value) {
float tempSliderBoxX = 0;
if (tempSliderBoxX >= 0 && tempSliderBoxX <= this.lineWidth) {
if (this.newValue > this.maxValue) {
this.newValue = this.maxValue;
this.value = this.newValue;
this.percentage = 100.0f;
this.sliderBoxX = this.lineX + this.lineWidth + Math.round(this.sliderBoxWidth / 2);
}
else if (this.newValue < this.minValue) {
this.newValue = this.minValue;
this.value = this.newValue;
this.percentage = 0.0f;
this.sliderBoxX = this.lineX - Math.round(this.sliderBoxWidth / 2);
}
else {
this.value = this.newValue;
this.percentage = (float) (tempSliderBoxX / this.lineWidth) * 100;
this.sliderBoxX = tempSliderBoxX;
}
}
}
}
Second snippet, the method update, is part of the state class which extends BasicGameState. The class contains update, render, init methods…
@Override
public void update(GameContainer gc, StateBasedGame sg, int delta) throws SlickException {
Input input = gc.getInput();
int mx = input.getMouseX();
int my = input.getMouseY();
setupTable.setTableStake((String) tableStakesCB.getSelectedItem());
setupTable.setGameSpeed((String) gameSpeedCB.getSelectedItem());
setupTable.setTableTheme((String) tableThemeCB.getSelectedItem());
if (!this.hasFocus) return;
float minValue = 500;
float maxValue = 1000;
// sets min & max values to slider component
tempSlider.setInitialValues(minValue, maxValue);
// performs actions
tempSlider.performAction(gc, mx, my);
// sets a slider value on the text field, if enter is pressed
tempField.setText(String.valueOf(tempSlider.getValue()));
System.out.println("Text field: " + tempField.getText() + " \t Slider: " + tempSlider.getValue());
tableStakesCB.performAction(gc, mx, my);
gameSpeedCB.performAction(gc, mx, my);
tableThemeCB.performAction(gc, mx, my);
}
One of the problems, which I was worried, when I was about to start to create these components, was listener.
I am familiar with it, and I had the opportunity to use an anonymous class listener, back when I made my first Swing project, but the problem is that I hadn’t the opportunity to create the functionality to interface on the simpler examples, that will produce the listener system for the component.
So my idea was that (and since this is my first slick2d project), to make the simple components with out listener interfaces, which will use the basic functions update / render, literally acting as an entity, and I succeeded in that.
The only component that I have not still made, is a TextField, but I hope to the already existing one from Slick2D, that I will be able to do something with it.
So I can finally go to the most fun part, which is to create a game logic, which I have already had made, but as a console game.