Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorial / TutorialActivity.java @ 36b9ee93

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
      RubikDebug debug = RubikDebug.getInstance();
202
      debug.onResume(this);
203
      RubikDebug.addDebug("RubikActivity: onResume");
204

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

    
210
      if( mWebView!=null ) mWebView.onResume();
211

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

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

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

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    TutorialState getState()
245
      {
246
      return mState;
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// PUBLIC API
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

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

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

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public TutorialPreRender getPreRender()
270
      {
271
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
272
      return view.getPreRender();
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

    
292
      return 3;
293
      }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

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

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
    public boolean isVertical()
313
      {
314
      TutorialSurfaceView view = findViewById(R.id.tutorialSurfaceView);
315
      return view.isVertical();
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    public void toggleLock()
321
      {
322
      mIsLocked = !mIsLocked;
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    public boolean isLocked()
328
      {
329
      StateList state = StateList.getCurrentState();
330

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

    
336
      return false;
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
    public boolean retLocked()
342
      {
343
      return mIsLocked;
344
      }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public void change()
349
    {
350
    finish();
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

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

    
361
    int angle = 360 / object.getBasicAngle();
362

    
363
    object.addNewRotation(1,1,angle,1000,this);
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

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