tcr/java/src/game/gui/Gui.java

340 lines
11 KiB
Java
Raw Normal View History

2025-03-11 14:09:49 +01:00
package game.gui;
import java.util.List;
2025-03-16 17:40:47 +01:00
import com.google.common.collect.Lists;
2025-03-11 14:09:49 +01:00
import game.Game;
import game.gui.element.Dropdown;
import game.gui.element.Dropdown.Handle;
2025-03-16 17:45:08 +01:00
import game.gui.element.Element;
2025-03-11 14:09:49 +01:00
import game.renderer.DefaultVertexFormats;
import game.renderer.Drawing;
import game.renderer.GlState;
import game.renderer.RenderBuffer;
import game.renderer.Tessellator;
import game.vars.CVar;
import game.vars.ColorVar;
import game.window.Bind;
import game.window.Button;
import game.window.Keysym;
import game.window.WCF;
public abstract class Gui {
public static final String DIRT_BACKGROUND = "textures/background.png";
protected final Game gm = Game.getGame();
public Element selected;
private int min_x;
private int min_y;
private int max_x;
private int max_y;
public final List<Element> elems = Lists.newArrayList();
public abstract void init(int width, int height);
public abstract String getTitle();
public void updateScreen() {
}
public void onGuiClosed() {
}
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
}
public void mouseReleased(int mouseX, int mouseY, int state) {
}
public void mouseDragged(int mouseX, int mouseY) {
}
public void drawPost() {
}
2025-03-11 19:38:37 +01:00
// public void drawGuiContainerForegroundLayer() {
// }
//
// public void drawGuiContainerBackgroundLayer() {
// }
2025-03-11 14:09:49 +01:00
public void drawOverlays() {
}
public void useHotbar(int slot) {
}
public void dropItem() {
}
public void drawBackground() {
}
public Element clicked(int x, int y) {
if(this.selected != null && this.selected.visible && (this.selected instanceof Handle) && this.selected.inside(x, y))
return this.selected; // fix (?)
for(Element elem : this.elems) {
if(elem.visible && elem.enabled && elem.inside(x, y))
return elem;
}
return null;
}
public void reformat() {
for(Element elem : this.elems) {
elem.reformat();
}
}
public void deselect() {
if(this.selected != null && !(this.selected instanceof Handle)) {
this.selected.deselect();
this.selected = null;
}
}
public void select(Element elem) {
if(this.selected != elem && !(this.selected instanceof Handle) && !(elem instanceof Handle)) {
if(this.selected != null)
this.selected.deselect();
if(elem != null)
elem.select();
this.selected = elem;
}
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
int prev;
Element elem = this.clicked(x, y);
if(this.selected != null && this.selected != elem && this.selected instanceof Handle) {
this.selected.deselect();
return;
}
else if(this.selected != elem) {
if(this.selected != null)
this.selected.deselect();
if(elem != null)
elem.select();
this.selected = elem;
}
if(elem != null)
elem.mouse(btn, x, y, ctrl, shift);
}
public void mouserel(Button btn, int x, int y) {
if(this.selected != null)
this.selected.mouserel();
}
public void drag(int x, int y) {
if(this.selected != null && (Button.MOUSE_LEFT.isDown() || Button.MOUSE_RIGHT.isDown()))
this.selected.drag(x, y);
}
public void scroll(int scr_x, int scr_y, int x, int y, boolean ctrl, boolean shift) {
Element elem = this.clicked(x, y);
if(elem != null)
elem.scroll(scr_x, scr_y, x, y, ctrl, shift);
}
public void key(Keysym key, boolean ctrl, boolean shift) {
if(this.selected != null)
this.selected.key(key, ctrl, shift);
}
public void character(char code) {
if(this.selected != null)
this.selected.character(code);
}
protected void shift() {
if(this.gm.fb_x != 0 && this.gm.fb_y != 0) {
int shift_x = (this.gm.fb_x - (this.max_x - this.min_x)) / 2 - this.min_x;
int shift_y = (this.gm.fb_y - (this.max_y - this.min_y)) / 2 - this.min_y;
for(Element elem : this.elems) {
elem.shift(shift_x, shift_y);
}
}
}
public void init() {
this.selected = null;
this.min_x = this.min_y = Integer.MAX_VALUE;
this.max_x = this.max_y = Integer.MIN_VALUE;
this.elems.clear();
this.init(this.gm.fb_x, this.gm.fb_y);
}
public void update() {
if(this.selected != null)
this.selected.update();
}
protected <T extends Element> T add(T elem) {
this.elems.add(elem);
elem.setGui(this);
this.min_x = Math.min(this.min_x, elem.getX());
this.min_y = Math.min(this.min_y, elem.getY());
this.max_x = Math.max(this.max_x, elem.getX() + elem.getWidth());
this.max_y = Math.max(this.max_y, elem.getY() + elem.getHeight());
if(elem instanceof Dropdown)
this.add(((Dropdown)elem).getHandle());
return elem;
}
protected Element addSelector(String cvar, int x, int y, int w, int h) {
CVar cv = this.gm.getVar(cvar);
if(cv instanceof ColorVar) {
ColorVar color = (ColorVar)cv;
if(!color.getDisplay().isEmpty())
this.add(color.label(x, y - 20, w, 20));
return this.add(color.editor(x, y, w, h));
}
return this.add(cv.selector(x, y, w, h));
}
public void draw() {
if(this.selected != null && /* this.selected.r_dirty && */ this.selected instanceof Handle && !this.selected.visible) {
this.selected = null;
}
for(Element elem : this.elems) {
if(/* this.selected != e || */ !(elem instanceof Handle)) // || !e.visible)
elem.draw();
}
if(this.selected != null && /* elem.r_dirty && */ this.selected instanceof Handle && this.selected.visible) {
this.selected.draw();
}
}
public void drawOverlay() {
Element elem = this.selected;
if(Button.isMouseDown() && elem != null && elem.enabled && elem.visible && elem.canClick()) {
elem.drawPress();
return;
}
if(elem != null && elem.enabled && elem.visible)
elem.drawOverlay();
elem = this.clicked(this.gm.mouse_x, this.gm.mouse_y);
if(elem != null && elem.enabled && elem.visible && elem.canHover())
elem.drawHover();
}
public static void drawRect(int left, int top, int right, int bottom, int color)
{
if (left < right)
{
int i = left;
left = right;
right = i;
}
if (top < bottom)
{
int j = top;
top = bottom;
bottom = j;
}
float f3 = (float)(color >> 24 & 255) / 255.0F;
float f = (float)(color >> 16 & 255) / 255.0F;
float f1 = (float)(color >> 8 & 255) / 255.0F;
float f2 = (float)(color & 255) / 255.0F;
RenderBuffer worldrenderer = Tessellator.getBuffer();
GlState.enableBlend();
GlState.disableTexture2D();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.color(f, f1, f2, f3);
worldrenderer.begin(7, DefaultVertexFormats.POSITION);
worldrenderer.pos((double)left, (double)bottom, 0.0D).endVertex();
worldrenderer.pos((double)right, (double)bottom, 0.0D).endVertex();
worldrenderer.pos((double)right, (double)top, 0.0D).endVertex();
worldrenderer.pos((double)left, (double)top, 0.0D).endVertex();
Tessellator.draw();
GlState.enableTexture2D();
GlState.disableBlend();
}
public static void drawTexturedModalRect(int x, int y, int textureX, int textureY, int width, int height)
{
float f = 0.00390625F;
float f1 = 0.00390625F;
RenderBuffer worldrenderer = Tessellator.getBuffer();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX);
worldrenderer.pos((double)(x + 0), (double)(y + height), 0.0D).tex((double)((float)(textureX + 0) * f), (double)((float)(textureY + height) * f1)).endVertex();
worldrenderer.pos((double)(x + width), (double)(y + height), 0.0D).tex((double)((float)(textureX + width) * f), (double)((float)(textureY + height) * f1)).endVertex();
worldrenderer.pos((double)(x + width), (double)(y + 0), 0.0D).tex((double)((float)(textureX + width) * f), (double)((float)(textureY + 0) * f1)).endVertex();
worldrenderer.pos((double)(x + 0), (double)(y + 0), 0.0D).tex((double)((float)(textureX + 0) * f), (double)((float)(textureY + 0) * f1)).endVertex();
Tessellator.draw();
}
public static void drawScaledCustomSizeModalRect(int x, int y, float u, float v, int uWidth, int vHeight, int width, int height, float tileWidth, float tileHeight)
{
float f = 1.0F / tileWidth;
float f1 = 1.0F / tileHeight;
RenderBuffer worldrenderer = Tessellator.getBuffer();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX);
worldrenderer.pos((double)x, (double)(y + height), 0.0D).tex((double)(u * f), (double)((v + (float)vHeight) * f1)).endVertex();
worldrenderer.pos((double)(x + width), (double)(y + height), 0.0D).tex((double)((u + (float)uWidth) * f), (double)((v + (float)vHeight) * f1)).endVertex();
worldrenderer.pos((double)(x + width), (double)y, 0.0D).tex((double)((u + (float)uWidth) * f), (double)(v * f1)).endVertex();
worldrenderer.pos((double)x, (double)y, 0.0D).tex((double)(u * f), (double)(v * f1)).endVertex();
Tessellator.draw();
}
public static void drawGradientRect(int left, int top, int right, int bottom, int startColor, int endColor)
{
float f = (float)(startColor >> 24 & 255) / 255.0F;
float f1 = (float)(startColor >> 16 & 255) / 255.0F;
float f2 = (float)(startColor >> 8 & 255) / 255.0F;
float f3 = (float)(startColor & 255) / 255.0F;
float f4 = (float)(endColor >> 24 & 255) / 255.0F;
float f5 = (float)(endColor >> 16 & 255) / 255.0F;
float f6 = (float)(endColor >> 8 & 255) / 255.0F;
float f7 = (float)(endColor & 255) / 255.0F;
GlState.disableTexture2D();
GlState.enableBlend();
GlState.disableAlpha();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.shadeModel(7425);
RenderBuffer worldrenderer = Tessellator.getBuffer();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_COLOR);
worldrenderer.pos((double)right, (double)top, 0.0).color(f1, f2, f3, f).endVertex();
worldrenderer.pos((double)left, (double)top, 0.0).color(f1, f2, f3, f).endVertex();
worldrenderer.pos((double)left, (double)bottom, 0.0).color(f5, f6, f7, f4).endVertex();
worldrenderer.pos((double)right, (double)bottom, 0.0).color(f5, f6, f7, f4).endVertex();
Tessellator.draw();
GlState.shadeModel(7424);
GlState.disableBlend();
GlState.enableAlpha();
GlState.enableTexture2D();
}
public void drawMainBackground() {
if(this.gm.theWorld != null) {
// Drawing.drawGradient(0, 0, this.fb_x, this.fb_y, this.theWorld == null ? this.style.bg_top : 0x3f202020,
// this.theWorld == null ? this.style.bg_btm : 0x3f000000);
Drawing.drawGradient(0, 0, this.gm.fb_x, this.gm.fb_y, 0xc0101010, 0xd0101010);
}
else {
2025-03-16 21:58:40 +01:00
Drawing.drawScaledBackground(this.gm, DIRT_BACKGROUND);
2025-03-11 14:09:49 +01:00
}
}
public void render() {
this.drawMainBackground();
this.drawBackground();
if(this.gm.fb_x != 0 && this.gm.fb_y != 0)
this.draw();
GlState.bindTexture(0);
GlState.setActiveTexture(WCF.GL_TEXTURE0);
GlState.enableTexture2D();
GlState.disableDepth();
this.drawPost();
GlState.disableDepth();
this.drawOverlays();
if(Bind.isWindowActive())
this.drawOverlay();
}
}