Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.content.Context;
24
import android.os.Bundle;
25
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
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
import org.distorted.main.R;
40
import org.distorted.objects.RubikObjectList;
41
import org.distorted.patterns.RubikPatternList;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikDialogPattern extends AppCompatDialogFragment
46
  {
47
  private RubikDialogPatternPagerAdapter mPagerAdapter;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  @Override
52
  public void onStart()
53
    {
54
    super.onStart();
55

    
56
    Dialog dialog = getDialog();
57

    
58
    if( dialog!=null )
59
      {
60
      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
      }
71
    }
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
    tv.setText(R.string.choose_pattern);
85
    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
    for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
110
      {
111
      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
      ImageView imageView = new ImageView(act);
117
      imageView.setImageResource(iconID);
118
      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
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  public void rememberState()
150
    {
151
    mPagerAdapter.rememberState();
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public static String getDialogTag()
157
    {
158
    return "DialogPattern";
159
    }
160
  }
(5-5/13)