Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 6b1998e0

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22 f57d0ad9 Leszek Koltunski
import android.annotation.SuppressLint;
23 0c52af30 Leszek Koltunski
import android.app.ActivityManager;
24
import android.content.Context;
25
import android.content.pm.ConfigurationInfo;
26 4489e68e Leszek Koltunski
import android.opengl.GLES30;
27 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
28
import android.util.AttributeSet;
29
import android.view.MotionEvent;
30
31 b96a20a4 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
32
33 dd1a65c1 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
34 a946af34 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObjectNode;
35 3f7a4363 Leszek Koltunski
36 fcd5b990 Leszek Koltunski
import org.distorted.screens.ScreenList;
37 0c52af30 Leszek Koltunski
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 1bafcba4 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
41 0c52af30 Leszek Koltunski
{
42 dd1a65c1 Leszek Koltunski
    private ObjectControl mObjectController;
43 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
44 dd1a65c1 Leszek Koltunski
    private int mScreenWidth, mScreenHeight;
45 338babe8 Leszek Koltunski
    private boolean mCreated;
46 ee5c2ae1 Leszek Koltunski
47 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
    void setScreenSize(int width, int height)
50
      {
51
      mScreenWidth = width;
52
      mScreenHeight= height;
53 dd1a65c1 Leszek Koltunski
      mObjectController.setScreenSize(width,height);
54 2da68298 Leszek Koltunski
55 338babe8 Leszek Koltunski
      if( !mCreated )
56
        {
57
        mCreated = true;
58
        mObjectController.createNode(width,height);
59 a946af34 Leszek Koltunski
        TwistyObjectNode objectNode = mObjectController.getNode();
60 338babe8 Leszek Koltunski
        mRenderer.getScreen().attach(objectNode);
61
        }
62
63 2da68298 Leszek Koltunski
      try
64
        {
65
        RubikActivity act = (RubikActivity)getContext();
66
        act.setScreenSize(width,height);
67
        }
68
      catch( Exception ex )
69
        {
70
        android.util.Log.e("D", "Failed to set screen size!");
71
        }
72 0fd3ac1f Leszek Koltunski
      }
73
74 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
    boolean isVertical()
77
      {
78
      return mScreenHeight>mScreenWidth;
79
      }
80
81 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83
    RubikRenderer getRenderer()
84
      {
85
      return mRenderer;
86
      }
87
88 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 2afc6754 Leszek Koltunski
    ObjectControl getObjectControl()
91 8becce57 Leszek Koltunski
      {
92 2afc6754 Leszek Koltunski
      return mObjectController;
93 e03e0352 Leszek Koltunski
      }
94
95 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// PUBLIC API
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99
    public RubikSurfaceView(Context context, AttributeSet attrs)
100
      {
101
      super(context,attrs);
102
103 338babe8 Leszek Koltunski
      mCreated = false;
104
105 47ba5ddc Leszek Koltunski
      if(!isInEditMode())
106
        {
107 cd83d0aa Leszek Koltunski
        RubikActivity act = (RubikActivity)context;
108 e019c70b Leszek Koltunski
        RubikObjectLibInterface ref = new RubikObjectLibInterface(act);
109
        mObjectController = new ObjectControl(act,ref);
110 2afc6754 Leszek Koltunski
        mRenderer = new RubikRenderer(this);
111 cd83d0aa Leszek Koltunski
112
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
113
114 4489e68e Leszek Koltunski
        try
115 cd83d0aa Leszek Koltunski
          {
116
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
117 e7e0a94d Leszek Koltunski
          int esVersion = configurationInfo.reqGlEsVersion>>16;
118
          setEGLContextClientVersion(esVersion);
119 cd83d0aa Leszek Koltunski
          setRenderer(mRenderer);
120
          }
121 4489e68e Leszek Koltunski
        catch(Exception ex)
122
          {
123 e7e0a94d Leszek Koltunski
          act.OpenGLError();
124 4489e68e Leszek Koltunski
125
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
126
          String version = GLES30.glGetString(GLES30.GL_VERSION);
127
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
128
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
129
130
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
131
          crashlytics.setCustomKey("GLSL Version"  , shading );
132 0812242b Leszek Koltunski
          crashlytics.setCustomKey("GL version"    , version );
133 4489e68e Leszek Koltunski
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
134 0812242b Leszek Koltunski
          crashlytics.setCustomKey("GLSL renderer" , renderer);
135 4489e68e Leszek Koltunski
          crashlytics.recordException(ex);
136
          }
137 47ba5ddc Leszek Koltunski
        }
138
      }
139
140 eaf46415 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 c65a5efe Leszek Koltunski
    @Override
143
    public void onPause()
144 eaf46415 Leszek Koltunski
      {
145 c65a5efe Leszek Koltunski
      super.onPause();
146
      mObjectController.onPause();
147 eaf46415 Leszek Koltunski
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 05c20dad Leszek Koltunski
    @Override
152
    public void onResume()
153 9205f15e Leszek Koltunski
      {
154 05c20dad Leszek Koltunski
      super.onResume();
155 c65a5efe Leszek Koltunski
      mObjectController.onResume();
156 9205f15e Leszek Koltunski
      }
157
158 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 f57d0ad9 Leszek Koltunski
    @SuppressLint("ClickableViewAccessibility")
161 47ba5ddc Leszek Koltunski
    @Override
162
    public boolean onTouchEvent(MotionEvent event)
163
      {
164 dd1a65c1 Leszek Koltunski
      int mode = ScreenList.getMode();
165
      return mObjectController.onTouchEvent(event,mode);
166 47ba5ddc Leszek Koltunski
      }
167 0c52af30 Leszek Koltunski
}