Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPattern.java @ d433b50e

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