Skip to content

Commit 40c3f8e

Browse files
committed
Handle anchor as numbers.
1 parent 5a42a16 commit 40c3f8e

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

newIDE/app/src/ObjectsRendering/Renderers/RenderedCustomObjectInstance.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,15 @@ const getLayouts = (
202202
}
203203

204204
const name = property.getName();
205+
const isNumber = property.getType() === 'Number';
205206
const propertyValueString = instanceProperties.get(name).getValue();
206-
const propertyValueNumber = Number.parseFloat(propertyValueString) || 0;
207+
const propertyValueNumber = isNumber
208+
? Number.parseFloat(propertyValueString) || 0
209+
: 0;
207210
const layoutField = layoutFields.find(field => name.includes(field));
208211

209212
let targetObjectName = '';
210-
let anchorTarget = null;
213+
let anchorTarget: ?number = null;
211214
if (
212215
layoutField === 'HorizontalAnchorOrigin' ||
213216
layoutField === 'VerticalAnchorOrigin'
@@ -218,11 +221,21 @@ const getLayouts = (
218221
targetProperty.getExtraInfo().size() > 0
219222
? targetProperty.getExtraInfo().at(0)
220223
: '';
221-
const anchorTargetName = instanceProperties.get(name).getValue();
224+
const anchorTargetStringValue = instanceProperties
225+
.get(targetPropertyName)
226+
.getValue();
227+
const targetIsNumber = targetProperty.getType() === 'Number';
228+
const anchorTargetValueNumber = isNumber
229+
? Number.parseFloat(anchorTargetStringValue) || 0
230+
: 0;
222231
if (layoutField === 'HorizontalAnchorOrigin') {
223-
anchorTarget = getHorizontalAnchorValue(anchorTargetName);
232+
anchorTarget = targetIsNumber
233+
? anchorTargetValueNumber
234+
: getHorizontalAnchorValue(anchorTargetStringValue);
224235
} else {
225-
anchorTarget = getVerticalAnchorValue(anchorTargetName);
236+
anchorTarget = targetIsNumber
237+
? anchorTargetValueNumber
238+
: getVerticalAnchorValue(anchorTargetStringValue);
226239
}
227240
}
228241

@@ -245,16 +258,26 @@ const getLayouts = (
245258
} else if (layoutField === 'BottomPadding') {
246259
layout.verticalLayout.maxSideAbsoluteMargin = propertyValueNumber;
247260
} else if (layoutField === 'HorizontalAnchorOrigin') {
248-
layout.horizontalLayout.anchorOrigin = getHorizontalAnchorValue(
249-
propertyValueString
250-
);
251-
layout.horizontalLayout.anchorTarget = anchorTarget;
261+
const anchorOrigin = isNumber
262+
? propertyValueNumber
263+
: getHorizontalAnchorValue(propertyValueString);
264+
if (anchorOrigin !== null) {
265+
layout.horizontalLayout.anchorOrigin = anchorOrigin;
266+
}
267+
if (anchorTarget !== null) {
268+
layout.horizontalLayout.anchorTarget = anchorTarget;
269+
}
252270
layout.horizontalLayout.anchorTargetObject = targetObjectName;
253271
} else if (layoutField === 'VerticalAnchorOrigin') {
254-
layout.verticalLayout.anchorOrigin = getVerticalAnchorValue(
255-
propertyValueString
256-
);
257-
layout.verticalLayout.anchorTarget = anchorTarget;
272+
const anchorOrigin = isNumber
273+
? propertyValueNumber
274+
: getVerticalAnchorValue(propertyValueString);
275+
if (anchorOrigin !== null) {
276+
layout.verticalLayout.anchorOrigin = anchorOrigin;
277+
}
278+
if (anchorTarget !== null) {
279+
layout.verticalLayout.anchorTarget = anchorTarget;
280+
}
258281
layout.verticalLayout.anchorTargetObject = targetObjectName;
259282
}
260283
}
@@ -428,23 +451,23 @@ export default class RenderedCustomObjectInstance extends RenderedInstance {
428451
height -
429452
(childLayout.verticalLayout.maxSideAbsoluteMargin ||
430453
(childLayout.verticalLayout.maxSideProportionalMargin || 0) * height);
431-
if (!childLayout.horizontalLayout.anchorOrigin) {
454+
if (childLayout.horizontalLayout.anchorOrigin == null) {
432455
childInstance.x = childMinX;
433456
childInstance.setCustomWidth(childMaxX - childMinX);
434457
} else {
435-
const anchorOrigin = childLayout.horizontalLayout.anchorOrigin;
436-
const anchorTarget = childLayout.horizontalLayout.anchorTarget;
458+
const anchorOrigin = childLayout.horizontalLayout.anchorOrigin || 0;
459+
const anchorTarget = childLayout.horizontalLayout.anchorTarget || 0;
437460
// TODO Use anchorTargetObject instead of defaulting on the background
438461
childInstance.x =
439462
anchorTarget * width -
440463
anchorOrigin * renderedInstance.getDefaultWidth();
441464
}
442-
if (!childLayout.verticalLayout.anchorOrigin) {
465+
if (childLayout.verticalLayout.anchorOrigin == null) {
443466
childInstance.y = childMinY;
444467
childInstance.setCustomHeight(childMaxY - childMinY);
445468
} else {
446-
const anchorOrigin = childLayout.verticalLayout.anchorOrigin;
447-
const anchorTarget = childLayout.verticalLayout.anchorTarget;
469+
const anchorOrigin = childLayout.verticalLayout.anchorOrigin || 0;
470+
const anchorTarget = childLayout.verticalLayout.anchorTarget || 0;
448471
// TODO Use anchorTargetObject instead of defaulting on the background
449472
childInstance.y =
450473
anchorTarget * height -
@@ -459,12 +482,12 @@ export default class RenderedCustomObjectInstance extends RenderedInstance {
459482
// This ensure objects are centered if their dimensions changed from the
460483
// custom ones (preferred ones).
461484
// For instance, text object dimensions change according to how the text is wrapped.
462-
if (!childLayout.horizontalLayout.anchorOrigin) {
485+
if (childLayout.horizontalLayout.anchorOrigin == null) {
463486
childInstance.x =
464487
(width - renderedInstance._pixiObject.width) / 2 +
465488
(childMinX + childMaxX - width) / 2;
466489
}
467-
if (!childLayout.verticalLayout.anchorOrigin) {
490+
if (childLayout.verticalLayout.anchorOrigin == null) {
468491
childInstance.y =
469492
(height - renderedInstance._pixiObject.height) / 2 +
470493
(childMinY + childMaxY - height) / 2;

0 commit comments

Comments
 (0)