@@ -66,9 +66,12 @@ public class PGraphicsAndroid2D extends PGraphics {
66
66
float [] curveDrawX ;
67
67
float [] curveDrawY ;
68
68
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 ;
72
75
73
76
// Line2D.Float line = new Line2D.Float();
74
77
// Ellipse2D.Float ellipse = new Ellipse2D.Float();
@@ -107,9 +110,10 @@ public class PGraphicsAndroid2D extends PGraphics {
107
110
108
111
109
112
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 ];
113
117
114
118
path = new Path ();
115
119
rect = new RectF ();
@@ -508,7 +512,6 @@ public void endShape(int mode) {
508
512
}
509
513
510
514
511
-
512
515
//////////////////////////////////////////////////////////////
513
516
514
517
// BEZIER VERTICES
@@ -1387,45 +1390,45 @@ protected void textLineImpl(char buffer[], int start, int stop,
1387
1390
1388
1391
@ Override
1389
1392
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);
1397
1400
}
1398
1401
1399
1402
1400
1403
@ Override
1401
1404
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();
1409
1414
}
1410
1415
1411
1416
1412
-
1413
1417
//////////////////////////////////////////////////////////////
1414
1418
1415
1419
// MATRIX TRANSFORMS
1416
1420
1417
1421
1418
1422
@ Override
1419
1423
public void translate (float tx , float ty ) {
1424
+ transform .translate (tx , ty );
1420
1425
canvas .translate (tx , ty );
1421
1426
}
1422
1427
1423
1428
1424
- //public void translate(float tx, float ty, float tz)
1425
-
1426
-
1427
1429
@ Override
1428
1430
public void rotate (float angle ) {
1431
+ transform .rotate (angle * RAD_TO_DEG );
1429
1432
canvas .rotate (angle * RAD_TO_DEG );
1430
1433
}
1431
1434
@@ -1456,12 +1459,14 @@ public void rotate(float angle, float vx, float vy, float vz) {
1456
1459
1457
1460
@ Override
1458
1461
public void scale (float s ) {
1462
+ transform .scale (s , s );
1459
1463
canvas .scale (s , s );
1460
1464
}
1461
1465
1462
1466
1463
1467
@ Override
1464
1468
public void scale (float sx , float sy ) {
1469
+ transform .scale (sx , sy );
1465
1470
canvas .scale (sx , sy );
1466
1471
}
1467
1472
@@ -1474,26 +1479,29 @@ public void scale(float sx, float sy, float sz) {
1474
1479
1475
1480
@ Override
1476
1481
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 );
1478
1485
}
1479
1486
1480
1487
1481
1488
@ Override
1482
1489
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 );
1484
1493
}
1485
1494
1486
1495
1487
-
1488
1496
//////////////////////////////////////////////////////////////
1489
1497
1490
1498
// MATRIX MORE
1491
1499
1492
1500
1493
1501
@ Override
1494
1502
public void resetMatrix () {
1495
- // canvas.setTransform(new AffineTransform() );
1496
- canvas .setMatrix (new Matrix () );
1503
+ transform . reset ( );
1504
+ canvas .setMatrix (null );
1497
1505
}
1498
1506
1499
1507
@@ -1503,15 +1511,9 @@ public void resetMatrix() {
1503
1511
@ Override
1504
1512
public void applyMatrix (float n00 , float n01 , float n02 ,
1505
1513
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 );
1515
1517
}
1516
1518
1517
1519
@@ -1544,15 +1546,7 @@ public PMatrix2D getMatrix(PMatrix2D target) {
1544
1546
if (target == null ) {
1545
1547
target = new PMatrix2D ();
1546
1548
}
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 );
1556
1550
return target ;
1557
1551
}
1558
1552
@@ -1569,16 +1563,9 @@ public PMatrix3D getMatrix(PMatrix3D target) {
1569
1563
1570
1564
@ Override
1571
1565
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 );
1582
1569
}
1583
1570
1584
1571
@@ -1595,11 +1582,27 @@ public void printMatrix() {
1595
1582
1596
1583
1597
1584
protected Matrix getMatrixImp () {
1598
- return parent .getSurfaceView ().getMatrix ();
1585
+ Matrix m = new Matrix ();
1586
+ updateTmpMatrix ();
1587
+ m .set (tmpMatrix );
1588
+ return m ;
1599
1589
// return canvas.getMatrix();
1600
1590
}
1601
1591
1602
1592
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
+
1603
1606
1604
1607
//////////////////////////////////////////////////////////////
1605
1608
@@ -2109,10 +2112,12 @@ public void set(int x, int y, PImage src) {
2109
2112
src .setModified (false );
2110
2113
}
2111
2114
// 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 ();
2113
2117
canvas .setMatrix (null ); // set to identity
2114
2118
canvas .drawBitmap (bitmap , x , y , null );
2115
- canvas .restore ();
2119
+ popMatrix ();
2120
+ // canvas.restore();
2116
2121
}
2117
2122
2118
2123
0 commit comments