From 1f826ed141fbcc7957ddf687b6de21bc9d018d7e Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 24 Oct 2022 21:16:29 +0200 Subject: [PATCH] [#1687] Save stage activeness to design file --- .../importt/MotorConfigurationHandler.java | 21 ++++++++++--- .../openrocket/importt/OpenRocketLoader.java | 6 +++- .../file/openrocket/savers/RocketSaver.java | 22 +++++++++---- .../rocketcomponent/FlightConfiguration.java | 31 +++++++++++++++++++ fileformat.txt | 1 + .../sf/openrocket/gui/main/BasicFrame.java | 1 - 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/MotorConfigurationHandler.java b/core/src/net/sf/openrocket/file/openrocket/importt/MotorConfigurationHandler.java index 5aaa42cc3..1b0a4a2dd 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/MotorConfigurationHandler.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/MotorConfigurationHandler.java @@ -20,6 +20,7 @@ class MotorConfigurationHandler extends AbstractElementHandler { private final Rocket rocket; private String name = null; private boolean inNameElement = false; + private HashMap stageActiveness = new HashMap<>(); public MotorConfigurationHandler(Rocket rocket, DocumentLoadingContext context) { this.rocket = rocket; @@ -30,11 +31,13 @@ class MotorConfigurationHandler extends AbstractElementHandler { public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { - if (inNameElement || !element.equals("name")) { + if ((inNameElement && element.equals("name")) || !(element.equals("name") || element.equals("stage"))) { warnings.add(Warning.FILE_INVALID_PARAMETER); return null; } - inNameElement = true; + if (element.equals("name")) { + inNameElement = true; + } return PlainTextHandler.INSTANCE; } @@ -42,7 +45,13 @@ class MotorConfigurationHandler extends AbstractElementHandler { @Override public void closeElement(String element, HashMap attributes, String content, WarningSet warnings) { - name = content; + if (element.equals("name")) { + name = content; + } else if (element.equals("stage")) { + int stageNr = Integer.parseInt(attributes.get("number")); + boolean isActive = Boolean.parseBoolean(attributes.get("active")); + stageActiveness.put(stageNr, isActive); + } } @Override @@ -60,7 +69,11 @@ class MotorConfigurationHandler extends AbstractElementHandler { if (name != null && name.trim().length() > 0) { rocket.getFlightConfiguration(fcid).setName(name); } - + + for (int stageNr : stageActiveness.keySet()) { + rocket.getFlightConfiguration(fcid).preloadStageActiveness(stageNr, stageActiveness.get(stageNr)); + } + if ("true".equals(attributes.remove("default"))) { rocket.setSelectedConfiguration( fcid); } diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java index 7662c80ad..5460d3c99 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import net.sf.openrocket.rocketcomponent.FlightConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.InputSource; @@ -53,7 +54,10 @@ public class OpenRocketLoader extends AbstractRocketLoader { throw new RocketLoadException("Malformed XML in input.", e); } - doc.getSelectedConfiguration().setAllStages(); + // load the stage activeness + for (FlightConfiguration config : doc.getRocket().getFlightConfigurations()) { + config.applyPreloadedStageActiveness(); + } // Deduce suitable time skip double timeSkip = StorageOptions.SIMULATION_DATA_NONE; diff --git a/core/src/net/sf/openrocket/file/openrocket/savers/RocketSaver.java b/core/src/net/sf/openrocket/file/openrocket/savers/RocketSaver.java index fb03fe8ee..b1e5f7cbd 100644 --- a/core/src/net/sf/openrocket/file/openrocket/savers/RocketSaver.java +++ b/core/src/net/sf/openrocket/file/openrocket/savers/RocketSaver.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import net.sf.openrocket.file.openrocket.OpenRocketSaver; +import net.sf.openrocket.rocketcomponent.AxialStage; import net.sf.openrocket.rocketcomponent.FlightConfiguration; import net.sf.openrocket.rocketcomponent.FlightConfigurationId; import net.sf.openrocket.rocketcomponent.ReferenceType; @@ -53,15 +55,23 @@ public class RocketSaver extends RocketComponentSaver { if ( rocket.getSelectedConfiguration().equals( flightConfig )){ str += " default=\"true\""; } - + + // close motorconfiguration opening tag + str += ">"; + elements.add(str); + + // flight configuration name if (flightConfig.isNameOverridden()){ - str += ">" + net.sf.openrocket.util.TextUtil.escapeXML(flightConfig.getNameRaw()) - + ""; - } else { - str += "/>"; + elements.add(OpenRocketSaver.INDENT + "" + net.sf.openrocket.util.TextUtil.escapeXML(flightConfig.getNameRaw()) + + ""); + } + // stage activeness + for (AxialStage stage : rocket.getStageList()) { + elements.add(OpenRocketSaver.INDENT + ""); } - elements.add(str); + elements.add(""); } // Reference diameter diff --git a/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java b/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java index 78ce41f66..2b58e7b64 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java +++ b/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java @@ -64,6 +64,7 @@ public class FlightConfiguration implements FlightConfigurableParameter stages = new HashMap<>(); // Map of stage number to StageFlags of the corresponding stage final protected Map motors = new HashMap(); + private Map preloadStageActiveness = null; final private Collection activeMotors = new ConcurrentLinkedQueue(); final private InstanceMap activeInstances = new InstanceMap(); @@ -276,6 +277,34 @@ public class FlightConfiguration implements FlightConfigurableParameter(); + } + this.preloadStageActiveness.put(stageNumber, isActive); + } + + /** + * Applies preloaded stage activeness. + * This method should be called after the rocket has been loaded from a file. + */ + public void applyPreloadedStageActiveness() { + if (preloadStageActiveness == null) { + return; + } + for (int stageNumber : preloadStageActiveness.keySet()) { + _setStageActive(stageNumber, preloadStageActiveness.get(stageNumber), false); + } + preloadStageActiveness.clear(); + preloadStageActiveness = null; + } + public Collection getAllComponents() { List traversalOrder = new ArrayList(); recurseAllComponentsDepthFirst(this.rocket,traversalOrder); @@ -795,6 +824,7 @@ public class FlightConfiguration implements FlightConfigurableParameter(this.preloadStageActiveness); clone.cachedBoundsAerodynamic = this.cachedBoundsAerodynamic.clone(); clone.cachedBounds = this.cachedBounds.clone(); @@ -824,6 +854,7 @@ public class FlightConfiguration implements FlightConfigurableParameter(this.preloadStageActiveness); copy.cachedBoundsAerodynamic = this.cachedBoundsAerodynamic.clone(); copy.cachedBounds = this.cachedBounds.clone(); copy.modID = this.modID; diff --git a/fileformat.txt b/fileformat.txt index 6b7f385cb..2a488f6cc 100644 --- a/fileformat.txt +++ b/fileformat.txt @@ -57,6 +57,7 @@ The following file format versions exist: Transitions, Inner Tubes, Launch Lugs, and Fins. Added PhotoStudio settings saving () Added override CD parameter () + Added stage activeness remembrance ( under ) Separated into individual parameters for mass, CG, and CD. Rename to ( remains for backward compatibility) Rename to ( remains for backward compatibility) diff --git a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java index cf2455623..263ca0858 100644 --- a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -154,7 +154,6 @@ public class BasicFrame extends JFrame { this.document = document; this.rocket = document.getRocket(); - this.rocket.getSelectedConfiguration().setAllStages(); BasicFrame.lastFrameInstance = this; // Create the component tree selection model that will be used