Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPattern.java @ 85248b04

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
  @NonNull
52
  @Override
53
  public Dialog onCreateDialog(Bundle savedInstanceState)
54
    {
55
    FragmentActivity act = getActivity();
56
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
57

    
58
    LayoutInflater layoutInflater = act.getLayoutInflater();
59
    TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
60
    tv.setText(R.string.choose_pattern);
61
    builder.setCustomTitle(tv);
62

    
63
    Bundle args = getArguments();
64
    int curTab;
65

    
66
    try
67
      {
68
      curTab = args.getInt("tab");
69
      }
70
    catch(Exception e)
71
      {
72
      curTab = 0;
73
      }
74

    
75
    LayoutInflater inflater = act.getLayoutInflater();
76
    final View view = inflater.inflate(R.layout.dialog_tabbed, null);
77
    builder.setView(view);
78

    
79
    ViewPager viewPager = view.findViewById(R.id.viewpager);
80
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
81
    mPagerAdapter = new RubikDialogPatternPagerAdapter(act, viewPager, this);
82
    tabLayout.setupWithViewPager(viewPager);
83
    viewPager.setCurrentItem(curTab);
84

    
85
    for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
86
      {
87
      RubikObjectList list = RubikPatternList.getObject(i);
88
      int size             = RubikPatternList.getSize(i);
89
      int sizeIndex        = RubikObjectList.getSizeIndex(list.ordinal(),size);
90
      int iconID           = list.getIconIDs()[sizeIndex];
91

    
92
      ImageView imageView = new ImageView(act);
93
      imageView.setImageResource(iconID);
94
      TabLayout.Tab tab = tabLayout.getTabAt(i);
95
      if(tab!=null) tab.setCustomView(imageView);
96
      }
97

    
98
    Dialog dialog = builder.create();
99

    
100
    dialog.setCanceledOnTouchOutside(false);
101

    
102
    Window window = dialog.getWindow();
103

    
104
    if( window!=null )
105
      {
106
      window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
107
                      WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
108
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
109
      }
110

    
111
    return dialog;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  @Override
117
  public void onResume()
118
    {
119
    super.onResume();
120

    
121
    Window window = getDialog().getWindow();
122
    Context context = getContext();
123

    
124
    if( window!=null && context!=null )
125
      {
126
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
127
      final float height= metrics.heightPixels;
128

    
129
      WindowManager.LayoutParams params = window.getAttributes();
130
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
131
      params.height = (int)(0.75f*height);
132
      window.setAttributes(params);
133
      }
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public void rememberState()
139
    {
140
    mPagerAdapter.rememberState();
141
    }
142

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

    
145
  public static String getDialogTag()
146
    {
147
    return "DialogPattern";
148
    }
149
  }
(5-5/13)