@@ -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 ();
@@ -167,7 +171,7 @@ public void dispose() {
167171
168172
169173 @ Override
170- public PSurface createSurface (AppComponent component , SurfaceHolder holder ) { // ignore
174+ public PSurface createSurface (AppComponent component , SurfaceHolder holder , boolean reset ) { // ignore
171175 return new PSurfaceAndroid2D (this , component , holder );
172176 }
173177
@@ -228,7 +232,7 @@ public void endDraw() {
228232// }
229233
230234 if (primaryGraphics ) {
231- SurfaceHolder holder = parent .getSurfaceHolder ();
235+ SurfaceHolder holder = parent .surface . getSurfaceHolder ();
232236 if (holder != null ) {
233237 Canvas screen = null ;
234238 try {
@@ -238,7 +242,10 @@ public void endDraw() {
238242 }
239243 } finally {
240244 if (screen != null ) {
241- parent .getSurfaceHolder ().unlockCanvasAndPost (screen );
245+ try {
246+ holder .unlockCanvasAndPost (screen );
247+ } catch (IllegalStateException ex ) {
248+ }
242249 }
243250 }
244251 }
@@ -505,6 +512,24 @@ public void endShape(int mode) {
505512 }
506513
507514
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+
508533
509534 //////////////////////////////////////////////////////////////
510535
@@ -1221,10 +1246,16 @@ public PShape loadShape(String filename) {
12211246 public void textFont (PFont which ) {
12221247 super .textFont (which );
12231248 fillPaint .setTypeface ((Typeface ) which .getNative ());
1249+ fillPaint .setTextSize (which .getDefaultSize ());
12241250 }
12251251
12261252
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+ }
12281259
12291260
12301261 //public void textLeading(float leading)
@@ -1256,14 +1287,7 @@ public void textSize(float size) {
12561287 fillPaint .setTextSize (size );
12571288 }
12581289
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 );
12671291 }
12681292
12691293
@@ -1384,45 +1408,45 @@ protected void textLineImpl(char buffer[], int start, int stop,
13841408
13851409 @ Override
13861410 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);
13941418 }
13951419
13961420
13971421 @ Override
13981422 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();
14061432 }
14071433
14081434
1409-
14101435 //////////////////////////////////////////////////////////////
14111436
14121437 // MATRIX TRANSFORMS
14131438
14141439
14151440 @ Override
14161441 public void translate (float tx , float ty ) {
1442+ transform .translate (tx , ty );
14171443 canvas .translate (tx , ty );
14181444 }
14191445
14201446
1421- //public void translate(float tx, float ty, float tz)
1422-
1423-
14241447 @ Override
14251448 public void rotate (float angle ) {
1449+ transform .rotate (angle * RAD_TO_DEG );
14261450 canvas .rotate (angle * RAD_TO_DEG );
14271451 }
14281452
@@ -1453,12 +1477,14 @@ public void rotate(float angle, float vx, float vy, float vz) {
14531477
14541478 @ Override
14551479 public void scale (float s ) {
1480+ transform .scale (s , s );
14561481 canvas .scale (s , s );
14571482 }
14581483
14591484
14601485 @ Override
14611486 public void scale (float sx , float sy ) {
1487+ transform .scale (sx , sy );
14621488 canvas .scale (sx , sy );
14631489 }
14641490
@@ -1471,26 +1497,29 @@ public void scale(float sx, float sy, float sz) {
14711497
14721498 @ Override
14731499 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 );
14751503 }
14761504
14771505
14781506 @ Override
14791507 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 );
14811511 }
14821512
14831513
1484-
14851514 //////////////////////////////////////////////////////////////
14861515
14871516 // MATRIX MORE
14881517
14891518
14901519 @ Override
14911520 public void resetMatrix () {
1492- // canvas.setTransform(new AffineTransform() );
1493- canvas .setMatrix (new Matrix () );
1521+ transform . reset ( );
1522+ canvas .setMatrix (null );
14941523 }
14951524
14961525
@@ -1500,15 +1529,9 @@ public void resetMatrix() {
15001529 @ Override
15011530 public void applyMatrix (float n00 , float n01 , float n02 ,
15021531 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 );
15121535 }
15131536
15141537
@@ -1541,15 +1564,7 @@ public PMatrix2D getMatrix(PMatrix2D target) {
15411564 if (target == null ) {
15421565 target = new PMatrix2D ();
15431566 }
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 );
15531568 return target ;
15541569 }
15551570
@@ -1566,16 +1581,9 @@ public PMatrix3D getMatrix(PMatrix3D target) {
15661581
15671582 @ Override
15681583 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 );
15791587 }
15801588
15811589
@@ -1592,11 +1600,27 @@ public void printMatrix() {
15921600
15931601
15941602 protected Matrix getMatrixImp () {
1595- return parent .getSurfaceView ().getMatrix ();
1603+ Matrix m = new Matrix ();
1604+ updateTmpMatrix ();
1605+ m .set (tmpMatrix );
1606+ return m ;
15961607// return canvas.getMatrix();
15971608 }
15981609
15991610
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+
16001624
16011625 //////////////////////////////////////////////////////////////
16021626
@@ -2106,10 +2130,12 @@ public void set(int x, int y, PImage src) {
21062130 src .setModified (false );
21072131 }
21082132 // 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 ();
21102135 canvas .setMatrix (null ); // set to identity
21112136 canvas .drawBitmap (bitmap , x , y , null );
2112- canvas .restore ();
2137+ popMatrix ();
2138+ // canvas.restore();
21132139 }
21142140
21152141
0 commit comments