Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPattern.java @ 66e777b0

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
  RubikDialogPatternPagerAdapter mPagerAdapter;
48

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

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

    
56
    Dialog dialog = getDialog();
57
    dialog.setCanceledOnTouchOutside(false);
58

    
59
    Window window = dialog.getWindow();
60

    
61
    if( window!=null )
62
      {
63
      window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
64
                      WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
65
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
66
      }
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  @NonNull
72
  @Override
73
  public Dialog onCreateDialog(Bundle savedInstanceState)
74
    {
75
    FragmentActivity act = getActivity();
76
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
77

    
78
    LayoutInflater layoutInflater = act.getLayoutInflater();
79
    TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
80
    tv.setText(R.string.choose_pattern);
81
    builder.setCustomTitle(tv);
82

    
83
    Bundle args = getArguments();
84
    int curTab;
85

    
86
    try
87
      {
88
      curTab = args.getInt("tab");
89
      }
90
    catch(Exception e)
91
      {
92
      curTab = 0;
93
      }
94

    
95
    LayoutInflater inflater = act.getLayoutInflater();
96
    final View view = inflater.inflate(R.layout.dialog_tabbed, null);
97
    builder.setView(view);
98

    
99
    ViewPager viewPager = view.findViewById(R.id.viewpager);
100
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
101
    mPagerAdapter = new RubikDialogPatternPagerAdapter(act, viewPager, this);
102
    tabLayout.setupWithViewPager(viewPager);
103
    viewPager.setCurrentItem(curTab);
104

    
105
    for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
106
      {
107
      RubikObjectList list = RubikPatternList.getObject(i);
108
      int size             = RubikPatternList.getSize(i);
109
      int sizeIndex        = RubikObjectList.getSizeIndex(list.ordinal(),size);
110
      int iconID           = list.getIconIDs()[sizeIndex];
111

    
112
      ImageView imageView = new ImageView(act);
113
      imageView.setImageResource(iconID);
114
      TabLayout.Tab tab = tabLayout.getTabAt(i);
115
      if(tab!=null) tab.setCustomView(imageView);
116
      }
117

    
118
    return builder.create();
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  @Override
124
  public void onResume()
125
    {
126
    super.onResume();
127

    
128
    Window window = getDialog().getWindow();
129
    Context context = getContext();
130

    
131
    if( window!=null && context!=null )
132
      {
133
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
134
      final float height= metrics.heightPixels;
135

    
136
      WindowManager.LayoutParams params = window.getAttributes();
137
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
138
      params.height = (int)(0.75f*height);
139
      window.setAttributes(params);
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public void rememberState()
146
    {
147
    mPagerAdapter.rememberState();
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  public static String getDialogTag()
153
    {
154
    return "DialogPattern";
155
    }
156
  }
(5-5/13)