Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialSurfaceView.java @ 032657c3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
5
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
6
///////////////////////////////////////////////////////////////////////////////////////////////////
7

    
8
package org.distorted.tutorials;
9

    
10
import android.annotation.SuppressLint;
11
import android.app.ActivityManager;
12
import android.content.Context;
13
import android.content.pm.ConfigurationInfo;
14
import android.opengl.GLES30;
15
import android.opengl.GLSurfaceView;
16
import android.util.AttributeSet;
17
import android.view.MotionEvent;
18

    
19
import com.google.firebase.crashlytics.FirebaseCrashlytics;
20

    
21
import org.distorted.objectlib.main.ObjectControl;
22
import org.distorted.objectlib.main.TwistyObjectNode;
23
import org.distorted.os.OSInterface;
24

    
25
import static org.distorted.objectlib.main.ObjectControl.MODE_ROTATE;
26
import static org.distorted.objectlib.main.ObjectControl.MODE_DRAG;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class TutorialSurfaceView extends GLSurfaceView
31
{
32
    private ObjectControl mObjectController;
33
    private OSInterface mInterface;
34
    private TutorialRenderer mRenderer;
35
    private int mScreenWidth, mScreenHeight;
36
    private boolean mCreated;
37
    private int mTouchMode;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
    void setScreenSize(int width, int height)
42
      {
43
      mScreenWidth = width;
44
      mScreenHeight= height;
45
      mObjectController.setScreenSizeAndScaling(width,height, Math.min(width,height));
46
      mObjectController.setObjectMove( (int)(-0.5f*width*TutorialActivity.BAR_RATIO), 0);
47
      mObjectController.setObjectScale(0.80f);
48

    
49
      if( !mCreated )
50
        {
51
        mCreated = true;
52
        mObjectController.createNode(width,height);
53
        TwistyObjectNode objectNode = mObjectController.getNode();
54
        mRenderer.getScreen().attach(objectNode);
55

    
56
        TutorialActivity act = (TutorialActivity) getContext();
57
        mTouchMode = act.getIsFree() ? MODE_ROTATE : MODE_DRAG;
58
        }
59
      }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
    boolean isVertical()
64
      {
65
      return mScreenHeight>mScreenWidth;
66
      }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    ObjectControl getObjectControl()
71
      {
72
      return mObjectController;
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
    OSInterface getInterface()
78
      {
79
      return mInterface;
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// PUBLIC API
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
    public TutorialSurfaceView(Context context, AttributeSet attrs)
87
      {
88
      super(context,attrs);
89

    
90
      mCreated = false;
91
      mTouchMode = MODE_ROTATE;
92

    
93
      if(!isInEditMode())
94
        {
95
        TutorialActivity act = (TutorialActivity)context;
96
        TutorialObjectLibInterface ref = new TutorialObjectLibInterface(act);
97
        mInterface = new OSInterface(act,ref);
98
        mObjectController = new ObjectControl(mInterface);
99
        mRenderer = new TutorialRenderer(this);
100

    
101
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
102

    
103
        try
104
          {
105
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
106
          int esVersion = configurationInfo.reqGlEsVersion>>16;
107
          setEGLContextClientVersion(esVersion);
108
          setRenderer(mRenderer);
109
          }
110
        catch(Exception ex)
111
          {
112
          act.OpenGLError();
113

    
114
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
115
          String version = GLES30.glGetString(GLES30.GL_VERSION);
116
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
117
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
118

    
119
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
120
          crashlytics.setCustomKey("GLSL Version"  , shading );
121
          crashlytics.setCustomKey("GL version"    , version );
122
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
123
          crashlytics.setCustomKey("GLSL renderer" , renderer);
124
          crashlytics.recordException(ex);
125
          }
126
        }
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    @Override
132
    public void onPause()
133
      {
134
      super.onPause();
135
      mObjectController.onPause();
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    @Override
141
    public void onResume()
142
      {
143
      super.onResume();
144
      mObjectController.onResume();
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
    @SuppressLint("ClickableViewAccessibility")
150
    @Override
151
    public boolean onTouchEvent(MotionEvent event)
152
      {
153
      mInterface.setMotionEvent(event);
154
      return mObjectController.onTouchEvent(mTouchMode);
155
      }
156
}
157

    
(5-5/6)