Skip to content

Commit c2a03b3

Browse files
committed
3 new Script Options
+ ScriptIntOption + ScriptFloatOption + ScriptKeyOption * made removing an option remove all it's references to it
1 parent 1533341 commit c2a03b3

File tree

9 files changed

+355
-1
lines changed

9 files changed

+355
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public boolean mouseClicked(double x, double y, int button) {
9393
DFScript.MC.setScreen(new ScriptAddSettingScreen(script, finalIndex + 1));
9494
});
9595
CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> {
96+
script.removeOption(script.getOptions().get(finalIndex).getName());
9697
script.getOptions().remove(finalIndex);
9798
scroll = panel.getScroll();
9899
DFScript.MC.setScreen(new ScriptSettingsScreen(script, true));
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package io.github.techstreet.dfscript.screen.widget;
2+
3+
import io.github.techstreet.dfscript.DFScript;
4+
import io.github.techstreet.dfscript.util.RenderUtil;
5+
import net.minecraft.client.font.TextRenderer;
6+
import net.minecraft.client.gui.DrawableHelper;
7+
import net.minecraft.client.util.InputUtil;
8+
import net.minecraft.client.util.math.MatrixStack;
9+
import net.minecraft.util.math.Vector4f;
10+
11+
import java.awt.*;
12+
13+
public class CKeyField implements CWidget {
14+
15+
final int x, y, width, height;
16+
17+
boolean selected;
18+
boolean editable;
19+
public int textColor = 0xFFFFFFFF;
20+
InputUtil.Key key;
21+
Runnable changedListener;
22+
23+
public CKeyField(int x, int y, int width, int height, boolean editable) {
24+
this(x, y, width, height, editable, null);
25+
}
26+
27+
public CKeyField(int x, int y, int width, int height, boolean editable, InputUtil.Key key) {
28+
this.key = key;
29+
this.x = x;
30+
this.y = y;
31+
this.width = width;
32+
this.height = height;
33+
this.editable = editable;
34+
}
35+
36+
@Override
37+
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
38+
stack.push();
39+
stack.translate(x, y, 0);
40+
41+
DrawableHelper.fill(stack, 0, 0, width, height, 0xFF888888);
42+
DrawableHelper.fill(stack, 1, 1, width - 1, height - 1, 0xFF000000);
43+
44+
Vector4f begin = new Vector4f(0, 0, 1, 1);
45+
Vector4f end = new Vector4f(width, height, 1, 1);
46+
begin.transform(stack.peek().getPositionMatrix());
47+
end.transform(stack.peek().getPositionMatrix());
48+
49+
int guiScale = (int) DFScript.MC.getWindow().getScaleFactor();
50+
RenderUtil.pushScissor(
51+
(int) begin.getX()*guiScale,
52+
(int) begin.getY()*guiScale,
53+
(int) (end.getX() - begin.getX())*guiScale,
54+
(int) (end.getY() - begin.getY())*guiScale
55+
);
56+
57+
stack.translate(2, 2, 0);
58+
stack.scale(0.5f, 0.5f, 0);
59+
60+
TextRenderer f = DFScript.MC.textRenderer;
61+
62+
stack.push();
63+
64+
String line = null;
65+
int color = textColor;
66+
67+
if(key != null) {
68+
line = key.getLocalizedText().getString();
69+
}
70+
71+
if(editable && selected) {
72+
color = 0xFFFF00;
73+
}
74+
75+
if(line != null) {
76+
f.draw(stack, line, 0, 0, color);
77+
}
78+
79+
stack.translate(0, f.fontHeight, 0);
80+
81+
stack.pop();
82+
stack.pop();
83+
RenderUtil.popScissor();
84+
}
85+
86+
@Override
87+
public boolean mouseClicked(double x, double y, int button) {
88+
if (editable) {
89+
if (button == 0) {
90+
if (x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height) {
91+
this.selected = true;
92+
}
93+
else {
94+
this.selected = false;
95+
}
96+
}
97+
}
98+
else {
99+
this.selected = false;
100+
}
101+
return false;
102+
}
103+
104+
@Override
105+
public void keyPressed(int keyCode, int scanCode, int modifiers) {
106+
if(editable && selected) {
107+
if(keyCode != -1) {
108+
if(keyCode == 10) {
109+
key = null;
110+
}
111+
else
112+
{
113+
key = InputUtil.fromKeyCode(keyCode, scanCode);
114+
}
115+
116+
changedListener.run();
117+
}
118+
119+
selected = false;
120+
}
121+
}
122+
123+
@Override
124+
public Rectangle getBounds() {
125+
return new Rectangle(x, y, width, height);
126+
}
127+
128+
public void setChangedListener(Runnable r) {
129+
changedListener = r;
130+
}
131+
132+
public InputUtil.Key getKey() {
133+
return key;
134+
}
135+
136+
public void setKey(InputUtil.Key k) {
137+
key = k;
138+
}
139+
}
140+

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ public void replaceOption(String oldOption, String newOption) {
330330
}
331331
}
332332

333+
public void removeOption(String option) {
334+
for(ScriptPart part : getParts()) {
335+
if(part instanceof ScriptAction a) {
336+
a.removeConfigArguments(option);
337+
}
338+
}
339+
}
340+
333341
public static class Serializer implements JsonSerializer<Script>, JsonDeserializer<Script> {
334342
@Override
335343
public Script deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
import io.github.techstreet.dfscript.script.execution.ScriptContext;
1515
import io.github.techstreet.dfscript.script.execution.ScriptScopeVariables;
1616
import io.github.techstreet.dfscript.script.execution.ScriptTask;
17+
import io.github.techstreet.dfscript.script.options.ScriptNamedOption;
18+
import io.github.techstreet.dfscript.util.chat.ChatUtil;
19+
1720
import java.lang.reflect.Type;
1821
import java.util.HashMap;
1922
import java.util.List;
23+
import java.util.Objects;
2024
import java.util.function.Consumer;
2125

2226
public class ScriptAction implements ScriptPart {
@@ -73,6 +77,23 @@ public void updateConfigArguments(String oldOption, String newOption) {
7377
}
7478
}
7579

80+
public void removeConfigArguments(String option) {
81+
int index = 0;
82+
83+
List<ScriptArgument> argList = getArguments();
84+
85+
while(index < argList.size()) {
86+
if (argList.get(index) instanceof ScriptConfigArgument carg) {
87+
if(Objects.equals(carg.getName(), option))
88+
{
89+
argList.remove(index);
90+
continue;
91+
}
92+
}
93+
index++;
94+
}
95+
}
96+
7697
public static class Serializer implements JsonSerializer<ScriptAction> {
7798

7899
@Override
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.techstreet.dfscript.script.options;
2+
3+
import com.google.gson.JsonPrimitive;
4+
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
5+
import io.github.techstreet.dfscript.screen.widget.CTextField;
6+
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
7+
import io.github.techstreet.dfscript.script.argument.ScriptNumberArgument;
8+
import net.minecraft.item.Item;
9+
import net.minecraft.item.Items;
10+
11+
public class ScriptFloatOption implements ScriptOption {
12+
13+
double value = 0;
14+
15+
public ScriptFloatOption(double value) {
16+
this.value = value;
17+
}
18+
19+
public ScriptFloatOption() {}
20+
21+
@Override
22+
public ScriptArgument getValue() {
23+
return new ScriptNumberArgument(value);
24+
}
25+
26+
@Override
27+
public String getName() { return "Floating-Point Value"; }
28+
29+
@Override
30+
public int create(CScrollPanel panel, int x, int y, int width) {
31+
CTextField field = new CTextField(String.valueOf(value), x, y, width, 10, true);
32+
field.setChangedListener(() -> {
33+
try {
34+
value = Double.parseDouble(field.getText());
35+
field.textColor = 0xFFFFFF;
36+
}
37+
catch(Exception e) {
38+
field.textColor = 0xFF3333;
39+
}
40+
});
41+
panel.add(field);
42+
43+
return y + 12;
44+
}
45+
46+
@Override
47+
public Item getIcon() {
48+
return Items.SLIME_BLOCK;
49+
}
50+
51+
@Override
52+
public String getType() {
53+
return "FLOAT";
54+
}
55+
56+
@Override
57+
public JsonPrimitive getJsonPrimitive() {
58+
return new JsonPrimitive(value);
59+
}
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.techstreet.dfscript.script.options;
2+
3+
import com.google.gson.JsonPrimitive;
4+
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
5+
import io.github.techstreet.dfscript.screen.widget.CTextField;
6+
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
7+
import io.github.techstreet.dfscript.script.argument.ScriptNumberArgument;
8+
import io.github.techstreet.dfscript.script.argument.ScriptTextArgument;
9+
import net.minecraft.item.Item;
10+
import net.minecraft.item.Items;
11+
12+
public class ScriptIntOption implements ScriptOption {
13+
14+
int value = 0;
15+
16+
public ScriptIntOption(int value) {
17+
this.value = value;
18+
}
19+
20+
public ScriptIntOption() {}
21+
22+
@Override
23+
public ScriptArgument getValue() {
24+
return new ScriptNumberArgument(value);
25+
}
26+
27+
@Override
28+
public String getName() { return "Integer"; }
29+
30+
@Override
31+
public int create(CScrollPanel panel, int x, int y, int width) {
32+
CTextField field = new CTextField(String.valueOf(value), x, y, width, 10, true);
33+
field.setChangedListener(() -> {
34+
try {
35+
value = Integer.parseInt(field.getText());
36+
field.textColor = 0xFFFFFF;
37+
}
38+
catch(Exception e) {
39+
field.textColor = 0xFF3333;
40+
}
41+
});
42+
panel.add(field);
43+
44+
return y + 12;
45+
}
46+
47+
@Override
48+
public Item getIcon() {
49+
return Items.SLIME_BALL;
50+
}
51+
52+
@Override
53+
public String getType() {
54+
return "INT";
55+
}
56+
57+
@Override
58+
public JsonPrimitive getJsonPrimitive() {
59+
return new JsonPrimitive(value);
60+
}
61+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.github.techstreet.dfscript.script.options;
2+
3+
import com.google.gson.JsonPrimitive;
4+
import io.github.techstreet.dfscript.screen.widget.CKeyField;
5+
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
6+
import io.github.techstreet.dfscript.screen.widget.CTextField;
7+
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
8+
import io.github.techstreet.dfscript.script.argument.ScriptNumberArgument;
9+
import net.minecraft.client.util.InputUtil;
10+
import net.minecraft.item.Item;
11+
import net.minecraft.item.Items;
12+
13+
public class ScriptKeyOption implements ScriptOption {
14+
15+
InputUtil.Key value = null;
16+
17+
public ScriptKeyOption(InputUtil.Key value) {
18+
this.value = value;
19+
}
20+
21+
public ScriptKeyOption() {}
22+
23+
@Override
24+
public ScriptArgument getValue() {
25+
return new ScriptNumberArgument(value.getCode());
26+
}
27+
28+
@Override
29+
public String getName() { return "Key"; }
30+
31+
@Override
32+
public int create(CScrollPanel panel, int x, int y, int width) {
33+
CKeyField field = new CKeyField(x, y, width, 10, true, value);
34+
field.setChangedListener(() -> {
35+
value = field.getKey();
36+
});
37+
panel.add(field);
38+
39+
return y + 12;
40+
}
41+
42+
@Override
43+
public Item getIcon() {
44+
return Items.STONE_BUTTON;
45+
}
46+
47+
@Override
48+
public String getType() {
49+
return "KEY";
50+
}
51+
52+
@Override
53+
public JsonPrimitive getJsonPrimitive() {
54+
return new JsonPrimitive(value.getTranslationKey());
55+
}
56+
}

src/main/java/io/github/techstreet/dfscript/script/options/ScriptNamedOption.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.techstreet.dfscript.script.ScriptPart;
88
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
99
import io.github.techstreet.dfscript.util.chat.ChatUtil;
10+
import net.minecraft.client.util.InputUtil;
1011
import net.minecraft.item.ItemStack;
1112
import net.minecraft.text.Style;
1213
import net.minecraft.text.Text;
@@ -62,6 +63,9 @@ public ScriptNamedOption deserialize(JsonElement json, Type typeOfT, JsonDeseria
6263
switch(type)
6364
{
6465
case "TEXT" -> option = new ScriptTextOption(object.get("value").getAsString());
66+
case "INT" -> option = new ScriptIntOption(object.get("value").getAsInt());
67+
case "FLOAT" -> option = new ScriptFloatOption(object.get("value").getAsDouble());
68+
case "KEY" -> option = new ScriptKeyOption(InputUtil.fromTranslationKey(object.get("value").getAsString()));
6569
default -> throw new JsonParseException("Unknown option type: " + type);
6670
}
6771

0 commit comments

Comments
 (0)