-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGuiLabel.java
More file actions
139 lines (129 loc) · 3.9 KB
/
GuiLabel.java
File metadata and controls
139 lines (129 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.compat;
import lombok.NonNull;
import lombok.val;
import org.lwjgl.opengl.GL11;
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;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.List;
/**
* A functional equivalent to GuiLabel present in Minecraft 1.12.
*
* @since 0.8.0
*/
@SideOnly(Side.CLIENT)
public class GuiLabel extends Gui {
private final List<String> lines = new ArrayList<>();
private final FontRenderer fontRenderer;
private final int textColor;
/**
* The id of the label.
*/
public int id;
/**
* The x position of the label.
*/
public int x;
/**
* The y position of the label.
*/
public int y;
/**
* The visibility of the label.
*/
public boolean visible = true;
/**
* The label width.
*/
protected int width;
/**
* The label height.
*/
protected int height;
private boolean centered = false;
/**
* 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
*/
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;
}
/**
* Add a line of text to the GuiLabel.
*
* @param text string to add
*/
public void addLine(@NonNull String text) {
lines.add(I18n.format(text));
}
/**
* Sets the label text to render centred with respect to the x and y.
*
* @return the GuiLabel itself
*/
public GuiLabel setCentered() {
centered = true;
return this;
}
/**
* Draw label.
*
* @param minecraft the minecraft
* @param mouseX the mouse x
* @param mouseY the mouse y
*/
public void drawLabel(@NonNull Minecraft minecraft, int mouseX, int mouseY) {
if (!visible) {
return;
}
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
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);
}
}
}
}