Skip to content

Commit 6a877ea

Browse files
committed
Reuse forge code for handling too large buttons, add support for more vanilla component types than GuiButton, use GuiLabel instead of custom implementation
1 parent e3bad2b commit 6a877ea

File tree

13 files changed

+334
-264
lines changed

13 files changed

+334
-264
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.asm.mixin.common.accessor;
25+
26+
import net.minecraft.client.gui.GuiLabel;
27+
import org.spongepowered.asm.mixin.Mixin;
28+
import org.spongepowered.asm.mixin.gen.Accessor;
29+
30+
import java.util.List;
31+
32+
@Mixin(GuiLabel.class)
33+
public interface IGuiLabel {
34+
@Accessor int getWidth();
35+
@Accessor int getHeight();
36+
@Accessor void setWidth(int width);
37+
@Accessor void setHeight(int height);
38+
@Accessor List<String> getLabels();
39+
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiCheckBox;
3030
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiLabel;
3131
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiSlider;
32-
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.WrappedVanillaButton;
32+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.WrappedVanillaComponent;
3333
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.converter.Converters;
3434
import net.malisis.core.client.gui.MalisisGui;
3535
import net.minecraft.client.gui.GuiButton;
36+
import net.minecraft.client.gui.GuiLabel;
37+
import net.minecraft.client.gui.GuiTextField;
3638

3739
import java.util.function.BiPredicate;
3840
import java.util.function.Consumer;
@@ -47,8 +49,16 @@ private CwgGuiFactory() {
4749
throw new Error();
4850
}
4951

50-
public static <T extends GuiButton> WrappedVanillaButton<T> wrap(MalisisGui gui, T vanillaComponent) {
51-
return new WrappedVanillaButton<>(gui, vanillaComponent);
52+
public static <T extends GuiButton> WrappedVanillaComponent<T> wrap(MalisisGui gui, T vanillaComponent) {
53+
return WrappedVanillaComponent.of(gui, vanillaComponent);
54+
}
55+
56+
public static <T extends GuiLabel> WrappedVanillaComponent<T> wrap(MalisisGui gui, T vanillaComponent) {
57+
return WrappedVanillaComponent.of(gui, vanillaComponent);
58+
}
59+
60+
public static <T extends GuiTextField> WrappedVanillaComponent<T> wrap(MalisisGui gui, T vanillaComponent) {
61+
return WrappedVanillaComponent.of(gui, vanillaComponent);
5262
}
5363

5464
public static CwgGuiLabel makeLabel(String formatString) {
@@ -64,9 +74,7 @@ public static CwgGuiLabel makeLabel(String formatString, int x, int y) {
6474
}
6575

6676
public static CwgGuiLabel makeLabel(String formatString, int color, int x, int y) {
67-
CwgGuiLabel label = new CwgGuiLabel(str(formatString), color);
68-
label.x = x;
69-
label.y = y;
77+
CwgGuiLabel label = new CwgGuiLabel(str(formatString), x, y, 10, 10, color);
7078
return label;
7179
}
7280

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component;
2525

2626
import net.minecraft.client.Minecraft;
27-
import net.minecraft.client.gui.GuiButton;
2827
import net.minecraft.client.resources.I18n;
28+
import net.minecraftforge.fml.client.config.GuiButtonExt;
2929

3030
import java.util.function.Consumer;
3131

3232
import javax.annotation.Nullable;
3333

34-
// TODO: fix vanilla rendering for large widths
35-
public class CwgGuiButton extends GuiButton {
34+
public class CwgGuiButton extends GuiButtonExt {
3635

3736
private Consumer<CwgGuiButton> onClick;
3837

@@ -52,20 +51,4 @@ public void onClick(Consumer<CwgGuiButton> handler) {
5251
}
5352
return false;
5453
}
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-
}
7154
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
*/
2424
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component;
2525

26+
import io.github.opencubicchunks.cubicchunks.cubicgen.asm.mixin.common.accessor.IGuiLabel;
2627
import net.minecraft.client.Minecraft;
27-
import net.minecraft.client.gui.GuiButton;
28+
import net.minecraft.client.gui.GuiLabel;
2829
import net.minecraft.client.resources.I18n;
2930

30-
public class CwgGuiLabel extends GuiButton {
31+
public class CwgGuiLabel extends GuiLabel {
3132

32-
public CwgGuiLabel(String formatString, int color) {
33-
super(0, 0, 0, I18n.format(formatString));
34-
this.packedFGColour = color;
35-
this.height = Minecraft.getMinecraft().fontRenderer.FONT_HEIGHT;
33+
public CwgGuiLabel(String text, int xIn, int yIn, int widthIn, int heightIn, int colorIn) {
34+
super(Minecraft.getMinecraft().fontRenderer, 0, xIn, yIn, widthIn, heightIn, colorIn);
35+
setText(text);
3636
}
3737

38-
@Override public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
39-
if (this.visible) {
40-
McGuiRender.drawWidgetString(mc, this, packedFGColour, enabled, false);
38+
public void setText(String text) {
39+
((IGuiLabel) this).getLabels().clear();
40+
for (String s : I18n.format(text).split("\n")) {
41+
addLine(s);
4142
}
4243
}
4344
}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525

2626
import com.google.common.base.Converter;
2727
import net.minecraft.client.Minecraft;
28-
import net.minecraft.client.gui.GuiButton;
2928
import net.minecraft.client.renderer.GlStateManager;
3029
import net.minecraft.client.resources.I18n;
3130
import net.minecraft.util.math.MathHelper;
31+
import net.minecraftforge.fml.client.config.GuiButtonExt;
3232

3333
import java.util.function.Consumer;
3434
import java.util.function.Function;
3535

3636
// TODO: fix vanilla rendering for large widths
37-
public class CwgGuiSlider extends GuiButton {
37+
public class CwgGuiSlider extends GuiButtonExt {
3838

3939
private final String textFormat;
4040
private final Function<Double, String> valueToString;
@@ -72,20 +72,6 @@ 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-
8975
// this is actually a draw() method
9076

9177
@Override protected void mouseDragged(Minecraft mc, int mouseX, int mouseY) {

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

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,9 @@
2424
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component;
2525

2626
import net.minecraft.client.Minecraft;
27-
import net.minecraft.client.gui.Gui;
2827
import net.minecraft.client.gui.GuiButton;
29-
import net.minecraft.client.renderer.GlStateManager;
30-
import net.minecraft.util.ResourceLocation;
3128

3229
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-
}
6030

6131
public static void drawWidgetString(Minecraft mc, GuiButton btn, int colorOverride, boolean enabled, boolean hovered) {
6232
int color = 0xe0e0e0;
@@ -69,34 +39,4 @@ public static void drawWidgetString(Minecraft mc, GuiButton btn, int colorOverri
6939
}
7040
btn.drawString(mc.fontRenderer, btn.displayString, btn.x, btn.y + (btn.height - 8) / 2, color);
7141
}
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-
}
10242
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
*/
2424
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component;
2525

26-
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UIVerticalTableLayout.GridLocation;
2726
import io.github.opencubicchunks.cubicchunks.cubicgen.preset.wrapper.BlockStateDesc;
28-
import net.malisis.core.client.gui.component.UIComponent;
2927
import net.malisis.core.client.gui.component.container.UIContainer;
3028
import net.malisis.core.client.gui.component.decoration.UISeparator;
3129
import net.minecraft.init.Blocks;
@@ -128,8 +126,8 @@ public UIFlatTerrainLayer(FlatCubicGui guiFor, FlatLayersTab flatLayersTabFor, F
128126
}
129127

130128
private void updateLabels() {
131-
blockName.displayString = block.getBlockName();
132-
blockProperties.displayString = block.getBlockProperties();
129+
blockName.setText(block.getBlockName());
130+
blockProperties.setText(block.getBlockProperties());
133131
}
134132

135133
protected void removeLayer() {

0 commit comments

Comments
 (0)