Skip to content

Commit c6e1a01

Browse files
committed
added comments
1 parent 92f2de7 commit c6e1a01

9 files changed

Lines changed: 217 additions & 35 deletions

File tree

src/main/java/io/github/techstreet/dfscript/screen/script/ScriptAddActionScreen.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.github.techstreet.dfscript.script.Script;
77
import io.github.techstreet.dfscript.script.action.ScriptAction;
88
import io.github.techstreet.dfscript.script.action.ScriptActionCategory;
9+
import io.github.techstreet.dfscript.script.action.ScriptActionCategoryExtra;
910
import io.github.techstreet.dfscript.script.action.ScriptActionType;
1011
import io.github.techstreet.dfscript.script.event.ScriptEvent;
1112
import io.github.techstreet.dfscript.script.event.ScriptEventType;
@@ -36,6 +37,23 @@ public ScriptAddActionScreen(Script script, int insertIndex, ScriptActionCategor
3637
});
3738
widgets.add(item);
3839
x += 10;
40+
if (x >= size - 10) {
41+
x = 3;
42+
y += 10;
43+
}
44+
}
45+
}
46+
47+
if (category != null)
48+
{
49+
for(ScriptActionCategoryExtra extra : category.getExtras()) {
50+
CItem item = new CItem(x, y, extra.getIcon());
51+
item.setClickListener((btn) -> {
52+
script.getParts().add(insertIndex, extra.getPart());
53+
DFScript.MC.setScreen(new ScriptEditScreen(script));
54+
});
55+
widgets.add(item);
56+
x += 10;
3957
if (x >= size-10) {
4058
x = 3;
4159
y += 10;
@@ -70,6 +88,7 @@ private static int size(ScriptActionCategory category) {
7088
if (category == null) {
7189
amount = ScriptEventType.values().length;
7290
} else {
91+
amount += category.getExtras().size();
7392
for (ScriptActionType type : ScriptActionType.values()) {
7493
if (type.getCategory() == category) {
7594
amount++;

src/main/java/io/github/techstreet/dfscript/screen/script/ScriptEditScreen.java

Lines changed: 95 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
import io.github.techstreet.dfscript.DFScript;
44
import io.github.techstreet.dfscript.screen.CScreen;
5-
import io.github.techstreet.dfscript.screen.widget.CButton;
6-
import io.github.techstreet.dfscript.screen.widget.CItem;
7-
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
8-
import io.github.techstreet.dfscript.screen.widget.CText;
9-
import io.github.techstreet.dfscript.screen.widget.CTexturedButton;
10-
import io.github.techstreet.dfscript.screen.widget.CWidget;
5+
import io.github.techstreet.dfscript.screen.widget.*;
116
import io.github.techstreet.dfscript.script.Script;
7+
import io.github.techstreet.dfscript.script.ScriptComment;
128
import io.github.techstreet.dfscript.script.ScriptManager;
139
import io.github.techstreet.dfscript.script.ScriptPart;
1410
import io.github.techstreet.dfscript.script.action.ScriptAction;
@@ -20,20 +16,27 @@
2016
import net.minecraft.client.gui.DrawableHelper;
2117
import net.minecraft.client.sound.PositionedSoundInstance;
2218
import net.minecraft.client.util.math.MatrixStack;
19+
import net.minecraft.item.ItemStack;
20+
import net.minecraft.item.Items;
2321
import net.minecraft.sound.SoundEvents;
22+
import net.minecraft.text.Style;
2423
import net.minecraft.text.Text;
24+
import net.minecraft.text.TextColor;
25+
import net.minecraft.util.Formatting;
2526
import net.minecraft.util.Identifier;
2627

2728
public class ScriptEditScreen extends CScreen {
2829
private final Identifier identifier_main = new Identifier(DFScript.MOD_ID + ":wrench.png");
2930

3031
private final Script script;
3132
private static int scroll = 0;
33+
34+
private final static int width = 125;
3235
private final CScrollPanel panel;
3336
private final List<CWidget> contextMenu = new ArrayList<>();
3437

3538
public ScriptEditScreen(Script script) {
36-
super(125, 100);
39+
super(width, 100);
3740
this.script = script;
3841
panel = new CScrollPanel(0, 3, 120, 94);
3942

@@ -113,41 +116,24 @@ public boolean mouseClicked(double x, double y, int button) {
113116
panel.add(new CItem(5 + indent, y, sa.getType().getIcon()));
114117
panel.add(new CText(15 + indent, y + 2, Text.literal(sa.getType().getName())));
115118

116-
for (int i = 0; i < indent; i += 5) {
117-
int xpos = 8 + i;
118-
int ypos = y;
119-
panel.add(new CWidget() {
120-
@Override
121-
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
122-
DrawableHelper.fill(stack, xpos, ypos, xpos + 1, ypos + 8, 0xFF333333);
123-
}
124-
125-
@Override
126-
public Rectangle getBounds() {
127-
return new Rectangle(0,0,0,0);
128-
}
129-
});
130-
}
119+
createIndent(indent, y);
131120

132121
int currentIndex = index;
133-
panel.add(new CButton(5, y-1, 115, 10, "",() -> {}) {
122+
panel.add(new CButton(5, y - 1, 115, 10, "", () -> {
123+
}) {
134124
@Override
135125
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
136126
Rectangle b = getBounds();
137127
if (b.contains(mouseX, mouseY)) {
138128
int color = 0x33000000;
139129

140-
if(sa.getType().isDeprecated())
141-
{
130+
if (sa.getType().isDeprecated()) {
142131
color = 0x80FF0000;
143132
}
144133

145134
DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, color);
146-
}
147-
else
148-
{
149-
if(sa.getType().isDeprecated())
150-
{
135+
} else {
136+
if (sa.getType().isDeprecated()) {
151137
DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, 0x33FF0000);
152138
}
153139
}
@@ -156,7 +142,7 @@ public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
156142
@Override
157143
public boolean mouseClicked(double x, double y, int button) {
158144
if (getBounds().contains(x, y)) {
159-
DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f,1f));
145+
DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f, 1f));
160146

161147
if (button == 0) {
162148
if (sa.getType() != ScriptActionType.CLOSE_BRACKET) {
@@ -167,10 +153,10 @@ public boolean mouseClicked(double x, double y, int button) {
167153
CButton insertBefore = new CButton((int) x, (int) y, 40, 8, "Insert Before", () -> {
168154
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex));
169155
});
170-
CButton insertAfter = new CButton((int) x, (int) y+8, 40, 8, "Insert After", () -> {
156+
CButton insertAfter = new CButton((int) x, (int) y + 8, 40, 8, "Insert After", () -> {
171157
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex + 1));
172158
});
173-
CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> {
159+
CButton delete = new CButton((int) x, (int) y + 16, 40, 8, "Delete", () -> {
174160
script.getParts().remove(currentIndex);
175161
scroll = panel.getScroll();
176162
DFScript.MC.setScreen(new ScriptEditScreen(script));
@@ -193,6 +179,63 @@ public boolean mouseClicked(double x, double y, int button) {
193179
if (sa.getType().hasChildren()) {
194180
indent += 5;
195181
}
182+
} else if (part instanceof ScriptComment sc) {
183+
panel.add(new CItem(5 + indent, y, new ItemStack(Items.MAP).setCustomName(Text.literal("Comment").setStyle(Style.EMPTY.withItalic(false)))));
184+
185+
CTextField cTextField = new CTextField(sc.getComment(),15+indent, y-1, width-(15+indent)-5, 10, true);
186+
187+
cTextField.setChangedListener(() -> sc.setComment(cTextField.getText()));
188+
189+
panel.add(cTextField);
190+
191+
int currentIndex = index;
192+
193+
panel.add(new CButton(5, y-1, 115, 10, "",() -> {}) {
194+
@Override
195+
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
196+
Rectangle b = getBounds();
197+
198+
if (b.contains(mouseX, mouseY)) {
199+
int color = 0x33000000;
200+
201+
DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, color);
202+
}
203+
}
204+
205+
@Override
206+
public boolean mouseClicked(double x, double y, int button) {
207+
if (getBounds().contains(x, y)) {
208+
DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f,1f));
209+
210+
if (button != 0) {
211+
CButton insertBefore = new CButton((int) x, (int) y, 40, 8, "Insert Before", () -> {
212+
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex));
213+
});
214+
CButton insertAfter = new CButton((int) x, (int) y+8, 40, 8, "Insert After", () -> {
215+
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex + 1));
216+
});
217+
CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> {
218+
script.getParts().remove(currentIndex);
219+
scroll = panel.getScroll();
220+
DFScript.MC.setScreen(new ScriptEditScreen(script));
221+
});
222+
DFScript.MC.send(() -> {
223+
panel.add(insertBefore);
224+
panel.add(insertAfter);
225+
panel.add(delete);
226+
contextMenu.add(insertBefore);
227+
contextMenu.add(insertAfter);
228+
contextMenu.add(delete);
229+
});
230+
231+
return true;
232+
}
233+
}
234+
return false;
235+
}
236+
});
237+
238+
createIndent(indent, y);
196239
} else {
197240
throw new IllegalArgumentException("Unknown script part type");
198241
}
@@ -209,6 +252,25 @@ public boolean mouseClicked(double x, double y, int button) {
209252
panel.setScroll(scroll);
210253
}
211254

255+
public void createIndent(int indent, int y)
256+
{
257+
for (int i = 0; i < indent; i += 5) {
258+
int xpos = 8 + i;
259+
int ypos = y;
260+
panel.add(new CWidget() {
261+
@Override
262+
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
263+
DrawableHelper.fill(stack, xpos, ypos, xpos + 1, ypos + 8, 0xFF333333);
264+
}
265+
266+
@Override
267+
public Rectangle getBounds() {
268+
return new Rectangle(0, 0, 0, 0);
269+
}
270+
});
271+
}
272+
}
273+
212274
@Override
213275
public void close() {
214276
scroll = panel.getScroll();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public void execute(ScriptTask task) {
116116
break;
117117
}
118118
}
119+
} else if (nextPart instanceof ScriptComment) {
120+
// ignore comments
119121
} else {
120122
throw new IllegalStateException("Unexpected script part type: " + nextPart.getClass().getName());
121123
}
@@ -160,6 +162,8 @@ public void execute(ScriptTask task) {
160162
context.breakLoop(-1);
161163
task.stack().pop(); // don't use endScope() because of the fact that endScope runs the condition to see if it is false before ending the scope
162164
}
165+
} else if (part instanceof ScriptComment) {
166+
// ignore the comment lol
163167
} else {
164168
throw new IllegalArgumentException("Invalid script part");
165169
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.techstreet.dfscript.script;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonSerializationContext;
6+
import com.google.gson.JsonSerializer;
7+
import io.github.techstreet.dfscript.event.system.Event;
8+
import io.github.techstreet.dfscript.script.action.ScriptActionType;
9+
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
10+
import io.github.techstreet.dfscript.script.argument.ScriptConfigArgument;
11+
import io.github.techstreet.dfscript.script.execution.ScriptActionContext;
12+
import io.github.techstreet.dfscript.script.execution.ScriptContext;
13+
import io.github.techstreet.dfscript.script.execution.ScriptScopeVariables;
14+
import io.github.techstreet.dfscript.script.execution.ScriptTask;
15+
16+
import java.lang.reflect.Type;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Objects;
20+
import java.util.function.Consumer;
21+
22+
public class ScriptComment implements ScriptPart {
23+
24+
private String comment;
25+
26+
public ScriptComment(String comment) {
27+
this.comment = comment;
28+
}
29+
30+
public ScriptComment setComment(String comment) {
31+
this.comment = comment;
32+
33+
return this;
34+
}
35+
36+
public String getComment() {
37+
return comment;
38+
}
39+
40+
@Override
41+
public ScriptGroup getGroup() {
42+
return ScriptGroup.COMMENT;
43+
}
44+
45+
public static class Serializer implements JsonSerializer<ScriptComment> {
46+
47+
@Override
48+
public JsonElement serialize(ScriptComment src, Type typeOfSrc, JsonSerializationContext context) {
49+
JsonObject obj = new JsonObject();
50+
obj.addProperty("type", "comment");
51+
obj.addProperty("comment", src.getComment());
52+
return obj;
53+
}
54+
}
55+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public enum ScriptGroup {
66
ACTION,
77
CONDITION,
88
REPETITION,
9-
EVENT
9+
EVENT,
10+
11+
COMMENT
1012
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class ScriptManager implements Loadable {
4646
.registerTypeAdapter(ScriptNamedOption.class, new ScriptNamedOption.Serializer())
4747
.registerTypeAdapter(ScriptAction.class, new ScriptAction.Serializer())
4848
.registerTypeAdapter(ScriptEvent.class, new ScriptEvent.Serializer())
49+
.registerTypeAdapter(ScriptComment.class, new ScriptComment.Serializer())
4950
.create();
5051

5152
public ScriptManager() {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public ScriptPart deserialize(JsonElement json, Type typeOfT, JsonDeserializatio
3737
String event = obj.get("event").getAsString();
3838
return new ScriptEvent(ScriptEventType.valueOf(event));
3939
}
40+
case "comment" -> {
41+
String comment = obj.get("comment").getAsString();
42+
return new ScriptComment(comment);
43+
}
4044
default -> throw new JsonParseException("Unknown script part type: " + type);
4145
}
4246
}

src/main/java/io/github/techstreet/dfscript/script/action/ScriptActionCategory.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package io.github.techstreet.dfscript.script.action;
22

3+
import io.github.techstreet.dfscript.script.ScriptComment;
34
import net.minecraft.item.Item;
45
import net.minecraft.item.ItemStack;
56
import net.minecraft.item.Items;
67
import net.minecraft.text.Style;
78
import net.minecraft.text.Text;
9+
import org.slf4j.helpers.FormattingTuple;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
813

914
public enum ScriptActionCategory {
1015

1116
VISUALS("Visuals", Items.ENDER_EYE),
1217
ACTIONS("Actions", Items.PLAYER_HEAD),
13-
MISC("Misc", Items.COMPASS),
18+
MISC("Misc", Items.COMPASS, List.of(
19+
new ScriptActionCategoryExtra(new ItemStack(Items.MAP).setCustomName(Text.literal("Comment").setStyle(Style.EMPTY.withItalic(false))), (Void) -> new ScriptComment(""))
20+
)),
1421
VARIABLES("Variables", Items.IRON_INGOT),
1522
NUMBERS("Numbers", Items.SLIME_BALL),
1623
LISTS("Lists", Items.BOOKSHELF),
@@ -22,12 +29,24 @@ public enum ScriptActionCategory {
2229

2330
private final ItemStack icon;
2431

32+
private List<ScriptActionCategoryExtra> extras = new ArrayList<>();
33+
2534
ScriptActionCategory(String name, Item icon) {
2635
this.icon = new ItemStack(icon);
2736
this.icon.setCustomName(Text.literal(name).fillStyle(Style.EMPTY.withItalic(false)));
2837
}
2938

39+
ScriptActionCategory(String name, Item icon, List<ScriptActionCategoryExtra> extras) {
40+
this.icon = new ItemStack(icon);
41+
this.icon.setCustomName(Text.literal(name).fillStyle(Style.EMPTY.withItalic(false)));
42+
this.extras = extras;
43+
}
44+
3045
public ItemStack getIcon() {
3146
return icon;
3247
}
48+
public List<ScriptActionCategoryExtra> getExtras()
49+
{
50+
return extras;
51+
}
3352
}

0 commit comments

Comments
 (0)