Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
XTerPL committed Nov 19, 2022
1 parent 92f2de7 commit c6e1a01
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.techstreet.dfscript.script.Script;
import io.github.techstreet.dfscript.script.action.ScriptAction;
import io.github.techstreet.dfscript.script.action.ScriptActionCategory;
import io.github.techstreet.dfscript.script.action.ScriptActionCategoryExtra;
import io.github.techstreet.dfscript.script.action.ScriptActionType;
import io.github.techstreet.dfscript.script.event.ScriptEvent;
import io.github.techstreet.dfscript.script.event.ScriptEventType;
Expand Down Expand Up @@ -36,6 +37,23 @@ public ScriptAddActionScreen(Script script, int insertIndex, ScriptActionCategor
});
widgets.add(item);
x += 10;
if (x >= size - 10) {
x = 3;
y += 10;
}
}
}

if (category != null)
{
for(ScriptActionCategoryExtra extra : category.getExtras()) {
CItem item = new CItem(x, y, extra.getIcon());
item.setClickListener((btn) -> {
script.getParts().add(insertIndex, extra.getPart());
DFScript.MC.setScreen(new ScriptEditScreen(script));
});
widgets.add(item);
x += 10;
if (x >= size-10) {
x = 3;
y += 10;
Expand Down Expand Up @@ -70,6 +88,7 @@ private static int size(ScriptActionCategory category) {
if (category == null) {
amount = ScriptEventType.values().length;
} else {
amount += category.getExtras().size();
for (ScriptActionType type : ScriptActionType.values()) {
if (type.getCategory() == category) {
amount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import io.github.techstreet.dfscript.DFScript;
import io.github.techstreet.dfscript.screen.CScreen;
import io.github.techstreet.dfscript.screen.widget.CButton;
import io.github.techstreet.dfscript.screen.widget.CItem;
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
import io.github.techstreet.dfscript.screen.widget.CText;
import io.github.techstreet.dfscript.screen.widget.CTexturedButton;
import io.github.techstreet.dfscript.screen.widget.CWidget;
import io.github.techstreet.dfscript.screen.widget.*;
import io.github.techstreet.dfscript.script.Script;
import io.github.techstreet.dfscript.script.ScriptComment;
import io.github.techstreet.dfscript.script.ScriptManager;
import io.github.techstreet.dfscript.script.ScriptPart;
import io.github.techstreet.dfscript.script.action.ScriptAction;
Expand All @@ -20,20 +16,27 @@
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

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

private final Script script;
private static int scroll = 0;

private final static int width = 125;
private final CScrollPanel panel;
private final List<CWidget> contextMenu = new ArrayList<>();

public ScriptEditScreen(Script script) {
super(125, 100);
super(width, 100);
this.script = script;
panel = new CScrollPanel(0, 3, 120, 94);

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

for (int i = 0; i < indent; i += 5) {
int xpos = 8 + i;
int ypos = y;
panel.add(new CWidget() {
@Override
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
DrawableHelper.fill(stack, xpos, ypos, xpos + 1, ypos + 8, 0xFF333333);
}

@Override
public Rectangle getBounds() {
return new Rectangle(0,0,0,0);
}
});
}
createIndent(indent, y);

int currentIndex = index;
panel.add(new CButton(5, y-1, 115, 10, "",() -> {}) {
panel.add(new CButton(5, y - 1, 115, 10, "", () -> {
}) {
@Override
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
Rectangle b = getBounds();
if (b.contains(mouseX, mouseY)) {
int color = 0x33000000;

if(sa.getType().isDeprecated())
{
if (sa.getType().isDeprecated()) {
color = 0x80FF0000;
}

DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, color);
}
else
{
if(sa.getType().isDeprecated())
{
} else {
if (sa.getType().isDeprecated()) {
DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, 0x33FF0000);
}
}
Expand All @@ -156,7 +142,7 @@ public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
@Override
public boolean mouseClicked(double x, double y, int button) {
if (getBounds().contains(x, y)) {
DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f,1f));
DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f, 1f));

if (button == 0) {
if (sa.getType() != ScriptActionType.CLOSE_BRACKET) {
Expand All @@ -167,10 +153,10 @@ public boolean mouseClicked(double x, double y, int button) {
CButton insertBefore = new CButton((int) x, (int) y, 40, 8, "Insert Before", () -> {
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex));
});
CButton insertAfter = new CButton((int) x, (int) y+8, 40, 8, "Insert After", () -> {
CButton insertAfter = new CButton((int) x, (int) y + 8, 40, 8, "Insert After", () -> {
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex + 1));
});
CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> {
CButton delete = new CButton((int) x, (int) y + 16, 40, 8, "Delete", () -> {
script.getParts().remove(currentIndex);
scroll = panel.getScroll();
DFScript.MC.setScreen(new ScriptEditScreen(script));
Expand All @@ -193,6 +179,63 @@ public boolean mouseClicked(double x, double y, int button) {
if (sa.getType().hasChildren()) {
indent += 5;
}
} else if (part instanceof ScriptComment sc) {
panel.add(new CItem(5 + indent, y, new ItemStack(Items.MAP).setCustomName(Text.literal("Comment").setStyle(Style.EMPTY.withItalic(false)))));

CTextField cTextField = new CTextField(sc.getComment(),15+indent, y-1, width-(15+indent)-5, 10, true);

cTextField.setChangedListener(() -> sc.setComment(cTextField.getText()));

panel.add(cTextField);

int currentIndex = index;

panel.add(new CButton(5, y-1, 115, 10, "",() -> {}) {
@Override
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
Rectangle b = getBounds();

if (b.contains(mouseX, mouseY)) {
int color = 0x33000000;

DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, color);
}
}

@Override
public boolean mouseClicked(double x, double y, int button) {
if (getBounds().contains(x, y)) {
DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f,1f));

if (button != 0) {
CButton insertBefore = new CButton((int) x, (int) y, 40, 8, "Insert Before", () -> {
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex));
});
CButton insertAfter = new CButton((int) x, (int) y+8, 40, 8, "Insert After", () -> {
DFScript.MC.setScreen(new ScriptActionCategoryScreen(script, currentIndex + 1));
});
CButton delete = new CButton((int) x, (int) y+16, 40, 8, "Delete", () -> {
script.getParts().remove(currentIndex);
scroll = panel.getScroll();
DFScript.MC.setScreen(new ScriptEditScreen(script));
});
DFScript.MC.send(() -> {
panel.add(insertBefore);
panel.add(insertAfter);
panel.add(delete);
contextMenu.add(insertBefore);
contextMenu.add(insertAfter);
contextMenu.add(delete);
});

return true;
}
}
return false;
}
});

createIndent(indent, y);
} else {
throw new IllegalArgumentException("Unknown script part type");
}
Expand All @@ -209,6 +252,25 @@ public boolean mouseClicked(double x, double y, int button) {
panel.setScroll(scroll);
}

public void createIndent(int indent, int y)
{
for (int i = 0; i < indent; i += 5) {
int xpos = 8 + i;
int ypos = y;
panel.add(new CWidget() {
@Override
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
DrawableHelper.fill(stack, xpos, ypos, xpos + 1, ypos + 8, 0xFF333333);
}

@Override
public Rectangle getBounds() {
return new Rectangle(0, 0, 0, 0);
}
});
}
}

@Override
public void close() {
scroll = panel.getScroll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public void execute(ScriptTask task) {
break;
}
}
} else if (nextPart instanceof ScriptComment) {
// ignore comments
} else {
throw new IllegalStateException("Unexpected script part type: " + nextPart.getClass().getName());
}
Expand Down Expand Up @@ -160,6 +162,8 @@ public void execute(ScriptTask task) {
context.breakLoop(-1);
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
}
} else if (part instanceof ScriptComment) {
// ignore the comment lol
} else {
throw new IllegalArgumentException("Invalid script part");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.techstreet.dfscript.script;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import io.github.techstreet.dfscript.event.system.Event;
import io.github.techstreet.dfscript.script.action.ScriptActionType;
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
import io.github.techstreet.dfscript.script.argument.ScriptConfigArgument;
import io.github.techstreet.dfscript.script.execution.ScriptActionContext;
import io.github.techstreet.dfscript.script.execution.ScriptContext;
import io.github.techstreet.dfscript.script.execution.ScriptScopeVariables;
import io.github.techstreet.dfscript.script.execution.ScriptTask;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

public class ScriptComment implements ScriptPart {

private String comment;

public ScriptComment(String comment) {
this.comment = comment;
}

public ScriptComment setComment(String comment) {
this.comment = comment;

return this;
}

public String getComment() {
return comment;
}

@Override
public ScriptGroup getGroup() {
return ScriptGroup.COMMENT;
}

public static class Serializer implements JsonSerializer<ScriptComment> {

@Override
public JsonElement serialize(ScriptComment src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty("type", "comment");
obj.addProperty("comment", src.getComment());
return obj;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public enum ScriptGroup {
ACTION,
CONDITION,
REPETITION,
EVENT
EVENT,

COMMENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ScriptManager implements Loadable {
.registerTypeAdapter(ScriptNamedOption.class, new ScriptNamedOption.Serializer())
.registerTypeAdapter(ScriptAction.class, new ScriptAction.Serializer())
.registerTypeAdapter(ScriptEvent.class, new ScriptEvent.Serializer())
.registerTypeAdapter(ScriptComment.class, new ScriptComment.Serializer())
.create();

public ScriptManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public ScriptPart deserialize(JsonElement json, Type typeOfT, JsonDeserializatio
String event = obj.get("event").getAsString();
return new ScriptEvent(ScriptEventType.valueOf(event));
}
case "comment" -> {
String comment = obj.get("comment").getAsString();
return new ScriptComment(comment);
}
default -> throw new JsonParseException("Unknown script part type: " + type);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package io.github.techstreet.dfscript.script.action;

import io.github.techstreet.dfscript.script.ScriptComment;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.slf4j.helpers.FormattingTuple;

import java.util.ArrayList;
import java.util.List;

public enum ScriptActionCategory {

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

private final ItemStack icon;

private List<ScriptActionCategoryExtra> extras = new ArrayList<>();

ScriptActionCategory(String name, Item icon) {
this.icon = new ItemStack(icon);
this.icon.setCustomName(Text.literal(name).fillStyle(Style.EMPTY.withItalic(false)));
}

ScriptActionCategory(String name, Item icon, List<ScriptActionCategoryExtra> extras) {
this.icon = new ItemStack(icon);
this.icon.setCustomName(Text.literal(name).fillStyle(Style.EMPTY.withItalic(false)));
this.extras = extras;
}

public ItemStack getIcon() {
return icon;
}
public List<ScriptActionCategoryExtra> getExtras()
{
return extras;
}
}
Loading

0 comments on commit c6e1a01

Please sign in to comment.