add new files

This commit is contained in:
Sen 2025-03-11 14:09:49 +01:00
parent 4e90a93d68
commit 8038516a66
65 changed files with 7996 additions and 0 deletions

View file

@ -0,0 +1,44 @@
package game.gui.element;
import game.Game;
import game.gui.Gui;
import game.util.Formatter;
import game.window.Button;
public class ActButton extends Element {
public static enum Mode {
PRIMARY, SECONDARY, TERTIARY;
}
public static interface Callback {
void use(ActButton elem, Mode action);
}
private final Callback func;
public ActButton(int x, int y, int w, int h, Callback callback, Formatter<ActButton> formatter) {
super(x, y, w, h, formatter);
this.func = callback;
this.formatText();
}
public ActButton(int x, int y, int w, int h, Callback callback, String text) {
super(x, y, w, h, null);
this.func = callback;
this.setText(text);
}
public ActButton(int x, int y, int w, int h, Gui gui, String text) {
this(x, y, w, h, new Callback() {
public void use(ActButton elem, Mode action) {
Game.getGame().displayGuiScreen(gui);
}
}, text);
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
this.func.use(this, (ctrl || (btn == Button.MOUSE_MIDDLE)) ? Mode.TERTIARY : ((shift || (btn == Button.MOUSE_RIGHT)) ? Mode.SECONDARY : Mode.PRIMARY));
this.formatText();
}
}

View file

@ -0,0 +1,126 @@
package game.gui.element;
import game.gui.Font;
import game.renderer.Drawing;
import game.util.Displayable;
import game.util.ExtMath;
import game.util.Formatter;
import game.util.Util;
import game.window.Button;
public class Dropdown<T> extends Element {
public class Handle extends Element {
private Handle(boolean up) {
super(Dropdown.this.pos_x, Dropdown.this.pos_y + (up ? -(Font.YGLYPH * Dropdown.this.values.length + 2) : Dropdown.this.size_y), Dropdown.this.size_x, Font.YGLYPH * Dropdown.this.values.length + 2, null);
StringBuilder sb = new StringBuilder();
for(T value : Dropdown.this.values) {
if(sb.length() > 0)
sb.append('\n');
sb.append(value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString());
}
this.setText(sb.toString());
this.visible = this.r_dirty = false;
}
public void updateText() {
this.r_dirty = true;
}
protected boolean isTextCenteredX() {
return false;
}
protected boolean isTextCenteredY() {
return false;
}
public boolean canClick() {
return false;
}
public void deselect() {
this.visible = false;
this.r_dirty = true;
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
Dropdown drop = Dropdown.this;
int prev = drop.value;
drop.value = (y - (this.pos_y + this.margin_y1)) * drop.values.length / (this.size_y - (this.margin_y1 + this.margin_y2));
drop.value = ExtMath.clampi(drop.value, 0, drop.values.length - 1);
if(drop.value != prev) {
drop.func.use(drop, drop.getValue());
drop.formatText();
}
this.visible = false;
this.r_dirty = true;
}
public void drawHover() {
int m = ((this.gm.mouse_y - (this.pos_y + this.margin_y1)) * Dropdown.this.values.length / (this.size_y - (this.margin_y1 + this.margin_y2)));
// if((sys.mouse_y - this.pos_y) < (this.size_y - this.margin_y2))
Drawing.drawRectColor(this.pos_x + this.margin_x1,
this.pos_y + this.margin_y1 + ExtMath.clampi(m, 0, Dropdown.this.values.length - 1) * ((this.size_y - (this.margin_y1 + this.margin_y2)) / Dropdown.this.values.length),
this.size_x - (this.margin_x1 + this.margin_x2),
(this.size_y - (this.margin_y1 + this.margin_y2)) / Dropdown.this.values.length, this.gm.style.hover);
}
}
public static interface Callback<T> {
void use(Dropdown<T> elem, T value);
}
private final Callback<T> func;
private final T[] values;
private final Handle handle;
private final int def;
private int value;
public Dropdown(int x, int y, int w, int h, boolean up, T[] values, T def, T init, Callback<T> callback, Formatter<Dropdown<T>> formatter) {
super(x, y, w, h, formatter);
this.func = callback;
this.values = values;
this.def = Util.indexOfChecked(this.values, def);
this.value = Util.indexOfChecked(this.values, init);
this.handle = new Handle(up);
this.formatText();
}
public Dropdown(int x, int y, int w, int h, boolean up, T[] values, T def, T init, Callback<T> callback, final String text) {
this(x, y, w, h, up, values, def, init, callback, new Formatter<Dropdown<T>>() {
public String use(Dropdown<T> elem) {
T value = elem.getValue();
return String.format("%s: %s", text, value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString());
}
});
}
public T getValue() {
return this.values[this.value];
}
public void setValue(T value) {
this.value = Util.indexOfChecked(this.values, value);
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
int prev = this.value;
if(ctrl || (btn == Button.MOUSE_MIDDLE)) {
if((this.value = this.def) != prev) {
this.func.use(this, this.getValue());
this.formatText();
}
}
else if(this.gui != null) {
Handle drop = this.handle;
drop.visible = true;
drop.r_dirty = true;
this.gui.selected = drop;
}
}
public Handle getHandle() {
return this.handle;
}
}

View file

@ -0,0 +1,278 @@
package game.gui.element;
import game.Game;
import game.gui.Font;
import game.gui.Gui;
import game.gui.element.Dropdown.Handle;
import game.renderer.Drawing;
import game.renderer.Drawing.Vec2i;
import game.util.Formatter;
import game.window.Button;
import game.window.Keysym;
import game.window.WCF;
public abstract class Element {
protected final Game gm = Game.getGame();
protected Gui gui;
protected Formatter format;
protected String text = "";
// String format_text;
// Object aux_data;
// String[] format_data;
// CVar cv_data;
protected int pos_x;
protected int pos_y;
protected int size_x;
protected int size_y;
protected int text_x = 0;
protected int text_y = 0;
protected int tsize_x = 0;
protected int tsize_y = 0;
protected final int margin_x1 = this.getMargin();
protected final int margin_y1 = this.getMargin();
protected final int margin_x2 = this.getMargin();
protected final int margin_y2 = this.getMargin();
public boolean visible = true;
public boolean enabled = true;
public boolean r_dirty = true;
public Element(int x, int y, int w, int h, Formatter formatter) {
this.pos_x = x;
this.pos_y = y;
this.size_x = w;
this.size_y = h;
this.format = formatter;
// gui_update_style(this, 1);
// if(type != ElemType.SLIDER) {
// this.margin_x1 = this.margin_x2 = (type == ElemType.FIELD || type == ElemType.DROPDOWN_HANDLE) ? this.border : 0;
// this.margin_y1 = this.margin_y2 = (type == ElemType.FIELD || type == ElemType.DROPDOWN_HANDLE) ? this.border : 0;
// }
}
public void setGui(Gui gui) {
if(gui == null)
throw new NullPointerException("gui");
if(this.gui != null)
throw new IllegalStateException("GUI bereits gesetzt");
this.gui = gui;
}
protected int getMargin() {
return 1;
}
public final int getX() {
return this.pos_x;
}
public final int getY() {
return this.pos_y;
}
public final int getWidth() {
return this.size_x;
}
public final int getHeight() {
return this.size_y;
}
protected boolean isTextCenteredX() {
return true;
}
protected boolean isTextCenteredY() {
return true;
}
protected boolean hasLinebreak() {
return false;
}
public boolean canHover() {
return true;
}
public boolean canClick() {
return this.canHover();
}
public boolean inside(int x, int y) {
return (x >= this.pos_x) && (x < (this.pos_x + this.size_x)) && (y >= this.pos_y) && (y < (this.pos_y + this.size_y));
}
public boolean intersects(Element elem) {
return (elem.pos_y + elem.size_y) > this.pos_y && elem.pos_y < (this.pos_y + this.size_y) && (elem.pos_x + elem.size_x) > this.pos_x && elem.pos_x < (this.pos_x + this.size_x);
}
public void updateText() {
Vec2i size = Drawing.txt_size(this.pos_x + this.margin_x1, this.pos_y + this.margin_y1,
this.pos_x + this.margin_x1, this.pos_y + this.margin_y1,
this.hasLinebreak() ? (this.pos_x + (this.size_x - (this.margin_x1 + this.margin_x2))) : Integer.MAX_VALUE, Integer.MAX_VALUE, this.text);
this.tsize_x = size.xpos;
this.tsize_y = size.ypos;
// if(this.type != ElemType.FIELD) {
if(this.isTextCenteredX())
this.text_x = (this.size_x - this.tsize_x) / 2;
if(this.isTextCenteredY())
this.text_y = (this.size_y - this.tsize_y - Font.YGLYPH) / 2;
// }
// logd("DBG", "s = %d %d; o = %d %d", this.tsize_x, this.tsize_y, this.text_x, this.text_y);
// gui_update_dropdown();
if(this.gui != null && this.gui.selected instanceof Handle && this.gui.selected.visible)
this.gui.selected.r_dirty = true;
this.r_dirty = true;
}
public void setText(String str) {
this.text = str;
this.updateText();
}
public String getText() {
return this.text;
}
protected void formatText() {
if(this.format != null) {
this.text = this.format.use(this);
this.updateText();
}
}
public void scroll(int scr_x, int scr_y, int x, int y, boolean ctrl, boolean shift) {
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
}
public void mouserel() {
}
public void drag(int x, int y) {
}
public void character(char code) {
}
public void key(Keysym key, boolean ctrl, boolean shift) {
}
public void select() {
}
public void deselect() {
}
public void update() {
}
public void setSelected() {
if(this.gui != null)
this.gui.select(this);
}
public void setDeselected() {
if(this.gui != null && this.gui.selected == this)
this.gui.deselect();
}
public void shift(int shift_x, int shift_y) {
this.pos_x += shift_x;
this.pos_y += shift_y;
}
public void reformat() {
if(this.format != null) {
// boolean flag = this.r_dirty;
this.formatText();
// if(!flag && !this.visible)
// this.r_dirty = false;
}
}
protected void drawBackground() {
Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_top, this.gm.style.fill_btm, this.gm.style.brdr_top, this.gm.style.brdr_btm);
// Drawing.drawGradient2GradBorder(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_top, this.gm.style.fill_btm, 0xff000000,
// this.gm.style.brdr_top, Drawing.mixColor(this.gm.style.brdr_top, this.gm.style.brdr_btm), Drawing.mixColor(this.gm.style.brdr_top, this.gm.style.brdr_btm), this.gm.style.brdr_btm);
}
protected void drawForeground(int x1, int y1, int x2, int y2) {
Drawing.drawText(this.text, x1 + this.text_x, y1 + this.text_y, this.gm.style.text_base);
}
public void draw() {
// if(this.r_dirty) {
if(this.visible) {
// WCF.glScissor(this.pos_x, (this.gm.fb_y - (this.pos_y + this.size_y)) < 0 ? 0 : (this.gm.fb_y - (this.pos_y + this.size_y)), this.size_x, this.size_y);
// WCF.glEnable(WCF.GL_SCISSOR_TEST);
// WCF.glClear(WCF.GL_COLOR_BUFFER_BIT);
// WCF.glDisable(WCF.GL_SCISSOR_TEST);
this.drawBackground();
// if(this.selected == this && this.type == ElemType.DROPDOWN_HANDLE)
// continue;
int x1 = this.pos_x + this.margin_x1;
int y1 = this.pos_y + this.margin_y1;
int x2 = this.size_x - (this.margin_x1 + this.margin_x2);
int y2 = this.size_y - (this.margin_y1 + this.margin_y2);
// if(elem.type == ElemType.FIELD) {
WCF.glScissor(x1 < 0 ? 0 : x1, (this.gm.fb_y - (y1 + y2)) < 0 ? 0 : (this.gm.fb_y - (y1 + y2)), x2 < 0 ? 0 : x2, y2 < 0 ? 0 : y2);
WCF.glEnable(WCF.GL_SCISSOR_TEST);
// }
// if(this.type == ElemType.CUSTOM)
// this.func(this, 1);
// else
this.drawForeground(x1, y1, x2, y2);
// logd("DBG", "%d @ %d %d -> %d %d", elem.id, x1, y1, elem.pos_x + x2, elem.pos_y + y2);
// if(elem.type == ElemType.FIELD) {
WCF.glDisable(WCF.GL_SCISSOR_TEST);
// glScissor(0, 0, sys.fb_x, sys.fb_y);
// }
}
// else {
// WCF.glScissor(this.pos_x, (this.gm.fb_y - (this.pos_y + this.size_y)) < 0 ? 0 : (this.gm.fb_y - (this.pos_y + this.size_y)), this.size_x, this.size_y);
// // logd("DBG", "%d @ %d %d -> %d %d", elem.id, elem.pos_x, elem.pos_y, elem.pos_x + elem.size_x, elem.pos_y + elem.size_y);
// WCF.glEnable(WCF.GL_SCISSOR_TEST);
//// if(this.type == ElemType.CUSTOM)
//// this.func(this, 0);
//// else
// WCF.glClear(WCF.GL_COLOR_BUFFER_BIT);
// WCF.glDisable(WCF.GL_SCISSOR_TEST);
// this.r_dirty = false;
// }
// this.r_dirty = this.visible;
// logd("DBG", "@ r");
// if(this.visible) {
// }
// else {
// }
// }
}
public void drawOverlay() {
}
public void drawHover() {
Drawing.drawRectColor(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.hover);
}
public void drawPress() {
Drawing.drawRectColor(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.press);
}
}

View file

@ -0,0 +1,46 @@
package game.gui.element;
public class Fill extends Element {
private final boolean left;
private final boolean top;
public Fill(int x, int y, int w, int h, String text, boolean top, boolean left) {
super(x, y, w, h, null);
this.top = top;
this.left = left;
this.setText(text);
}
public Fill(int x, int y, int w, int h, String text, boolean left) {
this(x, y, w, h, text, false, left);
}
public Fill(int x, int y, int w, int h, String text) {
this(x, y, w, h, text, false, false);
}
public Fill(int x, int y, int w, int h, boolean top, boolean left) {
this(x, y, w, h, "", top, left);
}
public Fill(int x, int y, int w, int h, boolean left) {
this(x, y, w, h, "", false, left);
}
public Fill(int x, int y, int w, int h) {
this(x, y, w, h, "", false, false);
}
public boolean canHover() {
return false;
}
protected boolean isTextCenteredX() {
return !this.left;
}
protected boolean isTextCenteredY() {
return !this.top;
}
}

View file

@ -0,0 +1,407 @@
package game.gui.element;
import java.util.List;
import game.collect.Lists;
import game.gui.Gui;
import game.renderer.DefaultVertexFormats;
import game.renderer.Drawing;
import game.renderer.GlState;
import game.renderer.Tessellator;
import game.util.ExtMath;
import game.window.Button;
import game.renderer.RenderBuffer;
public abstract class GuiList<T extends ListEntry> extends Gui
{
protected final List<T> elements = Lists.newArrayList();
protected int width;
protected int height;
protected int top;
protected int bottom;
protected int right;
protected int left;
protected int mouseX;
protected int mouseY;
protected int initialClickY = -2;
protected float scrollMultiplier;
protected float amountScrolled;
protected int selectedElement = -1;
protected long lastClicked;
public abstract int getListWidth(); // 220
public abstract int getSlotHeight();
protected int getScrollBarX()
{
return 0;
}
public void setDimensions(int widthIn, int heightIn, int topIn, int bottomIn)
{
this.width = widthIn;
this.height = heightIn;
this.top = topIn;
this.bottom = bottomIn;
this.left = 0;
this.right = widthIn;
}
public void init(int width, int height) {
this.selectedElement = -1;
}
public void setSlotXBoundsFromLeft(int leftIn)
{
this.left = leftIn;
this.right = leftIn + this.width;
}
public final T getListEntry(int index) {
return this.elements.get(index);
}
public final T getSelected() {
return this.selectedElement < 0 ? null : this.elements.get(this.selectedElement);
}
public final int getSize() {
return this.elements.size();
}
protected int getContentHeight()
{
return this.getSize() * this.getSlotHeight();
}
public int getSlotIndexFromScreenCoords(int x, int y)
{
int i = this.left + this.width / 2 - this.getListWidth() / 2;
int j = this.left + this.width / 2 + this.getListWidth() / 2;
int k = y - this.top + (int)this.amountScrolled - 4;
int l = k / this.getSlotHeight();
return this.isInList(x, y) && x >= i && x <= j && l >= 0 && k >= 0 && l < this.getSize() ? l : -1;
}
protected boolean isInList(int x, int y)
{
return x >= this.getScrollBarX() + 6; // x < this.getScrollBarX();
}
protected final boolean isSelected(int slotIndex)
{
return slotIndex == this.selectedElement;
}
protected void bindAmountScrolled()
{
this.amountScrolled = ExtMath.clampf(this.amountScrolled, 0.0F, (float)this.getMaxScroll());
}
public int getMaxScroll()
{
return Math.max(0, this.getContentHeight() - (this.bottom - this.top - 4));
}
public int getAmountScrolled()
{
return (int)this.amountScrolled;
}
public boolean isMouseYWithinSlotBounds(int p_148141_1_)
{
return p_148141_1_ >= this.top && p_148141_1_ <= this.bottom && this.mouseX >= this.left && this.mouseX <= this.right;
}
public void scrollBy(int amount)
{
this.amountScrolled += (float)amount;
this.bindAmountScrolled();
this.initialClickY = -2;
}
// public void actionPerformed(Button button)
// {
// if (button.enabled)
// {
// if (button.id == this.scrollUpButtonID)
// {
// this.amountScrolled -= (float)(this.slotHeight * 2 / 3);
// this.initialClickY = -2;
// this.bindAmountScrolled();
// }
// else if (button.id == this.scrollDownButtonID)
// {
// this.amountScrolled += (float)(this.slotHeight * 2 / 3);
// this.initialClickY = -2;
// this.bindAmountScrolled();
// }
// }
// }
public void draw()
{
int mouseXIn = this.gm.mouse_x;
int mouseYIn = this.gm.mouse_y;
this.mouseX = mouseXIn;
this.mouseY = mouseYIn;
this.drawBackground();
int i = this.getScrollBarX();
int j = i + 6;
this.bindAmountScrolled();
GlState.disableLighting();
GlState.disableFog();
GlState.enableTexture2D();
RenderBuffer worldrenderer = Tessellator.getBuffer();
this.gm.getTextureManager().bindTexture(Gui.DIRT_BACKGROUND);
GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
float f = 32.0F;
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
worldrenderer.pos((double)this.left, (double)this.bottom, 0.0D).tex((double)((float)this.left / f), (double)((float)(this.bottom + (int)this.amountScrolled) / f)).color(32, 32, 32, 255).endVertex();
worldrenderer.pos((double)this.right, (double)this.bottom, 0.0D).tex((double)((float)this.right / f), (double)((float)(this.bottom + (int)this.amountScrolled) / f)).color(32, 32, 32, 255).endVertex();
worldrenderer.pos((double)this.right, (double)this.top, 0.0D).tex((double)((float)this.right / f), (double)((float)(this.top + (int)this.amountScrolled) / f)).color(32, 32, 32, 255).endVertex();
worldrenderer.pos((double)this.left, (double)this.top, 0.0D).tex((double)((float)this.left / f), (double)((float)(this.top + (int)this.amountScrolled) / f)).color(32, 32, 32, 255).endVertex();
Tessellator.draw();
int x = this.left + this.width / 2 - this.getListWidth() / 2 + 2;
int y = this.top + 4 - (int)this.amountScrolled;
this.drawSelectionBox(x, y, mouseXIn, mouseYIn);
GlState.disableDepth();
this.overlayBackground(0, this.top, 255, 255);
this.overlayBackground(this.bottom, this.height, 255, 255);
GlState.enableBlend();
GlState.tryBlendFuncSeparate(770, 771, 0, 1);
GlState.disableAlpha();
GlState.shadeModel(7425);
GlState.disableTexture2D();
int i1 = 4;
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
worldrenderer.pos((double)this.left, (double)(this.top + i1), 0.0D).tex(0.0D, 1.0D).color(0, 0, 0, 0).endVertex();
worldrenderer.pos((double)this.right, (double)(this.top + i1), 0.0D).tex(1.0D, 1.0D).color(0, 0, 0, 0).endVertex();
worldrenderer.pos((double)this.right, (double)this.top, 0.0D).tex(1.0D, 0.0D).color(0, 0, 0, 255).endVertex();
worldrenderer.pos((double)this.left, (double)this.top, 0.0D).tex(0.0D, 0.0D).color(0, 0, 0, 255).endVertex();
Tessellator.draw();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
worldrenderer.pos((double)this.left, (double)this.bottom, 0.0D).tex(0.0D, 1.0D).color(0, 0, 0, 255).endVertex();
worldrenderer.pos((double)this.right, (double)this.bottom, 0.0D).tex(1.0D, 1.0D).color(0, 0, 0, 255).endVertex();
worldrenderer.pos((double)this.right, (double)(this.bottom - i1), 0.0D).tex(1.0D, 0.0D).color(0, 0, 0, 0).endVertex();
worldrenderer.pos((double)this.left, (double)(this.bottom - i1), 0.0D).tex(0.0D, 0.0D).color(0, 0, 0, 0).endVertex();
Tessellator.draw();
int j1 = this.getMaxScroll();
if (j1 > 0)
{
int k1 = (this.bottom - this.top) * (this.bottom - this.top) / this.getContentHeight();
k1 = ExtMath.clampi(k1, 32, this.bottom - this.top - 8);
int l1 = (int)this.amountScrolled * (this.bottom - this.top - k1) / j1 + this.top;
if (l1 < this.top)
{
l1 = this.top;
}
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
worldrenderer.pos((double)i, (double)this.bottom, 0.0D).tex(0.0D, 1.0D).color(0, 0, 0, 255).endVertex();
worldrenderer.pos((double)j, (double)this.bottom, 0.0D).tex(1.0D, 1.0D).color(0, 0, 0, 255).endVertex();
worldrenderer.pos((double)j, (double)this.top, 0.0D).tex(1.0D, 0.0D).color(0, 0, 0, 255).endVertex();
worldrenderer.pos((double)i, (double)this.top, 0.0D).tex(0.0D, 0.0D).color(0, 0, 0, 255).endVertex();
Tessellator.draw();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
worldrenderer.pos((double)i, (double)(l1 + k1), 0.0D).tex(0.0D, 1.0D).color(128, 128, 128, 255).endVertex();
worldrenderer.pos((double)j, (double)(l1 + k1), 0.0D).tex(1.0D, 1.0D).color(128, 128, 128, 255).endVertex();
worldrenderer.pos((double)j, (double)l1, 0.0D).tex(1.0D, 0.0D).color(128, 128, 128, 255).endVertex();
worldrenderer.pos((double)i, (double)l1, 0.0D).tex(0.0D, 0.0D).color(128, 128, 128, 255).endVertex();
Tessellator.draw();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
worldrenderer.pos((double)i, (double)(l1 + k1 - 1), 0.0D).tex(0.0D, 1.0D).color(192, 192, 192, 255).endVertex();
worldrenderer.pos((double)(j - 1), (double)(l1 + k1 - 1), 0.0D).tex(1.0D, 1.0D).color(192, 192, 192, 255).endVertex();
worldrenderer.pos((double)(j - 1), (double)l1, 0.0D).tex(1.0D, 0.0D).color(192, 192, 192, 255).endVertex();
worldrenderer.pos((double)i, (double)l1, 0.0D).tex(0.0D, 0.0D).color(192, 192, 192, 255).endVertex();
Tessellator.draw();
}
GlState.enableTexture2D();
GlState.shadeModel(7424);
GlState.enableAlpha();
GlState.disableBlend();
super.draw();
}
public void handleMouseInput()
{
if (this.isMouseYWithinSlotBounds(this.mouseY))
{
// if (Button.MOUSE_LEFT.isDown() && this.getEnabled())
// {
if (this.initialClickY == -1)
{
boolean flag1 = true;
if (this.mouseY >= this.top && this.mouseY <= this.bottom)
{
int j2 = (this.width - this.getListWidth()) / 2;
int k2 = (this.width + this.getListWidth()) / 2;
int l2 = this.mouseY - this.top + (int)this.amountScrolled - 4;
int i1 = l2 / this.getSlotHeight();
if (i1 < this.getSize() && this.mouseX >= j2 && this.mouseX <= k2 && i1 >= 0 && l2 >= 0)
{
}
else if (this.mouseX >= j2 && this.mouseX <= k2 && l2 < 0)
{
flag1 = false;
}
int i3 = this.getScrollBarX();
int j1 = i3 + 6;
if (this.mouseX >= i3 && this.mouseX <= j1)
{
this.scrollMultiplier = -1.0F;
int k1 = this.getMaxScroll();
if (k1 < 1)
{
k1 = 1;
}
int l1 = (int)((float)((this.bottom - this.top) * (this.bottom - this.top)) / (float)this.getContentHeight());
l1 = ExtMath.clampi(l1, 32, this.bottom - this.top - 8);
this.scrollMultiplier /= (float)(this.bottom - this.top - l1) / (float)k1;
}
else
{
this.scrollMultiplier = 1.0F;
}
if (flag1)
{
this.initialClickY = this.mouseY;
}
else
{
this.initialClickY = -2;
}
}
else
{
this.initialClickY = -2;
}
}
else if (this.initialClickY >= 0)
{
this.amountScrolled -= (float)(this.mouseY - this.initialClickY) * this.scrollMultiplier;
this.initialClickY = this.mouseY;
}
// }
// else
// {
// this.initialClickY = -1;
// }
}
}
protected void drawSelectionBox(int x, int y, int mouseXIn, int mouseYIn)
{
int size = this.getSize();
RenderBuffer rb = Tessellator.getBuffer();
for (int z = 0; z < size; z++)
{
int y1 = y + z * this.getSlotHeight();
int h = this.getSlotHeight() - 4;
if (this.isSelected(z))
{
int x1 = this.left + (this.width / 2 - this.getListWidth() / 2);
int x2 = this.left + this.width / 2 + this.getListWidth() / 2;
GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
GlState.disableTexture2D();
rb.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
rb.pos((double)x1, (double)(y1 + h + 2), 0.0D).tex(0.0D, 1.0D).color(128, 128, 128, 255).endVertex();
rb.pos((double)x2, (double)(y1 + h + 2), 0.0D).tex(1.0D, 1.0D).color(128, 128, 128, 255).endVertex();
rb.pos((double)x2, (double)(y1 - 2), 0.0D).tex(1.0D, 0.0D).color(128, 128, 128, 255).endVertex();
rb.pos((double)x1, (double)(y1 - 2), 0.0D).tex(0.0D, 0.0D).color(128, 128, 128, 255).endVertex();
rb.pos((double)(x1 + 1), (double)(y1 + h + 1), 0.0D).tex(0.0D, 1.0D).color(0, 0, 0, 255).endVertex();
rb.pos((double)(x2 - 1), (double)(y1 + h + 1), 0.0D).tex(1.0D, 1.0D).color(0, 0, 0, 255).endVertex();
rb.pos((double)(x2 - 1), (double)(y1 - 1), 0.0D).tex(1.0D, 0.0D).color(0, 0, 0, 255).endVertex();
rb.pos((double)(x1 + 1), (double)(y1 - 1), 0.0D).tex(0.0D, 0.0D).color(0, 0, 0, 255).endVertex();
Tessellator.draw();
GlState.enableTexture2D();
}
boolean hover = this.getSlotIndexFromScreenCoords(mouseXIn, mouseYIn) == z;
this.getListEntry(z).draw(x, y1, mouseXIn - x, mouseYIn - y1, hover);
if(hover)
Drawing.drawRectColor(x - 2, y1 - 2, this.getListWidth(), this.getSlotHeight(), this.gm.style.hover);
}
}
protected void overlayBackground(int startY, int endY, int startAlpha, int endAlpha)
{
RenderBuffer rb = Tessellator.getBuffer();
this.gm.getTextureManager().bindTexture(Gui.DIRT_BACKGROUND);
GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
float f = 32.0F;
rb.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
rb.pos((double)this.left, (double)endY, 0.0D).tex(0.0D, (double)((float)endY / 32.0F)).color(64, 64, 64, endAlpha).endVertex();
rb.pos((double)(this.left + this.width), (double)endY, 0.0D).tex((double)((float)this.width / 32.0F), (double)((float)endY / 32.0F)).color(64, 64, 64, endAlpha).endVertex();
rb.pos((double)(this.left + this.width), (double)startY, 0.0D).tex((double)((float)this.width / 32.0F), (double)((float)startY / 32.0F)).color(64, 64, 64, startAlpha).endVertex();
rb.pos((double)this.left, (double)startY, 0.0D).tex(0.0D, (double)((float)startY / 32.0F)).color(64, 64, 64, startAlpha).endVertex();
Tessellator.draw();
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift)
{
super.mouse(btn, x, y, ctrl, shift);
if (this.isMouseYWithinSlotBounds(y))
{
int i = this.getSlotIndexFromScreenCoords(x, y);
if (i >= 0)
{
int j = this.left + this.width / 2 - this.getListWidth() / 2 + 2;
int k = this.top + 4 - this.getAmountScrolled() + i * this.getSlotHeight();
int l = x - j;
int i1 = y - k;
boolean flag = i == this.selectedElement && System.currentTimeMillis() - this.lastClicked < (long)this.gm.dclickDelay;
this.selectedElement = i;
this.lastClicked = System.currentTimeMillis();
this.getListEntry(i).select(flag, l, i1);
}
}
if(btn == Button.MOUSE_LEFT && this.clicked(x, y) == null)
this.handleMouseInput();
}
public void mouserel(Button btn, int x, int y) {
super.mouserel(btn, x, y);
if(btn == Button.MOUSE_LEFT)
this.initialClickY = -1;
}
public void scroll(int scr_x, int scr_y, int x, int y, boolean ctrl, boolean shift) {
super.scroll(scr_x, scr_y, x, y, ctrl, shift);
if(scr_y != 0 && this.clicked(x, y) == null)
this.amountScrolled -= (float)(ExtMath.clampi(scr_y, -1, 1) * this.getSlotHeight() / 2);
}
public void drag(int x, int y) {
super.drag(x, y);
if(this.selected == null && Button.MOUSE_LEFT.isDown())
this.handleMouseInput();
}
}

View file

@ -0,0 +1,17 @@
package game.gui.element;
import game.renderer.Drawing;
public class InventoryButton extends Element {
public InventoryButton(int x, int y, int w, int h) {
super(x, y, w, h, null);
}
protected void drawBackground() {
// Drawing.drawRect2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, 0xff6f6f6f, 0xffafafaf);
Drawing.drawRect2GradBorder(this.pos_x, this.pos_y, this.size_x, this.size_y, 0xff6f6f6f, 0xff000000, 0xffafafaf, 0xff7f7f7f, 0xff7f7f7f, 0xff4f4f4f);
}
protected void drawForeground(int x1, int y1, int x2, int y2) {
}
}

View file

@ -0,0 +1,28 @@
package game.gui.element;
import game.renderer.Drawing;
public class Label extends Fill {
public Label(int x, int y, int w, int h, String text, boolean top, boolean left) {
super(x, y, w, h, text, top, left);
}
public Label(int x, int y, int w, int h, String text, boolean left) {
super(x, y, w, h, text, left);
}
public Label(int x, int y, int w, int h, String text) {
super(x, y, w, h, text);
}
protected void drawBackground() {
}
protected void drawForeground(int x1, int y1, int x2, int y2) {
Drawing.drawText(this.text, x1 + this.text_x, y1 + this.text_y, this.gm.style.text_label);
}
protected int getMargin() {
return 0;
}
}

View file

@ -0,0 +1,7 @@
package game.gui.element;
public interface ListEntry
{
void draw(int x, int y, int mouseX, int mouseY, boolean hovered);
void select(boolean dclick, int mx, int my);
}

View file

@ -0,0 +1,18 @@
package game.gui.element;
import game.gui.Gui;
import game.renderer.Drawing;
public class SelectedButton extends ActButton {
public SelectedButton(int x, int y, int w, int h, Callback callback, String text) {
super(x, y, w, h, callback, text);
}
public SelectedButton(int x, int y, int w, int h, Gui gui, String text) {
super(x, y, w, h, gui, text);
}
protected void drawBackground() {
Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, this.gm.style.brdr_top, this.gm.style.brdr_btm);
}
}

View file

@ -0,0 +1,165 @@
package game.gui.element;
import game.renderer.Drawing;
import game.util.ExtMath;
import game.util.Formatter;
import game.window.Button;
public class Slider extends Element {
public static interface Callback {
void use(Slider elem, int value);
}
public static interface FloatCallback {
void use(Slider elem, float value);
}
private static final int SLIDER_WIDTH = 10;
private final Callback func;
private final int def;
private final int min;
private final int max;
private final int precision;
private final int handle;
private int value;
private int position;
public Slider(int x, int y, int w, int h, int prec, int min, int max, int def, int init, Callback callback, Formatter<Slider> formatter) {
super(x, y, w, h, formatter);
this.handle = ((this.size_y * SLIDER_WIDTH) / 24) & ~1;
this.func = callback;
this.precision = prec;
this.min = min;
this.max = max;
this.def = def;
this.value = init;
this.position = gui_slider_pixel();
this.formatText();
}
public Slider(int x, int y, int w, int h, int prec, int min, int max, int def, int init, Callback callback, final String text) {
this(x, y, w, h, prec, min, max, def, init, callback, new Formatter<Slider>() {
public String use(Slider elem) {
return String.format("%s: %d", text, elem.value);
}
});
}
public Slider(int x, int y, int w, int h, int prec, int min, int max, int def, int init, Callback callback, final String text, final String unit) {
this(x, y, w, h, prec, min, max, def, init, callback, new Formatter<Slider>() {
private final String format = unit.isEmpty() ? "%s: %d" : "%s: %d %s";
public String use(Slider elem) {
return String.format(this.format, text, elem.value, unit);
}
});
}
public Slider(int x, int y, int w, int h, int prec, float min, float max, float def, float init, final FloatCallback callback, Formatter<Slider> formatter) {
this(x, y, w, h, prec, (int)(min * 1000.0f), (int)(max * 1000.0f), (int)(def * 1000.0f), (int)(init * 1000.0f), new Callback() {
public void use(Slider elem, int value) {
callback.use(elem, (float)value / 1000.0f);
}
}, formatter);
}
public Slider(int x, int y, int w, int h, final int prec, float min, float max, float def, float init, FloatCallback callback, final String text, final String unit) {
this(x, y, w, h, prec < 0 ? 0 : prec, min, max, def, init, callback, new Formatter<Slider>() {
private final String format = "%s: " + (prec <= 0 ? "%d" : ("%." + prec + "f")) + (unit.isEmpty() ? "" : " %s");
public String use(Slider elem) {
return prec <= 0 ? String.format(this.format, text, prec < 0 ? (int)((float)elem.value / 10.0f) : (elem.value / 1000), unit)
: String.format(this.format, text, (float)elem.value / 1000.0f, unit);
}
});
}
public void scroll(int scr_x, int scr_y, int x, int y, boolean ctrl, boolean shift) {
if(scr_y != 0) {
int prev = this.value;
this.value += (scr_y < 0 ? -1 : 1) * (ctrl ? (this.precision != 0 ? 1 : 10) : ((this.precision >= 2) ? 10 : (this.precision != 0 ? 100 : 1))) * (shift ? (this.precision != 0 ? 50 : 5) : 1);
this.value = ExtMath.clampi(this.value, this.min, this.max);
this.position = gui_slider_pixel();
if(this.value != prev) {
this.func.use(this, this.value);
this.formatText();
}
}
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
int prev = this.value;
if(ctrl || (btn == Button.MOUSE_MIDDLE)) {
this.value = this.def;
this.position = gui_slider_pixel();
if(this.value != prev) {
this.func.use(this, this.value);
this.formatText();
}
}
else {
this.position = x - this.pos_x;
this.value = gui_slider_value();
this.position = gui_slider_pixel();
if(this.value != prev) {
this.func.use(this, this.value);
this.formatText();
}
}
}
public void drag(int x, int y) {
x = (x < this.pos_x) ? this.pos_x : ((x >= (this.pos_x + this.size_x)) ? (this.pos_x - 1 + this.size_x) : x);
int prev = this.value;
this.position = x - this.pos_x;
this.value = gui_slider_value();
this.value = ExtMath.clampi(this.value, this.min, this.max);
this.position = gui_slider_pixel();
if(this.value != prev) {
this.func.use(this, this.value);
this.formatText();
}
}
public void reformat() {
this.position = gui_slider_pixel();
if(this.visible)
this.r_dirty = true;
super.reformat();
}
private int gui_slider_value() {
int r = this.min + (int)(((float)(int)(this.position - (this.handle / 2))) * ((float)(int)(this.max - this.min)) / ((float)(int)(this.size_x + 1 - this.handle)));
return ExtMath.clampi(r, this.min, this.max);
}
private int gui_slider_pixel() {
int r = ((int)(float)(((float)(int)(this.value - this.min)) * ((float)(int)(this.size_x + 1 - this.handle)) / ((float)(int)(this.max - this.min)))) + (this.handle / 2);
return ExtMath.clampi(r, this.handle / 2, this.size_x - (this.handle / 2));
}
public void setValue(int value) {
this.value = ExtMath.clampi(value, this.min, this.max);
this.position = gui_slider_pixel();
this.formatText();
}
public void setFloatValue(float value) {
this.setValue((int)(value * 1000.0f));
}
public int getValue() {
return this.value;
}
public float getFloatValue() {
return (float)this.value / 1000.0f;
}
protected void drawBackground() {
Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, this.gm.style.brdr_top, this.gm.style.brdr_btm);
Drawing.drawGradient2Border(this.pos_x + this.position - (this.handle / 2), this.pos_y, this.handle, this.size_y, this.gm.style.fill_top, this.gm.style.fill_btm, this.gm.style.brdr_btm, this.gm.style.brdr_top);
}
}

View file

@ -0,0 +1,54 @@
package game.gui.element;
import game.util.Displayable;
import game.util.Formatter;
import game.util.Util;
import game.window.Button;
public class Switch<T> extends Element {
public static interface Callback<T> {
void use(Switch<T> elem, T value);
}
private final Callback<T> func;
private final T[] values;
private final int def;
private int value;
public Switch(int x, int y, int w, int h, T[] values, T def, T init, Callback<T> callback, Formatter<Switch<T>> formatter) {
super(x, y, w, h, formatter);
this.func = callback;
this.values = values;
this.def = Util.indexOfChecked(this.values, def);
this.value = Util.indexOfChecked(this.values, init);
this.formatText();
}
public Switch(int x, int y, int w, int h, T[] values, T def, T init, Callback<T> callback, final String text) {
this(x, y, w, h, values, def, init, callback, new Formatter<Switch<T>>() {
public String use(Switch<T> elem) {
T value = elem.getValue();
return String.format("%s: %s", text, value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString());
}
});
}
public T getValue() {
return this.values[this.value];
}
public void setValue(T value) {
this.value = Util.indexOfChecked(this.values, value);
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
int prev = this.value;
this.value = (ctrl || (btn == Button.MOUSE_MIDDLE)) ? this.def : (this.value + ((shift || (btn == Button.MOUSE_RIGHT)) ? -1 : 1));
this.value = (this.value == this.values.length) ? 0 : ((this.value == -1) ? (this.values.length - 1) : this.value);
if(this.value != prev) {
this.func.use(this, this.getValue());
this.formatText();
}
}
}

View file

@ -0,0 +1,488 @@
package game.gui.element;
import game.gui.Font;
import game.renderer.Drawing;
import game.renderer.Drawing.Offset;
import game.renderer.Drawing.Vec2i;
import game.util.CharValidator;
import game.util.ExtMath;
import game.util.Timing;
import game.util.Util;
import game.window.Button;
import game.window.Keysym;
import game.window.WCF;
public class Textbox extends Element {
public static enum Action {
FOCUS, UNFOCUS, PREVIOUS, NEXT, FUNCTION, SEND, LINE, FORWARD, BACKWARD;
}
public static interface Callback {
void use(Textbox elem, Action value);
}
private final Callback func;
private final CharValidator validator;
private final int capacity;
private final boolean xbreak;
private final boolean editable;
private long tmr_scroll;
private long tmr_leftmb;
private int scrollx;
private int scrolly;
private int sel_start = -1;
private int sel_end = -1;
private int sel_drag = -1;
private int cursorX = 0;
private int cursorY = 0;
private Textbox(int x, int y, int w, int h, int cap, boolean line, boolean editable, Callback callback, CharValidator validator, String text) {
super(x, y, w, h, null);
this.func = callback;
this.validator = validator;
this.capacity = cap;
this.xbreak = !line;
this.editable = editable;
if(line)
this.text_y = (this.size_y - (this.margin_y1 + this.margin_y2 + Font.YGLYPH)) / 2;
this.setText(text);
}
public Textbox(int x, int y, int w, int h, int cap, boolean line, Callback callback, String text) {
this(x, y, w, h, cap, line, true, callback, null, text);
}
public Textbox(int x, int y, int w, int h, int cap, boolean line, Callback callback, CharValidator validator, String text) {
this(x, y, w, h, cap, line, true, callback, validator, text);
}
public Textbox(int x, int y, int w, int h, int cap, Callback callback, String text) {
this(x, y, w, h, cap, false, true, callback, null, text);
}
public Textbox(int x, int y, int w, int h, String text) {
this(x, y, w, h, Integer.MAX_VALUE, false, false, null, null, text);
}
// public void setEditable(boolean editable) {
// this.editable = editable;
// }
protected boolean isTextCenteredX() {
return false;
}
protected boolean isTextCenteredY() {
return false;
}
protected boolean hasLinebreak() {
return this.xbreak;
}
public boolean canHover() {
return false;
}
public void setText(String str) {
if(this.validator != null)
str = this.validator.filter(str);
this.text = str.length() > this.capacity ? str.substring(0, this.capacity) : str;
this.updateText();
this.sel_start = this.sel_end = this.sel_drag = this.text.length();
gui_text_update_cur(this.sel_start, true);
}
public void scroll(int scr_x, int scr_y, int x, int y, boolean ctrl, boolean shift) {
if(scr_y != 0 && this.xbreak) {
int limit = Font.YGLYPH + this.tsize_y - (this.size_y - (this.margin_y1 + this.margin_y2));
limit = ExtMath.clampi(limit, 0, 0x7fffffff);
int prev = this.text_y;
this.text_y += (scr_y < 0 ? -1 : 1) * (ctrl ? 1 : Font.YGLYPH) * this.gm.scrollLines * (shift ? 10 : 1);
this.text_y = ExtMath.clampi(this.text_y, -limit, 0);
if(this.sel_start >= 0)
this.cursorY += (this.text_y - prev);
this.r_dirty = true;
}
else if(scr_y != 0 || scr_x != 0) {
int limit = Font.XGLYPH + this.tsize_x - (this.size_x - (this.margin_x1 + this.margin_x2));
limit = ExtMath.clampi(limit, 0, 0x7fffffff);
int prev = this.text_x;
this.text_x += ((scr_y != 0 ? scr_y : (-scr_x)) < 0 ? -1 : 1) * (ctrl ? 1 : Font.XGLYPH) * this.gm.scrollLines * (shift ? 10 : 1);
this.text_x = ExtMath.clampi(this.text_x, -limit, 0);
if(this.sel_start >= 0)
this.cursorX += (this.text_x - prev);
this.r_dirty = true;
}
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
if(btn == Button.MOUSE_LEFT) {
if(!shift && ((Timing.tmr_current - this.tmr_leftmb) <= (((long)this.gm.dclickDelay) * 1000L))) {
this.sel_start = this.sel_drag = 0;
this.sel_end = this.text.length();
this.r_dirty = true;
}
else {
gui_text_select(x, y, shift);
}
this.tmr_leftmb = Timing.tmr_current;
}
else if((btn == Button.MOUSE_MIDDLE) && this.func != null) {
this.func.use(this, Action.FUNCTION);
}
}
public void mouserel() {
this.scrollx = this.scrolly = 0;
}
public void drag(int x, int y) {
gui_text_select(x, y, true);
}
public void character(char code) {
if(this.editable) {
// int pos = 0;
// char chr[8];
// utf_rwriten(chr, &pos, 8, code);
// chr[pos] = 0;
insertText(Character.toString(code));
}
}
public void key(Keysym key, boolean ctrl, boolean shift) {
if(ctrl && key == Keysym.A) {
this.sel_start = this.sel_drag = 0;
this.sel_end = this.text.length();
this.r_dirty = true;
}
else if(ctrl && (key == Keysym.C) || (this.editable && (key == Keysym.X))) {
if(this.sel_start >= 0 && this.sel_start != this.sel_end) { // fix empty
// char end = this.text[this.sel_end];
// this.text[this.sel_end] = 0;
String str = Util.strip(this.text, this.sel_start, this.sel_end - this.sel_start, '\n', (char)0, '?');
WCF.setClipboard(str);
// this.text[this.sel_end] = end;
if(key == Keysym.X)
insertText("");
}
}
else if(this.editable && ctrl && key == Keysym.V) {
insertText(WCF.getClipboard());
}
else if(this.editable && !ctrl && key == Keysym.RETURN) {
if(this.xbreak) {
insertText("\n");
}
else if(this.func != null) {
this.func.use(this, shift ? Action.LINE : Action.SEND);
}
}
else if(this.editable && (!ctrl) && (key == Keysym.BACKSPACE || key == Keysym.DELETE)) {
if(this.sel_start != this.sel_end) {
insertText("");
}
else if(key == Keysym.DELETE && this.sel_start >= 0) {
if(this.sel_end < this.text.length()) {
this.sel_end += 1;
insertText("");
}
else {
this.sel_end = this.sel_start;
}
}
else if(key == Keysym.BACKSPACE && this.sel_start > 0) {
this.sel_start -= 1;
insertText("");
}
}
else if(!ctrl && (key == Keysym.RIGHT || key == Keysym.LEFT)) {
if(key == Keysym.RIGHT && this.sel_start != this.sel_end) {
this.sel_start = this.sel_drag = this.sel_end;
}
else if(key == Keysym.LEFT && this.sel_start != this.sel_end) {
this.sel_end = this.sel_drag = this.sel_start;
}
if(key == Keysym.RIGHT && this.sel_start >= 0) {
if(this.sel_end < this.text.length()) {
this.sel_start = this.sel_drag = this.sel_end += 1;
}
else {
this.sel_end = this.sel_drag = this.sel_start;
}
gui_text_update_cur(this.sel_end, true);
}
else if(key == Keysym.LEFT && this.sel_start >= 0) {
if(this.sel_start > 0)
this.sel_start -= 1;
this.sel_end = this.sel_drag = this.sel_start;
gui_text_update_cur(this.sel_end, true);
}
}
else if(!ctrl && (key == Keysym.DOWN || key == Keysym.UP)) {
if(this.xbreak) {
if(key == Keysym.DOWN && this.sel_start != this.sel_end) {
this.sel_start = this.sel_drag = this.sel_end;
}
else if(key == Keysym.UP && this.sel_start != this.sel_end) {
this.sel_end = this.sel_drag = this.sel_start;
}
if(key == Keysym.DOWN && this.sel_start >= 0) {
if(this.sel_end < this.text.length()) {
// char ch = this.text.charAt(this.sel_end);
// this.sel_end += 1;
int nl = this.text.indexOf('\n', this.sel_end);
// while(ch && ch != '\n') {
// ch = utf_readn(this.text, &this.sel_end);
// }
this.sel_end = nl >= 0 ? nl + 1 : this.text.length();
// else
// this.sel_end -= 1;
this.sel_start = this.sel_drag = this.sel_end;
}
else {
this.sel_end = this.sel_drag = this.sel_start;
}
gui_text_update_cur(this.sel_end, true);
}
else if(key == Keysym.UP && this.sel_start >= 0) {
// uint ch;
if(this.sel_start > 0) {
int nl = this.text.lastIndexOf('\n', this.sel_start);
this.sel_start = nl >= 0 ? nl : 0;
// do {
// ch = utf_rreadn(this.text, &this.sel_start);
// }
// while(ch && ch != '\n');
}
this.sel_end = this.sel_drag = this.sel_start;
gui_text_update_cur(this.sel_end, true);
}
}
else if(this.func != null) {
this.func.use(this, (key == Keysym.DOWN) ? Action.NEXT : Action.PREVIOUS);
}
}
else if((!ctrl) && key == Keysym.TAB) {
if(this.func != null) {
this.func.use(this, shift ? Action.BACKWARD : Action.FORWARD);
}
}
}
public void select() {
if(this.func != null) {
this.func.use(this, Action.FOCUS);
}
}
public void deselect() {
this.sel_start = this.sel_end = this.sel_drag = -1;
this.tmr_leftmb = 0L;
this.r_dirty = true;
if(this.func != null) {
this.func.use(this, Action.UNFOCUS);
}
}
public void update() {
if((this.scrollx != 0 && !(this.xbreak)) || (this.scrolly != 0 && this.xbreak)) {
int n;
if(!this.xbreak)
this.text_x += (n = (int)((float)((float)this.tmr_scroll) / 1000000.0f * 4.0f * ((float)this.scrollx)));
else
this.text_y += (n = (int)((float)((float)this.tmr_scroll) / 1000000.0f * 4.0f * ((float)this.scrolly)));
if(n != 0) {
gui_text_clamp_scroll();
this.r_dirty = true;
}
if((((long)n) * 1000000L) <= this.tmr_scroll)
this.tmr_scroll -= ((long)n) * 1000000L;
else
this.tmr_scroll = 0L;
this.tmr_scroll += Timing.tmr_delta;
}
}
public void shift(int shift_x, int shift_y) {
super.shift(shift_x, shift_y);
this.cursorX += shift_x;
this.cursorY += shift_y;
}
private void gui_text_clamp_scroll() {
int limit;
if(this.xbreak) {
limit = Font.YGLYPH + this.tsize_y - (this.size_y - (this.margin_y1 + this.margin_y2));
limit = ExtMath.clampi(limit, 0, 0x7fffffff);
this.text_y = ExtMath.clampi(this.text_y, -limit, 0);
}
else {
limit = Font.XGLYPH + this.tsize_x - (this.size_x - (this.margin_x1 + this.margin_x2));
limit = ExtMath.clampi(limit, 0, 0x7fffffff);
this.text_x = ExtMath.clampi(this.text_x, -limit, 0);
}
}
private void gui_text_update_cur(int offset, boolean shift) {
int x1 = this.pos_x + this.margin_x1;
int y1 = this.pos_y + this.margin_y1;
int x2 = this.size_x - (this.margin_x1 + this.margin_x2);
int y2 = this.size_y - (this.margin_y1 + this.margin_y2);
gui_text_clamp_scroll();
Vec2i coord = Drawing.txt_coord(offset, x1 + (this.xbreak ? 0 : this.text_x), y1 + this.text_y,
x1 + this.text_x, y1 + this.text_y, this.xbreak ? (this.pos_x + x2) : 0x7fffffff, 0x7fffffff, this.text);
this.cursorX = coord.xpos;
this.cursorY = coord.ypos;
if(shift) {
if(this.xbreak && this.cursorY < y1)
this.text_y += y1 - this.cursorY;
else if(this.xbreak && (this.cursorY + Font.YGLYPH) >= (y1 + y2))
this.text_y -= (this.cursorY + Font.YGLYPH) - (y1 + y2);
if(!(this.xbreak) && this.cursorX < x1)
this.text_x += x1 - this.cursorX;
else if(!(this.xbreak) && (this.cursorX + Font.XGLYPH) >= (x1 + x2))
this.text_x -= (this.cursorX + Font.XGLYPH) - (x1 + x2);
gui_text_update_cur(offset, false);
}
else {
this.r_dirty = true;
}
}
public void insertText(String str) {
if(str == null || (this.sel_start == -1))
return;
str = Util.strip(str, 0, str.length(), this.xbreak ? '\n' : ' ', ' ', (char)0);
if(this.validator != null)
str = this.validator.filter(str);
// plen = strlen(&sys.work_buf[1 + olen - this.sel_end]);
// logd("t", "%d %d %d", olen, slen, plen);
if((str.length() + this.text.length() - (this.sel_end - this.sel_start)) > this.capacity)
return;
this.text = this.text.substring(0, this.sel_start) + str + this.text.substring(this.sel_end);
// memcpy(sys.work_buf, &this.text[this.sel_end], 1 + this.text.length() - this.sel_end);
// memcpy(&this.text[this.sel_start], &sys.work_buf[1 + this.text.length() - this.sel_end], str.length());
// memcpy(&this.text[this.sel_start + str.length()], sys.work_buf, 1 + this.text.length() - this.sel_end);
this.sel_start += str.length();
this.sel_end = this.sel_drag = this.sel_start;
this.updateText();
gui_text_update_cur(this.sel_end, true);
}
private void gui_text_select(int x, int y, boolean drag) {
int x1 = this.pos_x + this.margin_x1;
int y1 = this.pos_y + this.margin_y1;
int x2 = this.size_x - (this.margin_x1 + this.margin_x2);
int y2 = this.size_y - (this.margin_y1 + this.margin_y2);
Offset off = Drawing.txt_offset(x, y, x1 + (this.xbreak ? 0 : this.text_x), y1 + this.text_y,
x1 + this.text_x, y1 + this.text_y, this.xbreak ? (this.pos_x + x2) : 0x7fffffff, 0x7fffffff, this.text);
if(off != null) {
this.cursorX = off.xpos;
this.cursorY = off.ypos;
}
int offset = off == null ? 0 : off.offset;
// logd("tst", "@A %d %d -. %d %d", x1, y1, x2, y2);
// logd("tst", "@C %d %d -. %d, %d %d", x, y, offset, this.min, this.max);
if(!drag) {
this.sel_drag = this.sel_start = this.sel_end = offset;
}
else if(drag && this.sel_drag >= 0 && offset >= this.sel_drag) {
this.sel_start = this.sel_drag;
this.sel_end = offset;
}
else if(drag && this.sel_drag >= 0 && offset < this.sel_drag) {
this.sel_start = offset;
this.sel_end = this.sel_drag;
}
// logd("tst", "@S %d . %d . %d", this.sel_start, this.sel_drag, this.sel_end);
this.r_dirty = true;
if(x < x1)
this.scrollx = x1 - x;
else if(x >= (x1 + x2))
this.scrollx = -(x - (x1 + x2));
if(y < y1)
this.scrolly = y1 - y;
else if(y >= (y1 + y2))
this.scrolly = -(y - (y1 + y2));
}
protected void drawBackground() {
Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.field_top, this.gm.style.field_btm, this.gm.style.brdr_top, this.gm.style.brdr_btm);
}
protected void drawForeground(int x1, int y1, int x2, int y2) {
Drawing.txt_draw(x1 + (this.xbreak ? 0 : this.text_x), y1 + this.text_y,
x1 + this.text_x, y1 + this.text_y,
this.xbreak ? (this.pos_x + x2) : Integer.MAX_VALUE, Integer.MAX_VALUE, this.gm.style.text_field, this.text);
if(this.sel_start >= 0 && this.sel_end != this.sel_start)
Drawing.txt_draw_range(this.sel_start, this.sel_end, x1 + (this.xbreak ? 0 : this.text_x), y1 + this.text_y,
x1 + this.text_x, y1 + this.text_y,
this.xbreak ? (this.pos_x + x2) : Integer.MAX_VALUE, Integer.MAX_VALUE, this.gm.style.select, this.text);
}
public void drawOverlay() {
if(this.editable && this.sel_start >= 0 && this.sel_end == this.sel_start && this.gm.ftime() % 1.0f < 0.5f) {
int x1 = this.pos_x + this.margin_x1;
int y1 = this.pos_y + this.margin_y1;
int x2 = this.size_x - (this.margin_x1 + this.margin_x2);
int y2 = this.size_y - (this.margin_y1 + this.margin_y2);
WCF.glScissor(x1 < 0 ? 0 : x1, (this.gm.fb_y - (y1 + y2)) < 0 ? 0 : (this.gm.fb_y - (y1 + y2)), x2 < 0 ? 0 : x2, y2 < 0 ? 0 : y2);
WCF.glEnable(WCF.GL_SCISSOR_TEST);
Drawing.drawRectColor(this.cursorX, this.cursorY, 1, Font.YGLYPH, this.gm.style.cursor);
WCF.glDisable(WCF.GL_SCISSOR_TEST);
}
}
public void deleteFromCursor() {
int num = this.getNthCharFromPos() - this.sel_start;
if(this.text.length() != 0) {
if(this.sel_start != this.sel_end) {
this.insertText("");
}
else {
// boolean flag = num < 0;
int i = this.sel_start + num; // flag ? this.sel_start + num : this.sel_start;
int j = this.sel_start; // flag ? this.sel_start : this.sel_start + num;
String s = "";
if(i >= 0) {
s = this.text.substring(0, i);
}
if(j < this.text.length()) {
s = s + this.text.substring(j);
}
this.setText(s);
// this.text = s;
//
// if(flag) {
// this.moveCursorBy(num);
// }
}
}
}
public int getNthCharFromPos() {
int i = this.sel_start;
while(i > 0 && this.text.charAt(i - 1) != ' ') {
--i;
}
return i;
}
public int getCursorPosition() {
return this.sel_start == this.sel_end ? this.sel_start : -1;
}
}

View file

@ -0,0 +1,51 @@
package game.gui.element;
import game.renderer.Drawing;
import game.util.Formatter;
import game.window.Button;
public class Toggle extends Element {
public static interface Callback {
void use(Toggle elem, boolean value);
}
private final Callback func;
private final boolean def;
private boolean value;
public Toggle(int x, int y, int w, int h, boolean def, boolean init, Callback callback, Formatter<Toggle> formatter) {
super(x, y, w, h, formatter);
this.func = callback;
this.def = def;
this.value = init;
this.formatText();
}
public Toggle(int x, int y, int w, int h, boolean def, boolean init, Callback callback, final String text) {
this(x, y, w, h, def, init, callback, new Formatter<Toggle>() {
public String use(Toggle elem) {
return String.format("%s: %s", text, elem.value ? "An" : "Aus");
}
});
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
boolean prev = this.value;
if((this.value = (ctrl || (btn == Button.MOUSE_MIDDLE)) ? this.def : !this.value) != prev) {
// this.type = this.value != 0 ? ElemType.TOGGLE_ON : ElemType.TOGGLE_OFF;
// gui_update_style(this, 1);
this.r_dirty = true;
this.func.use(this, this.value);
this.formatText();
}
}
protected void drawBackground() {
if(this.value)
Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, this.gm.style.brdr_top, this.gm.style.brdr_btm);
else
super.drawBackground();
}
}

View file

@ -0,0 +1,12 @@
package game.gui.element;
public class TransparentBox extends Textbox {
public TransparentBox(int x, int y, int w, int h, String text) {
super(x, y, w, h, text);
}
protected void drawBackground() {
// Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y,
// this.gm.style.field_top & 0x80ffffff, this.gm.style.field_btm & 0x80ffffff, this.gm.style.brdr_top & 0x80ffffff, this.gm.style.brdr_btm & 0x80ffffff);
}
}