Merge pull request #2429 from SiboVG/parachute-size
Fix parachute length resizing when loading preset parachute
This commit is contained in:
commit
6b01afcd67
@ -14,6 +14,7 @@ import net.sf.openrocket.rocketcomponent.RecoveryDevice;
|
||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
||||
import net.sf.openrocket.rocketcomponent.AxialStage;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this.component = c;
|
||||
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
|
||||
@ -124,4 +129,12 @@ class ComponentParameterHandler extends AbstractElementHandler {
|
||||
+ 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);
|
||||
}
|
||||
}
|
@ -13,10 +13,16 @@ import net.sf.openrocket.util.Reflection;
|
||||
////ComponentPresetSetter - sets a ComponentPreset value
|
||||
class ComponentPresetSetter implements Setter {
|
||||
private final Reflection.Method setMethod;
|
||||
private Object[] extraParameters = null;
|
||||
|
||||
public ComponentPresetSetter(Reflection.Method set) {
|
||||
this.setMethod = set;
|
||||
}
|
||||
|
||||
public ComponentPresetSetter(Reflection.Method set, Object... parameters) {
|
||||
this.setMethod = set;
|
||||
this.extraParameters = parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(RocketComponent c, String name, HashMap<String, String> attributes,
|
||||
@ -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
|
||||
String componentName = c.getName();
|
||||
setMethod.invoke(c, matchingPreset);
|
||||
if (extraParameters != null) {
|
||||
setMethod.invoke(c, matchingPreset, extraParameters);
|
||||
} else {
|
||||
setMethod.invoke(c, matchingPreset);
|
||||
}
|
||||
c.setName(componentName);
|
||||
}
|
||||
}
|
||||
|
@ -452,6 +452,9 @@ class DocumentConfig {
|
||||
setters.put("Parachute:linematerial", new MaterialSetter(
|
||||
Reflection.findMethod(Parachute.class, "setLineMaterial", Material.class),
|
||||
Material.Type.LINE));
|
||||
setters.put("Parachute:preset", new ComponentPresetSetter(
|
||||
Reflection.findMethod(Parachute.class, "loadPreset", ComponentPreset.class, Object[].class),
|
||||
false));
|
||||
|
||||
// PodSet
|
||||
setters.put("PodSet:instancecount", new IntSetter(
|
||||
|
@ -81,6 +81,7 @@ class ParachuteHandler extends RecoveryDeviceHandler<Parachute> {
|
||||
packed = chute.getDiameter() * 0.025;
|
||||
}
|
||||
chute.setRadius(packed);
|
||||
chute.setLength(packed);
|
||||
}
|
||||
if (RockSimCommonConstants.SHROUD_LINE_COUNT.equals(element)) {
|
||||
chute.setLineCount(Math.max(0, Integer.parseInt(content)));
|
||||
|
@ -153,8 +153,18 @@ public class Parachute extends RecoveryDevice {
|
||||
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
|
||||
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
|
||||
// // Set preset parachute description
|
||||
@ -176,13 +186,13 @@ public class Parachute extends RecoveryDevice {
|
||||
this.diameter = DEFAULT_DIAMETER;
|
||||
}
|
||||
// // Set preset parachute drag coefficient
|
||||
if ((preset.has(ComponentPreset.CD)) && preset.get(ComponentPreset.CD) > 0){
|
||||
cdAutomatic = false;
|
||||
cd = preset.get(ComponentPreset.CD);
|
||||
} else {
|
||||
cdAutomatic = true;
|
||||
cd = Parachute.DEFAULT_CD;
|
||||
}
|
||||
if ((preset.has(ComponentPreset.CD)) && preset.get(ComponentPreset.CD) > 0){
|
||||
cdAutomatic = false;
|
||||
cd = preset.get(ComponentPreset.CD);
|
||||
} else {
|
||||
cdAutomatic = true;
|
||||
cd = Parachute.DEFAULT_CD;
|
||||
}
|
||||
// // Set preset parachute line count
|
||||
if ((preset.has(ComponentPreset.LINE_COUNT)) && preset.get(ComponentPreset.LINE_COUNT) > 0) {
|
||||
this.lineCount = preset.get(ComponentPreset.LINE_COUNT);
|
||||
@ -196,7 +206,7 @@ public class Parachute extends RecoveryDevice {
|
||||
this.lineLength = DEFAULT_LINE_LENGTH;
|
||||
}
|
||||
// // Set preset parachute line material
|
||||
// NEED a better way to set preset if field is empty ----
|
||||
// NEED a better way to set preset if field is empty ----
|
||||
if ((preset.has(ComponentPreset.LINE_MATERIAL))) {
|
||||
String lineMaterialEmpty = preset.get(ComponentPreset.LINE_MATERIAL).toString();
|
||||
int count = lineMaterialEmpty.length();
|
||||
@ -219,7 +229,8 @@ public class Parachute extends RecoveryDevice {
|
||||
}
|
||||
// // Size parachute packed diameter within parent inner diameter
|
||||
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);
|
||||
}
|
||||
|
||||
@ -232,7 +243,12 @@ public class Parachute extends RecoveryDevice {
|
||||
massOverridden = false;
|
||||
}
|
||||
|
||||
super.loadFromPreset(preset);
|
||||
super.loadFromPreset(preset, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadFromPreset(ComponentPreset preset) {
|
||||
loadFromPreset(preset, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -136,6 +136,9 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
|
||||
// Preset component this component is based upon
|
||||
private ComponentPreset presetComponent = null;
|
||||
|
||||
// If set to true, presets will not be cleared
|
||||
private boolean ignorePresetClearing = false;
|
||||
|
||||
// The realistic appearance of this component
|
||||
private Appearance appearance = null;
|
||||
@ -1176,10 +1179,11 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
* preset values.
|
||||
*
|
||||
* @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) {
|
||||
listener.loadPreset(preset);
|
||||
listener.loadPreset(preset, params);
|
||||
}
|
||||
|
||||
if (presetComponent == preset) {
|
||||
@ -1211,8 +1215,11 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
if (rocket != null) {
|
||||
rocket.freeze();
|
||||
}
|
||||
|
||||
loadFromPreset(preset);
|
||||
|
||||
if (params == null || params.length == 0)
|
||||
loadFromPreset(preset);
|
||||
else
|
||||
loadFromPreset(preset, params);
|
||||
|
||||
this.presetComponent = preset;
|
||||
|
||||
@ -1224,8 +1231,11 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
|
||||
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
|
||||
* to be of the correct type.
|
||||
@ -1235,14 +1245,19 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
* <p>
|
||||
* This method must FIRST perform the preset loading and THEN call super.loadFromPreset().
|
||||
* This is because mass setting requires the dimensions to be set beforehand.
|
||||
*
|
||||
*
|
||||
* @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)) {
|
||||
this.length = preset.get(ComponentPreset.LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
protected void loadFromPreset(ComponentPreset preset) {
|
||||
loadFromPreset(preset, (Object[]) null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@ -1254,14 +1269,16 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
|
||||
listener.clearPreset();
|
||||
}
|
||||
|
||||
if (presetComponent == null)
|
||||
if (presetComponent == null || ignorePresetClearing)
|
||||
return;
|
||||
presetComponent = null;
|
||||
fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setIgnorePresetClearing(boolean ignorePresetClearing) {
|
||||
this.ignorePresetClearing = ignorePresetClearing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique ID of the component.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user