Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPattern.java @ 52547ba7

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.content.DialogInterface;
25
import android.os.Bundle;
26
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
import android.util.DisplayMetrics;
33
import android.util.TypedValue;
34
import android.view.LayoutInflater;
35
import android.view.View;
36
import android.view.Window;
37
import android.view.WindowManager;
38
import android.widget.Button;
39
import android.widget.ImageView;
40
import android.widget.TextView;
41

    
42
import org.distorted.objectlib.main.ObjectType;
43

    
44
import org.distorted.main.R;
45
import org.distorted.main.RubikActivity;
46
import org.distorted.patterns.RubikPatternList;
47
import org.distorted.screens.RubikScreenPlay;
48
import org.distorted.screens.ScreenList;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class RubikDialogPattern extends AppCompatDialogFragment
53
  {
54
  private RubikDialogPatternPagerAdapter mPagerAdapter;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  @NonNull
59
  @Override
60
  public Dialog onCreateDialog(Bundle savedInstanceState)
61
    {
62
    final FragmentActivity act = getActivity();
63
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
64

    
65
    DisplayMetrics displaymetrics = new DisplayMetrics();
66
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
67
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
68
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
69

    
70
    LayoutInflater layoutInflater = act.getLayoutInflater();
71
    TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
72
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
73
    tv.setText(R.string.choose_pattern);
74
    builder.setCustomTitle(tv);
75

    
76
    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
    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
    viewPager.setCurrentItem(getPatternOrdinal());
94

    
95
    for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
96
      {
97
      ObjectType list = RubikPatternList.getObject(i);
98
      int iconSize    = RubikActivity.getDrawableSize();
99
      int iconID      = list.getIconID(iconSize);
100

    
101
      ImageView imageView = new ImageView(act);
102
      imageView.setImageResource(iconID);
103
      TabLayout.Tab tab = tabLayout.getTabAt(i);
104
      if(tab!=null) tab.setCustomView(imageView);
105
      }
106

    
107
    Dialog dialog = builder.create();
108
    dialog.setCanceledOnTouchOutside(false);
109
    Window window = dialog.getWindow();
110

    
111
    if( window!=null )
112
      {
113
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
114
      }
115

    
116
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
117
      {
118
      @Override
119
      public void onShow(DialogInterface dialog)
120
        {
121
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
122
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
123
        }
124
      });
125

    
126
    return dialog;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  private int getPatternOrdinal()
132
    {
133
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
134
    ObjectType obj  = play.getObject();
135
    int ret = RubikPatternList.getOrdinal(obj);
136

    
137
    if( ret<0 )
138
      {
139
      ret = RubikPatternList.getOrdinal(RubikScreenPlay.DEF_OBJECT);
140
      }
141

    
142
    return ret;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  @Override
148
  public void onResume()
149
    {
150
    super.onResume();
151

    
152
    Window window = getDialog().getWindow();
153
    Context context = getContext();
154

    
155
    if( window!=null && context!=null )
156
      {
157
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
158
      final float height= metrics.heightPixels;
159

    
160
      WindowManager.LayoutParams params = window.getAttributes();
161
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
162
      params.height = (int)(0.75f*height);
163
      window.setAttributes(params);
164
      }
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public void rememberState()
170
    {
171
    mPagerAdapter.rememberState();
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public static String getDialogTag()
177
    {
178
    return "DialogPattern";
179
    }
180
  }
(6-6/19)