Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ 9694d2d5

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
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
import android.view.MotionEvent;
29

    
30
import com.google.firebase.crashlytics.FirebaseCrashlytics;
31

    
32
import org.distorted.library.main.DistortedScreen;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class BandagedCreatorView extends GLSurfaceView
37
{
38
    private final static float DIRECTION_SENSITIVITY= 12.0f;
39
    public static final int INVALID_POINTER_ID = -1;
40

    
41
    private BandagedCreatorRenderer mRenderer;
42
    private BandagedCreatorTouchControl mTouchControl;
43
    private int mScreenWidth, mScreenHeight, mScreenMin;
44
    private int mTouchedIndex1, mTouchedIndex2;
45
    private float mX1, mY1, mX2, mY2, mX, mY;
46
    private int mPointer1, mPointer2;
47
    private float mRotAngle, mInitDistance;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// PUBLIC API
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    public BandagedCreatorView(Context context, AttributeSet attrs)
54
      {
55
      super(context,attrs);
56

    
57
      mX = -1;
58
      mY = -1;
59

    
60
      mTouchedIndex1 = -1;
61
      mTouchedIndex2 = -1;
62

    
63
      if(!isInEditMode())
64
        {
65
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
66
        mRenderer = new BandagedCreatorRenderer(this);
67
        DistortedScreen screen = mRenderer.getScreen();
68
        mTouchControl = new BandagedCreatorTouchControl(BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
69

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    public int getTouched()
101
      {
102
      return mTouchedIndex1;
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
    public BandagedCreatorRenderer getRenderer()
108
      {
109
      return mRenderer;
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
115
      {
116
      mTouchControl.setCubits(cubits,x,y,z);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    public void resetCubits()
122
      {
123
      if( mTouchedIndex1>=0 ) mRenderer.untouchCubit(mTouchedIndex1);
124
      if( mTouchedIndex2>=0 ) mRenderer.untouchCubit(mTouchedIndex2);
125

    
126
      mTouchedIndex1 = -1;
127
      mTouchedIndex2 = -1;
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
    public void setScreenSize(int width, int height)
133
      {
134
      mScreenWidth = width;
135
      mScreenHeight= height;
136
      mScreenMin   = Math.min(width, height);
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
    private float getAngle(float x1, float y1, float x2, float y2)
142
      {
143
      return (float) Math.atan2(y1-y2, x1-x2);
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    private void actionDown(MotionEvent event)
149
      {
150
      mPointer1 = event.getPointerId(0);
151
      mX1 = event.getX();
152
      mY1 = event.getY();
153
      mPointer2 = INVALID_POINTER_ID;
154

    
155
      float x1 = (mX1 -mScreenWidth*0.5f)/mScreenMin;
156
      float y1 = (mScreenHeight*0.5f-mY1)/mScreenMin;
157

    
158
      int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
159

    
160
      if( index<0 )
161
        {
162
        mX = mX1;
163
        mY = mY1;
164
        }
165
      else
166
        {
167
        mX = -1;
168
        mY = -1;
169

    
170
        if( mTouchedIndex1<0 )
171
          {
172
          mTouchedIndex1 = index;
173
          mRenderer.touchCubit(mTouchedIndex1);
174
          }
175
        else
176
          {
177
          mTouchedIndex2 = index;
178

    
179
          if( mTouchedIndex1 != index )
180
            {
181
            mRenderer.touchCubit(mTouchedIndex2);
182
            }
183
          }
184
        }
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
    private void actionMove(MotionEvent event)
190
      {
191
      int index1 = event.findPointerIndex(mPointer1);
192

    
193
      if( index1>=0 )
194
        {
195
        mX1 = event.getX(index1);
196
        mY1 = event.getY(index1);
197
        }
198

    
199
      int index2 = event.findPointerIndex(mPointer2);
200

    
201
      if( index2>=0 )
202
        {
203
        mX2 = event.getX(index2);
204
        mY2 = event.getY(index2);
205
        }
206

    
207
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
208
        {
209
        float angleNow = getAngle(mX1,mY1,mX2,mY2);
210
        float angleDiff = angleNow-mRotAngle;
211
        float sinA = (float)Math.sin(angleDiff);
212
        float cosA = (float)Math.cos(angleDiff);
213
        mRenderer.setQuatTemp(0,0,sinA,cosA);
214
        mRenderer.resetQuats();
215
        mRotAngle = angleNow;
216

    
217
/*
218
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
219
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
220
        mInitDistance = distNow;
221
        TwistyObject object = mPreRender.getObject();
222
        if( object!=null ) object.setObjectRatio(distQuot, mObjectNode.getMinSize() );
223
 */
224
        }
225
      else
226
        {
227
        float x = event.getX();
228
        float y = event.getY();
229

    
230
        if( mX>=0 && mY>= 0 )
231
          {
232
          float px = mY-y;
233
          float py = mX-x;
234
          float pz = 0;
235
          float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
236

    
237
          if( plen>0 )
238
            {
239
            px /= plen;
240
            py /= plen;
241
            pz /= plen;
242

    
243
            float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
244
            float sinA = (float)Math.sqrt(1-cosA*cosA);
245

    
246
            mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
247
            }
248
          }
249
        if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
250
          {
251
          mX = x;
252
          mY = y;
253
          mRenderer.resetQuats();
254
          }
255
        }
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
    private void actionUp()
261
      {
262
      mPointer1 = INVALID_POINTER_ID;
263
      mPointer2 = INVALID_POINTER_ID;
264

    
265
      if( mTouchedIndex2>=0 )
266
        {
267
        mRenderer.untouchCubit(mTouchedIndex1);
268
        mRenderer.untouchCubit(mTouchedIndex2);
269
        mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
270
        mTouchedIndex1 = -1;
271
        mTouchedIndex2 = -1;
272
        }
273

    
274
      mX = -1;
275
      mY = -1;
276

    
277
      mRenderer.resetQuats();
278
      }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    private void actionPointerDown(MotionEvent event)
283
      {
284
      int index = event.getActionIndex();
285

    
286
      if( mPointer1==INVALID_POINTER_ID )
287
        {
288
        mPointer1 = event.getPointerId(index);
289
        mX1 = event.getX(index);
290
        mY1 = event.getY(index);
291
        }
292
      else if( mPointer2==INVALID_POINTER_ID )
293
        {
294
        mPointer2 = event.getPointerId(index);
295
        mX2 = event.getX(index);
296
        mY2 = event.getY(index);
297
        }
298

    
299
      mRotAngle = getAngle(mX1,mY1,mX2,mY2);
300
      mInitDistance = -1;
301
      mRenderer.resetQuats();
302
      }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
    private void actionPointerUp(MotionEvent event)
307
      {
308
      int index = event.getActionIndex();
309

    
310
      if( index==event.findPointerIndex(mPointer1) )
311
        {
312
        mPointer1 = INVALID_POINTER_ID;
313
        mX = mX2;
314
        mY = mY2;
315
        }
316
      else if( index==event.findPointerIndex(mPointer2) )
317
        {
318
        mPointer2 = INVALID_POINTER_ID;
319
        mX = mX1;
320
        mY = mY1;
321
        }
322

    
323
      mRenderer.resetQuats();
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
    @Override
329
    public boolean onTouchEvent(MotionEvent event)
330
      {
331
      if( mRenderer.isBusy() ) return true;
332

    
333
      int action = event.getActionMasked();
334

    
335
      switch(action)
336
         {
337
         case MotionEvent.ACTION_DOWN        : actionDown(event)       ; break;
338
         case MotionEvent.ACTION_MOVE        : actionMove(event)       ; break;
339
         case MotionEvent.ACTION_UP          : actionUp()              ; break;
340
         case MotionEvent.ACTION_POINTER_DOWN: actionPointerDown(event); break;
341
         case MotionEvent.ACTION_POINTER_UP  : actionPointerUp  (event); break;
342
         }
343

    
344
      return true;
345
      }
346
}
347

    
(6-6/13)