/*
* This file is part of Spoutcraft (http://wiki.getspout.org/).
*
* Spoutcraft is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Spoutcraft is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.getspout.spout.gui;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.getspout.spout.client.SpoutClient;
import net.minecraft.src.GuiButton;
public class GenericSlider extends GenericControl implements Slider {
protected float slider = 0.5f;
private CustomGuiSlider sliderControl = null;
public GenericSlider() {
}
@Override
public int getNumBytes() {
return super.getNumBytes() + 4;
}
public int getVersion() {
return super.getVersion() + 0;
}
@Override
public void readData(DataInputStream input) throws IOException {
super.readData(input);
setSliderPosition(input.readFloat());
}
@Override
public void writeData(DataOutputStream output) throws IOException {
super.writeData(output);
output.writeFloat(getSliderPosition());
}
@Override
public float getSliderPosition() {
return slider;
}
@Override
public Slider setSliderPosition(float value) {
if (value > 1f) {
value = 1f;
}
else if (value < 0f) {
value = 0f;
}
slider = value;
return this;
}
@Override
public double getScreenX() {
return super.getScreenX();
}
@Override
public double getScreenY() {
return super.getScreenY();
}
@Override
public WidgetType getType() {
return WidgetType.Slider;
}
protected void setup(int x, int y) {
this.x = x;
this.y = y;
}
private int x;
private int y;
@Override
public void render() {
if (sliderControl == null) {
boolean success = false;
if (SpoutClient.getHandle().currentScreen instanceof CustomScreen) {
CustomScreen popup = (CustomScreen)SpoutClient.getHandle().currentScreen;
for (GuiButton control : popup.getControlList()) {
if (control instanceof CustomGuiSlider) {
if (control.equals(this)) {
sliderControl = (CustomGuiSlider)control;
sliderControl.updateWidget(this);
success = true;
break;
}
}
}
if (!success) {
sliderControl = new CustomGuiSlider(getScreen(), this);
popup.getControlList().add(sliderControl);
}
}
}
sliderControl.drawButton(SpoutClient.getHandle(), x, y);
}
}