Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ 903041cd

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
    private int mX, mY;
40
    private int mTouchedIndex;
41
    private BandagedCreatorRenderer mRenderer;
42
    private BandagedTouchControl mTouchControl;
43
    private int mScreenWidth, mScreenHeight;
44
    private int mYoffset;
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
      if(!isInEditMode())
58
        {
59
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
60
        mRenderer = new BandagedCreatorRenderer(this);
61
        BandagedCubit[] cubits = mRenderer.getCubits();
62
        DistortedScreen screen = mRenderer.getScreen();
63
        mTouchControl = new BandagedTouchControl(cubits, BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
64

    
65
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
66

    
67
        try
68
          {
69
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
70
          int esVersion = configurationInfo.reqGlEsVersion>>16;
71
          setEGLContextClientVersion(esVersion);
72
          setRenderer(mRenderer);
73
          }
74
        catch(Exception ex)
75
          {
76
          act.OpenGLError();
77

    
78
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
79
          String version = GLES30.glGetString(GLES30.GL_VERSION);
80
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
81
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
82

    
83
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
84
          crashlytics.setCustomKey("GLSL Version"  , shading );
85
          crashlytics.setCustomKey("GL version"    , version );
86
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
87
          crashlytics.setCustomKey("GLSL renderer" , renderer);
88
          crashlytics.recordException(ex);
89
          }
90
        }
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
    private void resetQuats()
96
      {
97
      float qx = mRenderer.mQuat1.get0();
98
      float qy = mRenderer.mQuat1.get1();
99
      float qz = mRenderer.mQuat1.get2();
100
      float qw = mRenderer.mQuat1.get3();
101

    
102
      float rx = mRenderer.mQuat2.get0();
103
      float ry = mRenderer.mQuat2.get1();
104
      float rz = mRenderer.mQuat2.get2();
105
      float rw = mRenderer.mQuat2.get3();
106

    
107
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
108
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
109
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
110
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
111

    
112
      mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
113
      mRenderer.mQuat2.set(tx, ty, tz, tw);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    public void setScreenSize(int width, int height, int yOffset)
119
      {
120
      mScreenWidth = width;
121
      mScreenHeight= height;
122
      mYoffset     = yOffset;
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    @Override
128
    public boolean onTouchEvent(MotionEvent event)
129
      {
130
      int action = event.getAction();
131
      int x = (int)event.getX();
132
      int y = (int)event.getY() + mYoffset;
133

    
134
      switch(action)
135
         {
136
         case MotionEvent.ACTION_DOWN: float x1 = (x -  mScreenWidth*0.5f)/mRenderer.mScreenMin;
137
                                       float y1 = (mScreenHeight*0.5f - y)/mRenderer.mScreenMin;
138

    
139
                                       mTouchedIndex = mTouchControl.cubitTouched(x1,y1,mRenderer.mQuat2);
140

    
141
                                       if( mTouchedIndex<0 )
142
                                         {
143
                                         mX = x;
144
                                         mY = y;
145
                                         }
146
                                       else
147
                                         {
148
                                         mX = -1;
149
                                         mY = -1;
150

    
151
                                         mTouchControl.markCubit(mTouchedIndex, BandagedCreatorRenderer.COLOR_MARKED);
152
                                         }
153

    
154
                                       break;
155

    
156
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
157
                                         {
158
                                         float px = mY-y;
159
                                         float py = mX-x;
160
                                         float pz = 0;
161
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
162

    
163
                                         if( plen>0 )
164
                                           {
165
                                           px /= plen;
166
                                           py /= plen;
167
                                           pz /= plen;
168

    
169
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
170
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
171

    
172
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
173
                                           }
174
                                         }
175
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
176
                                         {
177
                                         mX = x;
178
                                         mY = y;
179
                                         resetQuats();
180
                                         }
181
                                       break;
182

    
183
         case MotionEvent.ACTION_UP  : mX = -1;
184
                                       mY = -1;
185

    
186
                                       if( mTouchedIndex>=0 )
187
                                         {
188
                                         mTouchControl.markCubit(mTouchedIndex, BandagedCreatorRenderer.COLOR_DEFAULT);
189
                                         mTouchedIndex = -1;
190
                                         }
191

    
192
        	                             resetQuats();
193
                                       break;
194
         }
195

    
196
      return true;
197
      }
198
}
199

    
(4-4/11)