Skip to content

Commit b48a88f

Browse files
Added source processing inside core-androidx
1 parent 8de9a4c commit b48a88f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+95518
-0
lines changed

core-androidx/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 2386 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package processing.a2d;
2+
3+
import android.graphics.Shader;
4+
import processing.core.PGraphics;
5+
import processing.core.PShapeSVG;
6+
import processing.data.XML;
7+
8+
public class PShapeAndroid2D extends PShapeSVG {
9+
protected Shader strokeGradientPaint;
10+
protected Shader fillGradientPaint;
11+
12+
13+
public PShapeAndroid2D(XML svg) {
14+
super(svg);
15+
}
16+
17+
18+
public PShapeAndroid2D(PShapeSVG parent, XML properties, boolean parseKids) {
19+
super(parent, properties, parseKids);
20+
}
21+
22+
23+
@Override
24+
protected void setParent(PShapeSVG parent) {
25+
super.setParent(parent);
26+
27+
if (parent instanceof PShapeAndroid2D) {
28+
PShapeAndroid2D pj = (PShapeAndroid2D) parent;
29+
fillGradientPaint = pj.fillGradientPaint;
30+
strokeGradientPaint = pj.strokeGradientPaint;
31+
32+
} else { // parent is null or not Android2D
33+
fillGradientPaint = null;
34+
strokeGradientPaint = null;
35+
}
36+
}
37+
38+
39+
/** Factory method for subclasses. */
40+
@Override
41+
protected PShapeSVG createShape(PShapeSVG parent, XML properties, boolean parseKids) {
42+
return new PShapeAndroid2D(parent, properties, parseKids);
43+
}
44+
45+
46+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47+
48+
49+
protected Shader calcGradientPaint(Gradient gradient) {
50+
// TODO just do this with the other parsing
51+
int[] colors = new int[gradient.count];
52+
int opacityMask = ((int) (opacity * 255)) << 24;
53+
for (int i = 0; i < gradient.count; i++) {
54+
colors[i] = opacityMask | (gradient.color[i] & 0xFFFFFF);
55+
}
56+
57+
if (gradient instanceof LinearGradient) {
58+
LinearGradient grad = (LinearGradient) gradient;
59+
// return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
60+
// grad.offset, grad.color, grad.count,
61+
// opacity);
62+
return new android.graphics.LinearGradient(grad.x1, grad.y1,
63+
grad.x2, grad.y2,
64+
colors, grad.offset,
65+
Shader.TileMode.CLAMP );
66+
67+
} else if (gradient instanceof RadialGradient) {
68+
RadialGradient grad = (RadialGradient) gradient;
69+
// return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
70+
// grad.offset, grad.color, grad.count,
71+
// opacity);
72+
return new android.graphics.RadialGradient(grad.cx, grad.cy, grad.r,
73+
colors, grad.offset,
74+
Shader.TileMode.CLAMP);
75+
}
76+
return null;
77+
}
78+
79+
80+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81+
82+
83+
@Override
84+
protected void styles(PGraphics g) {
85+
super.styles(g);
86+
87+
if (g instanceof PGraphicsAndroid2D) {
88+
PGraphicsAndroid2D gg = (PGraphicsAndroid2D) g;
89+
90+
if (strokeGradient != null) {
91+
// gg.strokeGradient = true;
92+
if (strokeGradientPaint == null) {
93+
strokeGradientPaint = calcGradientPaint(strokeGradient);
94+
}
95+
gg.strokePaint.setShader(strokeGradientPaint);
96+
}
97+
if (fillGradient != null) {
98+
// gg.fillGradient = true;
99+
if (fillGradientPaint == null) {
100+
fillGradientPaint = calcGradientPaint(fillGradient);
101+
}
102+
gg.fillPaint.setShader(fillGradientPaint);
103+
} else {
104+
// need to shut off, in case parent object has a gradient applied
105+
//gg.fillGradient = false;
106+
}
107+
}
108+
}
109+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2016 The Processing Foundation
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License version 2.1 as published by the Free Software Foundation.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
package processing.a2d;
24+
25+
import android.content.Context;
26+
27+
import android.graphics.Color;
28+
import android.graphics.PixelFormat;
29+
import android.service.wallpaper.WallpaperService;
30+
import android.support.wearable.watchface.CanvasWatchFaceService;
31+
import android.view.MotionEvent;
32+
import android.view.SurfaceHolder;
33+
import android.view.SurfaceView;
34+
import processing.android.AppComponent;
35+
import processing.android.PFragment;
36+
import processing.core.PApplet;
37+
import processing.core.PGraphics;
38+
import processing.core.PSurfaceNone;
39+
40+
public class PSurfaceAndroid2D extends PSurfaceNone {
41+
42+
public PSurfaceAndroid2D() { }
43+
44+
public PSurfaceAndroid2D(PGraphics graphics, AppComponent component, SurfaceHolder holder) {
45+
this.sketch = graphics.parent;
46+
this.graphics = graphics;
47+
this.component = component;
48+
if (component.getKind() == AppComponent.FRAGMENT) {
49+
PFragment frag = (PFragment)component;
50+
activity = frag.getActivity();
51+
surfaceView = new SurfaceViewAndroid2D(activity, null);
52+
} else if (component.getKind() == AppComponent.WALLPAPER) {
53+
wallpaper = (WallpaperService)component;
54+
surfaceView = new SurfaceViewAndroid2D(wallpaper, holder);
55+
} else if (component.getKind() == AppComponent.WATCHFACE) {
56+
watchface = (CanvasWatchFaceService)component;
57+
surfaceView = null;
58+
// Set as ready here, as watch faces don't have a surface view with a
59+
// surfaceCreate() event to do it.
60+
surfaceReady = true;
61+
}
62+
}
63+
64+
///////////////////////////////////////////////////////////
65+
66+
// SurfaceView
67+
68+
public class SurfaceViewAndroid2D extends SurfaceView implements SurfaceHolder.Callback {
69+
SurfaceHolder holder;
70+
71+
public SurfaceViewAndroid2D(Context context, SurfaceHolder holder) {
72+
super(context);
73+
this.holder = holder;
74+
75+
// println("surface holder");
76+
// Install a SurfaceHolder.Callback so we get notified when the
77+
// underlying surface is created and destroyed
78+
SurfaceHolder h = getHolder();
79+
h.addCallback(this);
80+
// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU); // no longer needed.
81+
82+
// println("setting focusable, requesting focus");
83+
setFocusable(true);
84+
setFocusableInTouchMode(true);
85+
requestFocus();
86+
// println("done making surface view");
87+
88+
surfaceReady = false; // Will be ready when the surfaceCreated() event is called
89+
90+
// Solves screen flickering:
91+
// https://github.com/processing/processing-android/issues/570
92+
setBackgroundColor(Color.argb(0, 0, 0, 0));
93+
getHolder().setFormat(PixelFormat.TRANSPARENT);
94+
}
95+
96+
@Override
97+
public SurfaceHolder getHolder() {
98+
if (holder == null) {
99+
return super.getHolder();
100+
} else {
101+
return holder;
102+
}
103+
}
104+
105+
// part of SurfaceHolder.Callback
106+
public void surfaceCreated(SurfaceHolder holder) {
107+
surfaceReady = true;
108+
if (requestedThreadStart) {
109+
// Only start the thread once the surface has been created, otherwise it will not be able to draw
110+
startThread();
111+
}
112+
if (PApplet.DEBUG) {
113+
System.out.println("surfaceCreated()");
114+
}
115+
}
116+
117+
118+
// part of SurfaceHolder.Callback
119+
public void surfaceDestroyed(SurfaceHolder holder) {
120+
if (PApplet.DEBUG) {
121+
System.out.println("surfaceDestroyed()");
122+
}
123+
}
124+
125+
126+
// part of SurfaceHolder.Callback
127+
public void surfaceChanged(SurfaceHolder holder, int format, int iwidth, int iheight) {
128+
if (PApplet.DEBUG) {
129+
System.out.println("SketchSurfaceView.surfaceChanged() " + iwidth + " " + iheight);
130+
}
131+
132+
sketch.surfaceChanged();
133+
sketch.setSize(iwidth, iheight);
134+
}
135+
136+
@Override
137+
public void onWindowFocusChanged(boolean hasFocus) {
138+
super.onWindowFocusChanged(hasFocus);
139+
sketch.surfaceWindowFocusChanged(hasFocus);
140+
}
141+
142+
@Override
143+
public boolean onTouchEvent(MotionEvent event) {
144+
return sketch.surfaceTouchEvent(event);
145+
}
146+
147+
@Override
148+
public boolean onKeyDown(int code, android.view.KeyEvent event) {
149+
sketch.surfaceKeyDown(code, event);
150+
return super.onKeyDown(code, event);
151+
}
152+
153+
@Override
154+
public boolean onKeyUp(int code, android.view.KeyEvent event) {
155+
sketch.surfaceKeyUp(code, event);
156+
return super.onKeyUp(code, event);
157+
}
158+
}
159+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2016-17 The Processing Foundation
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License version 2.1 as published by the Free Software Foundation.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
package processing.android;
24+
25+
import android.content.Intent;
26+
import android.os.Bundle;
27+
import android.view.ContextMenu;
28+
import android.view.ContextMenu.ContextMenuInfo;
29+
import android.view.Menu;
30+
import android.view.MenuInflater;
31+
import android.view.MenuItem;
32+
import android.view.View;
33+
import android.view.Window;
34+
import android.app.FragmentManager;
35+
36+
37+
// Methods that should be implemented in PApplet to maintain backward
38+
// compatibility with (some) functionality available from Activity/Fragment
39+
public interface ActivityAPI {
40+
// Lifecycle events
41+
public void onCreate(Bundle savedInstanceState);
42+
public void onDestroy();
43+
public void onStart();
44+
public void onStop();
45+
public void onPause();
46+
public void onResume();
47+
48+
// Activity and intent events
49+
public void onActivityResult(int requestCode, int resultCode, Intent data);
50+
public void onNewIntent(Intent intent);
51+
52+
// Menu API
53+
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater);
54+
public boolean onOptionsItemSelected(MenuItem item);
55+
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
56+
public boolean onContextItemSelected(MenuItem item);
57+
public void setHasOptionsMenu(boolean hasMenu);
58+
59+
// IO events
60+
public void onBackPressed();
61+
62+
// Activity management
63+
public FragmentManager getFragmentManager();
64+
public Window getWindow();
65+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2016-17 The Processing Foundation
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License version 2.1 as published by the Free Software Foundation.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
package processing.android;
24+
25+
import android.content.Intent;
26+
import processing.core.PApplet;
27+
import processing.core.PConstants;
28+
29+
abstract public interface AppComponent extends PConstants {
30+
static public final int FRAGMENT = 0;
31+
static public final int WALLPAPER = 1;
32+
static public final int WATCHFACE = 2;
33+
34+
public void initDimensions();
35+
public int getDisplayWidth();
36+
public int getDisplayHeight();
37+
public float getDisplayDensity();
38+
public int getKind();
39+
public void setSketch(PApplet sketch);
40+
public PApplet getSketch();
41+
42+
public boolean isService();
43+
public ServiceEngine getEngine();
44+
45+
public void startActivity(Intent intent);
46+
47+
public void requestDraw();
48+
public boolean canDraw();
49+
50+
public void dispose();
51+
}

0 commit comments

Comments
 (0)