Skip to content

Commit 1cef3ee

Browse files
committed
Allow Choices properties for anchors.
1 parent 9f395d2 commit 1cef3ee

File tree

1 file changed

+93
-31
lines changed

1 file changed

+93
-31
lines changed

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

Lines changed: 93 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ const layoutFields = [
208208
'AnchorDeltaY',
209209
];
210210

211-
const getHorizontalAnchorValue = (anchorName: string) => {
211+
const getHorizontalAnchorValue = (
212+
anchorName: string,
213+
properties: ?gdNamedPropertyDescriptorsList
214+
): ?number => {
212215
const horizontalAnchorName = (anchorName.includes('-')
213216
? anchorName.split('-')[1]
214217
: anchorName
@@ -219,9 +222,16 @@ const getHorizontalAnchorValue = (anchorName: string) => {
219222
? 1
220223
: horizontalAnchorName === 'center'
221224
? 0.5
225+
: // Reference to another property to allow to expose a Choice property.
226+
properties && properties.has(anchorName)
227+
? getHorizontalAnchorValue(properties.get(anchorName).getValue(), null)
222228
: null;
223229
};
224-
const getVerticalAnchorValue = (anchorName: string) => {
230+
231+
const getVerticalAnchorValue = (
232+
anchorName: string,
233+
properties: ?gdNamedPropertyDescriptorsList
234+
): ?number => {
225235
const verticalAnchorName = (anchorName.includes('-')
226236
? anchorName.split('-')[0]
227237
: anchorName
@@ -232,9 +242,52 @@ const getVerticalAnchorValue = (anchorName: string) => {
232242
? 1
233243
: verticalAnchorName === 'center'
234244
? 0.5
245+
: // Reference to another property to allow to expose a Choice property.
246+
properties && properties.has(anchorName)
247+
? getVerticalAnchorValue(properties.get(anchorName).getValue(), null)
235248
: null;
236249
};
237250

251+
/**
252+
* Origin anchors can have smart value to only expose the target anchor property
253+
* and fill the origin anchor property accordingly.
254+
*/
255+
const getHorizontalOriginAnchorValue = (
256+
anchorName: string,
257+
properties: gdNamedPropertyDescriptorsList,
258+
targetAnchorValue: ?number
259+
): ?number => {
260+
const horizontalAnchorName = (anchorName.includes('-')
261+
? anchorName.split('-')[1]
262+
: anchorName
263+
).toLowerCase();
264+
return horizontalAnchorName === 'same'
265+
? targetAnchorValue
266+
: horizontalAnchorName === 'opposite' && targetAnchorValue != null
267+
? 1 - targetAnchorValue
268+
: getHorizontalAnchorValue(horizontalAnchorName, properties);
269+
};
270+
271+
/**
272+
* Origin anchors can have smart value to only expose the target anchor property
273+
* and fill the origin anchor property accordingly.
274+
*/
275+
const getVerticalOriginAnchorValue = (
276+
anchorName: string,
277+
properties: gdNamedPropertyDescriptorsList,
278+
targetAnchorValue: ?number
279+
): ?number => {
280+
const verticalAnchorName = (anchorName.includes('-')
281+
? anchorName.split('-')[0]
282+
: anchorName
283+
).toLowerCase();
284+
return verticalAnchorName === 'same'
285+
? targetAnchorValue
286+
: verticalAnchorName === 'opposite' && targetAnchorValue != null
287+
? 1 - targetAnchorValue
288+
: getVerticalAnchorValue(verticalAnchorName, properties);
289+
};
290+
238291
/**
239292
* Build the layouts description from the custom object properties.
240293
*/
@@ -280,31 +333,33 @@ const getLayouts = (
280333
layoutField === 'AnchorOrigin'
281334
) {
282335
const targetPropertyName = name.replace('AnchorOrigin', 'AnchorTarget');
283-
const targetProperty = properties.get(targetPropertyName);
284-
targetObjectName =
285-
targetProperty.getExtraInfo().size() > 0
286-
? targetProperty.getExtraInfo().at(0)
287-
: '';
288-
const anchorTargetStringValue = instanceProperties
289-
.get(targetPropertyName)
290-
.getValue();
291-
const anchorTargetValueNumber =
292-
Number.parseFloat(anchorTargetStringValue) || 0;
293-
if (
294-
layoutField === 'HorizontalAnchorOrigin' ||
295-
layoutField === 'AnchorOrigin'
296-
) {
297-
horizontalAnchorTarget =
298-
getHorizontalAnchorValue(anchorTargetStringValue) ||
299-
anchorTargetValueNumber;
300-
}
301-
if (
302-
layoutField === 'VerticalAnchorOrigin' ||
303-
layoutField === 'AnchorOrigin'
304-
) {
305-
verticalAnchorTarget =
306-
getVerticalAnchorValue(anchorTargetStringValue) ||
307-
anchorTargetValueNumber;
336+
if (properties.has(targetPropertyName)) {
337+
const targetProperty = properties.get(targetPropertyName);
338+
targetObjectName =
339+
targetProperty.getExtraInfo().size() > 0
340+
? targetProperty.getExtraInfo().at(0)
341+
: '';
342+
const anchorTargetStringValue = instanceProperties
343+
.get(targetPropertyName)
344+
.getValue();
345+
const anchorTargetValueNumber =
346+
Number.parseFloat(anchorTargetStringValue) || 0;
347+
if (
348+
layoutField === 'HorizontalAnchorOrigin' ||
349+
layoutField === 'AnchorOrigin'
350+
) {
351+
horizontalAnchorTarget =
352+
getHorizontalAnchorValue(anchorTargetStringValue, instanceProperties) ||
353+
anchorTargetValueNumber;
354+
}
355+
if (
356+
layoutField === 'VerticalAnchorOrigin' ||
357+
layoutField === 'AnchorOrigin'
358+
) {
359+
verticalAnchorTarget =
360+
getVerticalAnchorValue(anchorTargetStringValue, instanceProperties) ||
361+
anchorTargetValueNumber;
362+
}
308363
}
309364
}
310365

@@ -320,7 +375,7 @@ const getLayouts = (
320375
layouts.set(childName, layout);
321376
}
322377
if (layoutField === 'Show') {
323-
if (propertyValueString === 'false') {
378+
if (propertyValueString !== 'true') {
324379
layout.isShown = false;
325380
}
326381
} else if (layoutField === 'LeftPadding') {
@@ -341,8 +396,11 @@ const getLayouts = (
341396
layoutField === 'AnchorOrigin'
342397
) {
343398
const anchorOrigin =
344-
getHorizontalAnchorValue(propertyValueString) ||
345-
propertyValueNumber;
399+
getHorizontalOriginAnchorValue(
400+
propertyValueString,
401+
properties,
402+
horizontalAnchorTarget
403+
) || propertyValueNumber;
346404
if (anchorOrigin !== null) {
347405
layout.horizontalLayout.anchorOrigin = anchorOrigin;
348406
}
@@ -356,7 +414,11 @@ const getLayouts = (
356414
layoutField === 'AnchorOrigin'
357415
) {
358416
const anchorOrigin =
359-
getVerticalAnchorValue(propertyValueString) || propertyValueNumber;
417+
getVerticalOriginAnchorValue(
418+
propertyValueString,
419+
properties,
420+
horizontalAnchorTarget
421+
) || propertyValueNumber;
360422
if (anchorOrigin !== null) {
361423
layout.verticalLayout.anchorOrigin = anchorOrigin;
362424
}

0 commit comments

Comments
 (0)