mirror of
https://github.com/EDCD/coriolis.git
synced 2025-12-10 15:15:34 +00:00
Updates for modifications
This commit is contained in:
@@ -41,15 +41,18 @@ export default class ModificationsMenu extends TranslatedComponent {
|
||||
let formats = context.language.formats;
|
||||
let { m } = props;
|
||||
let list = [];
|
||||
let values = {};
|
||||
|
||||
for (let modName of Modifications.validity[m.grp]) {
|
||||
values[modName] = m.getModValue(modName) * 100;
|
||||
list.push(<div className={'cb'} key={modName}>
|
||||
<div className={'cb'}>{translate(modName)}{' (%)'}</div>
|
||||
<NumberEditor className={'cb'} style={{ width: '100%', textAlign: 'center' }} step={0.01} stepModifier={1} decimals={2} initialValue={m.getModValue(modName) ? m.getModValue(modName) * 100 : 0} value={m.getModValue(modName) ? m.getModValue(modName) * 100 : 0} onValueChange={this._updateValue.bind(this, modName)} />
|
||||
<NumberEditor className={'cb'} style={{ width: '100%', textAlign: 'center' }} step={0.01} stepModifier={1} decimals={2} value={this._getValue(modName, values[modName])} onValueChange={this._updateValue.bind(this, modName)} />
|
||||
</div>);
|
||||
}
|
||||
//<NumberEditor className={'cb'} style={{ width: '100%', textAlign: 'center' }} step={0.01} stepModifier={1} decimals={2} initialValue={m.getModValue(modName) ? m.getModValue(modName) * 100 : 0} value={m.getModValue(modName) ? m.getModValue(modName) * 100 : 0} onValueChange={this._updateValue.bind(this, modName)} />
|
||||
|
||||
return { list };
|
||||
return { list, values };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,19 +64,30 @@ export default class ModificationsMenu extends TranslatedComponent {
|
||||
this.setState(this._initState(nextProps, nextContext));
|
||||
}
|
||||
|
||||
_getValue(name, defaultValue) {
|
||||
let values = this.state ? this.state.values : null;
|
||||
return values ? values[name] : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update modification given a value.
|
||||
* @param {Number} name The name of the modification
|
||||
* @param {Number} value The value to set, in the range [0,1]
|
||||
*/
|
||||
_updateValue(name, value) {
|
||||
let scaledValue = Math.floor(value * 100) / 10000;
|
||||
|
||||
let m = this.props.m;
|
||||
let ship = this.props.ship;
|
||||
let values = this.state.values;
|
||||
values[name] = value;
|
||||
|
||||
ship.setModification(m, name, scaledValue);
|
||||
// Only update the modification if this is a valid number
|
||||
if (!isNaN(Number(value)) && !value.endsWith('.')) {
|
||||
let scaledValue = Math.floor(Number(value) * 100) / 10000;
|
||||
let m = this.props.m;
|
||||
let ship = this.props.ship;
|
||||
ship.setModification(m, name, scaledValue);
|
||||
}
|
||||
this.props.onChange();
|
||||
this.setState({values});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user