Skip to content

Commit 7944bd5

Browse files
committed
Futher work on script configuration
Added a way to add options Also fixed multiple text fields in a screen all responding to user input
1 parent 598eef8 commit 7944bd5

File tree

5 files changed

+133
-5
lines changed

5 files changed

+133
-5
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.github.techstreet.dfscript.screen.script;
2+
3+
import io.github.techstreet.dfscript.DFScript;
4+
import io.github.techstreet.dfscript.screen.CScreen;
5+
import io.github.techstreet.dfscript.screen.widget.CItem;
6+
import io.github.techstreet.dfscript.script.Script;
7+
import io.github.techstreet.dfscript.script.options.ScriptOptionEnum;
8+
import io.github.techstreet.dfscript.util.chat.ChatUtil;
9+
public class ScriptAddSettingScreen extends CScreen {
10+
private static final int size;
11+
12+
static {
13+
size = (int) (Math.ceil(Math.sqrt(ScriptOptionEnum.values().length)) * 10)+4;
14+
}
15+
16+
private final Script script;
17+
18+
public ScriptAddSettingScreen(Script script, int pos) {
19+
super(size, size);
20+
this.script = script;
21+
22+
int x = 3;
23+
int y = 3;
24+
25+
for(ScriptOptionEnum option : ScriptOptionEnum.values())
26+
{
27+
CItem citem = new CItem(x, y, option.getIcon());
28+
29+
citem.setClickListener((a) -> {
30+
try {
31+
script.addOption(pos, option.getOptionType().getConstructor().newInstance());
32+
} catch (Exception e) {
33+
ChatUtil.error(String.valueOf(e.getCause()));
34+
}
35+
36+
DFScript.MC.setScreen(new ScriptSettingsScreen(script));
37+
});
38+
39+
widgets.add(citem);
40+
41+
x += 10;
42+
if (x >= size - 10) {
43+
x = 3;
44+
y += 10;
45+
}
46+
}
47+
}
48+
49+
@Override
50+
public void close() {
51+
DFScript.MC.setScreen(new ScriptSettingsScreen(script));
52+
}
53+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
56
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
67
import io.github.techstreet.dfscript.screen.widget.CTextField;
78
import io.github.techstreet.dfscript.screen.widget.CWidget;
@@ -42,6 +43,12 @@ public ScriptSettingsScreen(Script script) {
4243
index++;
4344
}
4445

46+
CButton add = new CButton(37, y, 46, 8, "Add", () -> {
47+
DFScript.MC.setScreen(new ScriptAddSettingScreen(script, script.getOptions().size()));
48+
});
49+
50+
panel.add(add);
51+
4552
panel.setScroll(scroll);
4653
}
4754

src/main/java/io/github/techstreet/dfscript/screen/widget/CTextField.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
public class CTextField implements CWidget {
1313

1414
final int x, y, width, height;
15+
16+
boolean selected;
1517
boolean editable;
1618
public int textColor = 0xFFFFFFFF;
1719
String text;
@@ -28,6 +30,7 @@ public CTextField(String text, int x, int y, int width, int height, boolean edit
2830
this.width = width;
2931
this.height = height;
3032
this.editable = editable;
33+
this.selected = false;
3134
}
3235

3336
@Override
@@ -91,7 +94,7 @@ public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
9194
}
9295
stack.pop();
9396

94-
if (editable) {
97+
if (editable && selected) {
9598
int cursorLine = getCursorLineIndex();
9699
int cursorLinePos = getIndexInCursorLine();
97100

@@ -111,7 +114,7 @@ public void charTyped(char ch, int keyCode) {
111114
return;
112115
}
113116

114-
if (editable) {
117+
if (editable && selected) {
115118
if (hasSelection) {
116119
int selectionStart = Math.min(selectionPos, cursorPos);
117120
int selectionEnd = Math.max(selectionPos, cursorPos);
@@ -128,7 +131,7 @@ public void charTyped(char ch, int keyCode) {
128131

129132
@Override
130133
public void keyPressed(int keyCode, int scanCode, int modifiers) {
131-
if (editable) {
134+
if (editable && selected) {
132135
String lastText = text;
133136
TextRenderer f = DFScript.MC.textRenderer;
134137
boolean createSelection = modifiers != 0;
@@ -258,6 +261,7 @@ public boolean mouseClicked(double x, double y, int button) {
258261
if (editable) {
259262
if (button == 0) {
260263
if (x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height) {
264+
this.selected = true;
261265
TextRenderer f = DFScript.MC.textRenderer;
262266

263267
x -= 1 + this.x;
@@ -275,16 +279,24 @@ public boolean mouseClicked(double x, double y, int button) {
275279
if (hasSelection) {
276280
hasSelection = false;
277281
}
278-
return true;
282+
//return true;
283+
}
284+
else
285+
{
286+
this.selected = false;
279287
}
280288
}
281289
}
290+
else
291+
{
292+
this.selected = false;
293+
}
282294
return false;
283295
}
284296

285297
@Override
286298
public void mouseScrolled(double mouseX, double mouseY, double amount) {
287-
if (editable) {
299+
if (editable && selected) {
288300
scroll += amount * 5;
289301
TextRenderer f = DFScript.MC.textRenderer;
290302
scroll = Math.min(0, Math.max(scroll, -(getLines().length + 1) * f.fontHeight / 2 + height - 2));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ public List<ScriptOption> getOptions() {
266266
return options;
267267
}
268268

269+
public void addOption(int pos, ScriptOption option) {
270+
options.add(pos, option);
271+
}
272+
269273
public static class Serializer implements JsonSerializer<Script>, JsonDeserializer<Script> {
270274
@Override
271275
public Script deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.techstreet.dfscript.script.options;
2+
3+
import io.github.techstreet.dfscript.script.Script;
4+
import net.minecraft.item.Item;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.item.Items;
7+
import net.minecraft.nbt.NbtElement;
8+
import net.minecraft.nbt.NbtList;
9+
import net.minecraft.nbt.NbtString;
10+
import net.minecraft.text.Style;
11+
import net.minecraft.text.Text;
12+
import net.minecraft.util.Formatting;
13+
14+
import java.util.function.BiConsumer;
15+
16+
public enum ScriptOptionEnum {
17+
TEXT("Text Option", "A single option, no checks.", Items.BOOK, ScriptTextOption.class);
18+
19+
String name;
20+
String description;
21+
Item icon;
22+
Class<? extends ScriptOption> optionType;
23+
24+
ScriptOptionEnum(String name, String description, Item icon, Class<? extends ScriptOption> optionType) {
25+
this.name = name;
26+
this.description = description;
27+
this.icon = icon;
28+
this.optionType = optionType;
29+
}
30+
31+
public ItemStack getIcon()
32+
{
33+
ItemStack item = new ItemStack(icon);
34+
35+
item.setCustomName(Text.literal(name).fillStyle(Style.EMPTY.withColor(Formatting.WHITE).withItalic(false)));
36+
37+
NbtList lore = new NbtList();
38+
39+
lore.add(NbtString.of(Text.Serializer.toJson(Text.literal(description)
40+
.fillStyle(Style.EMPTY.withColor(Formatting.GRAY).withItalic(false))
41+
)));
42+
43+
item.getSubNbt("display")
44+
.put("Lore", lore);
45+
46+
return item;
47+
}
48+
49+
public Class<? extends ScriptOption> getOptionType() {
50+
return optionType;
51+
}
52+
}

0 commit comments

Comments
 (0)