Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialSurfaceView.java @ 4c6cbfa2

1 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 2971588c Leszek Koltunski
// Copyright 2020 Leszek Koltunski                                                               //
3 af88bf2e Leszek Koltunski
//                                                                                               //
4 bb62ca3f Leszek Koltunski
// 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 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
7
8 eaf87d1d Leszek Koltunski
package org.distorted.tutorials;
9 af88bf2e Leszek Koltunski
10 ea036986 Leszek Koltunski
import android.annotation.SuppressLint;
11 af88bf2e Leszek Koltunski
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 da57afae Leszek Koltunski
import org.distorted.objectlib.helpers.OperatingSystemInterface;
22 dd1a65c1 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
23 a946af34 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObjectNode;
24 ed0ea1c5 Leszek Koltunski
import org.distorted.os.OSInterface;
25 af88bf2e Leszek Koltunski
26 dd1a65c1 Leszek Koltunski
import static org.distorted.objectlib.main.ObjectControl.MODE_ROTATE;
27 ea036986 Leszek Koltunski
import static org.distorted.objectlib.main.ObjectControl.MODE_DRAG;
28 b9d4aa3b Leszek Koltunski
29 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 1bafcba4 Leszek Koltunski
public class TutorialSurfaceView extends GLSurfaceView
32 af88bf2e Leszek Koltunski
{
33 dd1a65c1 Leszek Koltunski
    private ObjectControl mObjectController;
34 ed0ea1c5 Leszek Koltunski
    private OSInterface mInterface;
35 af88bf2e Leszek Koltunski
    private TutorialRenderer mRenderer;
36 dd1a65c1 Leszek Koltunski
    private int mScreenWidth, mScreenHeight;
37 338babe8 Leszek Koltunski
    private boolean mCreated;
38 ea036986 Leszek Koltunski
    private int mTouchMode;
39 af88bf2e Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42
    void setScreenSize(int width, int height)
43
      {
44
      mScreenWidth = width;
45
      mScreenHeight= height;
46 b1178f5f Leszek Koltunski
      mObjectController.setScreenSizeAndScaling(width,height, Math.min(width,height));
47 99cedf39 Leszek Koltunski
      mObjectController.setObjectMove( (int)(-0.5f*width*TutorialActivity.BAR_RATIO), 0);
48
      mObjectController.setObjectScale(0.80f);
49 338babe8 Leszek Koltunski
50
      if( !mCreated )
51
        {
52
        mCreated = true;
53
        mObjectController.createNode(width,height);
54 a946af34 Leszek Koltunski
        TwistyObjectNode objectNode = mObjectController.getNode();
55 338babe8 Leszek Koltunski
        mRenderer.getScreen().attach(objectNode);
56 ea036986 Leszek Koltunski
57
        TutorialActivity act = (TutorialActivity) getContext();
58
        mTouchMode = act.getIsFree() ? MODE_ROTATE : MODE_DRAG;
59 338babe8 Leszek Koltunski
        }
60 af88bf2e Leszek Koltunski
      }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
    boolean isVertical()
65
      {
66
      return mScreenHeight>mScreenWidth;
67
      }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 2afc6754 Leszek Koltunski
    ObjectControl getObjectControl()
72 af88bf2e Leszek Koltunski
      {
73 2afc6754 Leszek Koltunski
      return mObjectController;
74 af88bf2e Leszek Koltunski
      }
75
76 da57afae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78
    OSInterface getInterface()
79
      {
80
      return mInterface;
81
      }
82
83 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// PUBLIC API
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
    public TutorialSurfaceView(Context context, AttributeSet attrs)
88
      {
89
      super(context,attrs);
90
91 338babe8 Leszek Koltunski
      mCreated = false;
92 ea036986 Leszek Koltunski
      mTouchMode = MODE_ROTATE;
93 338babe8 Leszek Koltunski
94 af88bf2e Leszek Koltunski
      if(!isInEditMode())
95
        {
96
        TutorialActivity act = (TutorialActivity)context;
97 e019c70b Leszek Koltunski
        TutorialObjectLibInterface ref = new TutorialObjectLibInterface(act);
98 88451205 Leszek Koltunski
        mInterface = new OSInterface(act,ref);
99
        mObjectController = new ObjectControl(mInterface);
100 2afc6754 Leszek Koltunski
        mRenderer = new TutorialRenderer(this);
101 af88bf2e Leszek Koltunski
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 dd1a65c1 Leszek Koltunski
          crashlytics.setCustomKey("GL version"    , version );
123 af88bf2e Leszek Koltunski
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
124 dd1a65c1 Leszek Koltunski
          crashlytics.setCustomKey("GLSL renderer" , renderer);
125 af88bf2e Leszek Koltunski
          crashlytics.recordException(ex);
126
          }
127
        }
128
      }
129
130 c65a5efe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132
    @Override
133
    public void onPause()
134
      {
135
      super.onPause();
136
      mObjectController.onPause();
137
      }
138
139 eaf46415 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141 05c20dad Leszek Koltunski
    @Override
142
    public void onResume()
143 dd1a65c1 Leszek Koltunski
      {
144 05c20dad Leszek Koltunski
      super.onResume();
145 c65a5efe Leszek Koltunski
      mObjectController.onResume();
146 dd1a65c1 Leszek Koltunski
      }
147
148 af88bf2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150 ea036986 Leszek Koltunski
    @SuppressLint("ClickableViewAccessibility")
151 af88bf2e Leszek Koltunski
    @Override
152
    public boolean onTouchEvent(MotionEvent event)
153
      {
154 401d9c0f Leszek Koltunski
      mInterface.setMotionEvent(event);
155
      return mObjectController.onTouchEvent(mTouchMode);
156 af88bf2e Leszek Koltunski
      }
157
}