Project

General

Profile

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

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

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
import android.os.Bundle;
25 66e777b0 Leszek Koltunski
import androidx.annotation.NonNull;
26
import androidx.fragment.app.FragmentActivity;
27
import androidx.viewpager.widget.ViewPager;
28
import androidx.appcompat.app.AlertDialog;
29
import androidx.appcompat.app.AppCompatDialogFragment;
30
import com.google.android.material.tabs.TabLayout;
31 e108b57e Leszek Koltunski
import android.util.DisplayMetrics;
32
import android.view.LayoutInflater;
33
import android.view.View;
34
import android.view.Window;
35
import android.view.WindowManager;
36
import android.widget.ImageView;
37
import android.widget.TextView;
38
39 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
40
import org.distorted.objects.RubikObjectList;
41 b498f3f6 Leszek Koltunski
import org.distorted.patterns.RubikPatternList;
42 e108b57e Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
public class RubikDialogPattern extends AppCompatDialogFragment
46
  {
47 b88be423 Leszek Koltunski
  private RubikDialogPatternPagerAdapter mPagerAdapter;
48 e108b57e Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
  @Override
52
  public void onStart()
53
    {
54
    super.onStart();
55
56 584585d0 Leszek Koltunski
    Dialog dialog = getDialog();
57
58 b88be423 Leszek Koltunski
    if( dialog!=null )
59 584585d0 Leszek Koltunski
      {
60 b88be423 Leszek Koltunski
      dialog.setCanceledOnTouchOutside(false);
61
62
      Window window = dialog.getWindow();
63
64
      if( window!=null )
65
        {
66
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
67
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
68
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
69
        }
70 584585d0 Leszek Koltunski
      }
71 e108b57e Leszek Koltunski
    }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
  @NonNull
76
  @Override
77
  public Dialog onCreateDialog(Bundle savedInstanceState)
78
    {
79
    FragmentActivity act = getActivity();
80
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
81
82
    LayoutInflater layoutInflater = act.getLayoutInflater();
83
    TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
84 6f2a942e Leszek Koltunski
    tv.setText(R.string.choose_pattern);
85 e108b57e Leszek Koltunski
    builder.setCustomTitle(tv);
86
87
    Bundle args = getArguments();
88
    int curTab;
89
90
    try
91
      {
92
      curTab = args.getInt("tab");
93
      }
94
    catch(Exception e)
95
      {
96
      curTab = 0;
97
      }
98
99
    LayoutInflater inflater = act.getLayoutInflater();
100
    final View view = inflater.inflate(R.layout.dialog_tabbed, null);
101
    builder.setView(view);
102
103
    ViewPager viewPager = view.findViewById(R.id.viewpager);
104
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
105
    mPagerAdapter = new RubikDialogPatternPagerAdapter(act, viewPager, this);
106
    tabLayout.setupWithViewPager(viewPager);
107
    viewPager.setCurrentItem(curTab);
108
109 b498f3f6 Leszek Koltunski
    for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
110 e108b57e Leszek Koltunski
      {
111 b498f3f6 Leszek Koltunski
      RubikObjectList list = RubikPatternList.getObject(i);
112
      int size             = RubikPatternList.getSize(i);
113
      int sizeIndex        = RubikObjectList.getSizeIndex(list.ordinal(),size);
114
      int iconID           = list.getIconIDs()[sizeIndex];
115
116 e108b57e Leszek Koltunski
      ImageView imageView = new ImageView(act);
117 b498f3f6 Leszek Koltunski
      imageView.setImageResource(iconID);
118 e108b57e Leszek Koltunski
      TabLayout.Tab tab = tabLayout.getTabAt(i);
119
      if(tab!=null) tab.setCustomView(imageView);
120
      }
121
122
    return builder.create();
123
    }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
  @Override
128
  public void onResume()
129
    {
130
    super.onResume();
131
132
    Window window = getDialog().getWindow();
133
    Context context = getContext();
134
135
    if( window!=null && context!=null )
136
      {
137
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
138
      final float height= metrics.heightPixels;
139
140
      WindowManager.LayoutParams params = window.getAttributes();
141
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
142
      params.height = (int)(0.75f*height);
143
      window.setAttributes(params);
144
      }
145
    }
146
147 044529c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149 c715128d Leszek Koltunski
  public void rememberState()
150 044529c1 Leszek Koltunski
    {
151 c715128d Leszek Koltunski
    mPagerAdapter.rememberState();
152 044529c1 Leszek Koltunski
    }
153
154 e108b57e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
  public static String getDialogTag()
157
    {
158
    return "DialogPattern";
159
    }
160
  }