Project

General

Profile

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

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

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 int DIRECTION_SENSITIVITY=  12;
39

    
40
    private BandagedCreatorRenderer mRenderer;
41
    private BandagedCreatorTouchControl mTouchControl;
42
    private int mScreenWidth, mScreenHeight, mScreenMin;
43
    private int mTouchedIndex1, mTouchedIndex2;
44
    private int mX, mY;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
// PUBLIC API
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
    public BandagedCreatorView(Context context, AttributeSet attrs)
51
      {
52
      super(context,attrs);
53

    
54
      mX = -1;
55
      mY = -1;
56

    
57
      mTouchedIndex1 = -1;
58
      mTouchedIndex2 = -1;
59

    
60
      if(!isInEditMode())
61
        {
62
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
63
        mRenderer = new BandagedCreatorRenderer(this);
64
        BandagedCubit[] cubits = mRenderer.getCubits();
65
        DistortedScreen screen = mRenderer.getScreen();
66
        mTouchControl = new BandagedCreatorTouchControl(cubits, BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
67

    
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
    public int getTouched()
99
      {
100
      return mTouchedIndex1;
101
      }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
    public BandagedCreatorRenderer getRenderer()
106
      {
107
      return mRenderer;
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    public void resetCubits()
113
      {
114
      if( mTouchedIndex1>=0 ) mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_DEFAULT);
115
      if( mTouchedIndex2>=0 ) mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_DEFAULT);
116

    
117
      mTouchedIndex1 = -1;
118
      mTouchedIndex2 = -1;
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    public void setScreenSize(int width, int height)
124
      {
125
      mScreenWidth = width;
126
      mScreenHeight= height;
127
      mScreenMin   = Math.min(width, height);
128
      }
129

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

    
132
    @Override
133
    public boolean onTouchEvent(MotionEvent event)
134
      {
135
      if( mRenderer.isBusy() ) return true;
136

    
137
      int action = event.getAction();
138
      int x = (int)event.getX();
139
      int y = (int)event.getY();
140

    
141
      switch(action)
142
         {
143
         case MotionEvent.ACTION_DOWN: float x1 = (x -  mScreenWidth*0.5f)/mScreenMin;
144
                                       float y1 = (mScreenHeight*0.5f - y)/mScreenMin;
145

    
146
                                       int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
147

    
148
                                       if( index<0 )
149
                                         {
150
                                         mX = x;
151
                                         mY = y;
152
                                         }
153
                                       else
154
                                         {
155
                                         mX = -1;
156
                                         mY = -1;
157

    
158
                                         if( mTouchedIndex1<0 )
159
                                           {
160
                                           mTouchedIndex1 = index;
161
                                           mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_MARKED);
162
                                           }
163
                                         else
164
                                           {
165
                                           mTouchedIndex2 = index;
166

    
167
                                           if( mTouchedIndex1 != index )
168
                                             {
169
                                             mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_MARKED);
170
                                             }
171
                                           }
172
                                         }
173

    
174
                                       break;
175

    
176
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
177
                                         {
178
                                         float px = mY-y;
179
                                         float py = mX-x;
180
                                         float pz = 0;
181
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
182

    
183
                                         if( plen>0 )
184
                                           {
185
                                           px /= plen;
186
                                           py /= plen;
187
                                           pz /= plen;
188

    
189
                                           float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
190
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
191

    
192
                                           mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
193
                                           }
194
                                         }
195
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
196
                                         {
197
                                         mX = x;
198
                                         mY = y;
199
                                         mRenderer.resetQuats();
200
                                         }
201
                                       break;
202

    
203
         case MotionEvent.ACTION_UP  : if( mTouchedIndex2>=0 )
204
                                         {
205
                                         mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_DEFAULT);
206
                                         mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_DEFAULT);
207

    
208
                                         mRenderer.tryConnectingCubits(mTouchedIndex1,mTouchedIndex2);
209

    
210
                                         mTouchedIndex1 = -1;
211
                                         mTouchedIndex2 = -1;
212
                                         }
213

    
214
                                       mX = -1;
215
                                       mY = -1;
216

    
217
        	                             mRenderer.resetQuats();
218
                                       break;
219
         }
220

    
221
      return true;
222
      }
223
}
224

    
(6-6/13)