Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPattern.java @ 400ff34d

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.main.R;
43
import org.distorted.main.RubikActivity;
44
import org.distorted.objects.RubikObject;
45
import org.distorted.objects.RubikObjectList;
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
      int ordinal  = RubikPatternList.getObject(i);
98
      RubikObject object = RubikObjectList.getObject(ordinal);
99
      int iconSize= RubikActivity.getDrawableSize();
100
      int iconID  = object==null ? 0 : object.getIconID(iconSize);
101

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

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

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

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

    
127
    return dialog;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private int getPatternOrdinal()
133
    {
134
    int obj = RubikObjectList.getCurrObject();
135
    int ret = RubikPatternList.getOrdinal(obj);
136

    
137
    if( ret<0 )
138
      {
139
      ret = RubikPatternList.getOrdinal(RubikObjectList.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)