Skip to content

Commit 00d442a

Browse files
committed
keep track of transformation stack, fixes #287
1 parent 43a41c6 commit 00d442a

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ public class PGraphicsAndroid2D extends PGraphics {
6666
float[] curveDrawX;
6767
float[] curveDrawY;
6868

69-
// int transformCount;
70-
// Matrix[] transformStack;
71-
float[] transform;
69+
static protected final int MATRIX_STACK_DEPTH = 32;
70+
protected float[][] transformStack;
71+
public PMatrix2D transform;
72+
protected Matrix tmpMatrix;
73+
protected float[] tmpArray;
74+
int transformCount;
7275

7376
// Line2D.Float line = new Line2D.Float();
7477
// Ellipse2D.Float ellipse = new Ellipse2D.Float();
@@ -107,9 +110,10 @@ public class PGraphicsAndroid2D extends PGraphics {
107110

108111

109112
public PGraphicsAndroid2D() {
110-
// transformStack = new Matrix[MATRIX_STACK_DEPTH];
111-
// transform = new float[6];
112-
transform = new float[9];
113+
transformStack = new float[MATRIX_STACK_DEPTH][6];
114+
transform = new PMatrix2D();
115+
tmpMatrix = new Matrix();
116+
tmpArray = new float[9];
113117

114118
path = new Path();
115119
rect = new RectF();
@@ -508,7 +512,6 @@ public void endShape(int mode) {
508512
}
509513

510514

511-
512515
//////////////////////////////////////////////////////////////
513516

514517
// BEZIER VERTICES
@@ -1387,45 +1390,45 @@ protected void textLineImpl(char buffer[], int start, int stop,
13871390

13881391
@Override
13891392
public void pushMatrix() {
1390-
// if (transformCount == transformStack.length) {
1391-
// throw new RuntimeException("pushMatrix() cannot use push more than " +
1392-
// transformStack.length + " times");
1393-
// }
1394-
// transformStack[transformCount] = canvas.getMatrix();
1395-
// transformCount++;
1396-
canvas.save(Canvas.MATRIX_SAVE_FLAG);
1393+
if (transformCount == transformStack.length) {
1394+
throw new RuntimeException("pushMatrix() cannot use push more than " +
1395+
transformStack.length + " times");
1396+
}
1397+
transform.get(transformStack[transformCount]);
1398+
transformCount++;
1399+
// canvas.save(Canvas.MATRIX_SAVE_FLAG);
13971400
}
13981401

13991402

14001403
@Override
14011404
public void popMatrix() {
1402-
// if (transformCount == 0) {
1403-
// throw new RuntimeException("missing a popMatrix() " +
1404-
// "to go with that pushMatrix()");
1405-
// }
1406-
// transformCount--;
1407-
// canvas.setMatrix(transformStack[transformCount]);
1408-
canvas.restore();
1405+
if (transformCount == 0) {
1406+
throw new RuntimeException("missing a popMatrix() " +
1407+
"to go with that pushMatrix()");
1408+
}
1409+
transformCount--;
1410+
transform.set(transformStack[transformCount]);
1411+
updateTmpMatrix();
1412+
canvas.setMatrix(tmpMatrix);
1413+
// canvas.restore();
14091414
}
14101415

14111416

1412-
14131417
//////////////////////////////////////////////////////////////
14141418

14151419
// MATRIX TRANSFORMS
14161420

14171421

14181422
@Override
14191423
public void translate(float tx, float ty) {
1424+
transform.translate(tx, ty);
14201425
canvas.translate(tx, ty);
14211426
}
14221427

14231428

1424-
//public void translate(float tx, float ty, float tz)
1425-
1426-
14271429
@Override
14281430
public void rotate(float angle) {
1431+
transform.rotate(angle * RAD_TO_DEG);
14291432
canvas.rotate(angle * RAD_TO_DEG);
14301433
}
14311434

@@ -1456,12 +1459,14 @@ public void rotate(float angle, float vx, float vy, float vz) {
14561459

14571460
@Override
14581461
public void scale(float s) {
1462+
transform.scale(s, s);
14591463
canvas.scale(s, s);
14601464
}
14611465

14621466

14631467
@Override
14641468
public void scale(float sx, float sy) {
1469+
transform.scale(sx, sy);
14651470
canvas.scale(sx, sy);
14661471
}
14671472

@@ -1474,26 +1479,29 @@ public void scale(float sx, float sy, float sz) {
14741479

14751480
@Override
14761481
public void shearX(float angle) {
1477-
canvas.skew((float) Math.tan(angle), 0);
1482+
float t = (float) Math.tan(angle);
1483+
transform.apply(1, t, 0, 0, 1, 0);
1484+
canvas.skew(t, 0);
14781485
}
14791486

14801487

14811488
@Override
14821489
public void shearY(float angle) {
1483-
canvas.skew(0, (float) Math.tan(angle));
1490+
float t = (float) Math.tan(angle);
1491+
transform.apply(1, 0, 0, t, 1, 0);
1492+
canvas.skew(0, t);
14841493
}
14851494

14861495

1487-
14881496
//////////////////////////////////////////////////////////////
14891497

14901498
// MATRIX MORE
14911499

14921500

14931501
@Override
14941502
public void resetMatrix() {
1495-
// canvas.setTransform(new AffineTransform());
1496-
canvas.setMatrix(new Matrix());
1503+
transform.reset();
1504+
canvas.setMatrix(null);
14971505
}
14981506

14991507

@@ -1503,15 +1511,9 @@ public void resetMatrix() {
15031511
@Override
15041512
public void applyMatrix(float n00, float n01, float n02,
15051513
float n10, float n11, float n12) {
1506-
// canvas.transform(new AffineTransform(n00, n10, n01, n11, n02, n12));
1507-
// TODO optimize
1508-
Matrix m = new Matrix();
1509-
m.setValues(new float[] {
1510-
n00, n01, n02,
1511-
n10, n11, n12,
1512-
0, 0, 1
1513-
});
1514-
canvas.concat(m);
1514+
transform.apply(n00, n01, n02, n10, n11, n12);
1515+
updateTmpMatrix();
1516+
canvas.concat(tmpMatrix);
15151517
}
15161518

15171519

@@ -1544,15 +1546,7 @@ public PMatrix2D getMatrix(PMatrix2D target) {
15441546
if (target == null) {
15451547
target = new PMatrix2D();
15461548
}
1547-
// canvas.getTransform().getMatrix(transform);
1548-
// Matrix m = new Matrix();
1549-
// canvas.getMatrix(m);
1550-
Matrix m = getMatrixImp();
1551-
m.getValues(transform);
1552-
// target.set((float) transform[0], (float) transform[2], (float) transform[4],
1553-
// (float) transform[1], (float) transform[3], (float) transform[5]);
1554-
target.set((float) transform[0], (float) transform[1], (float) transform[2],
1555-
(float) transform[3], (float) transform[4], (float) transform[5]);
1549+
target.set(transform);
15561550
return target;
15571551
}
15581552

@@ -1569,16 +1563,9 @@ public PMatrix3D getMatrix(PMatrix3D target) {
15691563

15701564
@Override
15711565
public void setMatrix(PMatrix2D source) {
1572-
// canvas.setTransform(new AffineTransform(source.m00, source.m10,
1573-
// source.m01, source.m11,
1574-
// source.m02, source.m12));
1575-
Matrix matrix = new Matrix();
1576-
matrix.setValues(new float[] {
1577-
source.m00, source.m01, source.m02,
1578-
source.m10, source.m11, source.m12,
1579-
0, 0, 1
1580-
});
1581-
canvas.setMatrix(matrix);
1566+
transform.set(source);
1567+
updateTmpMatrix();
1568+
canvas.setMatrix(tmpMatrix);
15821569
}
15831570

15841571

@@ -1595,11 +1582,27 @@ public void printMatrix() {
15951582

15961583

15971584
protected Matrix getMatrixImp() {
1598-
return parent.getSurfaceView().getMatrix();
1585+
Matrix m = new Matrix();
1586+
updateTmpMatrix();
1587+
m.set(tmpMatrix);
1588+
return m;
15991589
// return canvas.getMatrix();
16001590
}
16011591

16021592

1593+
protected void updateTmpMatrix() {
1594+
tmpArray[0] = transform.m00;
1595+
tmpArray[1] = transform.m01;
1596+
tmpArray[2] = transform.m02;
1597+
tmpArray[3] = transform.m10;
1598+
tmpArray[4] = transform.m11;
1599+
tmpArray[5] = transform.m12;
1600+
tmpArray[6] = 0;
1601+
tmpArray[7] = 0;
1602+
tmpArray[8] = 1;
1603+
tmpMatrix.setValues(tmpArray);
1604+
}
1605+
16031606

16041607
//////////////////////////////////////////////////////////////
16051608

@@ -2109,10 +2112,12 @@ public void set(int x, int y, PImage src) {
21092112
src.setModified(false);
21102113
}
21112114
// set() happens in screen coordinates, so need to clear the ctm
2112-
canvas.save(Canvas.MATRIX_SAVE_FLAG);
2115+
// canvas.save(Canvas.MATRIX_SAVE_FLAG);
2116+
pushMatrix();
21132117
canvas.setMatrix(null); // set to identity
21142118
canvas.drawBitmap(bitmap, x, y, null);
2115-
canvas.restore();
2119+
popMatrix();
2120+
// canvas.restore();
21162121
}
21172122

21182123

0 commit comments

Comments
 (0)