Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialSurfaceView.java @ 1d1f9ccf

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