Skip to content

Commit

Permalink
Add matrix computation support to ShadowRenderNode implementations
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 403792961
  • Loading branch information
Googler authored and copybara-robolectric committed Oct 18, 2021
1 parent 7810eec commit bc6c58d
Show file tree
Hide file tree
Showing 2 changed files with 288 additions and 0 deletions.
Expand Up @@ -3,6 +3,9 @@
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.P;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.graphics.Rect;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

Expand All @@ -28,6 +31,10 @@ public class ShadowRenderNode {
private float translationX;
private float translationY;
private float translationZ;
private int left;
private int top;
private int right;
private int bottom;

@Implementation
public boolean setAlpha(float alpha) {
Expand Down Expand Up @@ -177,6 +184,14 @@ public boolean isPivotExplicitlySet() {
return pivotExplicitlySet;
}

@Implementation
public boolean resetPivot() {
this.pivotExplicitlySet = false;
this.pivotX = 0;
this.pivotY = 0;
return true;
}

@Implementation
public boolean setPivotX(float pivotX) {
this.pivotX = pivotX;
Expand All @@ -201,6 +216,135 @@ public float getPivotY() {
return pivotY;
}

@Implementation
public boolean setLeft(int left) {
this.left = left;
return true;
}

@Implementation
public int getLeft() {
return left;
}

@Implementation
public boolean setTop(int top) {
this.top = top;
return true;
}

@Implementation
public int getTop() {
return top;
}

@Implementation
public boolean setRight(int right) {
this.right = right;
return true;
}

@Implementation
public int getRight() {
return right;
}

@Implementation
public boolean setBottom(int bottom) {
this.bottom = bottom;
return true;
}

@Implementation
public int getBottom() {
return bottom;
}

@Implementation
public int getWidth() {
return right - left;
}

@Implementation
public int getHeight() {
return bottom - top;
}

@Implementation
public boolean setLeftTopRightBottom(int left, int top, int right, int bottom) {
return setPosition(left, top, right, bottom);
}

@Implementation
public boolean setPosition(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
return true;
}

@Implementation
public boolean setPosition(Rect position) {
this.left = position.left;
this.top = position.top;
this.right = position.right;
this.bottom = position.bottom;
return true;
}

@Implementation
public boolean offsetLeftAndRight(int offset) {
this.left += offset;
this.right += offset;
return true;
}

@Implementation
public boolean offsetTopAndBottom(int offset) {
this.top += offset;
this.bottom += offset;
return true;
}

@Implementation
public void getInverseMatrix(Matrix matrix) {
getMatrix(matrix);
matrix.invert(matrix);
}

@Implementation
public void getMatrix(Matrix matrix) {
if (!pivotExplicitlySet) {
pivotX = getWidth() / 2f;
pivotY = getHeight() / 2f;
}
matrix.reset();
if (rotationX == 0 && rotationY == 0) {
matrix.setTranslate(translationX, translationY);
matrix.preRotate(rotation, pivotX, pivotY);
matrix.postScale(scaleX, scaleY, pivotX, pivotY);
} else {
matrix.preScale(scaleX, scaleY, pivotX, pivotY);
Camera camera = new Camera();
camera.rotateX(rotationX);
camera.rotateY(rotationY);
camera.rotateZ(-rotation);
Matrix transform = new Matrix();
camera.getMatrix(transform);
transform.preTranslate(-pivotX, -pivotY);
transform.postTranslate(pivotX + translationX, pivotY + translationY);
matrix.postConcat(transform);
}
}

@Implementation
public boolean hasIdentityMatrix() {
Matrix matrix = new Matrix();
getMatrix(matrix);
return matrix.isIdentity();
}

@Implementation
protected boolean isValid() {
return true;
Expand Down
@@ -1,5 +1,8 @@
package org.robolectric.shadows;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RenderNode;
import android.os.Build;
import org.robolectric.annotation.Implementation;
Expand All @@ -23,6 +26,10 @@ public class ShadowRenderNodeQ {
private float translationX;
private float translationY;
private float translationZ;
private int left;
private int top;
private int right;
private int bottom;

@Implementation
protected boolean setAlpha(float alpha) {
Expand Down Expand Up @@ -172,6 +179,14 @@ protected boolean isPivotExplicitlySet() {
return pivotExplicitlySet;
}

@Implementation
protected boolean resetPivot() {
this.pivotExplicitlySet = false;
this.pivotX = 0;
this.pivotY = 0;
return true;
}

@Implementation
protected boolean setPivotX(float pivotX) {
this.pivotX = pivotX;
Expand All @@ -196,6 +211,135 @@ protected float getPivotY() {
return pivotY;
}

@Implementation
protected boolean setLeft(int left) {
this.left = left;
return true;
}

@Implementation
protected int getLeft() {
return left;
}

@Implementation
protected boolean setTop(int top) {
this.top = top;
return true;
}

@Implementation
protected int getTop() {
return top;
}

@Implementation
protected boolean setRight(int right) {
this.right = right;
return true;
}

@Implementation
protected int getRight() {
return right;
}

@Implementation
protected boolean setBottom(int bottom) {
this.bottom = bottom;
return true;
}

@Implementation
protected int getBottom() {
return bottom;
}

@Implementation
protected int getWidth() {
return right - left;
}

@Implementation
protected int getHeight() {
return bottom - top;
}

@Implementation
protected boolean setLeftTopRightBottom(int left, int top, int right, int bottom) {
return setPosition(left, top, right, bottom);
}

@Implementation
protected boolean setPosition(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
return true;
}

@Implementation
protected boolean setPosition(Rect position) {
this.left = position.left;
this.top = position.top;
this.right = position.right;
this.bottom = position.bottom;
return true;
}

@Implementation
protected boolean offsetLeftAndRight(int offset) {
this.left += offset;
this.right += offset;
return true;
}

@Implementation
protected boolean offsetTopAndBottom(int offset) {
this.top += offset;
this.bottom += offset;
return true;
}

@Implementation
protected void getInverseMatrix(Matrix matrix) {
getMatrix(matrix);
matrix.invert(matrix);
}

@Implementation
protected void getMatrix(Matrix matrix) {
if (!pivotExplicitlySet) {
pivotX = getWidth() / 2f;
pivotY = getHeight() / 2f;
}
matrix.reset();
if (rotationX == 0 && rotationY == 0) {
matrix.setTranslate(translationX, translationY);
matrix.preRotate(rotation, pivotX, pivotY);
matrix.postScale(scaleX, scaleY, pivotX, pivotY);
} else {
matrix.preScale(scaleX, scaleY, pivotX, pivotY);
Camera camera = new Camera();
camera.rotateX(rotationX);
camera.rotateY(rotationY);
camera.rotateZ(-rotation);
Matrix transform = new Matrix();
camera.getMatrix(transform);
transform.preTranslate(-pivotX, -pivotY);
transform.postTranslate(pivotX + translationX, pivotY + translationY);
matrix.postConcat(transform);
}
}

@Implementation
protected boolean hasIdentityMatrix() {
Matrix matrix = new Matrix();
getMatrix(matrix);
return matrix.isIdentity();
}

@Implementation
protected static boolean nIsValid(long n) {
return true;
Expand Down

0 comments on commit bc6c58d

Please sign in to comment.