Skip to content

Instantly share code, notes, and snippets.

@MultiMote
Created August 14, 2014 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MultiMote/479db07ec89ce675c5a6 to your computer and use it in GitHub Desktop.
Save MultiMote/479db07ec89ce675c5a6 to your computer and use it in GitHub Desktop.
package com.multimote.microz.gui.elements;
import com.multimote.microz.gui.ScreenTools;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import org.lwjgl.opengl.GL11;
/**
* Created by multimote on 12.07.14.
*/
public class SpinnerElement extends Gui {
public int value;
private final FontRenderer fontRenderer;
private final int xPosition;
private final int yPosition;
private final int width;
private final int height;
private boolean canLoseFocus = true;
private boolean isFocused;
private boolean isEnabled = true;
private boolean visible = true;
private String text = "";
public boolean restrict;
public int min;
public int max;
public SpinnerElement(FontRenderer fontRenderer1, int x, int y, int width, int height, int value) {
this.fontRenderer = fontRenderer1;
this.xPosition = x;
this.yPosition = y;
this.width = width;
this.height = height;
setValue(value);
}
public SpinnerElement(FontRenderer fontRenderer1, int x, int y, int width, int height, int value, int min, int max) {
this(fontRenderer1, x, y, width, height, value);
this.min = min;
this.max = max;
this.restrict = true;
}
public void setValue(int val) {
this.text = Integer.toString(val);
this.value = val;
if (restrict) {
if (value > max) setValue(max);
if (value < min) setValue(min);
}
}
public int getValue() {
if (this.text.length() > 0)
try {
this.value = Integer.parseInt(this.text);
} catch (NumberFormatException ex) {
this.setValue(0);
}
else this.value = 0;
return this.value;
}
public boolean isNumber(char c) {
return ((c >= 48 && c <= 57) || c == 45);
}
public String filerNumbers(String par0Str) {
StringBuilder stringbuilder = new StringBuilder();
char[] achar = par0Str.toCharArray();
int i = achar.length;
for (int j = 0; j < i; ++j) {
char c0 = achar[j];
if (this.isNumber(c0)) {
stringbuilder.append(c0);
}
}
return stringbuilder.toString();
}
public void writeText(String str) {
this.text += filerNumbers(str);
try {
this.value = Integer.parseInt(text);
} catch (NumberFormatException ex) {
this.value = 0;
}
if (restrict) {
if (value > max) setValue(max);
if (value < min) setValue(min);
}
}
public boolean textboxKeyTyped(char letter, int id) {
if (!this.isFocused) {
return false;
} else {
switch (id) {
case 14:
if (this.isEnabled) {
this.deleteChar();
}
return true;
default:
if (this.isNumber(letter)) {
if (this.isEnabled) {
if ((int) letter == 45 && text.length() > 0) return false;
this.writeText(Character.toString(letter));
}
return true;
} else {
return false;
}
}
}
}
private void deleteChar() {
if (this.text.length() > 0) {
this.text = text.substring(0, this.text.length() - 1);
} else this.text = "0";
}
public boolean isHovering(int x, int y){
return x >= this.xPosition && x < this.xPosition + this.width && y >= this.yPosition && y < this.yPosition + this.height;
}
public boolean isDownHovering(int x, int y){
return isHovering(x,y) && x > this.xPosition + this.width - 15 && y > this.yPosition + this.height / 2;
}
public boolean isUpHovering(int x, int y){
return isHovering(x,y) && x > this.xPosition + this.width - 15 && y <= this.yPosition + this.height / 2;
}
public void mouseClicked(int x, int y, int button) {
boolean flag = isHovering(x,y);
if (this.canLoseFocus) {
this.setFocused(flag);
}
if(isUpHovering(x, y))setValue(getValue() + 1);
else
if(isDownHovering(x,y)) setValue(getValue() - 1);
}
public void drawTextBox(int x, int y) {
if (this.getVisible()) {
drawRect(this.xPosition - 1, this.yPosition - 1, this.xPosition + this.width + 1, this.yPosition + this.height + 1, this.isFocused ? 0xFFBBBBBB : 0xFF787878);
drawRect(this.xPosition, this.yPosition, this.xPosition + this.width - 16, this.yPosition + this.height, -16777216);
Minecraft.getMinecraft().renderEngine.bindTexture(ScreenTools.elements);
if(isUpHovering(x,y)) GL11.glColor4f(1F, 1F, 1F, 1F); else GL11.glColor4f(0.8F, 0.8F, 0.8F, 1F);
drawTexturedModalRect(this.xPosition + this.width - 15, this.yPosition, 0, 20, 15, 7);
if(isDownHovering(x, y)) GL11.glColor4f(1F, 1F, 1F, 1F); else GL11.glColor4f(0.8F, 0.8F, 0.8F, 1F);
drawTexturedModalRect(this.xPosition + this.width - 15, this.yPosition+7, 0, 27, 15, 7);
if (text.length() > 0) {
this.fontRenderer.drawString(text, this.xPosition + 2, this.yPosition + this.height / 2 - 4, this.isFocused ? 0xFFFFFFFF : 0xFF646464);
}
}
}
public void setFocused(boolean f) {
this.isFocused = f;
}
public boolean isFocused() {
return this.isFocused;
}
public void setEnabled(boolean e) {
this.isEnabled = e;
}
public int getWidth() {
return this.width;
}
public void setCanLoseFocus(boolean is) {
this.canLoseFocus = is;
}
public boolean getVisible() {
return this.visible;
}
public void setVisible(boolean p_146189_1_) {
this.visible = p_146189_1_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment