Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorial.java @ 52547ba7

1 a8576d91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.dialogs;
21
22
import android.app.Dialog;
23 234a7582 Leszek Koltunski
import android.content.Context;
24 a8576d91 Leszek Koltunski
import android.content.DialogInterface;
25
import android.os.Bundle;
26
import android.util.DisplayMetrics;
27
import android.util.TypedValue;
28
import android.view.LayoutInflater;
29
import android.view.View;
30
import android.view.Window;
31 234a7582 Leszek Koltunski
import android.view.WindowManager;
32 a8576d91 Leszek Koltunski
import android.widget.Button;
33 234a7582 Leszek Koltunski
import android.widget.ImageView;
34 a8576d91 Leszek Koltunski
import android.widget.TextView;
35
36
import androidx.annotation.NonNull;
37
import androidx.appcompat.app.AlertDialog;
38
import androidx.appcompat.app.AppCompatDialogFragment;
39
import androidx.fragment.app.FragmentActivity;
40 234a7582 Leszek Koltunski
import androidx.viewpager.widget.ViewPager;
41
42
import com.google.android.material.tabs.TabLayout;
43 a8576d91 Leszek Koltunski
44
import org.distorted.main.R;
45
import org.distorted.main.RubikActivity;
46 52547ba7 Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
47
import org.distorted.screens.RubikScreenPlay;
48
import org.distorted.screens.ScreenList;
49 eaf87d1d Leszek Koltunski
import org.distorted.tutorials.TutorialList;
50 a8576d91 Leszek Koltunski
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 234a7582 Leszek Koltunski
public class RubikDialogTutorial extends AppCompatDialogFragment
54 a8576d91 Leszek Koltunski
  {
55 234a7582 Leszek Koltunski
  private RubikDialogTutorialPagerAdapter mPagerAdapter;
56
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 a8576d91 Leszek Koltunski
  @NonNull
60
  @Override
61
  public Dialog onCreateDialog(Bundle savedInstanceState)
62
    {
63
    final FragmentActivity act = getActivity();
64
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
65
66
    DisplayMetrics displaymetrics = new DisplayMetrics();
67
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
68
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
69
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
70
71 234a7582 Leszek Koltunski
    LayoutInflater layoutInflater = act.getLayoutInflater();
72
    TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
73 a8576d91 Leszek Koltunski
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
74 234a7582 Leszek Koltunski
    tv.setText(R.string.tutorials);
75 a8576d91 Leszek Koltunski
    builder.setCustomTitle(tv);
76
77 234a7582 Leszek Koltunski
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
78 80218b07 Leszek Koltunski
      {
79
      @Override
80
      public void onClick(DialogInterface dialog, int which)
81
        {
82 234a7582 Leszek Koltunski
83 80218b07 Leszek Koltunski
        }
84
      });
85
86 234a7582 Leszek Koltunski
    LayoutInflater inflater = act.getLayoutInflater();
87
    final View view = inflater.inflate(R.layout.dialog_tabbed, null);
88 a8576d91 Leszek Koltunski
    builder.setView(view);
89
90 234a7582 Leszek Koltunski
    ViewPager viewPager = view.findViewById(R.id.viewpager);
91
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
92 5b4eaf7e Leszek Koltunski
    mPagerAdapter = new RubikDialogTutorialPagerAdapter(act,viewPager);
93 234a7582 Leszek Koltunski
    tabLayout.setupWithViewPager(viewPager);
94 52547ba7 Leszek Koltunski
    viewPager.setCurrentItem(getTutorialOrdinal());
95 a8576d91 Leszek Koltunski
96 234a7582 Leszek Koltunski
    for(int i=0; i<TutorialList.NUM_OBJECTS; i++)
97
      {
98
      TutorialList list = TutorialList.getObject(i);
99
      int iconID        = list.getIconID();
100
      ImageView imageView = new ImageView(act);
101
      imageView.setImageResource(iconID);
102
      TabLayout.Tab tab = tabLayout.getTabAt(i);
103
      if(tab!=null) tab.setCustomView(imageView);
104
      }
105
106
    Dialog dialog = builder.create();
107
    dialog.setCanceledOnTouchOutside(false);
108 a8576d91 Leszek Koltunski
    Window window = dialog.getWindow();
109
110
    if( window!=null )
111
      {
112
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
113
      }
114
115
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
116
      {
117
      @Override
118
      public void onShow(DialogInterface dialog)
119
        {
120
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
121
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
122
        }
123
      });
124
125
    return dialog;
126
    }
127 80218b07 Leszek Koltunski
128 52547ba7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
  private int getTutorialOrdinal()
131
    {
132
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
133
    ObjectType obj  = play.getObject();
134
135
    int ret = TutorialList.getOrdinal(obj);
136
137
    if( ret<0 )
138
      {
139
      ret = TutorialList.getOrdinal(RubikScreenPlay.DEF_OBJECT);
140
      }
141
142
    return ret;
143
    }
144
145 80218b07 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147 234a7582 Leszek Koltunski
  @Override
148
  public void onResume()
149
    {
150
    super.onResume();
151
152 a84c3a25 Leszek Koltunski
    if( mPagerAdapter!=null ) mPagerAdapter.clickPossible();
153
154 234a7582 Leszek Koltunski
    Window window = getDialog().getWindow();
155
    Context context = getContext();
156
157
    if( window!=null && context!=null )
158
      {
159
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
160
      WindowManager.LayoutParams params = window.getAttributes();
161
      params.width  = WindowManager.LayoutParams.WRAP_CONTENT;
162 de43f86a Leszek Koltunski
      params.height = (int)Math.min( 0.85f*metrics.heightPixels,1.3f*metrics.widthPixels );
163 234a7582 Leszek Koltunski
      window.setAttributes(params);
164
      }
165
    }
166
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169
  public static String getDialogTag()
170 80218b07 Leszek Koltunski
    {
171 234a7582 Leszek Koltunski
    return "DialogTutorial";
172 80218b07 Leszek Koltunski
    }
173 a8576d91 Leszek Koltunski
  }