Merge pull request #2429 from SiboVG/parachute-size

Fix parachute length resizing when loading preset parachute
This commit is contained in:
Sibo Van Gool 2023-12-30 22:32:48 +01:00 committed by GitHub
commit 6b01afcd67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 84 additions and 24 deletions

View File

@ -14,6 +14,7 @@ import net.sf.openrocket.rocketcomponent.RecoveryDevice;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.AxialStage; import net.sf.openrocket.rocketcomponent.AxialStage;
import org.xml.sax.SAXException;
/** /**
* A handler that populates the parameters of a previously constructed rocket component. * A handler that populates the parameters of a previously constructed rocket component.
@ -27,6 +28,10 @@ class ComponentParameterHandler extends AbstractElementHandler {
public ComponentParameterHandler(RocketComponent c, DocumentLoadingContext context) { public ComponentParameterHandler(RocketComponent c, DocumentLoadingContext context) {
this.component = c; this.component = c;
this.context = context; this.context = context;
// Sometimes setting certain component parameters will clear the preset. We don't want that to happen, so
// ignore preset clearing.
this.component.setIgnorePresetClearing(true);
} }
@Override @Override
@ -124,4 +129,12 @@ class ComponentParameterHandler extends AbstractElementHandler {
+ component.getComponentName() + ", ignoring.")); + component.getComponentName() + ", ignoring."));
} }
} }
@Override
public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
super.endHandler(element, attributes, content, warnings);
// Restore the preset clearing behavior
this.component.setIgnorePresetClearing(false);
}
} }

View File

@ -13,11 +13,17 @@ import net.sf.openrocket.util.Reflection;
////ComponentPresetSetter - sets a ComponentPreset value ////ComponentPresetSetter - sets a ComponentPreset value
class ComponentPresetSetter implements Setter { class ComponentPresetSetter implements Setter {
private final Reflection.Method setMethod; private final Reflection.Method setMethod;
private Object[] extraParameters = null;
public ComponentPresetSetter(Reflection.Method set) { public ComponentPresetSetter(Reflection.Method set) {
this.setMethod = set; this.setMethod = set;
} }
public ComponentPresetSetter(Reflection.Method set, Object... parameters) {
this.setMethod = set;
this.extraParameters = parameters;
}
@Override @Override
public void set(RocketComponent c, String name, HashMap<String, String> attributes, public void set(RocketComponent c, String name, HashMap<String, String> attributes,
WarningSet warnings) { WarningSet warnings) {
@ -71,7 +77,11 @@ class ComponentPresetSetter implements Setter {
// The preset loader can override the component name, so first store it and then apply it again // The preset loader can override the component name, so first store it and then apply it again
String componentName = c.getName(); String componentName = c.getName();
if (extraParameters != null) {
setMethod.invoke(c, matchingPreset, extraParameters);
} else {
setMethod.invoke(c, matchingPreset); setMethod.invoke(c, matchingPreset);
}
c.setName(componentName); c.setName(componentName);
} }
} }

View File

@ -452,6 +452,9 @@ class DocumentConfig {
setters.put("Parachute:linematerial", new MaterialSetter( setters.put("Parachute:linematerial", new MaterialSetter(
Reflection.findMethod(Parachute.class, "setLineMaterial", Material.class), Reflection.findMethod(Parachute.class, "setLineMaterial", Material.class),
Material.Type.LINE)); Material.Type.LINE));
setters.put("Parachute:preset", new ComponentPresetSetter(
Reflection.findMethod(Parachute.class, "loadPreset", ComponentPreset.class, Object[].class),
false));
// PodSet // PodSet
setters.put("PodSet:instancecount", new IntSetter( setters.put("PodSet:instancecount", new IntSetter(

View File

@ -81,6 +81,7 @@ class ParachuteHandler extends RecoveryDeviceHandler<Parachute> {
packed = chute.getDiameter() * 0.025; packed = chute.getDiameter() * 0.025;
} }
chute.setRadius(packed); chute.setRadius(packed);
chute.setLength(packed);
} }
if (RockSimCommonConstants.SHROUD_LINE_COUNT.equals(element)) { if (RockSimCommonConstants.SHROUD_LINE_COUNT.equals(element)) {
chute.setLineCount(Math.max(0, Integer.parseInt(content))); chute.setLineCount(Math.max(0, Integer.parseInt(content)));

View File

@ -153,8 +153,18 @@ public class Parachute extends RecoveryDevice {
return false; return false;
} }
/**
* Load the component from a preset.
* @param preset the preset to load from
* @param params extra parameters to be used in the preset loading
* params[0] = boolean allowAutoRadius: true = allow auto radius to be set during preset loading, false = do not allow auto radius
*/
@Override @Override
protected void loadFromPreset(ComponentPreset preset) { protected void loadFromPreset(ComponentPreset preset, Object...params) {
boolean allowAutoRadius = true;
if (params != null && params.length > 0) {
allowAutoRadius = (boolean) params[0];
}
// SUBSTITUTE preset parachute values for existing component values // SUBSTITUTE preset parachute values for existing component values
// // Set preset parachute description // // Set preset parachute description
@ -219,7 +229,8 @@ public class Parachute extends RecoveryDevice {
} }
// // Size parachute packed diameter within parent inner diameter // // Size parachute packed diameter within parent inner diameter
if (preset.has(ComponentPreset.PACKED_LENGTH) && (getLength() > 0) && if (preset.has(ComponentPreset.PACKED_LENGTH) && (getLength() > 0) &&
preset.has(ComponentPreset.PACKED_DIAMETER) && (getRadius() > 0)) { preset.has(ComponentPreset.PACKED_DIAMETER) && (getRadius() > 0) &&
allowAutoRadius) {
setRadiusAutomatic(true); setRadiusAutomatic(true);
} }
@ -232,7 +243,12 @@ public class Parachute extends RecoveryDevice {
massOverridden = false; massOverridden = false;
} }
super.loadFromPreset(preset); super.loadFromPreset(preset, params);
}
@Override
protected void loadFromPreset(ComponentPreset preset) {
loadFromPreset(preset, true);
} }
@Override @Override

View File

@ -137,6 +137,9 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
// Preset component this component is based upon // Preset component this component is based upon
private ComponentPreset presetComponent = null; private ComponentPreset presetComponent = null;
// If set to true, presets will not be cleared
private boolean ignorePresetClearing = false;
// The realistic appearance of this component // The realistic appearance of this component
private Appearance appearance = null; private Appearance appearance = null;
@ -1176,10 +1179,11 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
* preset values. * preset values.
* *
* @param preset the preset component to load, or <code>null</code> to clear the preset. * @param preset the preset component to load, or <code>null</code> to clear the preset.
* @param params extra parameters to be used in the preset loading
*/ */
public final void loadPreset(ComponentPreset preset) { public final void loadPreset(ComponentPreset preset, Object...params) {
for (RocketComponent listener : configListeners) { for (RocketComponent listener : configListeners) {
listener.loadPreset(preset); listener.loadPreset(preset, params);
} }
if (presetComponent == preset) { if (presetComponent == preset) {
@ -1212,7 +1216,10 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
rocket.freeze(); rocket.freeze();
} }
if (params == null || params.length == 0)
loadFromPreset(preset); loadFromPreset(preset);
else
loadFromPreset(preset, params);
this.presetComponent = preset; this.presetComponent = preset;
@ -1225,6 +1232,9 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE); fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
} }
public final void loadPreset(ComponentPreset preset) {
loadPreset(preset, (Object[]) null);
}
/** /**
* Load component properties from the specified preset. The preset is guaranteed * Load component properties from the specified preset. The preset is guaranteed
@ -1237,13 +1247,18 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
* This is because mass setting requires the dimensions to be set beforehand. * This is because mass setting requires the dimensions to be set beforehand.
* *
* @param preset the preset to load from * @param preset the preset to load from
* @param params extra parameters to be used in the preset loading
*/ */
protected void loadFromPreset(ComponentPreset preset) { protected void loadFromPreset(ComponentPreset preset, Object...params) {
if (preset.has(ComponentPreset.LENGTH)) { if (preset.has(ComponentPreset.LENGTH)) {
this.length = preset.get(ComponentPreset.LENGTH); this.length = preset.get(ComponentPreset.LENGTH);
} }
} }
protected void loadFromPreset(ComponentPreset preset) {
loadFromPreset(preset, (Object[]) null);
}
/** /**
* Clear the current component preset. This does not affect the component properties * Clear the current component preset. This does not affect the component properties
@ -1254,13 +1269,15 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
listener.clearPreset(); listener.clearPreset();
} }
if (presetComponent == null) if (presetComponent == null || ignorePresetClearing)
return; return;
presetComponent = null; presetComponent = null;
fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE); fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
} }
public void setIgnorePresetClearing(boolean ignorePresetClearing) {
this.ignorePresetClearing = ignorePresetClearing;
}
/** /**
* Returns the unique ID of the component. * Returns the unique ID of the component.