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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
package io.polyfrost.oneconfig.gui.elements;
import io.polyfrost.oneconfig.renderer.Renderer;
import io.polyfrost.oneconfig.themes.Theme;
import io.polyfrost.oneconfig.themes.Themes;
import io.polyfrost.oneconfig.themes.textures.ThemeElement;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import java.awt.*;
import static io.polyfrost.oneconfig.gui.Window.resolution;
/**
* Default simple block for all the GUI elements. If you are making custom ones, your class should extend this one.
*/
@SuppressWarnings("unused")
public class OCBlock {
public static final Theme theme = Themes.getActiveTheme();
private static final Minecraft mc = Minecraft.getMinecraft();
private final boolean bold;
private final Color elementColor = theme.getElementColor();
private final Color hoverColor = theme.getHoverColor();
private final Runnable draw;
/**
* Width of the element in pixels.
*/
public int width;
/**
* Height of the element in pixels.
*/
public int height;
private Color color;
private String text;
private ThemeElement element;
private boolean clicked = false;
private boolean rightClicked = false;
private int mouseX, mouseY;
private boolean hovered;
private float percentHoveredRed = 0f;
private float percentHoveredGreen = 0f;
private float percentHoveredBlue = 0f;
private float percentHoveredAlpha = 0f;
/**
* Create a basic element.
*/
public OCBlock(int width, int height) {
this(null, false, theme.getElementColor().getRGB(), width, height);
}
/**
* Create a new basic element.
*
* @param color color of the element
* @param width width of the element
* @param height height of the element
* @deprecated This method DOES NOT respect the theme colors for the element. Use of {@link #OCBlock(int, int)} is recommended instead.
*/
@Deprecated()
public OCBlock(int color, int width, int height) {
this(null, false, color, width, height);
}
/**
* Create a new element with the specified text, and automatic width/height + padding.
*
* @param text text to use
* @param bold weather or not to use bold text
* @param color color for the text
*/
public OCBlock(@NotNull String text, boolean bold, int color) {
this(text, bold, color, theme.getFont().getWidth(text) + 6, theme.getFont().getHeight() + 4);
}
/**
* Create a new element with the specified text, and custom width/height.
*
* @param text text to use
* @param bold weather or not to use bold text
* @param color color for the text (use {@link Theme#getTextColor()} or {@link Theme#getAccentTextColor()} for default colors)
*/
public OCBlock(String text, boolean bold, int color, int width, int height) {
this.draw = null;
this.text = text;
this.bold = bold;
this.color = Renderer.getColorFromInt(color);
this.width = width;
this.height = height;
}
/**
* Create a new Element with the specified image.
*
* @param element element to use
* @param colorMask color mast to use (-1 for default)
*/
public OCBlock(ThemeElement element, int colorMask, int width, int height) {
this.draw = null;
this.element = element;
this.color = Renderer.getColorFromInt(colorMask);
this.width = width;
this.height = height;
this.bold = false;
}
/**
* Create a new Element with a custom render script. The {@link Runnable} should ONLY contain #draw() calls or equivalent.
*
* @param whatToDraw a {@link Runnable}, containing draw scripts for elements. You will need to instantiate the objects first, if they are sub-elements.
*/
public OCBlock(Runnable whatToDraw, int width, int height) {
this.draw = whatToDraw;
this.bold = false;
this.width = width;
this.height = height;
}
/**
* Draw the element at the specified coordinates.
*/
public void draw(int x, int y) {
GlStateManager.enableBlend();
percentHoveredRed = smooth(percentHoveredRed, elementColor.getRed() / 255f, hoverColor.getRed() / 255f);
percentHoveredGreen = smooth(percentHoveredGreen, elementColor.getGreen() / 255f, hoverColor.getGreen() / 255f);
percentHoveredBlue = smooth(percentHoveredBlue, elementColor.getBlue() / 255f, hoverColor.getBlue() / 255f);
percentHoveredAlpha = smooth(percentHoveredAlpha, elementColor.getAlpha() / 255f, hoverColor.getAlpha() / 255f);
GlStateManager.color(percentHoveredRed, percentHoveredGreen, percentHoveredBlue, percentHoveredAlpha);
update(x, y);
if (draw != null) {
draw.run();
}
if (element != null) {
theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height);
GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f);
theme.getTextureManager().draw(element, x, y, width, height);
}
if (text == null) {
theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height);
} else {
theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height);
if (bold) {
theme.getBoldFont().drawString(text, x + 3, y + 2, 1f, 1f, color.getRGB());
} else {
theme.getFont().drawString(text, x + 3, y + 2, 1.1f, 1f, color.getRGB());
}
}
GlStateManager.disableBlend();
}
/**
* Update this elements click, key and hover status. Call this method at the end of your 'draw' function, if overridden.
*/
public void update(int x, int y) {
int mouseX = Mouse.getX() / resolution.getScaleFactor();
int mouseY = Math.abs((Mouse.getY() / resolution.getScaleFactor()) - resolution.getScaledHeight());
hovered = mouseX > x && mouseY > y && mouseX < x + width && mouseY < y + height;
if (hovered) {
onHover();
if (Mouse.isButtonDown(0) && !clicked) {
onClick(0);
}
clicked = Mouse.isButtonDown(0);
if (Mouse.isButtonDown(1) && !rightClicked) {
onClick(1);
}
rightClicked = Mouse.isButtonDown(1);
onKeyPress(Keyboard.getEventKey());
}
if (clicked) {
Renderer.color(theme.getClickColor());
}
if (!hovered && clicked) clicked = false;
}
/**
* Draw the element with the specified coordinates, width and height.
*/
public void draw(int x, int y, int width, int height) {
this.width = width;
this.height = height;
draw(x, y);
}
/**
* Override this method to set a function when a key is pressed while this element is hovered.
*
* @param keyCode key code that was pressed (check org.lwjgl.Keyboard for keymap)
*/
public void onKeyPress(int keyCode) {
}
/**
* Override this method to set a function when the element is hovered.
*
* @param button the button that was pressed (0 is left, 1 is right)
*/
public void onClick(int button) {
}
/**
* Override this method to set a function when the element is hovered.
*/
public void onHover() {
}
private float smooth(float current, float min, float max) {
current = Renderer.easeOut(current, isHovered() ? 1f : 0f);
if (current <= min) {
current = min;
}
if (current >= max) {
current = max;
}
return current;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean isHovered() {
return hovered;
}
public boolean isClicked() {
return clicked;
}
}
|