Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialActivity.java @ 318c0a7d

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 com.google.firebase.analytics.FirebaseAnalytics;
32

    
33
import org.distorted.library.main.DistortedLibrary;
34

    
35
import org.distorted.objectlib.main.ObjectType;
36
import org.distorted.objectlib.main.TwistyObject;
37

    
38
import org.distorted.dialogs.RubikDialogError;
39
import org.distorted.helpers.BlockController;
40
import org.distorted.helpers.TwistyActivity;
41
import org.distorted.helpers.TwistyPreRender;
42
import org.distorted.main.R;
43

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

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

    
48
public class TutorialActivity extends TwistyActivity
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 TutorialState 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
      unlock();
88
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
89

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

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

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

    
109
        decorView.setSystemUiVisibility(FLAGS);
110

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

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

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

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

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

    
148
      if( mState==null ) mState = new TutorialState();
149

    
150
      mState.createRightPane(this,width);
151

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// do not avoid cutouts
159

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

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

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

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

    
192
      DistortedLibrary.onPause(1);
193
      BlockController.onPause();
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
    
198
    @Override
199
    protected void onResume() 
200
      {
201
      super.onResume();
202
      DistortedLibrary.onResume(1);
203
      BlockController.onResume();
204
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
205
      view.onResume();
206
      view.initialize();
207

    
208
      if( mWebView!=null ) mWebView.onResume();
209

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    void OpenGLError()
229
      {
230
      RubikDialogError errDiag = new RubikDialogError();
231
      errDiag.show(getSupportFragmentManager(), null);
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
    TutorialState getState()
237
      {
238
      return mState;
239
      }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// PUBLIC API
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public FirebaseAnalytics getAnalytics()
246
      {
247
      return mFirebaseAnalytics;
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public TwistyObject getObject()
253
      {
254
      TutorialSurfaceView view = findViewById(R.id.rubikSurfaceView);
255
      TutorialPreRender pre = view.getPreRender();
256
      return pre.getObject();
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    public int getScreenWidthInPixels()
262
      {
263
      return mScreenWidth;
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    public int getScreenHeightInPixels()
269
      {
270
      return mScreenHeight;
271
      }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    public TutorialPreRender getPreRender()
276
      {
277
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
278
      return view.getPreRender();
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
    public TwistyPreRender getTwistyPreRender()
284
      {
285
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
286
      return view.getPreRender();
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    public static int getDrawableSize()
292
      {
293
      if( mScreenHeight<1000 )
294
        {
295
        return 0;
296
        }
297
      if( mScreenHeight<1600 )
298
        {
299
        return 1;
300
        }
301
      if( mScreenHeight<1900 )
302
        {
303
        return 2;
304
        }
305

    
306
      return 3;
307
      }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
    public static int getDrawable(int small, int medium, int big, int huge)
312
      {
313
      int size = getDrawableSize();
314

    
315
      switch(size)
316
        {
317
        case 0 : return small;
318
        case 1 : return medium;
319
        case 2 : return big;
320
        default: return huge;
321
        }
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    public boolean isVertical()
327
      {
328
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
329
      return view.isVertical();
330
      }
331
}
(1-1/7)