Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialSurfaceView.java @ 88451205

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.helpers.OperatingSystemInterface;
22
import org.distorted.objectlib.main.ObjectControl;
23
import org.distorted.objectlib.main.TwistyObjectNode;
24
import org.distorted.os.OSInterface;
25

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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

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

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// PUBLIC API
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

    
91
      mCreated = false;
92
      mTouchMode = MODE_ROTATE;
93

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

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

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

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

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

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
(5-5/6)