From 8872e35c98d36bac5ef8c1b25d27c2e77b34a61e Mon Sep 17 00:00:00 2001 From: Rex Chan Date: Thu, 15 Mar 2018 13:03:33 -0700 Subject: [PATCH] Added Attitude Editor --- src/StellarisDK/FileClasses/Attitude.java | 28 ++++ src/StellarisDK/FileClasses/DataParser.java | 2 + src/StellarisDK/FileClasses/GenericData.java | 23 ++-- .../FileClasses/Helper/DataCell.java | 9 +- .../FileClasses/Helper/DataMap.java | 7 + .../FileClasses/Helper/EntryArrayList.java | 13 ++ src/StellarisDK/GUI/AmbientObjectUI.java | 12 +- src/StellarisDK/GUI/AttitudeUI.java | 120 ++++++++++++++++++ src/StellarisDK/GUI/FXML/attitudeFX.fxml | 38 ++++++ src/StellarisDK/GUI/guiController.java | 36 +++--- 10 files changed, 248 insertions(+), 40 deletions(-) create mode 100644 src/StellarisDK/FileClasses/Attitude.java create mode 100644 src/StellarisDK/GUI/AttitudeUI.java create mode 100644 src/StellarisDK/GUI/FXML/attitudeFX.fxml diff --git a/src/StellarisDK/FileClasses/Attitude.java b/src/StellarisDK/FileClasses/Attitude.java new file mode 100644 index 0000000..a7c1032 --- /dev/null +++ b/src/StellarisDK/FileClasses/Attitude.java @@ -0,0 +1,28 @@ +package StellarisDK.FileClasses; + +import StellarisDK.FileClasses.Helper.DataEntry; +import StellarisDK.FileClasses.Helper.DataMap; +import StellarisDK.FileClasses.Helper.EntryArrayList; +import StellarisDK.GUI.AttitudeUI; + +import java.util.Arrays; + +public class Attitude extends GenericData { + + public Attitude() { + super(); + this.type = new DataEntry("new_attitude"); + data.put("behaviour", new EntryArrayList<>(Arrays.asList(new DataMap()))); + ui = new AttitudeUI(this); + } + + public Attitude(String input, String type) { + super(input, type); + ui = new AttitudeUI(this); + } + + @Override + public GenericData createNew() { + return new Attitude(); + } +} diff --git a/src/StellarisDK/FileClasses/DataParser.java b/src/StellarisDK/FileClasses/DataParser.java index ff29324..ec7f28e 100644 --- a/src/StellarisDK/FileClasses/DataParser.java +++ b/src/StellarisDK/FileClasses/DataParser.java @@ -101,6 +101,8 @@ public static ArrayList parseAll(File file) throws IOException { gData = new Tradition(tabby, obj.group(1), false); break; case "attitudes": + gData = new Attitude(tabby, obj.group(1)); + break; case "bombardment_stances": case "buildable_pops": case "building tags": diff --git a/src/StellarisDK/FileClasses/GenericData.java b/src/StellarisDK/FileClasses/GenericData.java index 57ec7b0..763b295 100644 --- a/src/StellarisDK/FileClasses/GenericData.java +++ b/src/StellarisDK/FileClasses/GenericData.java @@ -66,7 +66,14 @@ private String setName() { return name; } - public String getFirstValue(String key) { + public Object getFirstValue(String key) { + if (getKey(key)) + return ((EntryArrayList) data.get(key)).get(0); + else + return null; + } + + public String getFirstString(String key) { if (getKey(key)) return ((EntryArrayList) data.get(key)).getFirstString(); else @@ -110,6 +117,12 @@ public void setValue(String key, Object value, boolean addIfAbsent, int index) t } } + public void removeValue(String key) { + if (data.containsKey(key)) { + data.remove(key); + } + } + public void setData(DataMap data) { this.data = data; } @@ -219,19 +232,15 @@ public Object load(String input) { EntryArrayList temp; if (matcher.group(5) != null) { if (matcher.group(7).contains("{")) { -// ValueTriplet dat; DataEntry entry; Matcher color = DataPattern.color.matcher(matcher.group(7)); if (color.find()) { try { -// dat = new ValueTriplet<>(matcher.group(6).trim(), new StellarisColor(color.group(1).trim(), color.group(2).trim(), color.group(3).trim(), color.group(4).trim(), color.group(5).trim()), size++); entry = new DataEntry<>(matcher.group(5).trim(), new StellarisColor(color.group(1).trim(), color.group(2).trim(), color.group(3).trim(), color.group(4).trim(), color.group(5).trim()), size++ , 1110); } catch (NullPointerException e) { -// dat = new ValueTriplet<>(matcher.group(6).trim(), new StellarisColor(color.group(1).trim(), color.group(2).trim(), color.group(3).trim(), color.group(4).trim()), size++); entry = new DataEntry<>(matcher.group(5).trim(), new StellarisColor(color.group(1).trim(), color.group(2).trim(), color.group(3).trim(), color.group(4).trim()), size++ , 1110); } } else { -// dat = new ValueTriplet<>(matcher.group(6).trim(), sLrecursion(matcher.group(7).trim()), size++); entry = new DataEntry<>(matcher.group(5).trim(), sLrecursion(matcher.group(7).trim()), size++, 1010); } if (data.containsKey(matcher.group(5))) { @@ -242,7 +251,6 @@ public Object load(String input) { data.put(matcher.group(5).trim(), temp); } } else { -// ValueTriplet dat = new ValueTriplet<>(matcher.group(6).trim(), matcher.group(7).trim(), size++); DataEntry entry = new DataEntry<>(matcher.group(5).trim(), matcher.group(6).trim(), matcher.group(7).trim(), size++, 1110); if (data.containsKey(matcher.group(5).trim())) { data.get(matcher.group(5).trim()).add(entry); @@ -256,14 +264,11 @@ public Object load(String input) { temp = new EntryArrayList<>(); int order = size++; DataMap secMap; -// ValueTriplet dat; DataEntry entry; if (!matcher.group(4).contains("=")) { -// dat = new ValueTriplet<>(matcher.group(2).trim(), sLrecursion(matcher.group(4).trim().replaceAll("[\\t\\n\\r]", " ")), order); entry = new DataEntry<>(matcher.group(1).trim(), sLrecursion(matcher.group(4).trim().replaceAll("[\\t\\n\\r]", " ")), order, 1010); } else { secMap = (DataMap) load(matcher.group(4).replaceAll("(?m)^\t", "")); -// dat = new ValueTriplet<>(matcher.group(2).trim(), secMap, order); entry = new DataEntry<>(matcher.group(1).trim(), secMap, order, 1010); } temp.add(entry); diff --git a/src/StellarisDK/FileClasses/Helper/DataCell.java b/src/StellarisDK/FileClasses/Helper/DataCell.java index 8a35ae0..38a7bd2 100644 --- a/src/StellarisDK/FileClasses/Helper/DataCell.java +++ b/src/StellarisDK/FileClasses/Helper/DataCell.java @@ -101,8 +101,11 @@ private void startEditor() { super.startEdit(); if (getItem() instanceof DataEntry && ((DataEntry) getItem()).getValue() != null) { textField = new TextField(((DataEntry) getItem()).getValue().toString()); + setText(((DataEntry) getItem()).getKey() + ": "); + setContentDisplay(ContentDisplay.RIGHT); } else { textField = new TextField(getItem().toString()); + setText(null); } textField.setOnKeyReleased(event -> { if (event.getCode() == KeyCode.ENTER) { @@ -111,12 +114,6 @@ private void startEditor() { cancelEdit(); } }); - if (getItem() instanceof DataEntry && ((DataEntry) getItem()).getValue() != null) { - setText(((DataEntry) getItem()).getKey() + ": "); - setContentDisplay(ContentDisplay.RIGHT); - } else { - setText(null); - } setGraphic(textField); textField.selectAll(); } diff --git a/src/StellarisDK/FileClasses/Helper/DataMap.java b/src/StellarisDK/FileClasses/Helper/DataMap.java index ec51100..d1934ce 100644 --- a/src/StellarisDK/FileClasses/Helper/DataMap.java +++ b/src/StellarisDK/FileClasses/Helper/DataMap.java @@ -15,6 +15,13 @@ public int getFullSize() { return count; } + public Object getFirstValue(String key) { + if (get(key) != null) + return ((EntryArrayList) get(key)).get(0); + else + return null; + } + public TreeItem toTreeItem(DataEntry key) { TreeItem root = new TreeItem<>(new DataEntry(key.getKey(), key.getBinary())); DataEntry[] objs = compressToPairArray(); diff --git a/src/StellarisDK/FileClasses/Helper/EntryArrayList.java b/src/StellarisDK/FileClasses/Helper/EntryArrayList.java index 2fcce71..40528bf 100644 --- a/src/StellarisDK/FileClasses/Helper/EntryArrayList.java +++ b/src/StellarisDK/FileClasses/Helper/EntryArrayList.java @@ -3,9 +3,22 @@ import javafx.scene.control.TreeItem; import java.util.ArrayList; +import java.util.Collection; public class EntryArrayList extends ArrayList { + public EntryArrayList() { + super(); + } + + public EntryArrayList(T entry) { + super(); + this.add(entry); + } + public EntryArrayList(Collection c) { + super(c); + } + public TreeItem toTreeItem(DataEntry key) { TreeItem root = new TreeItem<>(new DataEntry(key.getKey(), key.getBinary())); for (Object obj : this) { diff --git a/src/StellarisDK/GUI/AmbientObjectUI.java b/src/StellarisDK/GUI/AmbientObjectUI.java index 15c29ce..7d7e6ba 100644 --- a/src/StellarisDK/GUI/AmbientObjectUI.java +++ b/src/StellarisDK/GUI/AmbientObjectUI.java @@ -33,16 +33,16 @@ public AmbientObjectUI(AmbientObject obj) { @Override public void load() { - name.setText(obj.getFirstValue(name.getId())); - tooltip.setText(obj.getFirstValue(tooltip.getId())); - description.setText(obj.getFirstValue(description.getId())); - entity.setText(obj.getFirstValue(entity.getId())); - if (obj.getFirstValue(show_name.getId()) != null && obj.getFirstValue(show_name.getId()).toLowerCase().equals("yes")) { + name.setText(obj.getFirstString(name.getId())); + tooltip.setText(obj.getFirstString(tooltip.getId())); + description.setText(obj.getFirstString(description.getId())); + entity.setText(obj.getFirstString(entity.getId())); + if (obj.getFirstString(show_name.getId()) != null && obj.getFirstString(show_name.getId()).toLowerCase().equals("yes")) { show_name.setSelected(true); } else { show_name.setSelected(false); } - if (obj.getFirstValue(selectable.getId()) != null && obj.getFirstValue(selectable.getId()).toLowerCase().equals("yes")) { + if (obj.getFirstString(selectable.getId()) != null && obj.getFirstString(selectable.getId()).toLowerCase().equals("yes")) { selectable.setSelected(true); } else { selectable.setSelected(false); diff --git a/src/StellarisDK/GUI/AttitudeUI.java b/src/StellarisDK/GUI/AttitudeUI.java new file mode 100644 index 0000000..5fda0a8 --- /dev/null +++ b/src/StellarisDK/GUI/AttitudeUI.java @@ -0,0 +1,120 @@ +package StellarisDK.GUI; + +import StellarisDK.FileClasses.Attitude; +import StellarisDK.FileClasses.Helper.DataEntry; +import StellarisDK.FileClasses.Helper.DataMap; +import StellarisDK.FileClasses.Helper.EntryArrayList; +import javafx.fxml.FXML; +import javafx.scene.control.CheckBox; +import javafx.scene.control.TextField; + +public class AttitudeUI extends AbstractUI { + + @FXML + private TextField type; + + @FXML + private CheckBox attack; + + @FXML + private CheckBox weaken; + + @FXML + private CheckBox alliance; + + @FXML + private CheckBox vassalize; + + @FXML + private CheckBox trade; + + @FXML + private CheckBox coexist; + + public AttitudeUI(Attitude obj) { + init("FXML/attitudeFX.fxml"); + window.setText("Attitude Editor"); + this.obj = obj; + } + + @Override + public void load() { + type.setText(obj.getFirstString(type.getId())); + DataMap temp = (DataMap)((DataEntry)obj.getFirstValue("behaviour")).getValue(); + if (temp.get(attack.getId()) != null && ((DataEntry)temp.getFirstValue(attack.getId())).getValue().toString().toLowerCase().equals("yes")) { + attack.setSelected(true); + } else { + attack.setSelected(false); + } + if (temp.get(weaken.getId()) != null && ((DataEntry)temp.getFirstValue(weaken.getId())).getValue().toString().toLowerCase().equals("yes")) { + weaken.setSelected(true); + } else { + weaken.setSelected(false); + } + if (temp.get(alliance.getId()) != null && ((DataEntry)temp.getFirstValue(alliance.getId())).getValue().toString().toLowerCase().equals("yes")) { + alliance.setSelected(true); + } else { + alliance.setSelected(false); + } + if (temp.get(vassalize.getId()) != null && ((DataEntry)temp.getFirstValue(vassalize.getId())).getValue().toString().toLowerCase().equals("yes")) { + vassalize.setSelected(true); + } else { + vassalize.setSelected(false); + } + if (temp.get(trade.getId()) != null && ((DataEntry)temp.getFirstValue(trade.getId())).getValue().toString().toLowerCase().equals("yes")) { + trade.setSelected(true); + } else { + trade.setSelected(false); + } + if (temp.get(coexist.getId()) != null && ((DataEntry)temp.getFirstValue(coexist.getId())).getValue().toString().toLowerCase().equals("yes")) { + coexist.setSelected(true); + } else { + coexist.setSelected(false); + } + } + + @Override + public Object save() { + DataMap temp = (DataMap)((DataEntry)obj.getFirstValue("behaviour")).getValue(); + if(type.getText() == null || type.getText().equals("")){ + obj.setValue(type.getId(), "Unnamed Object", true, 0); + } else { + obj.setValue(type.getId(), type.getText(), true, 0); + } + int size = 0; + if (attack.isSelected()) { + temp.put(attack.getId(), new EntryArrayList<>(new DataEntry<>(attack.getId(), "yes", size++, 1110))); + }else{ + temp.remove(attack.getId()); + } + if (weaken.isSelected()) { + temp.put(weaken.getId(), new EntryArrayList<>(new DataEntry<>(weaken.getId(), "yes", size++, 1110))); + }else{ + temp.remove(weaken.getId()); + } + if (alliance.isSelected()) { + temp.put(alliance.getId(), new EntryArrayList<>(new DataEntry<>(alliance.getId(), "yes", size++, 1110))); + }else{ + temp.remove(alliance.getId()); + } + if (vassalize.isSelected()) { + temp.put(vassalize.getId(), new EntryArrayList<>(new DataEntry<>(vassalize.getId(), "yes", size++, 1110))); + }else{ + temp.remove(vassalize.getId()); + } + if (trade.isSelected()) { + temp.put(trade.getId(), new EntryArrayList<>(new DataEntry<>(trade.getId(), "yes", size++, 1110))); + }else{ + temp.remove(trade.getId()); + } + if (coexist.isSelected()) { + temp.put(coexist.getId(), new EntryArrayList<>(new DataEntry<>(coexist.getId(), "yes", size++, 1110))); + }else{ + temp.remove(coexist.getId()); + } + + System.out.println(obj.export()); + itemView.refresh(); + return obj; + } +} \ No newline at end of file diff --git a/src/StellarisDK/GUI/FXML/attitudeFX.fxml b/src/StellarisDK/GUI/FXML/attitudeFX.fxml new file mode 100644 index 0000000..38e4353 --- /dev/null +++ b/src/StellarisDK/GUI/FXML/attitudeFX.fxml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + +