|
| 1 | +package io.github.techstreet.dfscript.screen.script; |
| 2 | + |
| 3 | +import io.github.techstreet.dfscript.DFScript; |
| 4 | +import io.github.techstreet.dfscript.screen.CReloadableScreen; |
| 5 | +import io.github.techstreet.dfscript.screen.ContextMenuButton; |
| 6 | +import io.github.techstreet.dfscript.screen.util.ItemMaterialSelectMenu; |
| 7 | +import io.github.techstreet.dfscript.screen.widget.*; |
| 8 | +import io.github.techstreet.dfscript.script.Script; |
| 9 | +import io.github.techstreet.dfscript.script.event.ScriptFunction; |
| 10 | +import net.minecraft.client.font.TextRenderer; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class ScriptEditFunctionScreen extends CReloadableScreen { |
| 16 | + |
| 17 | + private final Script script; |
| 18 | + private final ScriptFunction function; |
| 19 | + private final CScrollPanel panel; |
| 20 | + private final List<CWidget> contextMenu = new ArrayList<>(); |
| 21 | + |
| 22 | + public ScriptEditFunctionScreen(ScriptFunction function, Script script) { |
| 23 | + super(90, 100); |
| 24 | + panel = new CScrollPanel(0, 0, 90, 100); |
| 25 | + |
| 26 | + widgets.add(panel); |
| 27 | + |
| 28 | + this.script = script; |
| 29 | + this.function = function; |
| 30 | + |
| 31 | + reload(); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void close() { |
| 36 | + DFScript.MC.setScreen(new ScriptEditScreen(script)); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean mouseClicked(double mouseX, double mouseY, int button) { |
| 41 | + boolean b = super.mouseClicked(mouseX, mouseY, button); |
| 42 | + clearContextMenu(); |
| 43 | + return b; |
| 44 | + } |
| 45 | + |
| 46 | + private void clearContextMenu() { |
| 47 | + for (CWidget w : contextMenu) { |
| 48 | + panel.remove(w); |
| 49 | + } |
| 50 | + contextMenu.clear(); |
| 51 | + } |
| 52 | + |
| 53 | + public void contextMenu(int x, int y, List<ContextMenuButton> contextMenuButtons) { |
| 54 | + clearContextMenu(); |
| 55 | + |
| 56 | + int maxWidth = 0; |
| 57 | + |
| 58 | + for(ContextMenuButton w : contextMenuButtons) |
| 59 | + { |
| 60 | + TextRenderer t = DFScript.MC.textRenderer; |
| 61 | + int width = t.getWidth(w.getName())/2 + 4; |
| 62 | + |
| 63 | + if(width > maxWidth) maxWidth = width; |
| 64 | + } |
| 65 | + |
| 66 | + for(ContextMenuButton w : contextMenuButtons) |
| 67 | + { |
| 68 | + CButton button = new CButton(x, y, maxWidth, 8, w.getName(), w.getOnClick()); |
| 69 | + y += 8; |
| 70 | + |
| 71 | + panel.add(button); |
| 72 | + contextMenu.add(button); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void reload() { |
| 78 | + clearContextMenu(); |
| 79 | + panel.clear(); |
| 80 | + |
| 81 | + CTextField nameField = new CTextField(function.getName(), 15, 3, 90-10-10, 8, true); |
| 82 | + nameField.setMultiline(false); |
| 83 | + nameField.setChangedListener(() -> { |
| 84 | + if(script.functionExists(nameField.getText())) { |
| 85 | + nameField.textColor = 0xFF0000; |
| 86 | + } |
| 87 | + else { |
| 88 | + nameField.textColor = 0xFFFFFF; |
| 89 | + function.setName(nameField.getText()); |
| 90 | + script.replaceFunction(function.getName(), nameField.getText()); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + CItem icon = new CItem(5, 3, function.getIcon()); |
| 95 | + |
| 96 | + icon.setClickListener((button) -> { |
| 97 | + DFScript.MC.setScreen(new ItemMaterialSelectMenu(function.getRawIcon(), (newIcon) -> { |
| 98 | + function.setIcon(newIcon); |
| 99 | + DFScript.MC.setScreen(new ScriptEditFunctionScreen(function, script)); |
| 100 | + })); |
| 101 | + }); |
| 102 | + |
| 103 | + panel.add(icon); |
| 104 | + panel.add(nameField); |
| 105 | + |
| 106 | + /*int y = 15; |
| 107 | + int index = 0; |
| 108 | + for (ScriptArgument arg : action.getArguments()) { |
| 109 | + ItemStack icon = arg.getArgIcon(); |
| 110 | + String text = arg.getArgText(); |
| 111 | +
|
| 112 | + panel.add(new CItem(5, y, icon)); |
| 113 | + panel.add(new CText(15, y + 2, Text.literal(text))); |
| 114 | +
|
| 115 | + int currentIndex = index; |
| 116 | +
|
| 117 | +
|
| 118 | + panel.add(new CButton(5, y-1, 85, 10, "",() -> {}) { |
| 119 | + @Override |
| 120 | + public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) { |
| 121 | + Rectangle b = getBounds(); |
| 122 | + if (b.contains(mouseX, mouseY)) { |
| 123 | + DrawableHelper.fill(stack, b.x, b.y, b.x + b.width, b.y + b.height, 0x33000000); |
| 124 | + } |
| 125 | + } |
| 126 | +
|
| 127 | + @Override |
| 128 | + public boolean mouseClicked(double x, double y, int button) { |
| 129 | + if (getBounds().contains(x, y)) { |
| 130 | + DFScript.MC.getSoundManager().play(PositionedSoundInstance.ambient(SoundEvents.UI_BUTTON_CLICK, 1f,1f)); |
| 131 | +
|
| 132 | + if (button == 0) { |
| 133 | + ScriptArgument argument = action.getArguments().get(currentIndex); |
| 134 | + String value = "~"; |
| 135 | + if(argument instanceof ScriptClientValueArgument clientValue) value = clientValue.getName(); |
| 136 | + if(argument instanceof ScriptConfigArgument configArgument) value = configArgument.getName(); |
| 137 | + if(argument instanceof ScriptNumberArgument number) value = String.valueOf(number.value()); |
| 138 | + if(argument instanceof ScriptTextArgument text) value = text.value(); |
| 139 | + if(argument instanceof ScriptVariableArgument var) value = var.name(); |
| 140 | + DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, currentIndex, value)); |
| 141 | + } |
| 142 | +
|
| 143 | + if (button != 0) { |
| 144 | + List<ContextMenuButton> contextMenuButtons = new ArrayList<>(); |
| 145 | + contextMenuButtons.add(new ContextMenuButton("Insert Before", () -> { |
| 146 | + DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, currentIndex)); |
| 147 | + }, false)); |
| 148 | + contextMenuButtons.add(new ContextMenuButton("Insert After", () -> { |
| 149 | + DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, currentIndex+1)); |
| 150 | + }, false)); |
| 151 | + contextMenuButtons.add(new ContextMenuButton("Delete", () -> { |
| 152 | + action.getArguments().remove(currentIndex); |
| 153 | + })); |
| 154 | + contextMenuButtons.addAll(action.getArguments().get(currentIndex).getContextMenu()); |
| 155 | + DFScript.MC.send(() -> { |
| 156 | + if(DFScript.MC.currentScreen instanceof ScriptEditFunctionScreen screen) { |
| 157 | + screen.contextMenu((int) x, (int) y, contextMenuButtons); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | + return true; |
| 162 | + } |
| 163 | + return false; |
| 164 | + } |
| 165 | + }); |
| 166 | +
|
| 167 | + y += 10; |
| 168 | + index++; |
| 169 | +
|
| 170 | + } |
| 171 | +
|
| 172 | + CButton add = new CButton(25, y, 40, 8, "Add", () -> { |
| 173 | + DFScript.MC.setScreen(new ScriptAddArgumentScreen(script, action, action.getArguments().size())); |
| 174 | + }); |
| 175 | + panel.add(add);*/ |
| 176 | + } |
| 177 | +} |
0 commit comments