Skip to content

Commit e3bad2b

Browse files
committed
Fix button and slider rendering for large sizes
1 parent e720cc1 commit e3bad2b

File tree

4 files changed

+134
-12
lines changed

4 files changed

+134
-12
lines changed

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiButton.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,20 @@ public void onClick(Consumer<CwgGuiButton> handler) {
5252
}
5353
return false;
5454
}
55+
56+
57+
@Override public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
58+
if (!this.visible) {
59+
return;
60+
}
61+
this.hovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;
62+
63+
McGuiRender.prepareWidgetRender(mc);
64+
McGuiRender.ButtonMode mode = McGuiRender.ButtonMode.values()[this.getHoverState(this.hovered)];
65+
McGuiRender.drawButtonBg(mode, this.x, this.y, this.width, this.height);
66+
67+
this.mouseDragged(mc, mouseX, mouseY);
68+
69+
McGuiRender.drawWidgetStringCentered(mc, this, packedFGColour, enabled, hovered);
70+
}
5571
}

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiLabel.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,15 @@
2929

3030
public class CwgGuiLabel extends GuiButton {
3131

32-
private int color;
33-
3432
public CwgGuiLabel(String formatString, int color) {
3533
super(0, 0, 0, I18n.format(formatString));
36-
this.color = color;
34+
this.packedFGColour = color;
3735
this.height = Minecraft.getMinecraft().fontRenderer.FONT_HEIGHT;
3836
}
3937

40-
public int getColor() {
41-
return color;
42-
}
43-
44-
public void setColor(int color) {
45-
this.color = color;
46-
}
47-
4838
@Override public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
4939
if (this.visible) {
50-
drawString(mc.fontRenderer, displayString, x + 2, y + 2, color);
40+
McGuiRender.drawWidgetString(mc, this, packedFGColour, enabled, false);
5141
}
5242
}
5343
}

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiSlider.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ public void setSliderValue(double sliderValue) {
7272
return 0;
7373
}
7474

75+
@Override public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
76+
if (!this.visible) {
77+
return;
78+
}
79+
this.hovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;
80+
81+
McGuiRender.prepareWidgetRender(mc);
82+
McGuiRender.drawSliderBg(this.x, this.y, this.width, this.height);
83+
84+
this.mouseDragged(mc, mouseX, mouseY);
85+
86+
McGuiRender.drawWidgetStringCentered(mc, this, packedFGColour, enabled, hovered);
87+
}
88+
7589
// this is actually a draw() method
7690

7791
@Override protected void mouseDragged(Minecraft mc, int mouseX, int mouseY) {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* This file is part of Cubic World Generation, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2015-2020 contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component;
25+
26+
import net.minecraft.client.Minecraft;
27+
import net.minecraft.client.gui.Gui;
28+
import net.minecraft.client.gui.GuiButton;
29+
import net.minecraft.client.renderer.GlStateManager;
30+
import net.minecraft.util.ResourceLocation;
31+
32+
public class McGuiRender {
33+
protected static final ResourceLocation BUTTON_TEXTURES = new ResourceLocation("textures/gui/widgets.png");
34+
35+
private static final int BTN_TEX_MIN_X = 0;
36+
private static final int BTN_TEX_MIN_Y = 46;
37+
private static final int BTN_TEX_WIDTH = 200;
38+
private static final int BTN_TEX_HEIGHT = 20;
39+
private static final int WIDGETS_TEX_SIZE = 256;
40+
41+
public static void prepareWidgetRender(Minecraft mc) {
42+
mc.getTextureManager().bindTexture(BUTTON_TEXTURES);
43+
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
44+
GlStateManager.enableBlend();
45+
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
46+
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
47+
}
48+
49+
public static void drawWidgetStringCentered(Minecraft mc, GuiButton btn, int colorOverride, boolean enabled, boolean hovered) {
50+
int color = 0xe0e0e0;
51+
if (colorOverride != 0) {
52+
color = colorOverride;
53+
} else if (!enabled) {
54+
color = 0xa0a0a0;
55+
} else if (hovered) {
56+
color = 0xffffa0;
57+
}
58+
btn.drawCenteredString(mc.fontRenderer, btn.displayString, btn.x + btn.width / 2, btn.y + (btn.height - 8) / 2, color);
59+
}
60+
61+
public static void drawWidgetString(Minecraft mc, GuiButton btn, int colorOverride, boolean enabled, boolean hovered) {
62+
int color = 0xe0e0e0;
63+
if (colorOverride != 0) {
64+
color = colorOverride;
65+
} else if (!enabled) {
66+
color = 0xa0a0a0;
67+
} else if (hovered) {
68+
color = 0xffffa0;
69+
}
70+
btn.drawString(mc.fontRenderer, btn.displayString, btn.x, btn.y + (btn.height - 8) / 2, color);
71+
}
72+
73+
public static void drawSliderBg(int x, int y, int width, int height) {
74+
drawButtonBg(ButtonMode.DISABLED, x, y, width, height);
75+
}
76+
77+
public static void drawButtonBg(ButtonMode mode, int x, int y, int width, int height) {
78+
int yOffset = mode.ordinal() * BTN_TEX_HEIGHT;
79+
int texWidth = width / 2;
80+
if (texWidth > 180) {
81+
texWidth = 180;
82+
}
83+
Gui.drawScaledCustomSizeModalRect(
84+
x, y,
85+
BTN_TEX_MIN_X, BTN_TEX_MIN_Y + yOffset,
86+
texWidth, BTN_TEX_HEIGHT,
87+
width / 2, height,
88+
WIDGETS_TEX_SIZE, WIDGETS_TEX_SIZE
89+
);
90+
Gui.drawScaledCustomSizeModalRect(
91+
x + width / 2, y,
92+
BTN_TEX_MIN_X + BTN_TEX_WIDTH - texWidth, BTN_TEX_MIN_Y + yOffset,
93+
texWidth, BTN_TEX_HEIGHT,
94+
width - width / 2, height,
95+
WIDGETS_TEX_SIZE, WIDGETS_TEX_SIZE
96+
);
97+
}
98+
99+
public enum ButtonMode {
100+
DISABLED, ENABLED, HOVERED
101+
}
102+
}

0 commit comments

Comments
 (0)