Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 81dd7c6

Browse files
authored
add pivot support (#251)
1 parent 221ea16 commit 81dd7c6

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

constraintlayout/compose/src/main/java/androidx/constraintlayout/compose/ConstraintLayout.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.foundation.layout.LayoutScopeMarker
2323
import androidx.compose.runtime.*
2424
import androidx.compose.ui.Modifier
2525
import androidx.compose.ui.graphics.GraphicsLayerScope
26+
import androidx.compose.ui.graphics.TransformOrigin
2627
import androidx.compose.ui.layout.*
2728
import androidx.compose.ui.platform.InspectorValueInfo
2829
import androidx.compose.ui.platform.debugInspectorInfo
@@ -1645,18 +1646,23 @@ internal open class Measurer : BasicMeasure.Measurer, DesignInfoProvider {
16451646
for (child in root.children) {
16461647
val measurable = child.companionWidget
16471648
if (measurable !is Measurable) continue
1648-
var frame = WidgetFrame(child.frame.update())
1649+
val frame = WidgetFrame(child.frame.update())
16491650
frameCache[measurable] = frame
16501651
}
16511652
}
16521653
measurables.fastForEach { measurable ->
16531654
var frame = frameCache[measurable]!!
16541655
if (frame.isDefaultTransform()) {
1655-
var x = frameCache[measurable]!!.left
1656-
var y = frameCache[measurable]!!.top
1656+
val x = frameCache[measurable]!!.left
1657+
val y = frameCache[measurable]!!.top
16571658
placeables[measurable]?.place(IntOffset(x, y))
16581659
} else {
16591660
val layerBlock: GraphicsLayerScope.() -> Unit = {
1661+
if (!frame.pivotX.isNaN() || !frame.pivotY.isNaN()) {
1662+
val pivotX = if (frame.pivotX.isNaN()) 0.5f else frame.pivotX
1663+
val pivotY = if (frame.pivotY.isNaN()) 0.5f else frame.pivotY
1664+
transformOrigin = TransformOrigin(pivotX, pivotY)
1665+
}
16601666
rotationX = frame.rotationX
16611667
rotationY = frame.rotationY
16621668
rotationZ = frame.rotationZ
@@ -1666,8 +1672,8 @@ internal open class Measurer : BasicMeasure.Measurer, DesignInfoProvider {
16661672
scaleY = frame.scaleY
16671673
alpha = frame.alpha
16681674
}
1669-
var x = frameCache[measurable]!!.left
1670-
var y = frameCache[measurable]!!.top
1675+
val x = frameCache[measurable]!!.left
1676+
val y = frameCache[measurable]!!.top
16711677
placeables[measurable]?.placeWithLayer(x, y, layerBlock = layerBlock)
16721678
}
16731679
}

constraintlayout/compose/src/main/java/androidx/constraintlayout/compose/ConstraintSetParser.kt

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class LayoutVariables {
6363
}
6464
} else if (elementName is Int) {
6565
return elementName.toFloat()
66+
} else if (elementName is Double) {
67+
return elementName.toFloat()
6668
} else if (elementName is Float) {
6769
return elementName
6870
}
@@ -431,37 +433,44 @@ fun parseWidget(
431433
reference.bottomToBottom(targetReference)
432434
}
433435
"alpha" -> {
434-
var value = layoutVariables.get(element[constraintName])
436+
val value = layoutVariables.get(element[constraintName])
435437
reference.alpha(value)
436-
// reference.alpha(element.getDouble(constraintName).toFloat())
437438
}
438439
"scaleX" -> {
439-
var value = layoutVariables.get(element[constraintName])
440-
reference.scaleX(value) //element.getDouble(constraintName).toFloat())
440+
val value = layoutVariables.get(element[constraintName])
441+
reference.scaleX(value)
441442
}
442443
"scaleY" -> {
443-
var value = layoutVariables.get(element[constraintName])
444-
reference.scaleY(value) //element.getDouble(constraintName).toFloat())
444+
val value = layoutVariables.get(element[constraintName])
445+
reference.scaleY(value)
445446
}
446447
"translationX" -> {
447-
var value = layoutVariables.get(element[constraintName])
448-
reference.translationX(value) //element.getDouble(constraintName).toFloat())
448+
val value = layoutVariables.get(element[constraintName])
449+
reference.translationX(value)
449450
}
450451
"translationY" -> {
451-
var value = layoutVariables.get(element[constraintName])
452-
reference.translationY(value) //element.getDouble(constraintName).toFloat())
452+
val value = layoutVariables.get(element[constraintName])
453+
reference.translationY(value)
454+
}
455+
"pivotX" -> {
456+
val value = layoutVariables.get(element[constraintName])
457+
reference.pivotX(value)
458+
}
459+
"pivotY" -> {
460+
val value = layoutVariables.get(element[constraintName])
461+
reference.pivotY(value)
453462
}
454463
"rotationX" -> {
455-
var value = layoutVariables.get(element[constraintName])
456-
reference.rotationX(value) //element.getDouble(constraintName).toFloat())
464+
val value = layoutVariables.get(element[constraintName])
465+
reference.rotationX(value)
457466
}
458467
"rotationY" -> {
459-
var value = layoutVariables.get(element[constraintName])
460-
reference.rotationY(value) //element.getDouble(constraintName).toFloat())
468+
val value = layoutVariables.get(element[constraintName])
469+
reference.rotationY(value)
461470
}
462471
"rotationZ" -> {
463-
var value = layoutVariables.get(element[constraintName])
464-
reference.rotationZ(value) // element.getDouble(constraintName).toFloat())
472+
val value = layoutVariables.get(element[constraintName])
473+
reference.rotationZ(value)
465474
}
466475
"custom" -> {
467476
parseCustomProperties(element, reference, constraintName)

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/ConstraintReference.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public interface ConstraintReferenceFactory {
7676
int mMarginTopGone = 0;
7777
int mMarginBottomGone = 0;
7878

79+
float mPivotX = 0.5f;
80+
float mPivotY = 0.5f;
81+
7982
float mRotationX = 0;
8083
float mRotationY = 0;
8184
float mRotationZ = 0;
@@ -251,10 +254,22 @@ public ConstraintReference clearHorizontal() {
251254
public float getScaleX() { return mScaleX; }
252255
public float getScaleY() { return mScaleY; }
253256
public float getAlpha() { return mAlpha; }
257+
public float getPivotX() { return mPivotX; }
258+
public float getPivotY() { return mPivotY; }
254259
public float getRotationX() { return mRotationX; }
255260
public float getRotationY() { return mRotationY; }
256261
public float getRotationZ() { return mRotationZ; }
257262

263+
public ConstraintReference pivotX(float x) {
264+
mPivotX = x;
265+
return this;
266+
}
267+
268+
public ConstraintReference pivotY(float y) {
269+
mPivotY = y;
270+
return this;
271+
}
272+
258273
public ConstraintReference rotationX(float x) {
259274
mRotationX = x;
260275
return this;
@@ -846,6 +861,8 @@ public void apply() {
846861
mConstraintWidget.setHorizontalBiasPercent(mHorizontalBias);
847862
mConstraintWidget.setVerticalBiasPercent(mVerticalBias);
848863

864+
mConstraintWidget.frame.pivotX = mPivotX;
865+
mConstraintWidget.frame.pivotY = mPivotY;
849866
mConstraintWidget.frame.rotationX = mRotationX;
850867
mConstraintWidget.frame.rotationY = mRotationY;
851868
mConstraintWidget.frame.rotationZ = mRotationZ;

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/WidgetFrame.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class WidgetFrame {
3232

3333
// transforms
3434

35+
public float pivotX = Float.NaN;
36+
public float pivotY = Float.NaN;
37+
3538
public float rotationX = Float.NaN;
3639
public float rotationY = Float.NaN;
3740
public float rotationZ = Float.NaN;
@@ -84,6 +87,8 @@ public WidgetFrame(WidgetFrame frame) {
8487
top = frame.top;
8588
right = frame.right;
8689
bottom = frame.bottom;
90+
pivotX = frame.pivotX;
91+
pivotY = frame.pivotY;
8792
rotationX = frame.rotationX;
8893
rotationY = frame.rotationY;
8994
rotationZ = frame.rotationZ;
@@ -120,6 +125,9 @@ public static void interpolate(WidgetFrame frame, WidgetFrame start, WidgetFrame
120125
frame.right = (int) (start.right + progress*(end.right - start.right));
121126
frame.bottom = (int) (start.bottom + progress*(end.bottom - start.bottom));
122127

128+
frame.pivotX = interpolate(start.pivotX, end.pivotX, 0f, progress);
129+
frame.pivotY = interpolate(start.pivotY, end.pivotY, 0f, progress);
130+
123131
frame.rotationX = interpolate(start.rotationX, end.rotationX, 0f, progress);
124132
frame.rotationY = interpolate(start.rotationY, end.rotationY, 0f, progress);
125133
frame.rotationZ = interpolate(start.rotationZ, end.rotationZ, 0f, progress);
@@ -176,6 +184,8 @@ public WidgetFrame update() {
176184
right = widget.getRight();
177185
bottom = widget.getBottom();
178186
WidgetFrame frame = widget.frame;
187+
pivotX = frame.pivotX;
188+
pivotY = frame.pivotY;
179189
rotationX = frame.rotationX;
180190
rotationY = frame.rotationY;
181191
rotationZ = frame.rotationZ;

projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/test.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,9 @@ public fun ScreenExample12() {
938938
width: 200,
939939
height: 40,
940940
circular: ['parent', 'angle', 'distance'],
941+
pivotX: 0.1,
942+
pivotY: 0.1,
943+
translationX: 225,
941944
rotationZ: 'rotation'
942945
}
943946
}

0 commit comments

Comments
 (0)