Files
FalsePatternLib/src/main/java/com/falsepattern/lib/compat/GuiLabel.java
T

151 lines
4.2 KiB
Java
Raw Normal View History

2022-07-31 20:41:56 +02:00
/*
2024-04-17 17:23:43 +02:00
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
2022-07-30 15:21:11 +02:00
* All Rights Reserved
*
2024-04-17 17:23:43 +02:00
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
2022-07-30 15:21:11 +02:00
*
2024-04-17 17:23:43 +02:00
* FalsePatternLib is free software: you can redistribute it and/or modify
2022-07-30 15:21:11 +02:00
* 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.
*
2024-04-17 17:23:43 +02:00
* FalsePatternLib is distributed in the hope that it will be useful,
2022-07-30 15:21:11 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2024-04-17 17:23:43 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2022-07-30 15:21:11 +02:00
*
* You should have received a copy of the GNU Lesser General Public License
2024-04-17 17:23:43 +02:00
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
2022-07-30 15:21:11 +02:00
*/
2022-05-07 13:38:33 +02:00
package com.falsepattern.lib.compat;
2022-06-12 11:58:13 +02:00
import com.falsepattern.lib.StableAPI;
2022-06-08 16:01:06 +02:00
import lombok.NonNull;
import lombok.val;
import lombok.var;
2022-07-31 20:41:56 +02:00
import org.lwjgl.opengl.GL11;
2022-05-07 13:38:33 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.resources.I18n;
2022-07-31 20:41:56 +02:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.List;
2022-05-07 13:38:33 +02:00
2022-05-31 12:30:22 +01:00
/**
2022-05-31 14:11:33 +01:00
* A functional equivalent to GuiLabel present in Minecraft 1.12.
2022-05-31 12:30:22 +01:00
*/
2022-05-07 13:38:33 +02:00
@SideOnly(Side.CLIENT)
2022-06-12 11:58:13 +02:00
@StableAPI(since = "0.8.0")
2022-05-31 10:09:16 +01:00
public class GuiLabel extends Gui {
2022-05-31 11:05:07 +01:00
private final List<String> lines = new ArrayList<>();
private final FontRenderer fontRenderer;
private final int textColor;
2022-05-31 12:30:22 +01:00
/**
* The id of the label.
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-31 11:05:07 +01:00
public int id;
2022-05-31 12:30:22 +01:00
/**
* The x position of the label.
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-07 13:38:33 +02:00
public int x;
2022-05-31 12:30:22 +01:00
/**
* The y position of the label.
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-07 13:38:33 +02:00
public int y;
2022-05-31 12:30:22 +01:00
/**
* The visibility of the label.
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-07 13:38:33 +02:00
public boolean visible = true;
2022-06-08 16:01:06 +02:00
/**
* The label width.
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-06-08 16:01:06 +02:00
protected int width;
/**
* The label height.
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-06-08 16:01:06 +02:00
protected int height;
private boolean centered = false;
2022-05-07 13:38:33 +02:00
2022-05-31 11:05:07 +01:00
/**
* Instantiates a new Gui label.
*
* @param fontRenderer the minecraft font renderer
* @param id the id
* @param x the x
* @param y the y
* @param width the width
* @param height the height
* @param textColour the text colour
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-31 11:05:07 +01:00
public GuiLabel(@NonNull FontRenderer fontRenderer, int id, int x, int y, int width, int height, int textColour) {
this.fontRenderer = fontRenderer;
this.id = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.textColor = textColour;
2022-05-07 13:38:33 +02:00
}
2022-05-31 11:05:07 +01:00
/**
* Add a line of text to the GuiLabel.
*
2022-05-31 12:30:22 +01:00
* @param text string to add
2022-05-31 11:05:07 +01:00
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-31 11:05:07 +01:00
public void addLine(@NonNull String text) {
lines.add(I18n.format(text));
2022-05-07 13:38:33 +02:00
}
/**
2022-05-31 11:05:07 +01:00
* Sets the label text to render centred with respect to the x and y.
*
2022-05-31 12:30:22 +01:00
* @return the GuiLabel itself
2022-05-07 13:38:33 +02:00
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-31 10:09:16 +01:00
public GuiLabel setCentered() {
2022-05-31 11:05:07 +01:00
centered = true;
2022-05-07 13:38:33 +02:00
return this;
}
2022-05-31 11:05:07 +01:00
/**
* Draw label.
*
* @param minecraft the minecraft
* @param mouseX the mouse x
* @param mouseY the mouse y
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-31 11:05:07 +01:00
public void drawLabel(@NonNull Minecraft minecraft, int mouseX, int mouseY) {
2022-06-08 16:01:06 +02:00
if (!visible) {
2022-05-31 11:05:07 +01:00
return;
2022-06-08 16:01:06 +02:00
}
2022-05-31 11:05:07 +01:00
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
2022-05-07 13:38:33 +02:00
2022-05-31 11:05:07 +01:00
val topLeftY = y + height / 2 - lines.size() * 10 / 2;
for (var i = 0; i < lines.size(); ++i) {
if (centered) {
drawCenteredString(fontRenderer, lines.get(i), x + width / 2, topLeftY + i * 10, textColor);
} else {
drawString(fontRenderer, lines.get(i), x, topLeftY + i * 10, textColor);
}
2022-05-07 13:38:33 +02:00
}
}
}