Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ c65a5efe

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// 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
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// 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
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.tutorials;
21

    
22
import android.os.Build;
23
import android.os.Bundle;
24
import android.util.DisplayMetrics;
25
import android.view.View;
26
import android.view.ViewGroup;
27
import android.view.WindowManager;
28
import android.webkit.WebView;
29
import android.widget.LinearLayout;
30

    
31
import androidx.appcompat.app.AppCompatActivity;
32

    
33
import com.google.firebase.analytics.FirebaseAnalytics;
34

    
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37

    
38
import org.distorted.objectlib.main.ObjectControl;
39
import org.distorted.objectlib.main.ObjectType;
40

    
41
import org.distorted.main.R;
42
import org.distorted.dialogs.RubikDialogError;
43

    
44
import static org.distorted.main.RubikRenderer.BRIGHTNESS;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class TutorialActivity extends AppCompatActivity
49
{
50
    private static final String URL = "https://www.youtube.com/embed/";
51

    
52
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
53
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
54

    
55
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
56
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
57
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
58
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
59
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
60

    
61
    private FirebaseAnalytics mFirebaseAnalytics;
62
    private static int mScreenWidth, mScreenHeight;
63
    private int mCurrentApiVersion;
64
    private TutorialScreen mState;
65
    private String mURL;
66
    private int mObjectOrdinal;
67
    private TutorialWebView mWebView;
68

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

    
71
    @Override
72
    protected void onCreate(Bundle savedState)
73
      {
74
      super.onCreate(savedState);
75
      DistortedLibrary.onCreate(1);
76
      setTheme(R.style.CustomActivityThemeNoActionBar);
77
      setContentView(R.layout.tutorial);
78

    
79
      Bundle b = getIntent().getExtras();
80

    
81
      if(b != null)
82
        {
83
        mURL           = b.getString("url");
84
        mObjectOrdinal = b.getInt("obj");
85
        }
86

    
87
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
88

    
89
      DisplayMetrics displaymetrics = new DisplayMetrics();
90
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
91
      mScreenWidth =displaymetrics.widthPixels;
92
      mScreenHeight=displaymetrics.heightPixels;
93

    
94
      hideNavigationBar();
95
      cutoutHack();
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    private void hideNavigationBar()
101
      {
102
      mCurrentApiVersion = Build.VERSION.SDK_INT;
103

    
104
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
105
        {
106
        final View decorView = getWindow().getDecorView();
107

    
108
        decorView.setSystemUiVisibility(FLAGS);
109

    
110
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
111
          {
112
          @Override
113
          public void onSystemUiVisibilityChange(int visibility)
114
            {
115
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
116
              {
117
              decorView.setSystemUiVisibility(FLAGS);
118
              }
119
            }
120
          });
121
        }
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    @Override
127
    public void onAttachedToWindow()
128
      {
129
      super.onAttachedToWindow();
130

    
131
      final float RATIO = 0.15f;
132
      float width = getScreenWidthInPixels();
133

    
134
      TutorialSurfaceView viewL = findViewById(R.id.tutorialSurfaceView);
135
      ViewGroup.LayoutParams paramsL = viewL.getLayoutParams();
136
      paramsL.width = (int)(width*(1.0f-RATIO));
137
      viewL.setLayoutParams(paramsL);
138

    
139
      LinearLayout viewR = findViewById(R.id.tutorialRightBar);
140
      ViewGroup.LayoutParams paramsR = viewR.getLayoutParams();
141
      paramsR.width = (int)(width*RATIO);
142
      viewR.setLayoutParams(paramsR);
143

    
144
      final int color = (int)(BRIGHTNESS*255);
145
      viewR.setBackgroundColor( (0xFF<<24)+(color<<16)+(color<<8)+color);
146

    
147
      if( mState==null ) mState = new TutorialScreen();
148

    
149
      mState.createRightPane(this,width);
150

    
151
      WebView videoView = findViewById(R.id.tutorialVideoView);
152
      mWebView = new TutorialWebView(videoView);
153
      mWebView.load(URL+mURL);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
// do not avoid cutouts
158

    
159
    private void cutoutHack()
160
      {
161
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
162
        {
163
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
164
        }
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
    @Override
170
    public void onWindowFocusChanged(boolean hasFocus)
171
      {
172
      super.onWindowFocusChanged(hasFocus);
173

    
174
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
175
        {
176
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
177
        }
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
    @Override
183
    protected void onPause() 
184
      {
185
      super.onPause();
186
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
187
      view.onPause();
188

    
189
      if( mWebView!=null ) mWebView.onPause();
190

    
191
      DistortedLibrary.onPause(1);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
    
196
    @Override
197
    protected void onResume() 
198
      {
199
      super.onResume();
200
      DistortedLibrary.onResume(1);
201
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
202
      view.onResume();
203

    
204
      if( mWebView!=null ) mWebView.onResume();
205

    
206
      if( mObjectOrdinal>=0 && mObjectOrdinal< ObjectType.NUM_OBJECTS )
207
        {
208
        ObjectType obj = ObjectType.getObject(mObjectOrdinal);
209
        view.getObjectControl().changeObject(obj);
210
        }
211
      }
212
    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
    
215
    @Override
216
    protected void onDestroy() 
217
      {
218
      super.onDestroy();
219
      DistortedLibrary.onDestroy(1);
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    void OpenGLError()
225
      {
226
      RubikDialogError errDiag = new RubikDialogError();
227
      errDiag.show(getSupportFragmentManager(), null);
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    TutorialScreen getState()
233
      {
234
      return mState;
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
// PUBLIC API
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    public FirebaseAnalytics getAnalytics()
242
      {
243
      return mFirebaseAnalytics;
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    public int getScreenWidthInPixels()
249
      {
250
      return mScreenWidth;
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    public int getScreenHeightInPixels()
256
      {
257
      return mScreenHeight;
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public ObjectControl getControl()
263
      {
264
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
265
      return view.getObjectControl();
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public DistortedScreen getScreen()
271
      {
272
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
273
      TutorialRenderer renderer = view.getRenderer();
274
      return renderer.getScreen();
275
      }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
    public static int getDrawableSize()
280
      {
281
      if( mScreenHeight<1000 )
282
        {
283
        return 0;
284
        }
285
      if( mScreenHeight<1600 )
286
        {
287
        return 1;
288
        }
289
      if( mScreenHeight<1900 )
290
        {
291
        return 2;
292
        }
293

    
294
      return 3;
295
      }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
    public static int getDrawable(int small, int medium, int big, int huge)
300
      {
301
      int size = getDrawableSize();
302

    
303
      switch(size)
304
        {
305
        case 0 : return small;
306
        case 1 : return medium;
307
        case 2 : return big;
308
        default: return huge;
309
        }
310
      }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
    public boolean isVertical()
315
      {
316
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
317
      return view.isVertical();
318
      }
319
}
(1-1/7)