1 |
9530f6b0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
da56b12f
|
Leszek Koltunski
|
// Copyright 2022 Leszek Koltunski //
|
3 |
9530f6b0
|
Leszek Koltunski
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
|
|
// Magic Cube 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 |
|
|
// Magic Cube 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 Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.bandaged;
|
21 |
|
|
|
22 |
|
|
import android.app.ActivityManager;
|
23 |
|
|
import android.content.Context;
|
24 |
|
|
import android.content.pm.ConfigurationInfo;
|
25 |
|
|
import android.opengl.GLES30;
|
26 |
|
|
import android.opengl.GLSurfaceView;
|
27 |
|
|
import android.util.AttributeSet;
|
28 |
da56b12f
|
Leszek Koltunski
|
import android.view.MotionEvent;
|
29 |
9530f6b0
|
Leszek Koltunski
|
|
30 |
|
|
import com.google.firebase.crashlytics.FirebaseCrashlytics;
|
31 |
|
|
|
32 |
903041cd
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedScreen;
|
33 |
|
|
|
34 |
9530f6b0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
35 |
|
|
|
36 |
|
|
public class BandagedCreatorView extends GLSurfaceView
|
37 |
|
|
{
|
38 |
9694d2d5
|
Leszek Koltunski
|
private final static float DIRECTION_SENSITIVITY= 12.0f;
|
39 |
|
|
public static final int INVALID_POINTER_ID = -1;
|
40 |
75173f81
|
Leszek Koltunski
|
|
41 |
9530f6b0
|
Leszek Koltunski
|
private BandagedCreatorRenderer mRenderer;
|
42 |
ed32e32d
|
Leszek Koltunski
|
private BandagedCreatorTouchControl mTouchControl;
|
43 |
75173f81
|
Leszek Koltunski
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
44 |
|
|
private int mTouchedIndex1, mTouchedIndex2;
|
45 |
9694d2d5
|
Leszek Koltunski
|
private float mX1, mY1, mX2, mY2, mX, mY;
|
46 |
|
|
private int mPointer1, mPointer2;
|
47 |
|
|
private float mRotAngle, mInitDistance;
|
48 |
9530f6b0
|
Leszek Koltunski
|
|
49 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50 |
|
|
// PUBLIC API
|
51 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
|
|
public BandagedCreatorView(Context context, AttributeSet attrs)
|
54 |
|
|
{
|
55 |
|
|
super(context,attrs);
|
56 |
|
|
|
57 |
da56b12f
|
Leszek Koltunski
|
mX = -1;
|
58 |
|
|
mY = -1;
|
59 |
|
|
|
60 |
550db260
|
Leszek Koltunski
|
mTouchedIndex1 = -1;
|
61 |
|
|
mTouchedIndex2 = -1;
|
62 |
|
|
|
63 |
9530f6b0
|
Leszek Koltunski
|
if(!isInEditMode())
|
64 |
|
|
{
|
65 |
|
|
BandagedCreatorActivity act = (BandagedCreatorActivity)context;
|
66 |
|
|
mRenderer = new BandagedCreatorRenderer(this);
|
67 |
903041cd
|
Leszek Koltunski
|
DistortedScreen screen = mRenderer.getScreen();
|
68 |
f6bf4e77
|
Leszek Koltunski
|
mTouchControl = new BandagedCreatorTouchControl( mRenderer.getObjectRatio() , screen.getFOV() );
|
69 |
9530f6b0
|
Leszek Koltunski
|
|
70 |
|
|
final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
71 |
|
|
|
72 |
|
|
try
|
73 |
|
|
{
|
74 |
|
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
75 |
|
|
int esVersion = configurationInfo.reqGlEsVersion>>16;
|
76 |
|
|
setEGLContextClientVersion(esVersion);
|
77 |
|
|
setRenderer(mRenderer);
|
78 |
|
|
}
|
79 |
|
|
catch(Exception ex)
|
80 |
|
|
{
|
81 |
|
|
act.OpenGLError();
|
82 |
|
|
|
83 |
|
|
String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
|
84 |
|
|
String version = GLES30.glGetString(GLES30.GL_VERSION);
|
85 |
|
|
String vendor = GLES30.glGetString(GLES30.GL_VENDOR);
|
86 |
|
|
String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
|
87 |
|
|
|
88 |
|
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
89 |
|
|
crashlytics.setCustomKey("GLSL Version" , shading );
|
90 |
|
|
crashlytics.setCustomKey("GL version" , version );
|
91 |
|
|
crashlytics.setCustomKey("GL Vendor " , vendor );
|
92 |
|
|
crashlytics.setCustomKey("GLSL renderer" , renderer);
|
93 |
|
|
crashlytics.recordException(ex);
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
13a3dfa9
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
|
100 |
|
|
public int getTouched()
|
101 |
|
|
{
|
102 |
|
|
return mTouchedIndex1;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
f6bf4e77
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
|
107 |
|
|
public BandagedCreatorTouchControl getTouchControl()
|
108 |
|
|
{
|
109 |
|
|
return mTouchControl;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
50ec342b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113 |
|
|
|
114 |
|
|
public BandagedCreatorRenderer getRenderer()
|
115 |
|
|
{
|
116 |
|
|
return mRenderer;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
5d5ed376
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120 |
|
|
|
121 |
213c15de
|
Leszek Koltunski
|
public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
|
122 |
5d5ed376
|
Leszek Koltunski
|
{
|
123 |
213c15de
|
Leszek Koltunski
|
mTouchControl.setCubits(cubits,x,y,z);
|
124 |
5d5ed376
|
Leszek Koltunski
|
}
|
125 |
|
|
|
126 |
e0b71e6e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127 |
|
|
|
128 |
|
|
public void resetCubits()
|
129 |
|
|
{
|
130 |
6f3af598
|
Leszek Koltunski
|
if( mTouchedIndex1>=0 ) mRenderer.untouchCubit(mTouchedIndex1);
|
131 |
|
|
if( mTouchedIndex2>=0 ) mRenderer.untouchCubit(mTouchedIndex2);
|
132 |
e0b71e6e
|
Leszek Koltunski
|
|
133 |
|
|
mTouchedIndex1 = -1;
|
134 |
|
|
mTouchedIndex2 = -1;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
da56b12f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138 |
|
|
|
139 |
28cb1607
|
Leszek Koltunski
|
public void setScreenSize(int width, int height)
|
140 |
903041cd
|
Leszek Koltunski
|
{
|
141 |
|
|
mScreenWidth = width;
|
142 |
|
|
mScreenHeight= height;
|
143 |
75173f81
|
Leszek Koltunski
|
mScreenMin = Math.min(width, height);
|
144 |
903041cd
|
Leszek Koltunski
|
}
|
145 |
|
|
|
146 |
9694d2d5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147 |
|
|
|
148 |
|
|
private float getAngle(float x1, float y1, float x2, float y2)
|
149 |
|
|
{
|
150 |
|
|
return (float) Math.atan2(y1-y2, x1-x2);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154 |
|
|
|
155 |
|
|
private void actionDown(MotionEvent event)
|
156 |
|
|
{
|
157 |
|
|
mPointer1 = event.getPointerId(0);
|
158 |
|
|
mX1 = event.getX();
|
159 |
|
|
mY1 = event.getY();
|
160 |
|
|
mPointer2 = INVALID_POINTER_ID;
|
161 |
|
|
|
162 |
|
|
float x1 = (mX1 -mScreenWidth*0.5f)/mScreenMin;
|
163 |
|
|
float y1 = (mScreenHeight*0.5f-mY1)/mScreenMin;
|
164 |
|
|
|
165 |
|
|
int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
|
166 |
|
|
|
167 |
|
|
if( index<0 )
|
168 |
|
|
{
|
169 |
|
|
mX = mX1;
|
170 |
|
|
mY = mY1;
|
171 |
|
|
}
|
172 |
|
|
else
|
173 |
|
|
{
|
174 |
|
|
mX = -1;
|
175 |
|
|
mY = -1;
|
176 |
|
|
|
177 |
|
|
if( mTouchedIndex1<0 )
|
178 |
|
|
{
|
179 |
|
|
mTouchedIndex1 = index;
|
180 |
|
|
mRenderer.touchCubit(mTouchedIndex1);
|
181 |
|
|
}
|
182 |
|
|
else
|
183 |
|
|
{
|
184 |
|
|
mTouchedIndex2 = index;
|
185 |
|
|
|
186 |
|
|
if( mTouchedIndex1 != index )
|
187 |
|
|
{
|
188 |
|
|
mRenderer.touchCubit(mTouchedIndex2);
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195 |
|
|
|
196 |
|
|
private void actionMove(MotionEvent event)
|
197 |
|
|
{
|
198 |
|
|
int index1 = event.findPointerIndex(mPointer1);
|
199 |
|
|
|
200 |
|
|
if( index1>=0 )
|
201 |
|
|
{
|
202 |
|
|
mX1 = event.getX(index1);
|
203 |
|
|
mY1 = event.getY(index1);
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
int index2 = event.findPointerIndex(mPointer2);
|
207 |
|
|
|
208 |
|
|
if( index2>=0 )
|
209 |
|
|
{
|
210 |
|
|
mX2 = event.getX(index2);
|
211 |
|
|
mY2 = event.getY(index2);
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
|
215 |
|
|
{
|
216 |
|
|
float angleNow = getAngle(mX1,mY1,mX2,mY2);
|
217 |
|
|
float angleDiff = angleNow-mRotAngle;
|
218 |
|
|
float sinA = (float)Math.sin(angleDiff);
|
219 |
|
|
float cosA = (float)Math.cos(angleDiff);
|
220 |
|
|
mRenderer.setQuatTemp(0,0,sinA,cosA);
|
221 |
|
|
mRenderer.resetQuats();
|
222 |
|
|
mRotAngle = angleNow;
|
223 |
|
|
|
224 |
f6bf4e77
|
Leszek Koltunski
|
float distNow = (float)Math.sqrt( (mX1-mX2)*(mX1-mX2) + (mY1-mY2)*(mY1-mY2) );
|
225 |
9694d2d5
|
Leszek Koltunski
|
float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
|
226 |
|
|
mInitDistance = distNow;
|
227 |
f6bf4e77
|
Leszek Koltunski
|
mRenderer.mulObjectRatio(distQuot);
|
228 |
9694d2d5
|
Leszek Koltunski
|
}
|
229 |
|
|
else
|
230 |
|
|
{
|
231 |
|
|
float x = event.getX();
|
232 |
|
|
float y = event.getY();
|
233 |
|
|
|
234 |
|
|
if( mX>=0 && mY>= 0 )
|
235 |
|
|
{
|
236 |
|
|
float px = mY-y;
|
237 |
|
|
float py = mX-x;
|
238 |
|
|
float pz = 0;
|
239 |
|
|
float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
|
240 |
|
|
|
241 |
|
|
if( plen>0 )
|
242 |
|
|
{
|
243 |
|
|
px /= plen;
|
244 |
|
|
py /= plen;
|
245 |
|
|
pz /= plen;
|
246 |
|
|
|
247 |
|
|
float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
|
248 |
|
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
249 |
|
|
|
250 |
|
|
mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
|
251 |
|
|
}
|
252 |
|
|
}
|
253 |
|
|
if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
|
254 |
|
|
{
|
255 |
|
|
mX = x;
|
256 |
|
|
mY = y;
|
257 |
|
|
mRenderer.resetQuats();
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263 |
|
|
|
264 |
|
|
private void actionUp()
|
265 |
|
|
{
|
266 |
|
|
mPointer1 = INVALID_POINTER_ID;
|
267 |
|
|
mPointer2 = INVALID_POINTER_ID;
|
268 |
|
|
|
269 |
|
|
if( mTouchedIndex2>=0 )
|
270 |
|
|
{
|
271 |
|
|
mRenderer.untouchCubit(mTouchedIndex1);
|
272 |
3b8f5220
|
Leszek Koltunski
|
|
273 |
|
|
if( mTouchedIndex2!=mTouchedIndex1 )
|
274 |
|
|
{
|
275 |
|
|
mRenderer.untouchCubit(mTouchedIndex2);
|
276 |
|
|
mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
|
277 |
|
|
}
|
278 |
|
|
|
279 |
9694d2d5
|
Leszek Koltunski
|
mTouchedIndex1 = -1;
|
280 |
|
|
mTouchedIndex2 = -1;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
mX = -1;
|
284 |
|
|
mY = -1;
|
285 |
|
|
|
286 |
|
|
mRenderer.resetQuats();
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290 |
|
|
|
291 |
|
|
private void actionPointerDown(MotionEvent event)
|
292 |
|
|
{
|
293 |
|
|
int index = event.getActionIndex();
|
294 |
|
|
|
295 |
|
|
if( mPointer1==INVALID_POINTER_ID )
|
296 |
|
|
{
|
297 |
|
|
mPointer1 = event.getPointerId(index);
|
298 |
|
|
mX1 = event.getX(index);
|
299 |
|
|
mY1 = event.getY(index);
|
300 |
|
|
}
|
301 |
|
|
else if( mPointer2==INVALID_POINTER_ID )
|
302 |
|
|
{
|
303 |
|
|
mPointer2 = event.getPointerId(index);
|
304 |
|
|
mX2 = event.getX(index);
|
305 |
|
|
mY2 = event.getY(index);
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
mRotAngle = getAngle(mX1,mY1,mX2,mY2);
|
309 |
|
|
mInitDistance = -1;
|
310 |
|
|
mRenderer.resetQuats();
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314 |
|
|
|
315 |
|
|
private void actionPointerUp(MotionEvent event)
|
316 |
|
|
{
|
317 |
|
|
int index = event.getActionIndex();
|
318 |
|
|
|
319 |
|
|
if( index==event.findPointerIndex(mPointer1) )
|
320 |
|
|
{
|
321 |
|
|
mPointer1 = INVALID_POINTER_ID;
|
322 |
|
|
mX = mX2;
|
323 |
|
|
mY = mY2;
|
324 |
|
|
}
|
325 |
|
|
else if( index==event.findPointerIndex(mPointer2) )
|
326 |
|
|
{
|
327 |
|
|
mPointer2 = INVALID_POINTER_ID;
|
328 |
|
|
mX = mX1;
|
329 |
|
|
mY = mY1;
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
mRenderer.resetQuats();
|
333 |
|
|
}
|
334 |
|
|
|
335 |
903041cd
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
336 |
|
|
|
337 |
|
|
@Override
|
338 |
|
|
public boolean onTouchEvent(MotionEvent event)
|
339 |
da56b12f
|
Leszek Koltunski
|
{
|
340 |
e0b71e6e
|
Leszek Koltunski
|
if( mRenderer.isBusy() ) return true;
|
341 |
|
|
|
342 |
9694d2d5
|
Leszek Koltunski
|
int action = event.getActionMasked();
|
343 |
da56b12f
|
Leszek Koltunski
|
|
344 |
|
|
switch(action)
|
345 |
|
|
{
|
346 |
9694d2d5
|
Leszek Koltunski
|
case MotionEvent.ACTION_DOWN : actionDown(event) ; break;
|
347 |
|
|
case MotionEvent.ACTION_MOVE : actionMove(event) ; break;
|
348 |
|
|
case MotionEvent.ACTION_UP : actionUp() ; break;
|
349 |
|
|
case MotionEvent.ACTION_POINTER_DOWN: actionPointerDown(event); break;
|
350 |
|
|
case MotionEvent.ACTION_POINTER_UP : actionPointerUp (event); break;
|
351 |
da56b12f
|
Leszek Koltunski
|
}
|
352 |
|
|
|
353 |
|
|
return true;
|
354 |
|
|
}
|
355 |
9530f6b0
|
Leszek Koltunski
|
}
|