Implement units in modifications menu

This commit is contained in:
Felix Linker
2020-12-31 18:54:36 +01:00
parent 20e448fc0a
commit cdcda004f3
2 changed files with 35 additions and 23 deletions

View File

@@ -10,9 +10,10 @@ import { Module } from 'ed-forge';
*/ */
export default class Modification extends TranslatedComponent { export default class Modification extends TranslatedComponent {
static propTypes = { static propTypes = {
highlight: PropTypes.bool,
m: PropTypes.instanceOf(Module).isRequired, m: PropTypes.instanceOf(Module).isRequired,
property: PropTypes.string.isRequired, property: PropTypes.string.isRequired,
highlight: PropTypes.bool, onSet: PropTypes.func.isRequired,
}; };
/** /**
@@ -21,19 +22,22 @@ export default class Modification extends TranslatedComponent {
*/ */
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {}; const { m, property } = props;
const { beneficial, unit, value } = m.getFormatted(property, true);
this.state = { beneficial, unit, value };
} }
/** /**
* Notify listeners that a new value has been entered and commited. * Notify listeners that a new value has been entered and commited.
*/ */
_updateFinished() { _updateFinished() {
const { m, property, value } = this.props; const { onSet, m, property } = this.props;
const { inputValue } = this.state; const { inputValue } = this.state;
const numValue = Number(inputValue); const numValue = Number(inputValue);
if (!isNaN(numValue) && value !== numValue) { if (!isNaN(numValue) && this.state.value !== numValue) {
m.set(property, numValue); onSet(property, numValue);
this.setState({ inputValue: undefined }); const { beneficial, unit, value } = m.getFormatted(property, true);
this.setState({ beneficial, unit, value });
} }
} }
@@ -43,8 +47,8 @@ export default class Modification extends TranslatedComponent {
*/ */
render() { render() {
const { translate, formats } = this.context.language; const { translate, formats } = this.context.language;
const { m, property, highlight, value } = this.props; const { highlight, m, property } = this.props;
const { inputValue } = this.state; const { beneficial, unit, value, inputValue } = this.state;
// Some features only apply to specific modules; these features will be // Some features only apply to specific modules; these features will be
// undefined on items that do not belong to the same class. Filter these // undefined on items that do not belong to the same class. Filter these
@@ -66,10 +70,7 @@ export default class Modification extends TranslatedComponent {
<span> <span>
<NumberEditor value={inputValue || value} stepModifier={1} <NumberEditor value={inputValue || value} stepModifier={1}
decimals={2} step={0.01} style={{ textAlign: 'right' }} decimals={2} step={0.01} style={{ textAlign: 'right' }}
className={cn( className={cn('cb', { 'greyed-out': !highlight })}
'cb',
{ 'greyed-out': !highlight },
)}
onKeyDown={(event) => { onKeyDown={(event) => {
if (event.key == 'Enter') { if (event.key == 'Enter') {
this._updateFinished(); this._updateFinished();
@@ -81,17 +82,13 @@ export default class Modification extends TranslatedComponent {
this.setState({ inputValue }); this.setState({ inputValue });
} }
}} /> }} />
{/* TODO: support unit */} <span className="unit-container">{unit}</span>
<span className="unit-container">{/* unit */}</span>
</span> </span>
</td> </td>
<td style={{ textAlign: 'center' }} <td style={{ textAlign: 'center' }}
className={cn({ className={cn({
// TODO: secondary: beneficial,
// secondary: isBeneficial, warning: beneficial === false,
// Is beneficial might be undefined; in this case we have a 0%
// modifier. Check this here.
// warning: isBeneficial === false,
})} })}
// TODO: support absolute modifiers // TODO: support absolute modifiers
>{formats.pct(m.getModifier(property))}</td> >{formats.pct(m.getModifier(property))}</td>

View File

@@ -67,7 +67,7 @@ export default class ModificationsMenu extends TranslatedComponent {
// onMouseOver={termtip.bind(null, tooltipContent)} // onMouseOver={termtip.bind(null, tooltipContent)}
// onMouseOut={tooltip.bind(null, null)} // onMouseOut={tooltip.bind(null, null)}
onClick={() => { onClick={() => {
m.setBlueprint(blueprint, grade); m.setBlueprint(blueprint, grade, 1);
this.setState({ this.setState({
blueprintMenuOpened: false, blueprintMenuOpened: false,
specialMenuOpened: true, specialMenuOpened: true,
@@ -144,8 +144,19 @@ export default class ModificationsMenu extends TranslatedComponent {
*/ */
_mkModification(property, highlight) { _mkModification(property, highlight) {
const { m } = this.props; const { m } = this.props;
return <Modification key={property} highlight={highlight} m={m}
property={property} value={m.get(property)} />; let onSet = m.set.bind(m);
// Show resistance instead of effectiveness
if (property.endsWith('effectiveness')) {
const oldProperty = property;
property = property.replace('effectiveness', 'resistance');
onSet = (_, v) => {
m.set(oldProperty, 1 - v / 100);
};
}
return <Modification key={property} m={m} property={property}
onSet={onSet} highlight={highlight} />;
} }
/** /**
@@ -275,7 +286,11 @@ export default class ModificationsMenu extends TranslatedComponent {
m.resetEngineering(); m.resetEngineering();
this.selectedModRef = null; this.selectedModRef = null;
this.selectedSpecialRef = null; this.selectedSpecialRef = null;
this.setState({ blueprintProgress: undefined }); tooltip(null);
this.setState({
blueprintMenuOpened: true,
blueprintProgress: undefined,
});
}} }}
onMouseOver={termtip.bind(null, 'PHRASE_BLUEPRINT_RESET')} onMouseOver={termtip.bind(null, 'PHRASE_BLUEPRINT_RESET')}
onMouseOut={tooltip.bind(null, null)} onMouseOut={tooltip.bind(null, null)}