[WIP] Fix Expression object/behavior functions formatting and BehaviorField

This commit is contained in:
Florian Rival
2017-12-06 23:43:15 +01:00
parent fb40e908c0
commit 8332adf07b
7 changed files with 102 additions and 24 deletions

View File

@@ -100,6 +100,9 @@ export class InstructionOrExpressionSelector extends Component {
primaryText={instructionInfo.displayedName}
secondaryText={instructionInfo.fullGroupName}
value={instructionInfo.type}
onClick={() => {
this.props.onChoose(instructionInfo.type, instructionInfo);
}}
/>
);
});

View File

@@ -77,7 +77,7 @@ export default class BehaviorField extends Component {
this.setState({ errorText: this.getError() });
};
componentWillUpdate() {
componentDidUpdate() {
const { instructionOrExpression } = this.props;
if (
!instructionOrExpression ||
@@ -85,16 +85,8 @@ export default class BehaviorField extends Component {
)
return;
const objectName = instructionOrExpression.getParameter(0);
this._behaviorNames = gd
.getBehaviorsOfObject(
this.props.project,
this.props.layout,
objectName,
true
)
.toJSArray();
// This is a bit hacky:
// force the behavior selection if there is only one selectable behavior
if (this._behaviorNames.length === 1) {
if (this.props.value !== this._behaviorNames[0]) {
this.props.onChange(this._behaviorNames[0]);

View File

@@ -1,6 +1,23 @@
export const formatExpressionCall = (expressionInfo, parameterValues) => {
const functionArgs = parameterValues.join(', ');
const functionCall = `${expressionInfo.name}(${functionArgs})`;
//@flow
import { type InstructionOrExpressionInformation } from '../../InstructionOrExpressionSelector/InstructionOrExpressionInformation.flow.js';
import { type ParameterValues } from './ExpressionParametersEditorDialog';
return functionCall;
}
export const formatExpressionCall = (
expressionInfo: InstructionOrExpressionInformation,
parameterValues: ParameterValues
): string => {
if (expressionInfo.objectMetadata) {
const [objectName, ...otherParameters] = parameterValues;
const functionArgs = otherParameters.join(', ');
return `${objectName}.${expressionInfo.name}(${functionArgs})`;
} else if (expressionInfo.behaviorMetadata) {
const [objectName, behaviorName, ...otherParameters] = parameterValues;
const functionArgs = otherParameters.join(', ');
return `${objectName}.${behaviorName}::${expressionInfo.name}(${functionArgs})`;
} else {
const functionArgs = parameterValues.join(', ');
return `${expressionInfo.name}(${functionArgs})`;
}
};

View File

@@ -1 +1,56 @@
// TODO
// @flow
import { formatExpressionCall } from './FormatExpressionCall';
import {
enumerateExpressions,
filterExpressions,
} from '../../InstructionOrExpressionSelector/EnumerateExpressions';
describe('HelpButton', () => {
const {
freeExpressions,
objectsExpressions,
behaviorsExpressions,
} = enumerateExpressions('number');
it('should properly format a free function', () => {
const countExpression = filterExpressions(freeExpressions, 'Count')[0];
expect(formatExpressionCall(countExpression, ['MyObject'])).toBe(
'Count(MyObject)'
);
const atan2Expression = filterExpressions(freeExpressions, 'atan2')[0];
expect(formatExpressionCall(atan2Expression, ['1', '2'])).toBe(
'atan2(1, 2)'
);
});
it('should properly format an object function', () => {
const variableStringExpression = filterExpressions(
objectsExpressions,
'Variable'
)[0];
expect(
formatExpressionCall(variableStringExpression, ['MyObject', 'Variable1'])
).toBe('MyObject.Variable(Variable1)');
});
it('should properly format an object function with an argument', () => {
const pointXExpression = filterExpressions(objectsExpressions, 'PointX')[0];
expect(
formatExpressionCall(pointXExpression, ['MyObject', 'MyPoint'])
).toBe('MyObject.PointX(MyPoint)');
});
it('should properly format an object behavior function', () => {
const variableStringExpression = filterExpressions(
behaviorsExpressions,
'JumpSpeed'
)[0];
expect(
formatExpressionCall(variableStringExpression, [
'MyObject',
'PlatformerObject',
])
).toBe('MyObject.PlatformerObject::JumpSpeed()');
});
});

View File

@@ -1,5 +1,5 @@
import { enumerateObjects, filterObjectsList } from '../EnumerateObjects';
import { makeTestProject } from '../../fixtures/TestProject';
import { enumerateObjects, filterObjectsList } from './EnumerateObjects';
import { makeTestProject } from '../fixtures/TestProject';
const gd = global.gd;
describe('EnumerateObjects', () => {
@@ -12,9 +12,9 @@ describe('EnumerateObjects', () => {
allObjectsList,
} = enumerateObjects(project, testLayout);
expect(containerObjectsList).toHaveLength(5);
expect(containerObjectsList).toHaveLength(6);
expect(projectObjectsList).toHaveLength(2);
expect(allObjectsList).toHaveLength(7);
expect(allObjectsList).toHaveLength(8);
});
it('can do a case-insensitive search in the lists of objects', () => {

View File

@@ -116,6 +116,7 @@ export const makeTestProject = gd => {
testLayout.insertObject(tiledSpriteObject, 0);
testLayout.insertObject(panelSpriteObject, 0);
testLayout.insertObject(spriteObject, 0);
testLayout.insertObject(spriteObjectWithBehaviors, 0);
const group1 = new gd.ObjectGroup();
group1.setName('GroupOfSprites');

View File

@@ -122,7 +122,11 @@ storiesOf('ParameterFields', module)
.addDecorator(muiDecorator)
.add('ExpressionField', () => (
<ValueStateHolder initialValue={'MySpriteObject.X() + MouseX("", 0)'}>
<ExpressionField project={project} layout={testLayout} parameterRenderingService={ParameterRenderingService} />
<ExpressionField
project={project}
layout={testLayout}
parameterRenderingService={ParameterRenderingService}
/>
</ValueStateHolder>
));
@@ -212,13 +216,19 @@ storiesOf('EventsSheet', module)
storiesOf('ExpressionSelector', module)
.addDecorator(muiDecorator)
.add('default', () => (
<ExpressionSelector selectedType="" onChoose={action('Expression chosen')} />
<ExpressionSelector
selectedType=""
onChoose={action('Expression chosen')}
/>
));
storiesOf('InstructionSelector', module)
.addDecorator(muiDecorator)
.add('default', () => (
<InstructionSelector selectedType="" onChoose={action('Instruction chosen')} />
<InstructionSelector
selectedType=""
onChoose={action('Instruction chosen')}
/>
));
storiesOf('InstructionEditor', module)