Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorial.java @ eb985085

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