Skip to content

Commit

Permalink
Merge pull request #42 from XTerPL/Functions
Browse files Browse the repository at this point in the history
Functions
  • Loading branch information
TechStreetDev authored Dec 18, 2023
2 parents 064862c + b9bfef8 commit ecd0786
Show file tree
Hide file tree
Showing 68 changed files with 2,709 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.github.techstreet.dfscript.script.Script;
import io.github.techstreet.dfscript.script.ScriptManager;
import io.github.techstreet.dfscript.script.values.ScriptValue;
import io.github.techstreet.dfscript.script.values.ScriptVariable;
import io.github.techstreet.dfscript.util.chat.ChatUtil;
import java.util.List;
import java.util.Map.Entry;
Expand Down Expand Up @@ -64,11 +65,11 @@ public void register(CommandDispatcher<FabricClientCommandSource> cd) {
private void listVars(String script, String filter) {
for (Script s : ScriptManager.getInstance().getScripts()) {
if (s.getName().replaceAll(" ", "_").equals(script)) {
List<Entry<String, ScriptValue>> vars = s.getContext().listVariables(filter);
List<Entry<String, ScriptVariable>> vars = s.getContext().variables().list(filter);

int showing = Math.min(vars.size(), 50);
int filtered = vars.size();
int total = s.getContext().getVariableCount();
int total = s.getContext().variables().count();

ChatUtil.info("Script " + s.getName() + " has a total of " + total + " variables.");

Expand All @@ -79,7 +80,7 @@ private void listVars(String script, String filter) {
}

for (int i = 0; i < showing; i++) {
Entry<String, ScriptValue> e = vars.get(i);
Entry<String, ScriptVariable> e = vars.get(i);
ChatUtil.info(e.getKey() + ": " + e.getValue().asText());
}
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.techstreet.dfscript.screen;

import io.github.techstreet.dfscript.DFScript;
import io.github.techstreet.dfscript.screen.widget.CWidget;
import io.github.techstreet.dfscript.util.RenderUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

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

public abstract class CReloadableScreen extends CScreen {

public abstract void reload();

protected CReloadableScreen(int width, int height) {
super(width, height);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public Runnable getOnClick() {
if(reloadOnClick) {
return () -> {
onClick.run();
if(DFScript.MC.currentScreen instanceof ScriptEditScreen editScreen)
if(DFScript.MC.currentScreen instanceof CReloadableScreen screen)
{
editScreen.reload();
screen.reload();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import io.github.techstreet.dfscript.screen.widget.CTextField;
import io.github.techstreet.dfscript.script.Script;
import io.github.techstreet.dfscript.script.ScriptParametrizedPart;
import io.github.techstreet.dfscript.script.ScriptPart;
import io.github.techstreet.dfscript.script.action.ScriptAction;
import io.github.techstreet.dfscript.script.argument.ScriptNumberArgument;
import io.github.techstreet.dfscript.script.argument.ScriptTextArgument;
import io.github.techstreet.dfscript.script.argument.ScriptVariableArgument;
import io.github.techstreet.dfscript.script.argument.*;
import io.github.techstreet.dfscript.script.event.ScriptFunction;
import io.github.techstreet.dfscript.script.event.ScriptHeader;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Style;
Expand All @@ -19,15 +17,18 @@
public class ScriptAddArgumentScreen extends CScreen {

private final Script script;

private final ScriptHeader header;
private final ScriptParametrizedPart action;

public ScriptAddArgumentScreen(Script script, ScriptParametrizedPart action, int index) {
this(script,action,index,null);
public ScriptAddArgumentScreen(Script script, ScriptParametrizedPart action, int index, ScriptHeader header) {
this(script,action,index, header,null);
}
public ScriptAddArgumentScreen(Script script, ScriptParametrizedPart action, int index, String overwrite) {
public ScriptAddArgumentScreen(Script script, ScriptParametrizedPart action, int index, ScriptHeader header, String overwrite) {
super(100, 50);
this.script = script;
this.action = action;
this.header = header;

CTextField input = new CTextField("Input", 2, 2, 96, 35, true);
if(overwrite != null) input.setText(overwrite);
Expand All @@ -52,44 +53,53 @@ public ScriptAddArgumentScreen(Script script, ScriptParametrizedPart action, int
configValueIcon.setCustomName(Text.literal("Config Value")
.fillStyle(Style.EMPTY.withItalic(false)));

ItemStack functionArgumentIcon = new ItemStack(Items.BLUE_DYE);
functionArgumentIcon.setCustomName(Text.literal("Function Argument")
.fillStyle(Style.EMPTY.withItalic(false)));


CItem addNumber = new CItem(2, 40, numberIcon);
CItem addText = new CItem(12, 40, textIcon);
CItem addVariable = new CItem(22, 40, variableIcon);
CItem addClientValue = new CItem(32, 40, clientValueIcon);
CItem addConfigValue = new CItem(42, 40, configValueIcon);
CItem addFunctionArgument = new CItem(52, 40, functionArgumentIcon);

input.setChangedListener(() -> input.textColor = 0xFFFFFF);

addText.setClickListener((btn) -> {
if(overwrite != null) action.getArguments().remove(index);
action.getArguments().add(index, new ScriptTextArgument(input.getText()));
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script));
close();
});

addNumber.setClickListener((btn) -> {
try {
double number = Double.parseDouble(input.getText());
if(overwrite != null) action.getArguments().remove(index);
action.getArguments().add(index, new ScriptNumberArgument(number));
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script));
close();
} catch (Exception err) {
input.textColor = 0xFF3333;
}
});

addVariable.setClickListener((btn) -> {
if(overwrite != null) action.getArguments().remove(index);
action.getArguments().add(index, new ScriptVariableArgument(input.getText()));
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script));
action.getArguments().add(index, new ScriptVariableArgument(input.getText(), ScriptVariableScope.SCRIPT));
close();
});

addClientValue.setClickListener((btn) -> {
DFScript.MC.setScreen(new ScriptAddClientValueScreen(action, script, index, overwrite));
DFScript.MC.setScreen(new ScriptAddClientValueScreen(action, script, index, header, overwrite));
});

addConfigValue.setClickListener((btn) -> {
DFScript.MC.setScreen(new ScriptAddConfigValueScreen(action, script, index, overwrite));
DFScript.MC.setScreen(new ScriptAddConfigValueScreen(action, script, index, header, overwrite));
});

addFunctionArgument.setClickListener((btn) -> {
DFScript.MC.setScreen(new ScriptAddFunctionArgValueScreen(action, script, index, header, overwrite));
});

widgets.add(input);
Expand All @@ -98,10 +108,13 @@ public ScriptAddArgumentScreen(Script script, ScriptParametrizedPart action, int
widgets.add(addVariable);
widgets.add(addClientValue);
widgets.add(addConfigValue);
if(header instanceof ScriptFunction) {
widgets.add(addFunctionArgument);
}
}

@Override
public void close() {
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script));
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script, header));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
import io.github.techstreet.dfscript.script.ScriptPart;
import io.github.techstreet.dfscript.script.action.ScriptAction;
import io.github.techstreet.dfscript.script.argument.ScriptClientValueArgument;
import io.github.techstreet.dfscript.script.event.ScriptHeader;

public class ScriptAddClientValueScreen extends CScreen {

private final Script script;

private final ScriptHeader header;
private final ScriptParametrizedPart action;
private final int insertIndex;
private static final int WIDTH = 58;

public ScriptAddClientValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, String overwrite) {
public ScriptAddClientValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, ScriptHeader header, String overwrite) {
super(WIDTH, 58);
this.script = script;
this.action = action;
this.header = header;
this.insertIndex = insertIndex;

int x = 5;
Expand All @@ -29,7 +33,7 @@ public ScriptAddClientValueScreen(ScriptParametrizedPart action, Script script,
item.setClickListener((btn) -> {
if(overwrite != null) action.getArguments().remove(insertIndex);
action.getArguments().add(insertIndex, arg);
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script));
DFScript.MC.setScreen(new ScriptEditPartScreen(action, script, header));
});
widgets.add(item);
x += 10;
Expand All @@ -42,6 +46,6 @@ public ScriptAddClientValueScreen(ScriptParametrizedPart action, Script script,

@Override
public void close() {
DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, insertIndex));
DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, insertIndex, header));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,36 @@
import io.github.techstreet.dfscript.script.ScriptPart;
import io.github.techstreet.dfscript.script.action.ScriptAction;
import io.github.techstreet.dfscript.script.argument.ScriptConfigArgument;
import io.github.techstreet.dfscript.script.event.ScriptHeader;
import io.github.techstreet.dfscript.script.options.ScriptNamedOption;

public class ScriptAddConfigValueScreen extends CScreen {
private final Script script;

private final ScriptHeader header;
private final ScriptParametrizedPart action;
private final int insertIndex;

private static int WIDTH = 200;
private static int HEIGHT = 94;

public ScriptAddConfigValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, String overwrite) {
public ScriptAddConfigValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, ScriptHeader header, String overwrite) {
super(WIDTH, HEIGHT);
this.script = script;
this.action = action;
this.header = header;
this.insertIndex = insertIndex;

CScrollPanel panel = new CScrollPanel(0, 0, WIDTH, HEIGHT);

int x = 5;
int y = 5;
for (ScriptNamedOption arg : script.getOptions()) {
if(overwrite != null) action.getArguments().remove(insertIndex);
CItem item = new CItem(x, y, arg.getIcon());
item.setClickListener((btn) -> {
if(overwrite != null) action.getArguments().remove(insertIndex);
this.action.getArguments().add(insertIndex, new ScriptConfigArgument(arg.getName(), this.script));
DFScript.MC.setScreen(new ScriptEditPartScreen(this.action, this.script));
DFScript.MC.setScreen(new ScriptEditPartScreen(this.action, this.script, this.header));
});
panel.add(item);
x += 10;
Expand All @@ -49,6 +53,6 @@ public ScriptAddConfigValueScreen(ScriptParametrizedPart action, Script script,

@Override
public void close() {
DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, insertIndex));
DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, insertIndex, header));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github.techstreet.dfscript.screen.script;

import io.github.techstreet.dfscript.DFScript;
import io.github.techstreet.dfscript.screen.CScreen;
import io.github.techstreet.dfscript.screen.widget.CItem;
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
import io.github.techstreet.dfscript.script.Script;
import io.github.techstreet.dfscript.script.ScriptParametrizedPart;
import io.github.techstreet.dfscript.script.action.ScriptActionArgument;
import io.github.techstreet.dfscript.script.argument.ScriptConfigArgument;
import io.github.techstreet.dfscript.script.argument.ScriptFunctionArgument;
import io.github.techstreet.dfscript.script.event.ScriptFunction;
import io.github.techstreet.dfscript.script.event.ScriptHeader;

public class ScriptAddFunctionArgValueScreen extends CScreen {
private final Script script;

private final ScriptHeader header;
private final ScriptParametrizedPart action;
private final int insertIndex;

private static int WIDTH = 200;
private static int HEIGHT = 94;

public ScriptAddFunctionArgValueScreen(ScriptParametrizedPart action, Script script, int insertIndex, ScriptHeader header, String overwrite) {
super(WIDTH, HEIGHT);
this.script = script;
this.action = action;
this.header = header;
this.insertIndex = insertIndex;

CScrollPanel panel = new CScrollPanel(0, 0, WIDTH, HEIGHT);

if(header instanceof ScriptFunction f)
{
int x = 5;
int y = 5;
for (ScriptActionArgument arg : f.argList()) {
CItem item = new CItem(x, y, arg.getIcon());
item.setClickListener((btn) -> {
if(overwrite != null) action.getArguments().remove(insertIndex);
this.action.getArguments().add(insertIndex, new ScriptFunctionArgument(arg.name(), header));
DFScript.MC.setScreen(new ScriptEditPartScreen(this.action, this.script, this.header));
});
panel.add(item);
x += 10;
if (x > WIDTH-10) {
x = 5;
y += 10;
}
}

widgets.add(panel);
}
}

@Override
public void close() {
DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, insertIndex, header));
}
}
Loading

0 comments on commit ecd0786

Please sign in to comment.