Project

General

Profile

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

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

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
// PUBLIC API
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
    public TutorialSurfaceView(Context context, AttributeSet attrs)
80
      {
81
      super(context,attrs);
82

    
83
      mCreated = false;
84
      mTouchMode = MODE_ROTATE;
85

    
86
      if(!isInEditMode())
87
        {
88
        TutorialActivity act = (TutorialActivity)context;
89
        TutorialObjectLibInterface ref = new TutorialObjectLibInterface(act);
90
        mInterface = new OSInterface(act);
91
        mObjectController = new ObjectControl(ref,mInterface);
92
        mRenderer = new TutorialRenderer(this);
93

    
94
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
95

    
96
        try
97
          {
98
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
99
          int esVersion = configurationInfo.reqGlEsVersion>>16;
100
          setEGLContextClientVersion(esVersion);
101
          setRenderer(mRenderer);
102
          }
103
        catch(Exception ex)
104
          {
105
          act.OpenGLError();
106

    
107
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
108
          String version = GLES30.glGetString(GLES30.GL_VERSION);
109
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
110
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
111

    
112
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
113
          crashlytics.setCustomKey("GLSL Version"  , shading );
114
          crashlytics.setCustomKey("GL version"    , version );
115
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
116
          crashlytics.setCustomKey("GLSL renderer" , renderer);
117
          crashlytics.recordException(ex);
118
          }
119
        }
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
    @Override
125
    public void onPause()
126
      {
127
      super.onPause();
128
      mObjectController.onPause();
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    @SuppressLint("ClickableViewAccessibility")
143
    @Override
144
    public boolean onTouchEvent(MotionEvent event)
145
      {
146
      return mObjectController.onTouchEvent(event,mTouchMode);
147
      }
148
}
149

    
(5-5/6)