Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorial / TutorialActivity.java @ b6f7c7f2

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.tutorial;
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 org.distorted.dialogs.RubikDialogError;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.message.EffectListener;
36
import org.distorted.main.R;
37
import org.distorted.main.RubikDebug;
38
import org.distorted.objects.ObjectList;
39
import org.distorted.objects.TwistyObject;
40
import org.distorted.states.StateList;
41

    
42
import static org.distorted.main.RubikRenderer.BRIGHTNESS;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class TutorialActivity extends AppCompatActivity implements RubikDebug.ActivityChanger, EffectListener
47
{
48
    private static final String URL = "https://www.youtube.com/embed/";
49

    
50
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
51
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
52

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

    
59
    private boolean mIsLocked;
60
    private static int mScreenWidth, mScreenHeight;
61
    private int mCurrentApiVersion;
62
    private TutorialState mState;
63
    private String mURL;
64
    private int mObjectOrdinal, mObjectSize;
65
    private TutorialWebView mWebView;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
77
      Bundle b = getIntent().getExtras();
78

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

    
86
      mIsLocked = false;
87

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

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

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

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

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

    
107
        decorView.setSystemUiVisibility(FLAGS);
108

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

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

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

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

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

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

    
146
      if( mState==null ) mState = new TutorialState();
147

    
148
      mState.createRightPane(this,width);
149

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// do not avoid cutouts
157

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

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

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

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

    
192
      DistortedLibrary.onPause(1);
193
      }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
    
197
    @Override
198
    protected void onResume() 
199
      {
200
      super.onResume();
201

    
202
      RubikDebug debug = RubikDebug.getInstance();
203
      debug.onResume(this);
204
      TutorialRenderer.mStartDebug = true;
205
      RubikDebug.addDebug("TutorialActivity: onResume");
206

    
207
      DistortedLibrary.onResume(1);
208
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
209
      view.onResume();
210
      view.initialize();
211

    
212
      if( mWebView!=null ) mWebView.onResume();
213

    
214
      if( mObjectOrdinal>=0 && mObjectOrdinal< ObjectList.NUM_OBJECTS )
215
        {
216
        ObjectList obj = ObjectList.getObject(mObjectOrdinal);
217
        int[] sizes = obj.getSizes();
218
        int sizeIndex = ObjectList.getSizeIndex(mObjectOrdinal,mObjectSize);
219

    
220
        if( sizeIndex>=0 && sizeIndex<sizes.length )
221
          {
222
          view.getPreRender().changeObject(obj,mObjectSize);
223
          }
224
        }
225
      }
226
    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
    
229
    @Override
230
    protected void onDestroy() 
231
      {
232
      super.onDestroy();
233
      DistortedLibrary.onDestroy(1);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    void OpenGLError()
239
      {
240
      RubikDialogError errDiag = new RubikDialogError();
241
      errDiag.show(getSupportFragmentManager(), null);
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    TutorialState getState()
247
      {
248
      return mState;
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
// PUBLIC API
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
    public int getScreenWidthInPixels()
265
      {
266
      return mScreenWidth;
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
    public TutorialPreRender getPreRender()
272
      {
273
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
274
      return view.getPreRender();
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

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    public void toggleLock()
323
      {
324
      mIsLocked = !mIsLocked;
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    public boolean isLocked()
330
      {
331
      StateList state = StateList.getCurrentState();
332

    
333
      if( state== StateList.PLAY || state== StateList.READ || state== StateList.SOLV )
334
        {
335
        return mIsLocked;
336
        }
337

    
338
      return false;
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
    public boolean retLocked()
344
      {
345
      return mIsLocked;
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
  public void change()
351
    {
352
    finish();
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  public void assign()
358
    {
359
    TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
360
    TutorialPreRender pre = view.getPreRender();
361
    TwistyObject object = pre.getObject();
362

    
363
    int angle = 360 / object.getBasicAngle();
364

    
365
    object.addNewRotation(1,1,angle,1000,this);
366
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
  public void effectFinished(final long effectID)
371
    {
372
    RubikDebug debug = RubikDebug.getInstance();
373
    debug.onReturned();
374
    }
375
}
(1-1/7)