Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ 550db260

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 da56b12f Leszek Koltunski
    private final static int DIRECTION_SENSITIVITY=  12;
39
    private int mX, mY;
40 550db260 Leszek Koltunski
    private int mTouchedIndex1, mTouchedIndex2;
41 9530f6b0 Leszek Koltunski
    private BandagedCreatorRenderer mRenderer;
42 903041cd Leszek Koltunski
    private BandagedTouchControl mTouchControl;
43
    private int mScreenWidth, mScreenHeight;
44
    private int mYoffset;
45 9530f6b0 Leszek Koltunski
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
// PUBLIC API
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
    public BandagedCreatorView(Context context, AttributeSet attrs)
51
      {
52
      super(context,attrs);
53
54 da56b12f Leszek Koltunski
      mX = -1;
55
      mY = -1;
56
57 550db260 Leszek Koltunski
      mTouchedIndex1 = -1;
58
      mTouchedIndex2 = -1;
59
60 9530f6b0 Leszek Koltunski
      if(!isInEditMode())
61
        {
62
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
63
        mRenderer = new BandagedCreatorRenderer(this);
64 903041cd Leszek Koltunski
        BandagedCubit[] cubits = mRenderer.getCubits();
65
        DistortedScreen screen = mRenderer.getScreen();
66
        mTouchControl = new BandagedTouchControl(cubits, BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
67 9530f6b0 Leszek Koltunski
68
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
69
70
        try
71
          {
72
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
73
          int esVersion = configurationInfo.reqGlEsVersion>>16;
74
          setEGLContextClientVersion(esVersion);
75
          setRenderer(mRenderer);
76
          }
77
        catch(Exception ex)
78
          {
79
          act.OpenGLError();
80
81
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
82
          String version = GLES30.glGetString(GLES30.GL_VERSION);
83
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
84
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
85
86
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
87
          crashlytics.setCustomKey("GLSL Version"  , shading );
88
          crashlytics.setCustomKey("GL version"    , version );
89
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
90
          crashlytics.setCustomKey("GLSL renderer" , renderer);
91
          crashlytics.recordException(ex);
92
          }
93
        }
94
      }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 da56b12f Leszek Koltunski
    private void resetQuats()
99 9530f6b0 Leszek Koltunski
      {
100 da56b12f Leszek Koltunski
      float qx = mRenderer.mQuat1.get0();
101
      float qy = mRenderer.mQuat1.get1();
102
      float qz = mRenderer.mQuat1.get2();
103
      float qw = mRenderer.mQuat1.get3();
104
105
      float rx = mRenderer.mQuat2.get0();
106
      float ry = mRenderer.mQuat2.get1();
107
      float rz = mRenderer.mQuat2.get2();
108
      float rw = mRenderer.mQuat2.get3();
109
110
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
111
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
112
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
113
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
114
115
      mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
116
      mRenderer.mQuat2.set(tx, ty, tz, tw);
117 9530f6b0 Leszek Koltunski
      }
118 da56b12f Leszek Koltunski
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 903041cd Leszek Koltunski
    public void setScreenSize(int width, int height, int yOffset)
122
      {
123
      mScreenWidth = width;
124
      mScreenHeight= height;
125
      mYoffset     = yOffset;
126
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
    @Override
131
    public boolean onTouchEvent(MotionEvent event)
132 da56b12f Leszek Koltunski
      {
133
      int action = event.getAction();
134
      int x = (int)event.getX();
135 903041cd Leszek Koltunski
      int y = (int)event.getY() + mYoffset;
136 da56b12f Leszek Koltunski
137
      switch(action)
138
         {
139 903041cd Leszek Koltunski
         case MotionEvent.ACTION_DOWN: float x1 = (x -  mScreenWidth*0.5f)/mRenderer.mScreenMin;
140
                                       float y1 = (mScreenHeight*0.5f - y)/mRenderer.mScreenMin;
141
142 550db260 Leszek Koltunski
                                       int index = mTouchControl.cubitTouched(x1,y1,mRenderer.mQuat2);
143 903041cd Leszek Koltunski
144 550db260 Leszek Koltunski
                                       if( index<0 )
145 903041cd Leszek Koltunski
                                         {
146
                                         mX = x;
147
                                         mY = y;
148
                                         }
149
                                       else
150
                                         {
151
                                         mX = -1;
152
                                         mY = -1;
153
154 550db260 Leszek Koltunski
                                         if( mTouchedIndex1<0 )
155
                                           {
156
                                           mTouchedIndex1 = index;
157
                                           mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_MARKED);
158
                                           }
159
                                         else if( mTouchedIndex1 != index )
160
                                           {
161
                                           mTouchedIndex2 = index;
162
                                           mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_MARKED);
163
                                           }
164 903041cd Leszek Koltunski
                                         }
165
166 da56b12f Leszek Koltunski
                                       break;
167
168
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
169
                                         {
170
                                         float px = mY-y;
171
                                         float py = mX-x;
172
                                         float pz = 0;
173
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
174
175
                                         if( plen>0 )
176
                                           {
177
                                           px /= plen;
178
                                           py /= plen;
179
                                           pz /= plen;
180
181
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
182
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
183
184
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
185
                                           }
186
                                         }
187
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
188
                                         {
189
                                         mX = x;
190
                                         mY = y;
191
                                         resetQuats();
192
                                         }
193
                                       break;
194
195 550db260 Leszek Koltunski
         case MotionEvent.ACTION_UP  : if( mTouchedIndex2>=0 )
196 903041cd Leszek Koltunski
                                         {
197 550db260 Leszek Koltunski
                                         mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_DEFAULT);
198
                                         mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_DEFAULT);
199
200
                                         mRenderer.tryConnectingCubits(mTouchedIndex1,mTouchedIndex2);
201
202
                                         mTouchedIndex1 = -1;
203
                                         mTouchedIndex2 = -1;
204 903041cd Leszek Koltunski
                                         }
205
206 550db260 Leszek Koltunski
                                       mX = -1;
207
                                       mY = -1;
208
209 da56b12f Leszek Koltunski
        	                             resetQuats();
210
                                       break;
211
         }
212
213
      return true;
214
      }
215 9530f6b0 Leszek Koltunski
}