1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.examples.aroundtheworld;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.graphics.Canvas;
|
24
|
import android.util.AttributeSet;
|
25
|
import android.view.MotionEvent;
|
26
|
import android.view.SurfaceHolder;
|
27
|
import android.view.SurfaceView;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
class AroundTheWorldSurfaceViewPicker extends SurfaceView implements SurfaceHolder.Callback
|
32
|
{
|
33
|
private static final int FRAME_INTERVAL = 70;
|
34
|
|
35
|
private boolean refreshScreen = true;
|
36
|
private boolean mFinishedBooting=false;
|
37
|
private int scrWidth, scrHeight;
|
38
|
private GraphicsThread mThread;
|
39
|
private int mX, mY;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
private class GraphicsThread extends Thread
|
44
|
{
|
45
|
private SurfaceHolder mSurfaceHolder;
|
46
|
private AroundTheWorldSurfaceViewPicker mPicker;
|
47
|
private boolean mRun = false;
|
48
|
|
49
|
/////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
GraphicsThread(SurfaceHolder surfaceHolder, AroundTheWorldSurfaceViewPicker p)
|
52
|
{
|
53
|
mSurfaceHolder = surfaceHolder;
|
54
|
mPicker = p;
|
55
|
}
|
56
|
|
57
|
/////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
void setRunning(boolean run)
|
60
|
{
|
61
|
mRun = run;
|
62
|
}
|
63
|
|
64
|
/////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
public void run()
|
67
|
{
|
68
|
Canvas c;
|
69
|
long time;
|
70
|
|
71
|
while (mRun)
|
72
|
{
|
73
|
c = null;
|
74
|
time = 0;
|
75
|
|
76
|
if( refreshScreen && mFinishedBooting )
|
77
|
{
|
78
|
refreshScreen=false;
|
79
|
time = System.currentTimeMillis();
|
80
|
|
81
|
try
|
82
|
{
|
83
|
c = mSurfaceHolder.lockCanvas(null);
|
84
|
synchronized (mSurfaceHolder) { mPicker.draw(c); }
|
85
|
}
|
86
|
finally
|
87
|
{
|
88
|
if (c != null) mSurfaceHolder.unlockCanvasAndPost(c);
|
89
|
}
|
90
|
|
91
|
time = System.currentTimeMillis() -time;
|
92
|
}
|
93
|
|
94
|
if( time<FRAME_INTERVAL )
|
95
|
{
|
96
|
try { Thread.sleep(FRAME_INTERVAL-time); }
|
97
|
catch(InterruptedException ex) {}
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
public AroundTheWorldSurfaceViewPicker(Context c, AttributeSet attrs)
|
106
|
{
|
107
|
super(c, attrs);
|
108
|
|
109
|
mX = -100;
|
110
|
mY = -100;
|
111
|
|
112
|
getHolder().addCallback(this);
|
113
|
setFocusable(true);
|
114
|
setFocusableInTouchMode(true);
|
115
|
|
116
|
mFinishedBooting=true;
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
public void surfaceCreated(SurfaceHolder holder)
|
122
|
{
|
123
|
android.util.Log.e( "Picker", "surfaceCreated");
|
124
|
|
125
|
mThread = new GraphicsThread(getHolder(), this);
|
126
|
mThread.setRunning(true);
|
127
|
mThread.start();
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
|
133
|
{
|
134
|
android.util.Log.e( "Picker", "surfaceChanged: width="+w+" height="+h);
|
135
|
|
136
|
scrWidth = w;
|
137
|
scrHeight= h;
|
138
|
refreshScreen = true;
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
public void surfaceDestroyed(SurfaceHolder holder)
|
144
|
{
|
145
|
android.util.Log.e( "Picker", "surfaceDestroyed");
|
146
|
|
147
|
stopThread();
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
private void stopThread()
|
153
|
{
|
154
|
if( mThread!=null )
|
155
|
{
|
156
|
boolean retry = true;
|
157
|
mThread.setRunning(false);
|
158
|
|
159
|
while (retry)
|
160
|
{
|
161
|
try
|
162
|
{
|
163
|
mThread.join();
|
164
|
retry = false;
|
165
|
}
|
166
|
catch (InterruptedException e) { android.util.Log.e( "Picker", "Joining thread interrupted!"); }
|
167
|
}
|
168
|
|
169
|
mThread=null;
|
170
|
}
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public void draw(Canvas c)
|
176
|
{
|
177
|
if( c!=null )
|
178
|
{
|
179
|
|
180
|
}
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
public void setRepaint()
|
186
|
{
|
187
|
refreshScreen=true;
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
public boolean onTouchEvent(MotionEvent event)
|
193
|
{
|
194
|
mX = (int)event.getX();
|
195
|
mY = (int)event.getY();
|
196
|
|
197
|
android.util.Log.e( "Picker", "onTouchEvent: x="+mX+" y="+mY);
|
198
|
|
199
|
setRepaint();
|
200
|
|
201
|
return true;
|
202
|
}
|
203
|
}
|