Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ e5c9982e

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 75173f81 Leszek Koltunski
40 9530f6b0 Leszek Koltunski
    private BandagedCreatorRenderer mRenderer;
41 ed32e32d Leszek Koltunski
    private BandagedCreatorTouchControl mTouchControl;
42 75173f81 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
43
    private int mTouchedIndex1, mTouchedIndex2;
44
    private int mX, mY;
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
        DistortedScreen screen = mRenderer.getScreen();
65 5d5ed376 Leszek Koltunski
        mTouchControl = new BandagedCreatorTouchControl(BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
66 9530f6b0 Leszek Koltunski
67
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
68
69
        try
70
          {
71
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
72
          int esVersion = configurationInfo.reqGlEsVersion>>16;
73
          setEGLContextClientVersion(esVersion);
74
          setRenderer(mRenderer);
75
          }
76
        catch(Exception ex)
77
          {
78
          act.OpenGLError();
79
80
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
81
          String version = GLES30.glGetString(GLES30.GL_VERSION);
82
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
83
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
84
85
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
86
          crashlytics.setCustomKey("GLSL Version"  , shading );
87
          crashlytics.setCustomKey("GL version"    , version );
88
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
89
          crashlytics.setCustomKey("GLSL renderer" , renderer);
90
          crashlytics.recordException(ex);
91
          }
92
        }
93
      }
94
95 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97
    public int getTouched()
98
      {
99
      return mTouchedIndex1;
100
      }
101
102 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
    public BandagedCreatorRenderer getRenderer()
105
      {
106
      return mRenderer;
107
      }
108
109 5d5ed376 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
    public void setCubits(BandagedCubit[] cubits)
112
      {
113
      mTouchControl.setCubits(cubits);
114
      }
115
116 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
    public void resetCubits()
119
      {
120 6f3af598 Leszek Koltunski
      if( mTouchedIndex1>=0 ) mRenderer.untouchCubit(mTouchedIndex1);
121
      if( mTouchedIndex2>=0 ) mRenderer.untouchCubit(mTouchedIndex2);
122 e0b71e6e Leszek Koltunski
123
      mTouchedIndex1 = -1;
124
      mTouchedIndex2 = -1;
125
      }
126
127 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129 28cb1607 Leszek Koltunski
    public void setScreenSize(int width, int height)
130 903041cd Leszek Koltunski
      {
131
      mScreenWidth = width;
132
      mScreenHeight= height;
133 75173f81 Leszek Koltunski
      mScreenMin   = Math.min(width, height);
134 903041cd Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
    @Override
139
    public boolean onTouchEvent(MotionEvent event)
140 da56b12f Leszek Koltunski
      {
141 e0b71e6e Leszek Koltunski
      if( mRenderer.isBusy() ) return true;
142
143 da56b12f Leszek Koltunski
      int action = event.getAction();
144
      int x = (int)event.getX();
145 28cb1607 Leszek Koltunski
      int y = (int)event.getY();
146 da56b12f Leszek Koltunski
147
      switch(action)
148
         {
149 75173f81 Leszek Koltunski
         case MotionEvent.ACTION_DOWN: float x1 = (x -  mScreenWidth*0.5f)/mScreenMin;
150
                                       float y1 = (mScreenHeight*0.5f - y)/mScreenMin;
151 903041cd Leszek Koltunski
152 50ec342b Leszek Koltunski
                                       int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
153 903041cd Leszek Koltunski
154 550db260 Leszek Koltunski
                                       if( index<0 )
155 903041cd Leszek Koltunski
                                         {
156
                                         mX = x;
157
                                         mY = y;
158
                                         }
159
                                       else
160
                                         {
161
                                         mX = -1;
162
                                         mY = -1;
163
164 550db260 Leszek Koltunski
                                         if( mTouchedIndex1<0 )
165
                                           {
166
                                           mTouchedIndex1 = index;
167 6f3af598 Leszek Koltunski
                                           mRenderer.touchCubit(mTouchedIndex1);
168 550db260 Leszek Koltunski
                                           }
169 28cb1607 Leszek Koltunski
                                         else
170 550db260 Leszek Koltunski
                                           {
171
                                           mTouchedIndex2 = index;
172 28cb1607 Leszek Koltunski
173
                                           if( mTouchedIndex1 != index )
174
                                             {
175 6f3af598 Leszek Koltunski
                                             mRenderer.touchCubit(mTouchedIndex2);
176 28cb1607 Leszek Koltunski
                                             }
177 550db260 Leszek Koltunski
                                           }
178 903041cd Leszek Koltunski
                                         }
179
180 da56b12f Leszek Koltunski
                                       break;
181
182
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
183
                                         {
184
                                         float px = mY-y;
185
                                         float py = mX-x;
186
                                         float pz = 0;
187
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
188
189
                                         if( plen>0 )
190
                                           {
191
                                           px /= plen;
192
                                           py /= plen;
193
                                           pz /= plen;
194
195 75173f81 Leszek Koltunski
                                           float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
196 da56b12f Leszek Koltunski
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
197
198 50ec342b Leszek Koltunski
                                           mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
199 da56b12f Leszek Koltunski
                                           }
200
                                         }
201 75173f81 Leszek Koltunski
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
202 da56b12f Leszek Koltunski
                                         {
203
                                         mX = x;
204
                                         mY = y;
205 75173f81 Leszek Koltunski
                                         mRenderer.resetQuats();
206 da56b12f Leszek Koltunski
                                         }
207
                                       break;
208
209 550db260 Leszek Koltunski
         case MotionEvent.ACTION_UP  : if( mTouchedIndex2>=0 )
210 903041cd Leszek Koltunski
                                         {
211 6f3af598 Leszek Koltunski
                                         mRenderer.untouchCubit(mTouchedIndex1);
212
                                         mRenderer.untouchCubit(mTouchedIndex2);
213 1d4592a2 Leszek Koltunski
                                         mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
214 550db260 Leszek Koltunski
                                         mTouchedIndex1 = -1;
215
                                         mTouchedIndex2 = -1;
216 903041cd Leszek Koltunski
                                         }
217
218 550db260 Leszek Koltunski
                                       mX = -1;
219
                                       mY = -1;
220
221 75173f81 Leszek Koltunski
        	                             mRenderer.resetQuats();
222 da56b12f Leszek Koltunski
                                       break;
223
         }
224
225
      return true;
226
      }
227 9530f6b0 Leszek Koltunski
}