@@ -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 ();
@@ -167,7 +171,7 @@ public void dispose() {
167
171
168
172
169
173
@ Override
170
- public PSurface createSurface (AppComponent component , SurfaceHolder holder ) { // ignore
174
+ public PSurface createSurface (AppComponent component , SurfaceHolder holder , boolean reset ) { // ignore
171
175
return new PSurfaceAndroid2D (this , component , holder );
172
176
}
173
177
@@ -228,7 +232,7 @@ public void endDraw() {
228
232
// }
229
233
230
234
if (primaryGraphics ) {
231
- SurfaceHolder holder = parent .getSurfaceHolder ();
235
+ SurfaceHolder holder = parent .surface . getSurfaceHolder ();
232
236
if (holder != null ) {
233
237
Canvas screen = null ;
234
238
try {
@@ -238,7 +242,10 @@ public void endDraw() {
238
242
}
239
243
} finally {
240
244
if (screen != null ) {
241
- parent .getSurfaceHolder ().unlockCanvasAndPost (screen );
245
+ try {
246
+ holder .unlockCanvasAndPost (screen );
247
+ } catch (IllegalStateException ex ) {
248
+ }
242
249
}
243
250
}
244
251
}
@@ -505,6 +512,24 @@ public void endShape(int mode) {
505
512
}
506
513
507
514
515
+ //////////////////////////////////////////////////////////////
516
+
517
+ // CLIPPING
518
+
519
+
520
+ @ Override
521
+ protected void clipImpl (float x1 , float y1 , float x2 , float y2 ) {
522
+ // canvas.save(Canvas.CLIP_SAVE_FLAG);
523
+ canvas .clipRect (x1 , y1 , x2 , y2 );
524
+ }
525
+
526
+
527
+ @ Override
528
+ public void noClip () {
529
+ canvas .clipRect (0 , 0 , width , height , Region .Op .REPLACE );
530
+ // canvas.restore();
531
+ }
532
+
508
533
509
534
//////////////////////////////////////////////////////////////
510
535
@@ -1221,10 +1246,16 @@ public PShape loadShape(String filename) {
1221
1246
public void textFont (PFont which ) {
1222
1247
super .textFont (which );
1223
1248
fillPaint .setTypeface ((Typeface ) which .getNative ());
1249
+ fillPaint .setTextSize (which .getDefaultSize ());
1224
1250
}
1225
1251
1226
1252
1227
- //public void textFont(PFont which, float size)
1253
+ @ Override
1254
+ public void textFont (PFont which , float size ) {
1255
+ super .textFont (which , size );
1256
+ fillPaint .setTypeface ((Typeface ) which .getNative ());
1257
+ fillPaint .setTextSize (size );
1258
+ }
1228
1259
1229
1260
1230
1261
//public void textLeading(float leading)
@@ -1256,14 +1287,7 @@ public void textSize(float size) {
1256
1287
fillPaint .setTextSize (size );
1257
1288
}
1258
1289
1259
- // take care of setting the textSize and textLeading vars
1260
- // this has to happen second, because it calls textAscent()
1261
- // (which requires the native font metrics to be set)
1262
- textSize = size ;
1263
- // PApplet.println("P2D textSize textAscent -> " + textAscent());
1264
- // PApplet.println("P2D textSize textDescent -> " + textDescent());
1265
- textLeading = (textAscent () + textDescent ()) * 1.275f ;
1266
- // PApplet.println("P2D textSize textLeading = " + textLeading);
1290
+ handleTextSize (size );
1267
1291
}
1268
1292
1269
1293
@@ -1384,45 +1408,45 @@ protected void textLineImpl(char buffer[], int start, int stop,
1384
1408
1385
1409
@ Override
1386
1410
public void pushMatrix () {
1387
- // if (transformCount == transformStack.length) {
1388
- // throw new RuntimeException("pushMatrix() cannot use push more than " +
1389
- // transformStack.length + " times");
1390
- // }
1391
- // transformStack[transformCount] = canvas.getMatrix( );
1392
- // transformCount++;
1393
- canvas .save (Canvas .MATRIX_SAVE_FLAG );
1411
+ if (transformCount == transformStack .length ) {
1412
+ throw new RuntimeException ("pushMatrix() cannot use push more than " +
1413
+ transformStack .length + " times" );
1414
+ }
1415
+ transform . get ( transformStack [transformCount ]);
1416
+ transformCount ++;
1417
+ // canvas.save(Canvas.MATRIX_SAVE_FLAG);
1394
1418
}
1395
1419
1396
1420
1397
1421
@ Override
1398
1422
public void popMatrix () {
1399
- // if (transformCount == 0) {
1400
- // throw new RuntimeException("missing a popMatrix() " +
1401
- // "to go with that pushMatrix()");
1402
- // }
1403
- // transformCount--;
1404
- // canvas.setMatrix(transformStack[transformCount]);
1405
- canvas .restore ();
1423
+ if (transformCount == 0 ) {
1424
+ throw new RuntimeException ("missing a popMatrix() " +
1425
+ "to go with that pushMatrix()" );
1426
+ }
1427
+ transformCount --;
1428
+ transform .set (transformStack [transformCount ]);
1429
+ updateTmpMatrix ();
1430
+ canvas .setMatrix (tmpMatrix );
1431
+ // canvas.restore();
1406
1432
}
1407
1433
1408
1434
1409
-
1410
1435
//////////////////////////////////////////////////////////////
1411
1436
1412
1437
// MATRIX TRANSFORMS
1413
1438
1414
1439
1415
1440
@ Override
1416
1441
public void translate (float tx , float ty ) {
1442
+ transform .translate (tx , ty );
1417
1443
canvas .translate (tx , ty );
1418
1444
}
1419
1445
1420
1446
1421
- //public void translate(float tx, float ty, float tz)
1422
-
1423
-
1424
1447
@ Override
1425
1448
public void rotate (float angle ) {
1449
+ transform .rotate (angle * RAD_TO_DEG );
1426
1450
canvas .rotate (angle * RAD_TO_DEG );
1427
1451
}
1428
1452
@@ -1453,12 +1477,14 @@ public void rotate(float angle, float vx, float vy, float vz) {
1453
1477
1454
1478
@ Override
1455
1479
public void scale (float s ) {
1480
+ transform .scale (s , s );
1456
1481
canvas .scale (s , s );
1457
1482
}
1458
1483
1459
1484
1460
1485
@ Override
1461
1486
public void scale (float sx , float sy ) {
1487
+ transform .scale (sx , sy );
1462
1488
canvas .scale (sx , sy );
1463
1489
}
1464
1490
@@ -1471,26 +1497,29 @@ public void scale(float sx, float sy, float sz) {
1471
1497
1472
1498
@ Override
1473
1499
public void shearX (float angle ) {
1474
- canvas .skew ((float ) Math .tan (angle ), 0 );
1500
+ float t = (float ) Math .tan (angle );
1501
+ transform .apply (1 , t , 0 , 0 , 1 , 0 );
1502
+ canvas .skew (t , 0 );
1475
1503
}
1476
1504
1477
1505
1478
1506
@ Override
1479
1507
public void shearY (float angle ) {
1480
- canvas .skew (0 , (float ) Math .tan (angle ));
1508
+ float t = (float ) Math .tan (angle );
1509
+ transform .apply (1 , 0 , 0 , t , 1 , 0 );
1510
+ canvas .skew (0 , t );
1481
1511
}
1482
1512
1483
1513
1484
-
1485
1514
//////////////////////////////////////////////////////////////
1486
1515
1487
1516
// MATRIX MORE
1488
1517
1489
1518
1490
1519
@ Override
1491
1520
public void resetMatrix () {
1492
- // canvas.setTransform(new AffineTransform() );
1493
- canvas .setMatrix (new Matrix () );
1521
+ transform . reset ( );
1522
+ canvas .setMatrix (null );
1494
1523
}
1495
1524
1496
1525
@@ -1500,15 +1529,9 @@ public void resetMatrix() {
1500
1529
@ Override
1501
1530
public void applyMatrix (float n00 , float n01 , float n02 ,
1502
1531
float n10 , float n11 , float n12 ) {
1503
- // canvas.transform(new AffineTransform(n00, n10, n01, n11, n02, n12));
1504
- // TODO optimize
1505
- Matrix m = new Matrix ();
1506
- m .setValues (new float [] {
1507
- n00 , n01 , n02 ,
1508
- n10 , n11 , n12 ,
1509
- 0 , 0 , 1
1510
- });
1511
- canvas .concat (m );
1532
+ transform .apply (n00 , n01 , n02 , n10 , n11 , n12 );
1533
+ updateTmpMatrix ();
1534
+ canvas .concat (tmpMatrix );
1512
1535
}
1513
1536
1514
1537
@@ -1541,15 +1564,7 @@ public PMatrix2D getMatrix(PMatrix2D target) {
1541
1564
if (target == null ) {
1542
1565
target = new PMatrix2D ();
1543
1566
}
1544
- // canvas.getTransform().getMatrix(transform);
1545
- // Matrix m = new Matrix();
1546
- // canvas.getMatrix(m);
1547
- Matrix m = getMatrixImp ();
1548
- m .getValues (transform );
1549
- // target.set((float) transform[0], (float) transform[2], (float) transform[4],
1550
- // (float) transform[1], (float) transform[3], (float) transform[5]);
1551
- target .set ((float ) transform [0 ], (float ) transform [1 ], (float ) transform [2 ],
1552
- (float ) transform [3 ], (float ) transform [4 ], (float ) transform [5 ]);
1567
+ target .set (transform );
1553
1568
return target ;
1554
1569
}
1555
1570
@@ -1566,16 +1581,9 @@ public PMatrix3D getMatrix(PMatrix3D target) {
1566
1581
1567
1582
@ Override
1568
1583
public void setMatrix (PMatrix2D source ) {
1569
- // canvas.setTransform(new AffineTransform(source.m00, source.m10,
1570
- // source.m01, source.m11,
1571
- // source.m02, source.m12));
1572
- Matrix matrix = new Matrix ();
1573
- matrix .setValues (new float [] {
1574
- source .m00 , source .m01 , source .m02 ,
1575
- source .m10 , source .m11 , source .m12 ,
1576
- 0 , 0 , 1
1577
- });
1578
- canvas .setMatrix (matrix );
1584
+ transform .set (source );
1585
+ updateTmpMatrix ();
1586
+ canvas .setMatrix (tmpMatrix );
1579
1587
}
1580
1588
1581
1589
@@ -1592,11 +1600,27 @@ public void printMatrix() {
1592
1600
1593
1601
1594
1602
protected Matrix getMatrixImp () {
1595
- return parent .getSurfaceView ().getMatrix ();
1603
+ Matrix m = new Matrix ();
1604
+ updateTmpMatrix ();
1605
+ m .set (tmpMatrix );
1606
+ return m ;
1596
1607
// return canvas.getMatrix();
1597
1608
}
1598
1609
1599
1610
1611
+ protected void updateTmpMatrix () {
1612
+ tmpArray [0 ] = transform .m00 ;
1613
+ tmpArray [1 ] = transform .m01 ;
1614
+ tmpArray [2 ] = transform .m02 ;
1615
+ tmpArray [3 ] = transform .m10 ;
1616
+ tmpArray [4 ] = transform .m11 ;
1617
+ tmpArray [5 ] = transform .m12 ;
1618
+ tmpArray [6 ] = 0 ;
1619
+ tmpArray [7 ] = 0 ;
1620
+ tmpArray [8 ] = 1 ;
1621
+ tmpMatrix .setValues (tmpArray );
1622
+ }
1623
+
1600
1624
1601
1625
//////////////////////////////////////////////////////////////
1602
1626
@@ -2106,10 +2130,12 @@ public void set(int x, int y, PImage src) {
2106
2130
src .setModified (false );
2107
2131
}
2108
2132
// set() happens in screen coordinates, so need to clear the ctm
2109
- canvas .save (Canvas .MATRIX_SAVE_FLAG );
2133
+ // canvas.save(Canvas.MATRIX_SAVE_FLAG);
2134
+ pushMatrix ();
2110
2135
canvas .setMatrix (null ); // set to identity
2111
2136
canvas .drawBitmap (bitmap , x , y , null );
2112
- canvas .restore ();
2137
+ popMatrix ();
2138
+ // canvas.restore();
2113
2139
}
2114
2140
2115
2141
0 commit comments