This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathButton.java
More file actions
414 lines (365 loc) · 11.3 KB
/
Copy pathButton.java
File metadata and controls
414 lines (365 loc) · 11.3 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package com.mrcrayfish.device.api.app.component;
import com.mrcrayfish.device.api.app.Component;
import com.mrcrayfish.device.api.app.Icon;
import com.mrcrayfish.device.api.app.listener.ClickListener;
import com.mrcrayfish.device.api.utils.RenderUtil;
import com.mrcrayfish.device.core.Laptop;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.init.SoundEvents;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import java.util.Arrays;
public class Button extends Component
{
protected static final ResourceLocation BUTTON_TEXTURES = new ResourceLocation("textures/gui/widgets.png");
protected String text;
protected String toolTip, toolTipTitle;
protected boolean hovered;
protected int padding = 5;
protected int width, height;
protected boolean explicitSize = false;
protected ResourceLocation iconResource;
protected int iconU, iconV;
protected int iconWidth, iconHeight;
protected int iconSourceWidth;
protected int iconSourceHeight;
protected ClickListener clickListener = null;
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
* @param text text to be displayed in the button
*/
public Button(int left, int top, String text)
{
super(left, top);
this.width = getTextWidth(text) + padding * 2;
this.height = 16;
this.text = text;
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
* @param text text to be displayed in the button
*/
public Button(int left, int top, int buttonWidth, int buttonHeight, String text)
{
super(left, top);
this.explicitSize = true;
this.width = buttonWidth;
this.height = buttonHeight;
this.text = text;
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
* @param icon
*/
public Button(int left, int top, Icon icon)
{
super(left, top);
this.padding = 3;
this.width = Icon.ICON_SIZE + padding * 2;
this.height = Icon.ICON_SIZE + padding * 2;
this.setIcon(icon);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
* @param icon
*/
public Button(int left, int top, int buttonWidth, int buttonHeight, Icon icon)
{
super(left, top);
this.explicitSize = true;
this.width = buttonWidth;
this.height = buttonHeight;
this.setIcon(icon);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
* @param icon
*/
public Button(int left, int top, String text, Icon icon)
{
this(left, top, text);
this.setIcon(icon);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
* @param icon
*/
public Button(int left, int top, int buttonWidth, int buttonHeight, String text, Icon icon)
{
super(left, top);
this.text = text;
this.explicitSize = true;
this.width = buttonWidth;
this.height = buttonHeight;
this.setIcon(icon);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
*/
public Button(int left, int top, ResourceLocation iconResource, int iconU, int iconV, int iconWidth, int iconHeight)
{
super(left, top);
this.padding = 3;
this.setIcon(iconResource, iconU, iconV, iconWidth, iconHeight);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
*/
public Button(int left, int top, int buttonWidth, int buttonHeight, ResourceLocation iconResource, int iconU, int iconV, int iconWidth, int iconHeight)
{
super(left, top);
this.explicitSize = true;
this.width = buttonWidth;
this.height = buttonHeight;
this.setIcon(iconResource, iconU, iconV, iconWidth, iconHeight);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
*/
public Button(int left, int top, String text, ResourceLocation iconResource, int iconU, int iconV, int iconWidth, int iconHeight)
{
super(left, top);
this.text = text;
this.setIcon(iconResource, iconU, iconV, iconWidth, iconHeight);
}
/**
* Alternate button constructor
*
* @param left how many pixels from the left
* @param top how many pixels from the top
*/
public Button(int left, int top, int buttonWidth, int buttonHeight, String text, ResourceLocation iconResource, int iconU, int iconV, int iconWidth, int iconHeight)
{
super(left, top);
this.text = text;
this.explicitSize = true;
this.width = buttonWidth;
this.height = buttonHeight;
this.setIcon(iconResource, iconU, iconV, iconWidth, iconHeight);
}
@Override
public void render(Laptop laptop, Minecraft mc, int x, int y, int mouseX, int mouseY, boolean windowActive, float partialTicks)
{
if (this.visible)
{
mc.getTextureManager().bindTexture(Component.COMPONENTS_GUI);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.hovered = isInside(mouseX, mouseY) && windowActive;
int i = this.getHoverState(this.hovered);
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
GlStateManager.blendFunc(770, 771);
/* Corners */
RenderUtil.drawRectWithTexture(xPosition, yPosition, 96 + i * 5, 12, 2, 2, 2, 2);
RenderUtil.drawRectWithTexture(xPosition + width - 2, yPosition, 99 + i * 5, 12, 2, 2, 2, 2);
RenderUtil.drawRectWithTexture(xPosition + width - 2, yPosition + height - 2, 99 + i * 5, 15, 2, 2, 2, 2);
RenderUtil.drawRectWithTexture(xPosition, yPosition + height - 2, 96 + i * 5, 15, 2, 2, 2, 2);
/* Middles */
RenderUtil.drawRectWithTexture(xPosition + 2, yPosition, 98 + i * 5, 12, width - 4, 2, 1, 2);
RenderUtil.drawRectWithTexture(xPosition + width - 2, yPosition + 2, 99 + i * 5, 14, 2, height - 4, 2, 1);
RenderUtil.drawRectWithTexture(xPosition + 2, yPosition + height - 2, 98 + i * 5, 15, width - 4, 2, 1, 2);
RenderUtil.drawRectWithTexture(xPosition, yPosition + 2, 96 + i * 5, 14, 2, height - 4, 2, 1);
/* Center */
RenderUtil.drawRectWithTexture(xPosition + 2, yPosition + 2, 98 + i * 5, 14, width - 4, height - 4, 1, 1);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
int contentWidth = (iconResource != null ? iconWidth: 0) + getTextWidth(text);
if(iconResource != null && text != null) contentWidth += 3;
int contentX = (int) Math.ceil((width - contentWidth) / 2.0);
if(iconResource != null)
{
int iconY = (height - iconHeight) / 2;
mc.getTextureManager().bindTexture(iconResource);
RenderUtil.drawRectWithTexture(x + contentX, y + iconY, iconU, iconV, iconWidth, iconHeight, iconWidth, iconHeight, iconSourceWidth, iconSourceHeight);
}
if(text != null)
{
int textY = (height - mc.fontRendererObj.FONT_HEIGHT) / 2 + 1;
int textOffsetX = iconResource != null ? iconWidth + 3 : 0;
int textColour = !Button.this.enabled ? 10526880 : (Button.this.hovered ? 16777120 : 14737632);
drawString(mc.fontRendererObj, text, x + contentX + textOffsetX, y + textY, textColour);
}
}
}
@Override
public void renderOverlay(Laptop laptop, Minecraft mc, int mouseX, int mouseY, boolean windowActive)
{
if(this.hovered && this.toolTip != null)
{
laptop.drawHoveringText(Arrays.asList(TextFormatting.GOLD + this.toolTipTitle, this.toolTip), mouseX, mouseY);
}
}
@Override
public void handleMouseClick(int mouseX, int mouseY, int mouseButton)
{
if(!this.visible || !this.enabled)
return;
if(this.hovered)
{
if(clickListener != null)
{
clickListener.onClick(this, mouseButton);
}
playClickSound(Minecraft.getMinecraft().getSoundHandler());
}
}
/**
* Sets the click listener. Use this to handle custom actions
* when you press the button.
*
* @param clickListener the click listener
*/
public final void setClickListener(ClickListener clickListener)
{
this.clickListener = clickListener;
}
protected int getHoverState(boolean mouseOver)
{
int i = 1;
if (!this.enabled)
{
i = 0;
}
else if (mouseOver)
{
i = 2;
}
return i;
}
protected void playClickSound(SoundHandler handler)
{
handler.playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
}
public boolean isInside(int mouseX, int mouseY)
{
return mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
}
public void setSize(int width, int height)
{
this.explicitSize = true;
this.width = width;
this.height = height;
}
public void setPadding(int padding)
{
this.padding = padding;
updateSize();
}
/**
* Sets the text to display in the button
*
* @param text the text
*/
public void setText(String text)
{
this.text = text;
updateSize();
}
/**
* Gets the text currently displayed in the button
*
* @return the button text
*/
public String getText()
{
return text;
}
public void setIcon(ResourceLocation iconResource, int iconU, int iconV, int iconWidth, int iconHeight)
{
this.iconU = iconU;
this.iconV = iconV;
this.iconResource = iconResource;
this.iconWidth = iconWidth;
this.iconHeight = iconHeight;
this.iconSourceWidth = 256;
this.iconSourceHeight = 256;
updateSize();
}
public void setIcon(Icon icon)
{
this.iconU = icon.getU();
this.iconV = icon.getV();
this.iconResource = Icon.ICON_ASSET;
this.iconWidth = Icon.ICON_SIZE;
this.iconHeight = Icon.ICON_SIZE;
this.iconSourceWidth = Icon.GRID_SIZE * Icon.ICON_SIZE;
this.iconSourceHeight = Icon.GRID_SIZE * Icon.ICON_SIZE;
updateSize();
}
public void removeIcon()
{
this.iconResource = null;
updateSize();
}
private void updateSize()
{
if(explicitSize) return;
int height = Math.max(iconHeight + padding * 2, 16);
int width = padding * 2;
if(iconResource != null)
{
width += iconWidth;
}
if(text != null)
{
width += getTextWidth(text);
}
if(iconResource != null && text != null)
{
width += 3;
}
this.width = width;
this.height = height;
}
/**
* Displays a message when hovering the button.
*
* @param toolTipTitle title of the tool tip
* @param toolTip description of the tool tip
*/
public void setToolTip(String toolTipTitle, String toolTip)
{
this.toolTipTitle = toolTipTitle;
this.toolTip = toolTip;
}
private static int getTextWidth(String text)
{
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
boolean flag = fontRenderer.getUnicodeFlag();
fontRenderer.setUnicodeFlag(false);
int width = fontRenderer.getStringWidth(text);
fontRenderer.setUnicodeFlag(flag);
return width;
}
//TODO add button text colour and button colour
}