Skip to content

Commit 80ee54f

Browse files
committed
added the ability to hide snippets of code
1 parent 99d835b commit 80ee54f

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

src/main/java/io/github/techstreet/dfscript/script/ScriptSnippet.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.github.techstreet.dfscript.screen.script.ScriptPartCategoryScreen;
99
import io.github.techstreet.dfscript.screen.widget.CButton;
1010
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
11+
import io.github.techstreet.dfscript.screen.widget.CText;
1112
import io.github.techstreet.dfscript.screen.widget.CWidget;
1213
import io.github.techstreet.dfscript.script.action.ScriptActionType;
1314
import io.github.techstreet.dfscript.script.action.ScriptBuiltinAction;
@@ -22,10 +23,13 @@
2223
import io.github.techstreet.dfscript.script.render.ScriptPartRender;
2324
import io.github.techstreet.dfscript.script.repetitions.ScriptBuiltinRepetition;
2425
import io.github.techstreet.dfscript.script.repetitions.ScriptRepetitionType;
26+
import io.github.techstreet.dfscript.util.RenderUtil;
2527
import net.minecraft.client.gui.DrawableHelper;
2628
import net.minecraft.client.sound.PositionedSoundInstance;
2729
import net.minecraft.client.util.math.MatrixStack;
2830
import net.minecraft.sound.SoundEvents;
31+
import net.minecraft.text.Text;
32+
import net.minecraft.util.Formatting;
2933

3034
import javax.naming.Context;
3135
import java.awt.*;
@@ -34,6 +38,7 @@
3438
import java.util.List;
3539

3640
public class ScriptSnippet extends ArrayList<ScriptPart> {
41+
boolean hidden = false;
3742
ScriptSnippet() {
3843

3944
}
@@ -44,8 +49,44 @@ public void run(ScriptTask task, ScriptScopeParent parent)
4449
}
4550

4651
public int create(CScrollPanel panel, int y, int indent, Script script) {
47-
int index = 0;
4852
ScriptSnippet thisSnippet = this;
53+
panel.add(new CButton(3, y, 2, 8, "", () -> {
54+
thisSnippet.hidden = !thisSnippet.hidden;
55+
if(DFScript.MC.currentScreen instanceof ScriptEditScreen e) {
56+
e.reload();
57+
}
58+
}) {
59+
@Override
60+
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
61+
Rectangle b = getBounds();
62+
63+
int color = 0xFF323232;
64+
65+
if(b.contains(mouseX, mouseY)) {
66+
color = 0xFF707070;
67+
}
68+
69+
if(thisSnippet.hidden) {
70+
RenderUtil.renderLine(stack, b.x, b.y, b.x+b.width, b.y+(b.height/2f), color, 0.5f);
71+
RenderUtil.renderLine(stack, b.x, b.y+b.height, b.x+b.width, b.y+(b.height/2f), color, 0.5f);
72+
}
73+
else {
74+
RenderUtil.renderLine(stack, b.x, b.y, b.x+(b.width/2f), b.y+b.height, color, 0.5f);
75+
RenderUtil.renderLine(stack, b.x+b.width, b.y, b.x+(b.width/2f), b.y+b.height, color, 0.5f);
76+
}
77+
}
78+
});
79+
80+
if(hidden) {
81+
ScriptPartRender.createIndent(panel, indent, y, 8);
82+
83+
panel.add(new CText(15 + indent * 5, y + 2, Text.literal("...")));
84+
85+
return y+10;
86+
}
87+
88+
int index = 0;
89+
4990
for(ScriptPart part : this) {
5091
ScriptPartRender render = new ScriptPartRender();
5192
part.create(render, script);
@@ -215,6 +256,10 @@ public ScriptSnippet deserialize(JsonElement json, Type typeOfT, JsonDeserializa
215256
snippet.add(context.deserialize(element, ScriptPart.class));
216257
}
217258

259+
if(obj.has("hidden")) {
260+
snippet.hidden = obj.get("hidden").getAsBoolean();
261+
}
262+
218263
return snippet;
219264
}
220265

@@ -228,6 +273,8 @@ public JsonElement serialize(ScriptSnippet src, Type typeOfSrc, JsonSerializatio
228273
}
229274

230275
obj.add("parts", parts);
276+
obj.addProperty("hidden", src.hidden);
277+
231278
return obj;
232279
}
233280
}

src/main/java/io/github/techstreet/dfscript/util/RenderUtil.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
import java.util.ArrayList;
66
import java.util.List;
77
import net.minecraft.client.MinecraftClient;
8-
import net.minecraft.client.render.BufferBuilder;
9-
import net.minecraft.client.render.GameRenderer;
10-
import net.minecraft.client.render.Tessellator;
11-
import net.minecraft.client.render.VertexFormat;
12-
import net.minecraft.client.render.VertexFormats;
8+
import net.minecraft.client.gui.DrawableHelper;
9+
import net.minecraft.client.render.*;
1310
import net.minecraft.client.render.item.ItemRenderer;
1411
import net.minecraft.client.toast.SystemToast;
1512
import net.minecraft.client.util.math.MatrixStack;
1613
import net.minecraft.item.ItemStack;
1714
import net.minecraft.text.MutableText;
1815
import net.minecraft.text.Text;
1916
import net.minecraft.util.Identifier;
17+
import net.minecraft.util.math.Matrix4f;
2018
import net.minecraft.util.math.Vector4f;
2119
import org.lwjgl.opengl.GL11;
2220

@@ -179,4 +177,53 @@ public static void sendToaster(String title, String description, SystemToast.Typ
179177
public static void sendToaster(MutableText title, MutableText description, SystemToast.Type type) {
180178
MinecraftClient.getInstance().getToastManager().add(new SystemToast(type, title, description));
181179
}
180+
181+
static int renderSteps = 100;
182+
183+
public static void renderLine(MatrixStack stack, float x1, float y1, float x2, float y2, int color, float size) {
184+
float stepX = (x2-x1)/renderSteps;
185+
float stepY = (y2-y1)/renderSteps;
186+
187+
for(int i = 0; i <= renderSteps; i++) {
188+
fill(stack, (x1-(size/2)), (y1-(size/2)), (x1+(size/2)), (y1+(size/2)), color);
189+
190+
x1 += stepX;
191+
y1 += stepY;
192+
}
193+
}
194+
195+
// these functions exist because fabric api dumb and used an integer for positions in the DrawableHelper class...
196+
public static void fill(MatrixStack stack, float x1, float y1, float x2, float y2, int color) {
197+
fill(stack.peek().getPositionMatrix(), x1, y1, x2, y2, color);
198+
}
199+
public static void fill(Matrix4f matrix, float x1, float y1, float x2, float y2, int color) {
200+
float i;
201+
if (x1 < x2) {
202+
i = x1;
203+
x1 = x2;
204+
x2 = i;
205+
}
206+
if (y1 < y2) {
207+
i = y1;
208+
y1 = y2;
209+
y2 = i;
210+
}
211+
float f = (float)(color >> 24 & 0xFF) / 255.0f;
212+
float g = (float)(color >> 16 & 0xFF) / 255.0f;
213+
float h = (float)(color >> 8 & 0xFF) / 255.0f;
214+
float j = (float)(color & 0xFF) / 255.0f;
215+
BufferBuilder bufferBuilder = Tessellator.getInstance().getBuffer();
216+
RenderSystem.enableBlend();
217+
RenderSystem.disableTexture();
218+
RenderSystem.defaultBlendFunc();
219+
RenderSystem.setShader(GameRenderer::getPositionColorShader);
220+
bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
221+
bufferBuilder.vertex(matrix, x1, y2, 0.0f).color(g, h, j, f).next();
222+
bufferBuilder.vertex(matrix, x2, y2, 0.0f).color(g, h, j, f).next();
223+
bufferBuilder.vertex(matrix, x2, y1, 0.0f).color(g, h, j, f).next();
224+
bufferBuilder.vertex(matrix, x1, y1, 0.0f).color(g, h, j, f).next();
225+
BufferRenderer.drawWithShader(bufferBuilder.end());
226+
RenderSystem.enableTexture();
227+
RenderSystem.disableBlend();
228+
}
182229
}

0 commit comments

Comments
 (0)