Skip to content

Commit 04a5637

Browse files
committed
Port user function editor to vanilla, handle clipping
1 parent ef5c64f commit 04a5637

File tree

7 files changed

+247
-201
lines changed

7 files changed

+247
-201
lines changed

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

Lines changed: 58 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,65 +25,67 @@
2525

2626
import static java.lang.Math.max;
2727

28-
import net.malisis.core.client.gui.GuiRenderer;
29-
import net.malisis.core.client.gui.element.GuiShape;
30-
import net.malisis.core.client.gui.element.SimpleGuiShape;
31-
import net.malisis.core.renderer.RenderParameters;
32-
import net.malisis.core.renderer.element.Face;
33-
import net.malisis.core.renderer.element.Vertex;
34-
import net.malisis.core.renderer.font.FontOptions;
35-
import net.malisis.core.renderer.font.MalisisFont;
28+
import net.minecraft.client.Minecraft;
29+
import net.minecraft.client.gui.FontRenderer;
30+
import net.minecraft.client.renderer.BufferBuilder;
3631
import net.minecraft.client.renderer.GlStateManager;
32+
import net.minecraft.client.renderer.Tessellator;
33+
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
3734
import net.minecraft.util.math.MathHelper;
3835

3936
import java.math.BigInteger;
4037
import java.text.DecimalFormat;
4138

4239
public class DrawUtils {
4340

44-
public static void drawLineF(GuiRenderer render, float x1, float y1, float x2, float y2, int argb, float width) {
41+
public static void drawLineF(float x1, float y1, float x2, float y2, int argb, float width) {
4542
double dx = x2 - x1;
4643
double dy = y2 - y1;
4744
double lenInv = width / Math.sqrt(dx * dx + dy * dy);
4845
dx *= lenInv;
4946
dy *= lenInv;
5047

51-
GuiShape shape = directShape(
52-
new Vertex(x2 - dy, y2 + dx, 0),
53-
new Vertex(x2 + dy, y2 - dx, 0),
54-
new Vertex(x1 + dy, y1 - dx, 0),
55-
new Vertex(x1 - dy, y1 + dx, 0)
56-
);
57-
RenderParameters rp = new RenderParameters();
58-
rp.setColor(argb & 0xFFFFFF);
59-
rp.setAlpha(argb >>> 24);
60-
render.drawShape(shape, rp);
61-
}
62-
63-
public static void drawRectF(GuiRenderer render, float x1, float y1, float x2, float y2, int argb) {
64-
GuiShape shape = directShape(
65-
new Vertex(x1, y1, 0),
66-
new Vertex(x1, y2, 0),
67-
new Vertex(x2, y2, 0),
68-
new Vertex(x2, y1, 0)
69-
);
70-
RenderParameters rp = new RenderParameters();
71-
rp.setColor(argb & 0xFFFFFF);
72-
rp.setAlpha(argb >>> 24);
73-
render.drawShape(shape, rp);
48+
float alpha = (float) (argb >> 24 & 255) / 255.0F;
49+
float r = (float) (argb >> 16 & 255) / 255.0F;
50+
float g = (float) (argb >> 8 & 255) / 255.0F;
51+
float b = (float) (argb & 255) / 255.0F;
52+
Tessellator tessellator = Tessellator.getInstance();
53+
BufferBuilder buf = tessellator.getBuffer();
54+
GlStateManager.enableBlend();
55+
GlStateManager.disableTexture2D();
56+
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA,
57+
GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
58+
GlStateManager.color(r, g, b, alpha);
59+
buf.begin(7, DefaultVertexFormats.POSITION);
60+
buf.pos(x2 - dy, y2 + dx, 0.0D).endVertex();
61+
buf.pos(x2 + dy, y2 - dx, 0.0D).endVertex();
62+
buf.pos(x1 + dy, y1 - dx, 0.0D).endVertex();
63+
buf.pos(x1 - dy, y1 + dx, 0.0D).endVertex();
64+
tessellator.draw();
65+
GlStateManager.enableTexture2D();
66+
GlStateManager.disableBlend();
7467
}
7568

76-
public static GuiShape directShape(Vertex... vertices) {
77-
return new GuiShape(new Face(vertices)) {
78-
79-
@Override public void setSize(int i, int i1) {
80-
throw new UnsupportedOperationException();
81-
}
82-
83-
@Override public void scale(float v, float v1) {
84-
throw new UnsupportedOperationException();
85-
}
86-
};
69+
public static void drawRectF(float x1, float y1, float x2, float y2, int argb) {
70+
float alpha = (float) (argb >> 24 & 255) / 255.0F;
71+
float r = (float) (argb >> 16 & 255) / 255.0F;
72+
float g = (float) (argb >> 8 & 255) / 255.0F;
73+
float b = (float) (argb & 255) / 255.0F;
74+
Tessellator tessellator = Tessellator.getInstance();
75+
BufferBuilder buf = tessellator.getBuffer();
76+
GlStateManager.enableBlend();
77+
GlStateManager.disableTexture2D();
78+
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA,
79+
GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
80+
GlStateManager.color(r, g, b, alpha);
81+
buf.begin(7, DefaultVertexFormats.POSITION);
82+
buf.pos(x1, y1, 0.0D).endVertex();
83+
buf.pos(x1, y2, 0.0D).endVertex();
84+
buf.pos(x2, y2, 0.0D).endVertex();
85+
buf.pos(x2, y1, 0.0D).endVertex();
86+
tessellator.draw();
87+
GlStateManager.enableTexture2D();
88+
GlStateManager.disableBlend();
8789
}
8890

8991
private static String formatFloatX(double f) {
@@ -94,22 +96,18 @@ private static String formatFloatY(double f) {
9496
return new DecimalFormat("#.#####").format(f);
9597
}
9698

97-
public static void drawXScale(GuiRenderer render, int width, int height, double offsetX, double scaleX) {
99+
public static void drawXScale(int posX, int posY, int width, int height, double offsetX, double scaleX) {
98100
double blockLeft = posToX(width, 0, offsetX, scaleX);
99101
double blockRight = posToX(width, width, offsetX, scaleX);
100102

101-
FontOptions fo = new FontOptions.FontOptionsBuilder().color(0xFFFFFF).shadow(true).build();
102-
103+
FontRenderer font = Minecraft.getMinecraft().fontRenderer;
103104

104105
String maxFormatted = formatFloatX(max(blockLeft, blockRight));
105106
String minFormatted = formatFloatX(Math.min(blockLeft, blockRight));
106107
String withFractionFormatted = formatFloatX(Math.min(blockLeft, blockRight) < 0 ? -0.11111111 : 0.11111111);
107108
float entryWidth = max(
108-
max(
109-
MalisisFont.minecraftFont.getStringWidth(maxFormatted, fo),
110-
MalisisFont.minecraftFont.getStringWidth(minFormatted, fo)
111-
),
112-
MalisisFont.minecraftFont.getStringWidth(withFractionFormatted, fo)
109+
max(font.getStringWidth(maxFormatted), font.getStringWidth(minFormatted)),
110+
font.getStringWidth(withFractionFormatted)
113111
);
114112

115113
int count = max(1, (int) (width / entryWidth));
@@ -120,34 +118,25 @@ public static void drawXScale(GuiRenderer render, int width, int height, double
120118
double x = start + i * increment;
121119
int pos = (int) xToPos(width, x, offsetX, scaleX);
122120
String formatted = formatFloatX(x);
123-
int strWidth = (int) MalisisFont.minecraftFont.getStringWidth(formatted, fo) / 2;
121+
int strWidth = font.getStringWidth(formatted) / 2;
124122
int strPos = pos - strWidth + 1;
125123
if (strPos < 30) {
126124
continue;// avoid intersecting with y axis
127125
}
128-
render.drawText(MalisisFont.minecraftFont, formatted, strPos, height - 10, 0, fo);
126+
font.drawString(formatted, posX + strPos, posY + height - 10, 0xFFFFFFFF);
129127
}
130128

131-
render.next();
132129
GlStateManager.disableTexture2D();
133-
SimpleGuiShape shape = new SimpleGuiShape();
134-
shape.setSize(1, 2);
135-
RenderParameters rp = new RenderParameters();
136130
for (int i = 0; i < count; i++) {
137131
double x = start + i * increment;
138132
int pos = (int) xToPos(width, x, offsetX, scaleX);
139-
shape.storeState();
140-
shape.setPosition(pos, height - 1);
141-
render.drawShape(shape, rp);
142-
143-
shape.resetState();
133+
drawRectF(posX + pos, posY + height - 1, posX + pos + 1, posY + height + 1, 0xFFFFFFFF);
144134
}
145-
render.next();
146135
GlStateManager.enableTexture2D();
147136
}
148137

149138

150-
public static void drawYScale(GuiRenderer render, int width, int height, double offsetY, double scaleY) {
139+
public static void drawYScale(int posX, int posY, int width, int height, double offsetY, double scaleY) {
151140
double blockBottom = posToY(height, height, offsetY, scaleY);// bottom -> getHeight()
152141
double blockTop = posToY(height, 0, offsetY, scaleY);
153142

@@ -156,9 +145,9 @@ public static void drawYScale(GuiRenderer render, int width, int height, double
156145

157146
double start = Math.round(blockBottom / increment) * increment;
158147

159-
FontOptions fo = new FontOptions.FontOptionsBuilder().color(0xFFFFFF).shadow(true).build();
148+
FontRenderer font = Minecraft.getMinecraft().fontRenderer;
160149

161-
int maxSrtY = MathHelper.ceil(height - MalisisFont.minecraftFont.getStringHeight(fo));
150+
int maxSrtY = MathHelper.ceil(height - font.FONT_HEIGHT);
162151

163152
float[] yMarkYCoords = new float[count];
164153
for (int i = 0; i < count; i++) {
@@ -167,20 +156,18 @@ public static void drawYScale(GuiRenderer render, int width, int height, double
167156
if (pos < -1 || pos > height) {
168157
continue;
169158
}
170-
int strHeight = (int) (MalisisFont.minecraftFont.getStringHeight() / 2);
159+
int strHeight = font.FONT_HEIGHT / 2;
171160

172161
int yDraw = pos - strHeight;
173162
int yDrawStr = MathHelper.clamp(yDraw, 0, maxSrtY);
174163
yMarkYCoords[i] = pos;
175-
render.drawText(MalisisFont.minecraftFont, formatFloatY(y), 10, yDrawStr, 0, fo);
164+
font.drawString(formatFloatY(y), posX + 10, posY + yDrawStr, 0xFFFFFFFF);
176165
}
177166

178-
render.next();
179167
GlStateManager.disableTexture2D();
180168
for (float pos : yMarkYCoords) {
181-
DrawUtils.drawLineF(render, 0, pos, 4, pos, 0xFFFFFFFF, 1f);
169+
DrawUtils.drawLineF(posX, posY + pos, posX + 4, posY + pos, 0xFFFFFFFF, 1f);
182170
}
183-
render.next();
184171
GlStateManager.enableTexture2D();
185172
}
186173

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public abstract class CwgGuiComponent extends Gui implements ICwgGuiComponent {
3535

3636
private int x, y, width, height;
37-
private boolean enabled, visible, hovered;
37+
private boolean enabled = true, visible = true, hovered;
3838
private final List<String> tooltipLines = new ArrayList<>();
3939
private Consumer<? super CwgGuiComponent> onClick;
4040

@@ -128,7 +128,10 @@ public boolean isHovered() {
128128
}
129129

130130
@Override
131-
public boolean onMousePressed(Minecraft mc, int x, int y) {
131+
public boolean onMousePressed(Minecraft mc, int x, int y, int mouseButton) {
132+
if (mouseButton != 0) {
133+
return false;
134+
}
132135
if (onClick == null) {
133136
return false;
134137
}

0 commit comments

Comments
 (0)