Project

General

Profile

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

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

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.ViewGroup;
37
import android.view.Window;
38
import android.view.WindowManager;
39
import android.widget.Button;
40
import android.widget.ImageView;
41
import android.widget.TextView;
42

    
43
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45
import org.distorted.objects.RubikObject;
46
import org.distorted.objects.RubikObjectList;
47
import org.distorted.patterns.RubikPatternList;
48
import org.distorted.screens.RubikScreenPlay;
49
import org.distorted.screens.ScreenList;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
66
    DisplayMetrics displaymetrics = new DisplayMetrics();
67
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
68

    
69
    int scrWidth = displaymetrics.widthPixels;
70
    final float titleSize= scrWidth*RubikActivity.MENU_BIG_TEXT_SIZE;
71
    final float okSize   = scrWidth*RubikActivity.DIALOG_BUTTON_SIZE;
72
    final int   tabHeight= (int)(scrWidth*RubikActivity.TAB_HEIGHT);
73
    final int   tabWidth = (int)(scrWidth*RubikActivity.TAB_WIDTH);
74

    
75
    LayoutInflater layoutInflater = act.getLayoutInflater();
76
    TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
77
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
78
    tv.setText(R.string.choose_pattern);
79
    builder.setCustomTitle(tv);
80

    
81
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
82
      {
83
      @Override
84
      public void onClick(DialogInterface dialog, int which)
85
        {
86

    
87
        }
88
      });
89

    
90
    LayoutInflater inflater = act.getLayoutInflater();
91
    final View view = inflater.inflate(R.layout.dialog_tabbed, null);
92
    builder.setView(view);
93

    
94
    ViewPager viewPager = view.findViewById(R.id.viewpager);
95
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
96
    mPagerAdapter = new RubikDialogPatternPagerAdapter(act, viewPager, this);
97
    tabLayout.setupWithViewPager(viewPager);
98
    viewPager.setCurrentItem(getPatternOrdinal());
99

    
100
    ViewGroup.LayoutParams paramsView = new ViewGroup.LayoutParams( tabWidth,tabHeight );
101

    
102
    for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
103
      {
104
      int ordinal = RubikPatternList.getObject(i);
105
      RubikObject object = RubikObjectList.getObject(ordinal);
106
      ImageView imageView = new ImageView(act);
107
      if( object!=null ) object.setIconTo(act,imageView);
108
      imageView.setLayoutParams(paramsView);
109
      TabLayout.Tab tab = tabLayout.getTabAt(i);
110
      if(tab!=null) tab.setCustomView(imageView);
111
      }
112

    
113
    Dialog dialog = builder.create();
114
    dialog.setCanceledOnTouchOutside(false);
115
    Window window = dialog.getWindow();
116

    
117
    if( window!=null )
118
      {
119
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
120
      }
121

    
122
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
123
      {
124
      @Override
125
      public void onShow(DialogInterface dialog)
126
        {
127
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
128
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
129
        }
130
      });
131

    
132
    return dialog;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private int getPatternOrdinal()
138
    {
139
    int obj = RubikObjectList.getCurrObject();
140
    int ret = RubikPatternList.getOrdinal(obj);
141

    
142
    if( ret<0 )
143
      {
144
      ret = RubikPatternList.getOrdinal(RubikObjectList.DEF_OBJECT);
145
      }
146

    
147
    return ret;
148
    }
149

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

    
152
  @Override
153
  public void onResume()
154
    {
155
    super.onResume();
156

    
157
    Window window = getDialog().getWindow();
158
    Context context = getContext();
159

    
160
    if( window!=null && context!=null )
161
      {
162
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
163
      final float height= metrics.heightPixels;
164

    
165
      WindowManager.LayoutParams params = window.getAttributes();
166
      params.width = WindowManager.LayoutParams.WRAP_CONTENT;
167
      params.height = (int)(0.75f*height);
168
      window.setAttributes(params);
169
      }
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public void rememberState()
175
    {
176
    mPagerAdapter.rememberState();
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  public static String getDialogTag()
182
    {
183
    return "DialogPattern";
184
    }
185
  }
(6-6/21)