From acd9cc6717b4ad69a053eb3e2e7102f1f806e87e Mon Sep 17 00:00:00 2001 From: qwertysam Date: Sun, 25 Sep 2016 18:02:13 -0400 Subject: [PATCH] Can now copy and paste HitboxToggle and HitboxTrigger info from their editors + Generalised saving and loading of HitboxToggle and HitboxTrigger information + Object can no longer be drawn when in a build mode editor --- src/ca/afroman/client/ClientGame.java | 15 +- ...xToggleReceiver.java => HitboxToggle.java} | 45 +++- .../afroman/events/HitboxToggleWrapper.java | 117 ++++++++++ src/ca/afroman/events/HitboxTrigger.java | 46 ++++ .../afroman/events/HitboxTriggerWrapper.java | 126 +++++++++++ .../gui/build/GuiHitboxToggleEditor.java | 93 +++++--- .../gui/build/GuiHitboxTriggerEditor.java | 107 ++++++---- src/ca/afroman/input/InputHandler.java | 22 ++ src/ca/afroman/level/ClientLevel.java | 54 ++--- src/ca/afroman/level/Level.java | 201 ++---------------- .../afroman/packet/PacketAddHitboxToggle.java | 4 +- .../packet/PacketEditHitboxToggle.java | 4 +- src/ca/afroman/server/ServerGame.java | 12 +- src/ca/afroman/util/ParamUtil.java | 32 +++ src/ca/afroman/util/VersionUtil.java | 2 +- 15 files changed, 584 insertions(+), 296 deletions(-) rename src/ca/afroman/events/{HitboxToggleReceiver.java => HitboxToggle.java} (68%) create mode 100644 src/ca/afroman/events/HitboxToggleWrapper.java create mode 100644 src/ca/afroman/events/HitboxTriggerWrapper.java create mode 100644 src/ca/afroman/util/ParamUtil.java diff --git a/src/ca/afroman/client/ClientGame.java b/src/ca/afroman/client/ClientGame.java index 05f12a5..7438395 100644 --- a/src/ca/afroman/client/ClientGame.java +++ b/src/ca/afroman/client/ClientGame.java @@ -33,7 +33,7 @@ import ca.afroman.entity.api.Direction; import ca.afroman.entity.api.Entity; import ca.afroman.entity.api.Hitbox; -import ca.afroman.events.HitboxToggleReceiver; +import ca.afroman.events.HitboxToggle; import ca.afroman.events.HitboxTrigger; import ca.afroman.events.IEvent; import ca.afroman.events.TriggerType; @@ -762,8 +762,9 @@ public void parsePacket(BytePacket packet) case SET_PLAYER_LOCATION: { ByteBuffer buf = ByteBuffer.wrap(packet.getContent()); + Role role = Role.fromOrdinal(buf.get()); - PlayerEntity player = getPlayer(Role.fromOrdinal(buf.get())); + PlayerEntity player = getPlayer(role); if (player != null) { @@ -771,6 +772,10 @@ public void parsePacket(BytePacket packet) player.setLastDirection(Direction.fromOrdinal(buf.get())); player.setPosition(new Vector2DDouble(buf.getInt(), buf.getInt())); } + else + { + logger().log(ALogType.WARNING, "No player with role " + role); + } } break; case CONFIRM_RECEIVED: @@ -910,7 +915,7 @@ public void parsePacket(BytePacket packet) int width = buf.getInt(); int height = buf.getInt(); - HitboxToggleReceiver trig = new HitboxToggleReceiver(true, id, x, y, width, height, null, null); + HitboxToggle trig = new HitboxToggle(true, id, x, y, width, height, null, null); trig.addToLevel(level); } else @@ -934,9 +939,9 @@ public void parsePacket(BytePacket packet) if (eHitbox != null) { - if (eHitbox instanceof HitboxToggleReceiver) + if (eHitbox instanceof HitboxToggle) { - HitboxToggleReceiver hitbox = (HitboxToggleReceiver) eHitbox; + HitboxToggle hitbox = (HitboxToggle) eHitbox; boolean enabled = buf.get() == 1; List triggersIn = ByteUtil.extractIntList(buf, Byte.MIN_VALUE, Byte.MAX_VALUE); diff --git a/src/ca/afroman/events/HitboxToggleReceiver.java b/src/ca/afroman/events/HitboxToggle.java similarity index 68% rename from src/ca/afroman/events/HitboxToggleReceiver.java rename to src/ca/afroman/events/HitboxToggle.java index 34c0aa2..82332e3 100644 --- a/src/ca/afroman/events/HitboxToggleReceiver.java +++ b/src/ca/afroman/events/HitboxToggle.java @@ -7,8 +7,9 @@ import ca.afroman.entity.api.Hitbox; import ca.afroman.entity.api.IServerClient; import ca.afroman.level.Level; +import ca.afroman.level.LevelObjectType; -public class HitboxToggleReceiver implements IEvent, IServerClient +public class HitboxToggle implements IEvent, IServerClient { private boolean isServerSide; @@ -17,12 +18,12 @@ public class HitboxToggleReceiver implements IEvent, IServerClient private List outTriggers; private Hitbox hitbox; - public HitboxToggleReceiver(boolean isServerSide, int id, double x, double y, double width, double height, List inTriggers, List outTriggers) + public HitboxToggle(boolean isServerSide, int id, double x, double y, double width, double height, List inTriggers, List outTriggers) { this(isServerSide, id, new Hitbox(id, x, y, width, height), inTriggers, outTriggers); } - public HitboxToggleReceiver(boolean isServerSide, int id, Hitbox box, List inTriggers, List outTriggers) + public HitboxToggle(boolean isServerSide, int id, Hitbox box, List inTriggers, List outTriggers) { this.isServerSide = isServerSide; level = null; @@ -156,6 +157,44 @@ public void tick() } + @Override + public String toString() + { + StringBuilder sb = new StringBuilder(); + sb.append(LevelObjectType.HITBOX_TOGGLE.toString()); + sb.append('('); + sb.append(isEnabled() ? "true" : "false"); + sb.append(", "); + sb.append(getX()); + sb.append(", "); + sb.append(getY()); + sb.append(", "); + sb.append(getWidth()); + sb.append(", "); + sb.append(getHeight()); + sb.append(", {"); + + // Saves in triggers + for (int k = 0; k < getInTriggers().size(); k++) + { + sb.append(getInTriggers().get(k)); + if (k != getInTriggers().size() - 1) sb.append(", "); + } + + sb.append("}, {"); + + // Saves out triggers + for (int k = 0; k < getOutTriggers().size(); k++) + { + sb.append(getOutTriggers().get(k)); + if (k != getOutTriggers().size() - 1) sb.append(", "); + } + + sb.append("})"); + + return sb.toString(); + } + @Override public void trigger(Entity triggerer) { diff --git a/src/ca/afroman/events/HitboxToggleWrapper.java b/src/ca/afroman/events/HitboxToggleWrapper.java new file mode 100644 index 0000000..2aae47a --- /dev/null +++ b/src/ca/afroman/events/HitboxToggleWrapper.java @@ -0,0 +1,117 @@ +package ca.afroman.events; + +import java.util.ArrayList; +import java.util.List; + +import ca.afroman.level.LevelObjectType; +import ca.afroman.util.ParamUtil; + +public class HitboxToggleWrapper +{ + /** + * Takes a saved version of a HitboxToggle's information and parses it. + * + * @param input must be formatted as it is saved. e.g. HITBOX_TOGGLE(false, 0.0, 85.0, 33.0, 4.0, {22}, {}) + * @return + */ + public static HitboxToggleWrapper fromString(String input) + { + String[] split = input.split("\\("); + LevelObjectType objectType = LevelObjectType.valueOf(split[0]); + + if (objectType == LevelObjectType.HITBOX_TOGGLE) + { + String[] split2 = split[1].split("\\)"); + String rawParameters = split2.length > 0 ? split2[0] : ""; + String[] parameters = ParamUtil.getParameters(rawParameters); + + boolean isEnabled = Boolean.parseBoolean(parameters[0]); + double x = Double.parseDouble(parameters[1]); + double y = Double.parseDouble(parameters[2]); + double width = Double.parseDouble(parameters[3]); + double height = Double.parseDouble(parameters[4]); + + String[] rSubParameters = ParamUtil.getRawSubParameters(rawParameters); + + List inTriggers = new ArrayList(); + String[] inTriggerP = ParamUtil.getParameters(rSubParameters[0]); + if (inTriggerP != null) + { + for (String e : inTriggerP) + { + inTriggers.add(Integer.parseInt(e)); + } + } + + List outTriggers = new ArrayList(); + String[] outTriggerP = ParamUtil.getParameters(rSubParameters[1]); + if (outTriggerP != null) + { + for (String e : outTriggerP) + { + outTriggers.add(Integer.parseInt(e)); + } + } + + return new HitboxToggleWrapper(isEnabled, x, y, width, height, inTriggers, outTriggers); + } + else + { + return null; + } + } + private boolean isEnabled; + private double x; + private double y; + private double width; + private double height; + private List inTriggers; + + private List outTriggers; + + public HitboxToggleWrapper(boolean isEnabled, double x, double y, double width, double height, List inTriggers, List outTriggers) + { + this.isEnabled = isEnabled; + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.inTriggers = inTriggers; + this.outTriggers = outTriggers; + } + + public double getHeight() + { + return height; + } + + public List getInTriggers() + { + return inTriggers; + } + + public List getOutTriggers() + { + return outTriggers; + } + + public double getWidth() + { + return width; + } + + public double getX() + { + return x; + } + + public double getY() + { + return y; + } + + public boolean isEnabled() + { + return isEnabled; + } +} diff --git a/src/ca/afroman/events/HitboxTrigger.java b/src/ca/afroman/events/HitboxTrigger.java index 6fabb9e..13c42f7 100644 --- a/src/ca/afroman/events/HitboxTrigger.java +++ b/src/ca/afroman/events/HitboxTrigger.java @@ -9,6 +9,7 @@ import ca.afroman.entity.api.IServerClient; import ca.afroman.input.InputType; import ca.afroman.level.Level; +import ca.afroman.level.LevelObjectType; import ca.afroman.packet.PacketActivateTrigger; import ca.afroman.resource.IDCounter; import ca.afroman.server.ServerGame; @@ -214,6 +215,51 @@ public void tick() } } + @Override + public String toString() + { + StringBuilder sb = new StringBuilder(); + sb.append(LevelObjectType.HITBOX_TRIGGER.toString()); + sb.append('('); + sb.append(getX()); + sb.append(", "); + sb.append(getY()); + sb.append(", "); + sb.append(getWidth()); + sb.append(", "); + sb.append(getHeight()); + sb.append(", {"); + + // Saves trigger types + for (int k = 0; k < getTriggerTypes().size(); k++) + { + sb.append(getTriggerTypes().get(k).toString()); + if (k != getTriggerTypes().size() - 1) sb.append(", "); + } + + sb.append("}, {"); + + // Saves in triggers + for (int k = 0; k < getInTriggers().size(); k++) + { + sb.append(getInTriggers().get(k)); + if (k != getInTriggers().size() - 1) sb.append(", "); + } + + sb.append("}, {"); + + // Saves out triggers + for (int k = 0; k < getOutTriggers().size(); k++) + { + sb.append(getOutTriggers().get(k)); + if (k != getOutTriggers().size() - 1) sb.append(", "); + } + + sb.append("})"); + + return sb.toString(); + } + @Override public void trigger(Entity triggerer) { diff --git a/src/ca/afroman/events/HitboxTriggerWrapper.java b/src/ca/afroman/events/HitboxTriggerWrapper.java new file mode 100644 index 0000000..b1e8931 --- /dev/null +++ b/src/ca/afroman/events/HitboxTriggerWrapper.java @@ -0,0 +1,126 @@ +package ca.afroman.events; + +import java.util.ArrayList; +import java.util.List; + +import ca.afroman.level.LevelObjectType; +import ca.afroman.util.ParamUtil; + +public class HitboxTriggerWrapper +{ + /** + * Takes a saved version of a HitboxToggle's information and parses it. + * + * @param input must be formatted as it is saved. e.g. HITBOX_TRIGGER(-1.0, 82.0, 35.0, 10.0, {PLAYER_COLLIDE, PLAYER_UNCOLLIDE}, {}, {22}) + * @return + */ + public static HitboxTriggerWrapper fromString(String input) + { + String[] split = input.split("\\("); + LevelObjectType objectType = LevelObjectType.valueOf(split[0]); + + if (objectType == LevelObjectType.HITBOX_TRIGGER) + { + String[] split2 = split[1].split("\\)"); + String rawParameters = split2.length > 0 ? split2[0] : ""; + String[] parameters = ParamUtil.getParameters(rawParameters); + + double x = Double.parseDouble(parameters[0]); + double y = Double.parseDouble(parameters[1]); + double width = Double.parseDouble(parameters[2]); + double height = Double.parseDouble(parameters[3]); + + String[] rSubParameters = ParamUtil.getRawSubParameters(rawParameters); + + List triggerTypes = new ArrayList(); + String[] triggerParameters = ParamUtil.getParameters(rSubParameters[0]); + if (triggerParameters != null) + { + for (String e : triggerParameters) + { + triggerTypes.add(TriggerType.valueOf(e)); + } + } + + List inTriggers = new ArrayList(); + String[] inTriggerP = ParamUtil.getParameters(rSubParameters[1]); + if (inTriggerP != null) + { + for (String e : inTriggerP) + { + inTriggers.add(Integer.parseInt(e)); + } + } + + List outTriggers = new ArrayList(); + String[] outTriggerP = ParamUtil.getParameters(rSubParameters[2]); + if (outTriggerP != null) + { + for (String e : outTriggerP) + { + outTriggers.add(Integer.parseInt(e)); + } + } + + return new HitboxTriggerWrapper(x, y, width, height, triggerTypes, inTriggers, outTriggers); + } + else + { + return null; + } + } + private double x; + private double y; + private double width; + private double height; + private List triggers; + private List inTriggers; + + private List outTriggers; + + public HitboxTriggerWrapper(double x, double y, double width, double height, List triggers, List inTriggers, List outTriggers) + { + this.triggers = triggers; + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.inTriggers = inTriggers; + this.outTriggers = outTriggers; + } + + public double getHeight() + { + return height; + } + + public List getInTriggers() + { + return inTriggers; + } + + public List getOutTriggers() + { + return outTriggers; + } + + public List getTriggers() + { + return triggers; + } + + public double getWidth() + { + return width; + } + + public double getX() + { + return x; + } + + public double getY() + { + return y; + } +} diff --git a/src/ca/afroman/gui/build/GuiHitboxToggleEditor.java b/src/ca/afroman/gui/build/GuiHitboxToggleEditor.java index 3558bd5..d34479e 100644 --- a/src/ca/afroman/gui/build/GuiHitboxToggleEditor.java +++ b/src/ca/afroman/gui/build/GuiHitboxToggleEditor.java @@ -5,13 +5,16 @@ import ca.afroman.assets.Texture; import ca.afroman.client.ClientGame; -import ca.afroman.events.HitboxToggleReceiver; +import ca.afroman.events.HitboxToggle; +import ca.afroman.events.HitboxToggleWrapper; import ca.afroman.gui.GuiScreen; import ca.afroman.gui.GuiTextButton; import ca.afroman.gui.GuiTextField; +import ca.afroman.input.InputHandler; import ca.afroman.input.TypingMode; import ca.afroman.level.ClientLevel; import ca.afroman.level.LevelObjectType; +import ca.afroman.log.ALogType; import ca.afroman.packet.PacketEditHitboxToggle; import ca.afroman.packet.PacketRemoveLevelObject; import ca.afroman.resource.Vector2DInt; @@ -24,64 +27,44 @@ public class GuiHitboxToggleEditor extends GuiScreen private GuiTextButton finish; private GuiTextButton cancel; - private GuiTextButton delete; private GuiTextButton enabled; private boolean isEnabled; private ClientLevel level; - private int triggerID; + private HitboxToggle trigger; public GuiHitboxToggleEditor(ClientLevel level, int triggerID) { super(null); this.level = level; - this.triggerID = triggerID; int width = (ClientGame.WIDTH - 40); - HitboxToggleReceiver trigger = (HitboxToggleReceiver) level.getScriptedEvent(triggerID); + trigger = (HitboxToggle) level.getScriptedEvent(triggerID); isEnabled = trigger.isEnabled(); - StringBuilder sb2 = new StringBuilder(); - - for (int e : trigger.getInTriggers()) - { - sb2.append(e); - sb2.append(','); - } - inTriggers = new GuiTextField(this, 20, 58, width); - inTriggers.setText(sb2.toString()); inTriggers.setTypingMode(TypingMode.ONLY_NUMBERS_AND_COMMA); inTriggers.setMaxLength(5000); addButton(inTriggers); - StringBuilder sb3 = new StringBuilder(); - - for (int e : trigger.getOutTriggers()) - { - sb3.append(e); - sb3.append(','); - } - outTriggers = new GuiTextField(this, 20, 88, width); - outTriggers.setText(sb3.toString()); outTriggers.setTypingMode(TypingMode.ONLY_NUMBERS_AND_COMMA); outTriggers.setMaxLength(5000); addButton(outTriggers); cancel = new GuiTextButton(this, 201, (ClientGame.WIDTH / 2) + 8, 112, 84, blackFont, "Cancel"); - delete = new GuiTextButton(this, 202, (ClientGame.WIDTH / 2) + 46, 6, 54, blackFont, "Delete"); finish = new GuiTextButton(this, 200, (ClientGame.WIDTH / 2) - 84 - 8, 112, 84, blackFont, "Finished"); - enabled = new GuiTextButton(this, 203, (ClientGame.WIDTH / 2) - 33, 26, 66, blackFont, (isEnabled ? "0" : "X") + " Enabled"); + enabled = new GuiTextButton(this, 203, (ClientGame.WIDTH / 2) - 33, 26, 66, blackFont, " Enabled"); addButton(cancel); - addButton(delete); addButton(finish); addButton(enabled); - keyTyped(); + addButton(new GuiTextButton(this, 202, (ClientGame.WIDTH / 2) + 60, 6, 54, blackFont, "Delete")); + addButton(new GuiTextButton(this, 204, (ClientGame.WIDTH / 2) + 14, 6, 42, blackFont, "Copy")); + initInfo(isEnabled, trigger.getInTriggers(), trigger.getOutTriggers()); } @Override @@ -91,6 +74,34 @@ public void drawScreen(Texture renderTo) nobleFont.render(renderTo, new Vector2DInt(36, 78), "Out Triggers"); } + private void initInfo(boolean isEnabled, List inTriggers, List outTriggers) + { + this.isEnabled = isEnabled; + enabled.setText((isEnabled ? "0" : "X") + " Enabled"); + + StringBuilder sb2 = new StringBuilder(); + + for (int e : inTriggers) + { + sb2.append(e); + sb2.append(','); + } + + this.inTriggers.setText(sb2.toString()); + + StringBuilder sb3 = new StringBuilder(); + + for (int e : outTriggers) + { + sb3.append(e); + sb3.append(','); + } + + this.outTriggers.setText(sb3.toString()); + + keyTyped(); + } + @Override public void keyTyped() { @@ -147,8 +158,11 @@ public void pressAction(int buttonID) switch (buttonID) { + case 204:// COPY + ClientGame.instance().input().setClipboard(trigger.toString()); + break; case 202: - ClientGame.instance().sockets().sender().sendPacket(new PacketRemoveLevelObject(triggerID, level.getType(), LevelObjectType.HITBOX_TRIGGER)); + ClientGame.instance().sockets().sender().sendPacket(new PacketRemoveLevelObject(trigger.getID(), level.getType(), LevelObjectType.HITBOX_TRIGGER)); case 201: goToParentScreen(); break; @@ -173,7 +187,7 @@ public void pressAction(int buttonID) } } - PacketEditHitboxToggle pack = new PacketEditHitboxToggle(level.getType(), isEnabled, triggerID, inTriggers, outTriggers); + PacketEditHitboxToggle pack = new PacketEditHitboxToggle(level.getType(), isEnabled, trigger.getID(), inTriggers, outTriggers); ClientGame.instance().sockets().sender().sendPacket(pack); goToParentScreen(); break; @@ -195,6 +209,27 @@ public void tick() { super.tick(); + if (ClientGame.instance().input().control.isPressed() && ClientGame.instance().input().v.isPressedFiltered()) + { + try + { + HitboxToggleWrapper w = HitboxToggleWrapper.fromString(InputHandler.getClipboard()); + + if (w != null) + { + initInfo(w.isEnabled(), w.getInTriggers(), w.getOutTriggers()); + } + else + { + ClientGame.instance().logger().log(ALogType.DEBUG, "Failed to parse pasted text into GuiHitboxTriggerEditor"); + } + } + catch (Exception e) + { + ClientGame.instance().logger().log(ALogType.DEBUG, "Failed to parse pasted text into GuiHitboxTriggerEditor"); + } + } + if (ClientGame.instance().input().escape.isPressedFiltered()) { goToParentScreen(); diff --git a/src/ca/afroman/gui/build/GuiHitboxTriggerEditor.java b/src/ca/afroman/gui/build/GuiHitboxTriggerEditor.java index 8409d05..6e85038 100644 --- a/src/ca/afroman/gui/build/GuiHitboxTriggerEditor.java +++ b/src/ca/afroman/gui/build/GuiHitboxTriggerEditor.java @@ -6,13 +6,16 @@ import ca.afroman.assets.Texture; import ca.afroman.client.ClientGame; import ca.afroman.events.HitboxTrigger; +import ca.afroman.events.HitboxTriggerWrapper; import ca.afroman.events.TriggerType; import ca.afroman.gui.GuiScreen; import ca.afroman.gui.GuiTextButton; import ca.afroman.gui.GuiTextField; +import ca.afroman.input.InputHandler; import ca.afroman.input.TypingMode; import ca.afroman.level.ClientLevel; import ca.afroman.level.LevelObjectType; +import ca.afroman.log.ALogType; import ca.afroman.packet.PacketEditTrigger; import ca.afroman.packet.PacketRemoveLevelObject; import ca.afroman.resource.Vector2DInt; @@ -26,73 +29,44 @@ public class GuiHitboxTriggerEditor extends GuiScreen private GuiTextButton finish; private GuiTextButton cancel; - private GuiTextButton delete; private ClientLevel level; - private int triggerID; + private HitboxTrigger trigger; public GuiHitboxTriggerEditor(ClientLevel level, int triggerID) { super(null); this.level = level; - this.triggerID = triggerID; int width = (ClientGame.WIDTH - 40); - HitboxTrigger trigger = (HitboxTrigger) level.getScriptedEvent(triggerID); - - StringBuilder sb = new StringBuilder(); - - for (TriggerType t : trigger.getTriggerTypes()) - { - sb.append(t.ordinal()); - sb.append(','); - } + trigger = (HitboxTrigger) level.getScriptedEvent(triggerID); triggers = new GuiTextField(this, 20, 28, width); - triggers.setFocussed(); - triggers.setText(sb.toString()); triggers.setMaxLength(5000); triggers.setTypingMode(TypingMode.ONLY_NUMBERS_AND_COMMA); addButton(triggers); - StringBuilder sb2 = new StringBuilder(); - - for (int e : trigger.getInTriggers()) - { - sb2.append(e); - sb2.append(','); - } - inTriggers = new GuiTextField(this, 20, 58, width); - inTriggers.setText(sb2.toString()); + inTriggers.setTypingMode(TypingMode.ONLY_NUMBERS_AND_COMMA); inTriggers.setMaxLength(5000); addButton(inTriggers); - StringBuilder sb3 = new StringBuilder(); - - for (int e : trigger.getOutTriggers()) - { - sb3.append(e); - sb3.append(','); - } - outTriggers = new GuiTextField(this, 20, 88, width); - outTriggers.setText(sb3.toString()); outTriggers.setTypingMode(TypingMode.ONLY_NUMBERS_AND_COMMA); outTriggers.setMaxLength(5000); addButton(outTriggers); cancel = new GuiTextButton(this, 201, (ClientGame.WIDTH / 2) + 8, 112, 84, blackFont, "Cancel"); - delete = new GuiTextButton(this, 202, (ClientGame.WIDTH / 2) + 46, 6, 54, blackFont, "Delete"); finish = new GuiTextButton(this, 200, (ClientGame.WIDTH / 2) - 84 - 8, 112, 84, blackFont, "Finished"); addButton(cancel); - addButton(delete); addButton(finish); - keyTyped(); + addButton(new GuiTextButton(this, 202, (ClientGame.WIDTH / 2) + 60, 6, 54, blackFont, "Delete")); + addButton(new GuiTextButton(this, 204, (ClientGame.WIDTH / 2) + 14, 6, 42, blackFont, "Copy")); + initInfo(trigger.getTriggerTypes(), trigger.getInTriggers(), trigger.getOutTriggers()); } @Override @@ -103,6 +77,41 @@ public void drawScreen(Texture renderTo) nobleFont.render(renderTo, new Vector2DInt(36, 78), "Out Triggers"); } + private void initInfo(List triggers, List inTriggers, List outTriggers) + { + StringBuilder sb = new StringBuilder(); + + for (TriggerType t : triggers) + { + sb.append(t.ordinal()); + sb.append(','); + } + + this.triggers.setText(sb.toString()); + + StringBuilder sb2 = new StringBuilder(); + + for (int e : inTriggers) + { + sb2.append(e); + sb2.append(','); + } + + this.inTriggers.setText(sb2.toString()); + + StringBuilder sb3 = new StringBuilder(); + + for (int e : outTriggers) + { + sb3.append(e); + sb3.append(','); + } + + this.outTriggers.setText(sb3.toString()); + + keyTyped(); + } + @Override public void keyTyped() { @@ -184,8 +193,11 @@ public void pressAction(int buttonID) switch (buttonID) { + case 204: // COPY + ClientGame.instance().input().setClipboard(trigger.toString()); + break; case 202: - ClientGame.instance().sockets().sender().sendPacket(new PacketRemoveLevelObject(triggerID, level.getType(), LevelObjectType.HITBOX_TRIGGER)); + ClientGame.instance().sockets().sender().sendPacket(new PacketRemoveLevelObject(trigger.getID(), level.getType(), LevelObjectType.HITBOX_TRIGGER)); case 201: goToParentScreen(); break; @@ -220,7 +232,7 @@ public void pressAction(int buttonID) } } - PacketEditTrigger pack = new PacketEditTrigger(level.getType(), triggerID, triggers, inTriggers, outTriggers); + PacketEditTrigger pack = new PacketEditTrigger(level.getType(), trigger.getID(), triggers, inTriggers, outTriggers); ClientGame.instance().sockets().sender().sendPacket(pack); goToParentScreen(); break; @@ -238,6 +250,27 @@ public void tick() { super.tick(); + if (ClientGame.instance().input().control.isPressed() && ClientGame.instance().input().v.isPressedFiltered()) + { + try + { + HitboxTriggerWrapper w = HitboxTriggerWrapper.fromString(InputHandler.getClipboard()); + + if (w != null) + { + initInfo(w.getTriggers(), w.getInTriggers(), w.getOutTriggers()); + } + else + { + ClientGame.instance().logger().log(ALogType.DEBUG, "Failed to parse pasted text into GuiHitboxTriggerEditor"); + } + } + catch (Exception e) + { + ClientGame.instance().logger().log(ALogType.DEBUG, "Failed to parse pasted text into GuiHitboxTriggerEditor"); + } + } + if (ClientGame.instance().input().escape.isPressedFiltered()) { goToParentScreen(); diff --git a/src/ca/afroman/input/InputHandler.java b/src/ca/afroman/input/InputHandler.java index 98a9316..25980eb 100644 --- a/src/ca/afroman/input/InputHandler.java +++ b/src/ca/afroman/input/InputHandler.java @@ -2,7 +2,9 @@ import java.awt.HeadlessException; import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; @@ -258,6 +260,26 @@ public void mouseWheelMoved(MouseWheelEvent e) mouseWheelUp.setPressed((e.getWheelRotation() < 0)); } + public void setClipboard(List toAdd) + { + setClipboard((String[]) toAdd.toArray()); + } + + public void setClipboard(String... toAdd) + { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < toAdd.length - 1; i++) + { + sb.append(toAdd[i]); + sb.append("\n"); + } + sb.append(toAdd[toAdd.length - 1]); + + StringSelection stringSelection = new StringSelection(sb.toString()); + Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); + clpbrd.setContents(stringSelection, null); + } + public void toggleKey(int keyCode, boolean isPressed) { for (Key key : keys) diff --git a/src/ca/afroman/level/ClientLevel.java b/src/ca/afroman/level/ClientLevel.java index a135b33..3b32839 100644 --- a/src/ca/afroman/level/ClientLevel.java +++ b/src/ca/afroman/level/ClientLevel.java @@ -19,7 +19,7 @@ import ca.afroman.entity.api.Entity; import ca.afroman.entity.api.Hitbox; import ca.afroman.entity.api.YComparator; -import ca.afroman.events.HitboxToggleReceiver; +import ca.afroman.events.HitboxToggle; import ca.afroman.events.HitboxTrigger; import ca.afroman.events.IEvent; import ca.afroman.gfx.LightMap; @@ -117,13 +117,13 @@ public Vector2DDouble getCameraOffset() } // Only used for right clicks in Build Mode - private HitboxToggleReceiver getHitboxToggle(Vector2DDouble pos) + private HitboxToggle getHitboxToggle(Vector2DDouble pos) { for (IEvent event : getScriptedEvents()) { - if (event instanceof HitboxToggleReceiver) + if (event instanceof HitboxToggle) { - if (new Rectangle2D.Double(event.getX(), event.getY(), event.getWidth(), event.getHeight()).contains(pos.getX(), pos.getY())) return (HitboxToggleReceiver) event; + if (new Rectangle2D.Double(event.getX(), event.getY(), event.getWidth(), event.getHeight()).contains(pos.getX(), pos.getY())) return (HitboxToggle) event; } } return null; @@ -204,7 +204,7 @@ private void hitboxBehaviour(BuildMode mode, boolean leftClick) } else { - HitboxToggleReceiver event = getHitboxToggle(screenToWorld(ClientGame.instance().input().getMousePos())); + HitboxToggle event = getHitboxToggle(screenToWorld(ClientGame.instance().input().getMousePos())); if (event != null) { @@ -216,6 +216,7 @@ private void hitboxBehaviour(BuildMode mode, boolean leftClick) } break; } + } private void loadBuildMode(BuildMode mode) @@ -446,7 +447,7 @@ public void render(Texture renderTo) { renderTo.drawRect(new Color(0.3F, 0.3F, 1F, 1F), pos, (int) e.getWidth(), (int) e.getHeight());// Blue } - else if (e instanceof HitboxToggleReceiver && (ClientGame.instance().isHitboxDebugging() || (buildMode == BuildMode.HITBOX_TOGGLE))) + else if (e instanceof HitboxToggle && (ClientGame.instance().isHitboxDebugging() || (buildMode == BuildMode.HITBOX_TOGGLE))) { renderTo.drawRect(new Color(1F, 0.3F, 0.3F, 1F), pos, (int) e.getWidth(), (int) e.getHeight());// Red } @@ -685,29 +686,32 @@ else if (!newIsBuildMode && lastIsBuildMode) case HITBOX: case TRIGGER: case HITBOX_TOGGLE: - if (ClientGame.instance().input().mouseLeft.isPressedFiltered()) + if (ClientGame.instance().getCurrentScreen() instanceof GuiGrid || ClientGame.instance().getCurrentScreen() == null) { - if (hitboxClickCount == 0) - { - hitboxClickCount = 1; - hitbox1.setPosition(screenToWorld(ClientGame.instance().input().getMousePos())).add(1, 1); - } - else if (hitboxClickCount == 1) + if (ClientGame.instance().input().mouseLeft.isPressedFiltered()) { - hitboxClickCount = 0; - hitboxBehaviour(buildMode, true); + if (hitboxClickCount == 0) + { + hitboxClickCount = 1; + hitbox1.setPosition(screenToWorld(ClientGame.instance().input().getMousePos())).add(1, 1); + } + else if (hitboxClickCount == 1) + { + hitboxClickCount = 0; + hitboxBehaviour(buildMode, true); + } } - } - - if (ClientGame.instance().input().mouseRight.isPressedFiltered()) - { - if (hitboxClickCount == 1) - { - hitboxClickCount = 0; - } - else + + if (ClientGame.instance().input().mouseRight.isPressedFiltered()) { - hitboxBehaviour(buildMode, false); + if (hitboxClickCount == 1) + { + hitboxClickCount = 0; + } + else + { + hitboxBehaviour(buildMode, false); + } } } break; diff --git a/src/ca/afroman/level/Level.java b/src/ca/afroman/level/Level.java index 776a9f6..7df5795 100644 --- a/src/ca/afroman/level/Level.java +++ b/src/ca/afroman/level/Level.java @@ -1,19 +1,19 @@ package ca.afroman.level; -import java.awt.Toolkit; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.List; import ca.afroman.assets.AssetType; +import ca.afroman.client.ClientGame; import ca.afroman.entity.PlayerEntity; import ca.afroman.entity.api.Entity; import ca.afroman.entity.api.Hitbox; import ca.afroman.entity.api.IServerClient; -import ca.afroman.events.HitboxToggleReceiver; +import ca.afroman.events.HitboxToggle; +import ca.afroman.events.HitboxToggleWrapper; import ca.afroman.events.HitboxTrigger; +import ca.afroman.events.HitboxTriggerWrapper; import ca.afroman.events.IEvent; import ca.afroman.events.TriggerType; import ca.afroman.gfx.FlickeringLight; @@ -23,6 +23,7 @@ import ca.afroman.resource.Vector2DDouble; import ca.afroman.server.ServerGame; import ca.afroman.util.FileUtil; +import ca.afroman.util.ParamUtil; public class Level implements IServerClient { @@ -45,7 +46,7 @@ public static Level fromFile(boolean isServerSide, LevelType levelType) LevelObjectType objectType = LevelObjectType.valueOf(split[0]); String[] split2 = split[1].split("\\)"); String rawParameters = split2.length > 0 ? split2[0] : ""; - String[] parameters = getParameters(rawParameters); + String[] parameters = ParamUtil.getParameters(rawParameters); switch (objectType) { @@ -96,79 +97,18 @@ public static Level fromFile(boolean isServerSide, LevelType levelType) break; case HITBOX_TRIGGER: { - double x = Double.parseDouble(parameters[0]); - double y = Double.parseDouble(parameters[1]); - double width = Double.parseDouble(parameters[2]); - double height = Double.parseDouble(parameters[3]); + HitboxTriggerWrapper w = HitboxTriggerWrapper.fromString(line); - String[] rSubParameters = getRawSubParameters(rawParameters); - - List triggerTypes = new ArrayList(); - String[] triggerParameters = getParameters(rSubParameters[0]); - if (triggerParameters != null) - { - for (String e : triggerParameters) - { - triggerTypes.add(TriggerType.valueOf(e)); - } - } - - List inTriggers = new ArrayList(); - String[] inTriggerP = getParameters(rSubParameters[1]); - if (inTriggerP != null) - { - for (String e : inTriggerP) - { - inTriggers.add(Integer.parseInt(e)); - } - } - - List outTriggers = new ArrayList(); - String[] outTriggerP = getParameters(rSubParameters[2]); - if (outTriggerP != null) - { - for (String e : outTriggerP) - { - outTriggers.add(Integer.parseInt(e)); - } - } - - new HitboxTrigger(isServerSide, HitboxTrigger.getIDCounter().getNext(), x, y, width, height, triggerTypes, inTriggers, outTriggers).addToLevel(level);; + new HitboxTrigger(isServerSide, HitboxTrigger.getIDCounter().getNext(), w.getX(), w.getY(), w.getWidth(), w.getHeight(), w.getTriggers(), w.getInTriggers(), w.getOutTriggers()).addToLevel(level);; } break; case HITBOX_TOGGLE: { - boolean isEnabled = Boolean.parseBoolean(parameters[0]); - double x = Double.parseDouble(parameters[1]); - double y = Double.parseDouble(parameters[2]); - double width = Double.parseDouble(parameters[3]); - double height = Double.parseDouble(parameters[4]); + HitboxToggleWrapper w = HitboxToggleWrapper.fromString(line); - String[] rSubParameters = getRawSubParameters(rawParameters); - - List inTriggers = new ArrayList(); - String[] inTriggerP = getParameters(rSubParameters[0]); - if (inTriggerP != null) - { - for (String e : inTriggerP) - { - inTriggers.add(Integer.parseInt(e)); - } - } - - List outTriggers = new ArrayList(); - String[] outTriggerP = getParameters(rSubParameters[1]); - if (outTriggerP != null) - { - for (String e : outTriggerP) - { - outTriggers.add(Integer.parseInt(e)); - } - } - - HitboxToggleReceiver r = new HitboxToggleReceiver(isServerSide, HitboxTrigger.getIDCounter().getNext(), x, y, width, height, inTriggers, outTriggers); + HitboxToggle r = new HitboxToggle(isServerSide, HitboxTrigger.getIDCounter().getNext(), w.getX(), w.getY(), w.getWidth(), w.getHeight(), w.getInTriggers(), w.getOutTriggers()); r.addToLevel(level); - r.setEnabled(isEnabled); + r.setEnabled(w.isEnabled()); } break; } @@ -185,34 +125,6 @@ public static Level fromFile(boolean isServerSide, LevelType levelType) return level; } - public static String[] getParameters(String in) - { - return in.length() > 0 ? in.split(", ") : null; - } - - public static String[] getRawSubParameters(String in) - { - int count = in.split("\\{").length - 1; - - if (count >= 1) - { - String[] ret = new String[count]; - - // isolates all the sub-parameters - for (int i = 0; i < count; i++) - { - String[] r1 = in.split("\\{")[1 + i].split("\\}"); - ret[i] = r1.length > 0 ? r1[0] : ""; - } - - return ret; - } - else - { - return null; - } - } - private boolean isServerSide; /** The level identified. */ private LevelType type; @@ -633,48 +545,7 @@ public List toSaveFile() { if (e instanceof HitboxTrigger) { - HitboxTrigger t = (HitboxTrigger) e; - - StringBuilder sb = new StringBuilder(); - sb.append(LevelObjectType.HITBOX_TRIGGER.toString()); - sb.append('('); - sb.append(e.getX()); - sb.append(", "); - sb.append(e.getY()); - sb.append(", "); - sb.append(e.getWidth()); - sb.append(", "); - sb.append(e.getHeight()); - sb.append(", {"); - - // Saves trigger types - for (int k = 0; k < t.getTriggerTypes().size(); k++) - { - sb.append(t.getTriggerTypes().get(k).toString()); - if (k != t.getTriggerTypes().size() - 1) sb.append(", "); - } - - sb.append("}, {"); - - // Saves in triggers - for (int k = 0; k < t.getInTriggers().size(); k++) - { - sb.append(t.getInTriggers().get(k)); - if (k != t.getInTriggers().size() - 1) sb.append(", "); - } - - sb.append("}, {"); - - // Saves out triggers - for (int k = 0; k < t.getOutTriggers().size(); k++) - { - sb.append(t.getOutTriggers().get(k)); - if (k != t.getOutTriggers().size() - 1) sb.append(", "); - } - - sb.append("})"); - - toReturn.add(sb.toString()); + toReturn.add(e.toString()); } } @@ -685,56 +556,14 @@ public List toSaveFile() for (IEvent e : getScriptedEvents()) { - if (e instanceof HitboxToggleReceiver) + if (e instanceof HitboxToggle) { - HitboxToggleReceiver t = (HitboxToggleReceiver) e; - - StringBuilder sb = new StringBuilder(); - sb.append(LevelObjectType.HITBOX_TOGGLE.toString()); - sb.append('('); - sb.append(t.isEnabled() ? "true" : "false"); - sb.append(", "); - sb.append(e.getX()); - sb.append(", "); - sb.append(e.getY()); - sb.append(", "); - sb.append(e.getWidth()); - sb.append(", "); - sb.append(e.getHeight()); - sb.append(", {"); - - // Saves in triggers - for (int k = 0; k < t.getInTriggers().size(); k++) - { - sb.append(t.getInTriggers().get(k)); - if (k != t.getInTriggers().size() - 1) sb.append(", "); - } - - sb.append("}, {"); - - // Saves out triggers - for (int k = 0; k < t.getOutTriggers().size(); k++) - { - sb.append(t.getOutTriggers().get(k)); - if (k != t.getOutTriggers().size() - 1) sb.append(", "); - } - - sb.append("})"); - - toReturn.add(sb.toString()); + toReturn.add(e.toString()); } } // Copies the level data to the clipboard - StringBuilder sb = new StringBuilder(); - for (String copy : toReturn) - { - sb.append(copy); - sb.append("\n"); - } - StringSelection stringSelection = new StringSelection(sb.toString()); - Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); - clpbrd.setContents(stringSelection, null); + ClientGame.instance().input().setClipboard(toReturn); return toReturn; } diff --git a/src/ca/afroman/packet/PacketAddHitboxToggle.java b/src/ca/afroman/packet/PacketAddHitboxToggle.java index 45d87e1..9436c11 100644 --- a/src/ca/afroman/packet/PacketAddHitboxToggle.java +++ b/src/ca/afroman/packet/PacketAddHitboxToggle.java @@ -2,7 +2,7 @@ import java.nio.ByteBuffer; -import ca.afroman.events.HitboxToggleReceiver; +import ca.afroman.events.HitboxToggle; import ca.afroman.level.LevelType; import ca.afroman.network.IPConnection; import ca.afroman.util.ByteUtil; @@ -11,7 +11,7 @@ public class PacketAddHitboxToggle extends BytePacket { private byte[] toSend; - public PacketAddHitboxToggle(LevelType level, HitboxToggleReceiver hitbox, IPConnection... connection) + public PacketAddHitboxToggle(LevelType level, HitboxToggle hitbox, IPConnection... connection) { this(level, hitbox.getID(), (int) hitbox.getX(), (int) hitbox.getY(), (int) hitbox.getWidth(), (int) hitbox.getHeight(), connection); } diff --git a/src/ca/afroman/packet/PacketEditHitboxToggle.java b/src/ca/afroman/packet/PacketEditHitboxToggle.java index 677b17e..df037be 100644 --- a/src/ca/afroman/packet/PacketEditHitboxToggle.java +++ b/src/ca/afroman/packet/PacketEditHitboxToggle.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.List; -import ca.afroman.events.HitboxToggleReceiver; +import ca.afroman.events.HitboxToggle; import ca.afroman.level.LevelType; import ca.afroman.network.IPConnection; import ca.afroman.util.ByteUtil; @@ -65,7 +65,7 @@ public PacketEditHitboxToggle(LevelType level, boolean enabled, int id, List triggersIn = ByteUtil.extractIntList(buf, Byte.MIN_VALUE, Byte.MAX_VALUE); diff --git a/src/ca/afroman/util/ParamUtil.java b/src/ca/afroman/util/ParamUtil.java new file mode 100644 index 0000000..0aff2b2 --- /dev/null +++ b/src/ca/afroman/util/ParamUtil.java @@ -0,0 +1,32 @@ +package ca.afroman.util; + +public class ParamUtil +{ + public static String[] getParameters(String in) + { + return in.length() > 0 ? in.split(", ") : null; + } + + public static String[] getRawSubParameters(String in) + { + int count = in.split("\\{").length - 1; + + if (count >= 1) + { + String[] ret = new String[count]; + + // isolates all the sub-parameters + for (int i = 0; i < count; i++) + { + String[] r1 = in.split("\\{")[1 + i].split("\\}"); + ret[i] = r1.length > 0 ? r1[0] : ""; + } + + return ret; + } + else + { + return null; + } + } +} diff --git a/src/ca/afroman/util/VersionUtil.java b/src/ca/afroman/util/VersionUtil.java index 0758f29..629df1b 100644 --- a/src/ca/afroman/util/VersionUtil.java +++ b/src/ca/afroman/util/VersionUtil.java @@ -3,7 +3,7 @@ public class VersionUtil { // Sub versions are backwards compatible with each other - public static final int SUB_VERSION = 0; + public static final int SUB_VERSION = 1; public static final byte DAY = 24; public static final byte MONTH = 9;