diff --git a/src/main/java/kamkeel/npcs/editorgui/AbstractEditorComponent.java b/src/main/java/kamkeel/npcs/editorgui/AbstractEditorComponent.java new file mode 100644 index 000000000..d18368af9 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/AbstractEditorComponent.java @@ -0,0 +1,49 @@ +package kamkeel.npcs.editorgui; + +import net.minecraft.client.gui.Gui; + +/** + * AbstractEditorComponent provides common position/size fields and a method to draw + * a yellow selection outline when the component is selected. + */ +public abstract class AbstractEditorComponent extends Gui implements IEditorComponent { + // These fields are public for easy access. + public int posX, posY, width, height; + protected boolean selected = false; + + public AbstractEditorComponent(int posX, int posY, int width, int height) { + this.posX = posX; + this.posY = posY; + this.width = width; + this.height = height; + } + + @Override + public void setSelected(boolean selected) { + this.selected = selected; + } + + @Override + public boolean isSelected() { + return selected; + } + + /** + * Draws a 2–pixel thick yellow border if this component is selected. + */ + protected void renderSelectionOutline() { + if (!selected) + return; + // Top edge + drawRect(posX - 2, posY - 2, posX + width + 2, posY - 1, 0xFFFFFF00); + // Bottom edge + drawRect(posX - 2, posY + height + 1, posX + width + 2, posY + height + 2, 0xFFFFFF00); + // Left edge + drawRect(posX - 2, posY - 2, posX - 1, posY + height + 2, 0xFFFFFF00); + // Right edge + drawRect(posX + width + 1, posY - 2, posX + width + 2, posY + height + 2, 0xFFFFFF00); + } + + @Override + public void update() { } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/CustomGuiEditorController.java b/src/main/java/kamkeel/npcs/editorgui/CustomGuiEditorController.java new file mode 100644 index 000000000..c41dddce4 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/CustomGuiEditorController.java @@ -0,0 +1,42 @@ +package kamkeel.npcs.editorgui; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.scripted.gui.ScriptGui; +import noppes.npcs.util.NBTJsonUtil; +import java.io.File; + +/** + * CustomGuiEditorController handles saving and loading of ScriptGui objects. + */ +public class CustomGuiEditorController { + private ScriptGui currentGui; + + public CustomGuiEditorController() { + currentGui = new ScriptGui(); + } + + public void saveGui(File file, ScriptGui gui) { + NBTTagCompound compound = gui.toNBT(); + try { + NBTJsonUtil.SaveFile(file, compound); + } catch(Exception e) { + e.printStackTrace(); + } + currentGui = gui; + } + + public ScriptGui loadGui(File file) { + try { + NBTTagCompound compound = NBTJsonUtil.LoadFile(file); + currentGui = (ScriptGui) (new ScriptGui()).fromNBT(compound); + } catch(Exception e) { + e.printStackTrace(); + } + return currentGui; + } + + public void applyGuiToPlayer(EntityPlayer player) { + // player.showCustomGui(currentGui); + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/EditorButtonComponent.java b/src/main/java/kamkeel/npcs/editorgui/EditorButtonComponent.java new file mode 100644 index 000000000..1b1aa14b6 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/EditorButtonComponent.java @@ -0,0 +1,123 @@ +package kamkeel.npcs.editorgui; + +import noppes.npcs.scripted.gui.ScriptGuiButton; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import org.lwjgl.input.Mouse; +import java.util.List; + +/** + * EditorButtonComponent wraps a ScriptGuiButton. + * It supports dragging and provides custom editing buttons to modify width, height, + * scale, label text, and the component ID. + */ +public class EditorButtonComponent extends AbstractEditorComponent { + private ScriptGuiButton buttonComponent; + + public EditorButtonComponent(ScriptGuiButton button) { + super(button.getPosX(), button.getPosY(), button.getWidth(), button.getHeight()); + this.buttonComponent = button; + } + + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + FontRenderer fr = Minecraft.getMinecraft().fontRenderer; + drawRect(posX, posY, posX + width, posY + height, 0xFF888888); + String label = buttonComponent.getLabel(); + if (label != null) + fr.drawStringWithShadow(label, posX + (width - fr.getStringWidth(label)) / 2, posY + (height - 8) / 2, buttonComponent.getColor()); + renderSelectionOutline(); + } + + @Override + public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) { + if (mouseX >= posX && mouseX <= posX + width && + mouseY >= posY && mouseY <= posY + height) { + selected = true; + return true; + } + selected = false; + return false; + } + + @Override + public void mouseDragged(int mouseX, int mouseY, int mouseButton) { + if (selected) { + buttonComponent.setPos(posX, posY); + } + } + + @Override + public void mouseReleased(int mouseX, int mouseY, int state) { } + + @Override + public Object toScriptComponent() { + return buttonComponent; + } + + @Override + public void addEditorButtons(List buttonList) { + // Create custom editing buttons with dummy positions. + // The updateCustomButtons() in the editor repositions them to the top row. + buttonList.add(new GuiButton(101, 0, 0, 0, 0, "W+")); + buttonList.add(new GuiButton(102, 0, 0, 0, 0, "W-")); + buttonList.add(new GuiButton(103, 0, 0, 0, 0, "H+")); + buttonList.add(new GuiButton(104, 0, 0, 0, 0, "H-")); + buttonList.add(new GuiButton(105, 0, 0, 0, 0, "Sc")); + buttonList.add(new GuiButton(107, 0, 0, 0, 0, "Lbl")); + buttonList.add(new GuiButton(110, 0, 0, 0, 0, "ID")); + } + + @Override + public void onEditorButtonPressed(GuiButton button) { + switch (button.id) { + case 101: + width += 5; + buttonComponent.setSize(width, height); + break; + case 102: + width = Math.max(10, width - 5); + buttonComponent.setSize(width, height); + break; + case 103: + height += 5; + buttonComponent.setSize(width, height); + break; + case 104: + height = Math.max(10, height - 5); + buttonComponent.setSize(width, height); + break; + case 105: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Scale", Float.toString(buttonComponent.getScale()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + float newScale = Float.parseFloat(newValue); + buttonComponent.setScale(newScale); + } catch (NumberFormatException e) { } + } + })); + break; + case 107: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Label", buttonComponent.getLabel(), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + buttonComponent.setLabel(newValue); + } + })); + break; + case 110: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "ID", Integer.toString(buttonComponent.getID()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + int newID = Integer.parseInt(newValue); + buttonComponent.setID(newID); + } catch (NumberFormatException e) { } + } + })); + break; + } + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/EditorLabelComponent.java b/src/main/java/kamkeel/npcs/editorgui/EditorLabelComponent.java new file mode 100644 index 000000000..c403ece7e --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/EditorLabelComponent.java @@ -0,0 +1,118 @@ +package kamkeel.npcs.editorgui; + +import noppes.npcs.scripted.gui.ScriptGuiLabel; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import java.util.List; + +/** + * EditorLabelComponent wraps a ScriptGuiLabel and adds editing buttons for width, height, scale, text, and ID. + */ +public class EditorLabelComponent extends AbstractEditorComponent { + private ScriptGuiLabel labelComponent; + + public EditorLabelComponent(ScriptGuiLabel label) { + super(label.getPosX(), label.getPosY(), label.getWidth(), label.getHeight()); + this.labelComponent = label; + } + + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + FontRenderer fr = Minecraft.getMinecraft().fontRenderer; + drawRect(posX, posY, posX + width, posY + height, 0xFFAAAAAA); + String text = labelComponent.getText(); + if(text != null) + fr.drawStringWithShadow(text, posX + 2, posY + (height - 8) / 2, labelComponent.getColor()); + renderSelectionOutline(); + } + + @Override + public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) { + if (mouseX >= posX && mouseX <= posX + width && + mouseY >= posY && mouseY <= posY + height) { + selected = true; + return true; + } + selected = false; + return false; + } + + @Override + public void mouseDragged(int mouseX, int mouseY, int mouseButton) { + if(selected) { + labelComponent.setPos(posX, posY); + } + } + + @Override + public void mouseReleased(int mouseX, int mouseY, int state) { } + + @Override + public Object toScriptComponent() { + return labelComponent; + } + + @Override + public void addEditorButtons(List buttonList) { + buttonList.add(new GuiButton(201, 0, 0, 0, 0, "W+")); + buttonList.add(new GuiButton(202, 0, 0, 0, 0, "W-")); + buttonList.add(new GuiButton(203, 0, 0, 0, 0, "H+")); + buttonList.add(new GuiButton(204, 0, 0, 0, 0, "H-")); + buttonList.add(new GuiButton(205, 0, 0, 0, 0, "Sc")); + buttonList.add(new GuiButton(207, 0, 0, 0, 0, "Txt")); + buttonList.add(new GuiButton(210, 0, 0, 0, 0, "ID")); + } + + @Override + public void onEditorButtonPressed(GuiButton button) { + switch(button.id) { + case 201: + width += 5; + labelComponent.setSize(width, height); + break; + case 202: + width = Math.max(10, width - 5); + labelComponent.setSize(width, height); + break; + case 203: + height += 5; + labelComponent.setSize(width, height); + break; + case 204: + height = Math.max(10, height - 5); + labelComponent.setSize(width, height); + break; + case 205: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Scale", Float.toString(labelComponent.getScale()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + float newScale = Float.parseFloat(newValue); + labelComponent.setScale(newScale); + } catch (NumberFormatException e) { } + } + })); + break; + case 207: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Text", labelComponent.getText(), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + labelComponent.setText(newValue); + } + })); + break; + case 210: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "ID", Integer.toString(labelComponent.getID()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + int newID = Integer.parseInt(newValue); + labelComponent.setID(newID); + } catch (NumberFormatException e) { } + } + })); + break; + } + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/EditorTextFieldComponent.java b/src/main/java/kamkeel/npcs/editorgui/EditorTextFieldComponent.java new file mode 100644 index 000000000..6e0167fbe --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/EditorTextFieldComponent.java @@ -0,0 +1,108 @@ +package kamkeel.npcs.editorgui; + +import noppes.npcs.scripted.gui.ScriptGuiTextField; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import java.util.List; + +/** + * EditorTextFieldComponent wraps a ScriptGuiTextField. + * Since ScriptGuiTextField only supports width, height, and default text, + * this editor only provides editing buttons for width, height, and default text. + */ +public class EditorTextFieldComponent extends AbstractEditorComponent { + private ScriptGuiTextField textFieldComponent; + + public EditorTextFieldComponent(ScriptGuiTextField textField) { + super(textField.getPosX(), textField.getPosY(), textField.getWidth(), textField.getHeight()); + this.textFieldComponent = textField; + } + + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + FontRenderer fr = Minecraft.getMinecraft().fontRenderer; + drawRect(posX, posY, posX + width, posY + height, 0xFFFFFFFF); + String text = textFieldComponent.getText(); + if(text != null) + fr.drawStringWithShadow(text, posX + 2, posY + (height - 8) / 2, textFieldComponent.getColor()); + renderSelectionOutline(); + } + + @Override + public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) { + if(mouseX >= posX && mouseX <= posX + width && + mouseY >= posY && mouseY <= posY + height) { + selected = true; + return true; + } + selected = false; + return false; + } + + @Override + public void mouseDragged(int mouseX, int mouseY, int mouseButton) { + if(selected) { + textFieldComponent.setPos(posX, posY); + } + } + + @Override + public void mouseReleased(int mouseX, int mouseY, int state) { } + + @Override + public Object toScriptComponent() { + return textFieldComponent; + } + + @Override + public void addEditorButtons(List buttonList) { + buttonList.add(new GuiButton(301, 0, 0, 0, 0, "W+")); + buttonList.add(new GuiButton(302, 0, 0, 0, 0, "W-")); + buttonList.add(new GuiButton(303, 0, 0, 0, 0, "H+")); + buttonList.add(new GuiButton(304, 0, 0, 0, 0, "H-")); + buttonList.add(new GuiButton(307, 0, 0, 0, 0, "Text")); + buttonList.add(new GuiButton(310, 0, 0, 0, 0, "ID")); + } + + @Override + public void onEditorButtonPressed(GuiButton button) { + switch(button.id) { + case 301: + width += 5; + textFieldComponent.setSize(width, height); + break; + case 302: + width = Math.max(10, width - 5); + textFieldComponent.setSize(width, height); + break; + case 303: + height += 5; + textFieldComponent.setSize(width, height); + break; + case 304: + height = Math.max(10, height - 5); + textFieldComponent.setSize(width, height); + break; + case 307: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Default Text", textFieldComponent.getText(), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + textFieldComponent.setText(newValue); + } + })); + break; + case 310: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "ID", Integer.toString(textFieldComponent.getID()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + int newID = Integer.parseInt(newValue); + textFieldComponent.setID(newID); + } catch (NumberFormatException e) { } + } + })); + break; + } + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/EditorTexturedRectComponent.java b/src/main/java/kamkeel/npcs/editorgui/EditorTexturedRectComponent.java new file mode 100644 index 000000000..6cae728b3 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/EditorTexturedRectComponent.java @@ -0,0 +1,143 @@ +package kamkeel.npcs.editorgui; + +import noppes.npcs.scripted.gui.ScriptGuiTexturedRect; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import java.util.List; + +/** + * EditorTexturedRectComponent wraps a ScriptGuiTexturedRect. + * It adds editing buttons for width, height, scale, texture string, texture X/Y offsets, and ID. + */ +public class EditorTexturedRectComponent extends AbstractEditorComponent { + private ScriptGuiTexturedRect texturedRect; + + public EditorTexturedRectComponent(ScriptGuiTexturedRect rect) { + super(rect.getPosX(), rect.getPosY(), rect.getWidth(), rect.getHeight()); + this.texturedRect = rect; + } + + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + FontRenderer fr = Minecraft.getMinecraft().fontRenderer; + drawRect(posX, posY, posX + width, posY + height, 0xFF000088); + String tex = texturedRect.getTexture(); + if(tex != null) + fr.drawStringWithShadow(tex, posX + 2, posY + 2, 0xFFFFFF); + renderSelectionOutline(); + } + + @Override + public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) { + if(mouseX >= posX && mouseX <= posX + width && + mouseY >= posY && mouseY <= posY + height) { + selected = true; + return true; + } + selected = false; + return false; + } + + @Override + public void mouseDragged(int mouseX, int mouseY, int mouseButton) { + if(selected) { + texturedRect.setPos(posX, posY); + } + } + + @Override + public void mouseReleased(int mouseX, int mouseY, int state) { } + + @Override + public Object toScriptComponent() { + return texturedRect; + } + + @Override + public void addEditorButtons(List buttonList) { + buttonList.add(new GuiButton(401, 0, 0, 0, 0, "W+")); + buttonList.add(new GuiButton(402, 0, 0, 0, 0, "W-")); + buttonList.add(new GuiButton(403, 0, 0, 0, 0, "H+")); + buttonList.add(new GuiButton(404, 0, 0, 0, 0, "H-")); + buttonList.add(new GuiButton(405, 0, 0, 0, 0, "Sc")); + buttonList.add(new GuiButton(407, 0, 0, 0, 0, "Tex")); + buttonList.add(new GuiButton(408, 0, 0, 0, 0, "TxX")); + buttonList.add(new GuiButton(409, 0, 0, 0, 0, "TxY")); + buttonList.add(new GuiButton(410, 0, 0, 0, 0, "ID")); + } + + @Override + public void onEditorButtonPressed(GuiButton button) { + switch(button.id) { + case 401: + width += 5; + texturedRect.setSize(width, height); + break; + case 402: + width = Math.max(10, width - 5); + texturedRect.setSize(width, height); + break; + case 403: + height += 5; + texturedRect.setSize(width, height); + break; + case 404: + height = Math.max(10, height - 5); + texturedRect.setSize(width, height); + break; + case 405: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Scale", Float.toString(texturedRect.getScale()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + float newScale = Float.parseFloat(newValue); + texturedRect.setScale(newScale); + } catch (NumberFormatException e) { } + } + })); + break; + case 407: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "Texture", texturedRect.getTexture(), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + texturedRect.setTexture(newValue); + } + })); + break; + case 408: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "TxX", Integer.toString(texturedRect.getTextureX()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + int newX = Integer.parseInt(newValue); + texturedRect.setTextureOffset(newX, texturedRect.getTextureY()); + } catch (NumberFormatException e) { } + } + })); + break; + case 409: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "TxY", Integer.toString(texturedRect.getTextureY()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + int newY = Integer.parseInt(newValue); + texturedRect.setTextureOffset(texturedRect.getTextureX(), newY); + } catch (NumberFormatException e) { } + } + })); + break; + case 410: + Minecraft.getMinecraft().displayGuiScreen(new SubGuiEditProperty(Minecraft.getMinecraft().currentScreen, "ID", Integer.toString(texturedRect.getID()), new IPropertyEditorCallback() { + @Override + public void propertyUpdated(String newValue) { + try { + int newID = Integer.parseInt(newValue); + texturedRect.setID(newID); + } catch (NumberFormatException e) { } + } + })); + break; + } + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/EditorToolBar.java b/src/main/java/kamkeel/npcs/editorgui/EditorToolBar.java new file mode 100644 index 000000000..ed872b031 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/EditorToolBar.java @@ -0,0 +1,92 @@ +package kamkeel.npcs.editorgui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiButton; +import java.util.ArrayList; +import java.util.List; + +/** + * EditorToolBar renders a toolbar with buttons for adding new components and saving the GUI. + * Its dimensions are provided by the editor (full-screen canvas). + */ +public class EditorToolBar extends Gui { + private List buttons = new ArrayList<>(); + private int x, y, width, height; + + public EditorToolBar(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + initButtons(); + } + + private void initButtons() { + buttons.clear(); + // IDs: 1 = Add Button, 2 = Add Label, 3 = Add TextField, 4 = Add Rect, 8 = Save. + buttons.add(new GuiButton(1, x + 10, y + 10, 80, 20, "Add Button")); + buttons.add(new GuiButton(2, x + 100, y + 10, 80, 20, "Add Label")); + buttons.add(new GuiButton(3, x + 190, y + 10, 80, 20, "Add TextField")); + buttons.add(new GuiButton(4, x + 280, y + 10, 80, 20, "Add Rect")); + buttons.add(new GuiButton(8, x + 370, y + 10, 80, 20, "Save")); + } + + public void render(int mouseX, int mouseY, float partialTicks) { + // Optionally, uncomment the background draw. + // drawRect(x, y, x + width, y + height, 0xAA333333); + for (GuiButton btn : buttons) { + btn.drawButton(Minecraft.getMinecraft(), mouseX, mouseY); + } + } + + public List getButtons() { + return buttons; + } + + public void updateDimensions(int newX, int newY, int newWidth, int newHeight) { + this.x = newX; + this.y = newY; + this.width = newWidth; + this.height = newHeight; + initButtons(); + } + + private int getDefaultId() { + return -1; + } + + public void actionPerformed(GuiButton button, GuiCustomGuiEditor editor) { + int centerX = editor.canvasX + editor.canvasWidth / 2; + int centerY = editor.canvasY + editor.canvasHeight / 2; + switch (button.id) { + case 1: + noppes.npcs.scripted.gui.ScriptGuiButton newButton = + new noppes.npcs.scripted.gui.ScriptGuiButton(getDefaultId(), "New Button", centerX - 40, centerY - 10, 80, 20); + EditorButtonComponent eButton = new EditorButtonComponent(newButton); + editor.addEditorComponent(eButton); + break; + case 2: + noppes.npcs.scripted.gui.ScriptGuiLabel newLabel = + new noppes.npcs.scripted.gui.ScriptGuiLabel(getDefaultId(), "New Label", centerX - 40, centerY - 10, 80, 20); + EditorLabelComponent eLabel = new EditorLabelComponent(newLabel); + editor.addEditorComponent(eLabel); + break; + case 3: + noppes.npcs.scripted.gui.ScriptGuiTextField newField = + new noppes.npcs.scripted.gui.ScriptGuiTextField(getDefaultId(), centerX - 50, centerY - 10, 100, 20); + EditorTextFieldComponent eField = new EditorTextFieldComponent(newField); + editor.addEditorComponent(eField); + break; + case 4: + noppes.npcs.scripted.gui.ScriptGuiTexturedRect newRect = + new noppes.npcs.scripted.gui.ScriptGuiTexturedRect(getDefaultId(), "default:texture", centerX - 40, centerY - 10, 80, 20); + EditorTexturedRectComponent eRect = new EditorTexturedRectComponent(newRect); + editor.addEditorComponent(eRect); + break; + case 8: + editor.saveAndClose(); + break; + } + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/GuiCustomGuiEditor.java b/src/main/java/kamkeel/npcs/editorgui/GuiCustomGuiEditor.java new file mode 100644 index 000000000..fff6a13fb --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/GuiCustomGuiEditor.java @@ -0,0 +1,220 @@ +package kamkeel.npcs.editorgui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.ScaledResolution; +import noppes.npcs.scripted.gui.ScriptGui; +import org.lwjgl.input.Keyboard; +import org.lwjgl.input.Mouse; +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * GuiCustomGuiEditor is the main editor screen. + * The canvas covers the full scaled resolution. + * The toolbar is positioned at the bottom. + * Custom editing buttons (IDs 101–500) appear in a single row at the top with a 20-pixel height. + */ +public class GuiCustomGuiEditor extends GuiScreen { + // Full-screen canvas. + public int canvasX = 0; + public int canvasY = 0; + public int canvasWidth; + public int canvasHeight; + + private List editorComponents = new ArrayList<>(); + private EditorToolBar toolBar; + private IEditorComponent selectedComponent; + private CustomGuiEditorController controller; + + // For dragging: offset from mouse to component's top–left. + private int dragOffsetX, dragOffsetY; + private boolean isDragging = false; + + public GuiCustomGuiEditor() { + controller = new CustomGuiEditorController(); + mc = Minecraft.getMinecraft(); + } + + @Override + public void initGui() { + ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + canvasWidth = res.getScaledWidth(); + canvasHeight = res.getScaledHeight(); + canvasX = 0; + canvasY = 0; + toolBar = new EditorToolBar(canvasX, canvasY + canvasHeight - 40, canvasWidth, 40); + toolBar.updateDimensions(canvasX, canvasY + canvasHeight - 40, canvasWidth, 40); + } + + @Override + public void drawDefaultBackground() { + // Fill the full screen with a light background. + drawRect(canvasX, canvasY, canvasX + canvasWidth, canvasY + canvasHeight, 0xFFCCCCCC); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + drawDefaultBackground(); + for (IEditorComponent comp : editorComponents) { + comp.render(mouseX, mouseY, partialTicks); + } + toolBar.render(mouseX, mouseY, partialTicks); + super.drawScreen(mouseX, mouseY, partialTicks); + } + + @Override + public void mouseClicked(int mouseX, int mouseY, int mouseButton) { + // FIRST, check if any custom editing button (IDs 101-500) is clicked. + for (Object obj : this.buttonList) { + GuiButton btn = (GuiButton) obj; + if (btn.id >= 101 && btn.id < 500 && btn.mousePressed(mc, mouseX, mouseY)) { + // Dispatch custom editing button; do not clear selection. + this.actionPerformed(btn); + updateCustomButtons(); + return; + } + } + // Next, let the toolbar buttons handle the click. + for (GuiButton btn : toolBar.getButtons()) { + if (btn.mousePressed(mc, mouseX, mouseY)) { + toolBar.actionPerformed(btn, this); + updateCustomButtons(); + return; + } + } + boolean anySelected = false; + for (IEditorComponent comp : editorComponents) { + if (comp.mouseClicked(mouseX, mouseY, mouseButton)) { + selectedComponent = comp; + anySelected = true; + } else { + comp.setSelected(false); + } + } + if (!anySelected) { + selectedComponent = null; + } else if (selectedComponent != null) { + dragOffsetX = mouseX - ((AbstractEditorComponent) selectedComponent).posX; + dragOffsetY = mouseY - ((AbstractEditorComponent) selectedComponent).posY; + isDragging = true; + } + updateCustomButtons(); + super.mouseClicked(mouseX, mouseY, mouseButton); + } + + @Override + protected void mouseMovedOrUp(int mouseX, int mouseY, int state) { + if (selectedComponent != null) { + selectedComponent.mouseReleased(mouseX, mouseY, state); + } + isDragging = false; + super.mouseMovedOrUp(mouseX, mouseY, state); + } + + @Override + public void mouseClickMove(int mouseX, int mouseY, int mouseButton, long timeSinceLastClick) { + if (isDragging && selectedComponent != null) { + int newX = mouseX - dragOffsetX; + int newY = mouseY - dragOffsetY; + int compW = ((AbstractEditorComponent) selectedComponent).width; + int compH = ((AbstractEditorComponent) selectedComponent).height; + if (newX < canvasX) newX = canvasX; + if (newY < canvasY) newY = canvasY; + if (newX + compW > canvasWidth) newX = canvasWidth - compW; + if (newY + compH > canvasHeight) newY = canvasHeight - compH; + ((AbstractEditorComponent) selectedComponent).posX = newX; + ((AbstractEditorComponent) selectedComponent).posY = newY; + selectedComponent.mouseDragged(mouseX, mouseY, mouseButton); + } + super.mouseClickMove(mouseX, mouseY, mouseButton, timeSinceLastClick); + } + + @Override + public void keyTyped(char typedChar, int keyCode) { + if (keyCode == Keyboard.KEY_ESCAPE) { + saveAndClose(); + return; + } + if (keyCode == Keyboard.KEY_DELETE || keyCode == Keyboard.KEY_BACK) { + if (selectedComponent != null) { + Iterator iter = editorComponents.iterator(); + while (iter.hasNext()) { + IEditorComponent comp = iter.next(); + if (comp == selectedComponent) { + iter.remove(); + selectedComponent = null; + break; + } + } + } + } + updateCustomButtons(); + super.keyTyped(typedChar, keyCode); + } + + @Override + protected void actionPerformed(GuiButton button) { + if (button.id >= 101 && button.id < 500) { + if (selectedComponent != null) + selectedComponent.onEditorButtonPressed(button); + updateCustomButtons(); + } else { + toolBar.actionPerformed(button, this); + } + } + + /** + * Updates custom editing buttons (IDs 101–500) so they appear in a single row at the top. + * Uses a height of 20 pixels. + */ + private void updateCustomButtons() { + for (int i = buttonList.size() - 1; i >= 0; i--) { + GuiButton btn = (GuiButton) buttonList.get(i); + if (btn.id >= 101 && btn.id < 500) { + buttonList.remove(i); + } + } + if (selectedComponent != null) { + List customButtons = new ArrayList<>(); + selectedComponent.addEditorButtons(customButtons); + int startX = 5; + int startY = 5; + int buttonWidth = 30; // adjust if needed + int buttonHeight = 20; // now 20 pixels tall + int spacing = 5; + int currentX = startX; + for (GuiButton btn : customButtons) { + btn.xPosition = currentX; + btn.yPosition = startY; + btn.width = buttonWidth; + btn.height = buttonHeight; + currentX += buttonWidth + spacing; + } + buttonList.addAll(customButtons); + } + } + + /** + * Adds a new editor component. + */ + public void addEditorComponent(IEditorComponent comp) { + editorComponents.add(comp); + } + + /** + * Builds a ScriptGui from the editor components, saves it to file, and closes the editor. + */ + public void saveAndClose() { + ScriptGui newGui = new ScriptGui(); + for (IEditorComponent comp : editorComponents) { + newGui.updateComponent((noppes.npcs.api.gui.ICustomGuiComponent) comp.toScriptComponent()); + } + CustomGuiEditorController controller = new CustomGuiEditorController(); + controller.saveGui(new File("customgui.json"), newGui); + Minecraft.getMinecraft().displayGuiScreen(null); + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/IEditorComponent.java b/src/main/java/kamkeel/npcs/editorgui/IEditorComponent.java new file mode 100644 index 000000000..f52baf6d9 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/IEditorComponent.java @@ -0,0 +1,36 @@ +package kamkeel.npcs.editorgui; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.nbt.NBTTagCompound; +import java.util.List; + +/** + * IEditorComponent defines the methods that all editor wrappers must implement. + * It handles rendering (with optional editing buttons), mouse events, updating, + * conversion to the underlying ScriptGui component, and adding custom editor buttons. + */ +public interface IEditorComponent { + void render(int mouseX, int mouseY, float partialTicks); + boolean mouseClicked(int mouseX, int mouseY, int mouseButton); + void mouseDragged(int mouseX, int mouseY, int mouseButton); + void mouseReleased(int mouseX, int mouseY, int state); + void update(); + Object toScriptComponent(); + void setSelected(boolean selected); + boolean isSelected(); + + /** + * Adds custom editing buttons to the provided list. + * (IDs 101–500 are reserved for custom editing.) + */ + default void addEditorButtons(List buttonList) { } + + /** + * Called when a custom editing button is pressed. + */ + default void onEditorButtonPressed(GuiButton button) { } + + default NBTTagCompound toEditorNBT() { + return new NBTTagCompound(); + } +} diff --git a/src/main/java/kamkeel/npcs/editorgui/IPropertyEditorCallback.java b/src/main/java/kamkeel/npcs/editorgui/IPropertyEditorCallback.java new file mode 100644 index 000000000..2243f0879 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/IPropertyEditorCallback.java @@ -0,0 +1,9 @@ +package kamkeel.npcs.editorgui; + +/** + * IPropertyEditorCallback is used by the property editor (SubGuiEditProperty) + * to return the updated value. + */ +public interface IPropertyEditorCallback { + void propertyUpdated(String newValue); +} diff --git a/src/main/java/kamkeel/npcs/editorgui/SubGuiEditProperty.java b/src/main/java/kamkeel/npcs/editorgui/SubGuiEditProperty.java new file mode 100644 index 000000000..f2ce5f200 --- /dev/null +++ b/src/main/java/kamkeel/npcs/editorgui/SubGuiEditProperty.java @@ -0,0 +1,77 @@ +package kamkeel.npcs.editorgui; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.GuiTextField; +import org.lwjgl.input.Keyboard; + +/** + * SubGuiEditProperty is a pop-up for editing a property. + * It accepts a parent screen and returns to that parent when “Done” is pressed. + */ +public class SubGuiEditProperty extends GuiScreen { + private GuiScreen parent; + private IPropertyEditorCallback callback; + private GuiTextField textField; + private String propertyName; + private String initialValue; + + public SubGuiEditProperty(GuiScreen parent, String propertyName, String initialValue, IPropertyEditorCallback callback) { + this.parent = parent; + this.propertyName = propertyName; + // Ensure initialValue is not null + this.initialValue = (initialValue == null ? "" : initialValue); + this.callback = callback; + } + + @Override + public void initGui() { + Keyboard.enableRepeatEvents(true); + int w = 150; + int h = 20; + int x = (width - w) / 2; + int y = (height - h) / 2; + textField = new GuiTextField(fontRendererObj, x, y, w, h); + // Ensure the text field is set with a non-null string. + textField.setText(initialValue); + textField.setFocused(true); + buttonList.clear(); + buttonList.add(new GuiButton(0, x, y + h + 5, w, 20, "Done")); + } + + @Override + public void updateScreen() { + textField.updateCursorCounter(); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + drawDefaultBackground(); + drawCenteredString(fontRendererObj, "Edit " + propertyName, width / 2, height / 2 - 40, 0xFFFFFF); + textField.drawTextBox(); + super.drawScreen(mouseX, mouseY, partialTicks); + } + + @Override + protected void keyTyped(char typedChar, int keyCode) { + textField.textboxKeyTyped(typedChar, keyCode); + if (keyCode == Keyboard.KEY_ESCAPE) { + mc.displayGuiScreen(parent); + } + } + + @Override + protected void actionPerformed(GuiButton button) { + if (button.id == 0) { + if (callback != null) { + callback.propertyUpdated(textField.getText()); + } + mc.displayGuiScreen(parent); + } + } + + @Override + public void onGuiClosed() { + Keyboard.enableRepeatEvents(false); + } +}