1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.dialogs;
|
11
|
|
12
|
import android.app.Dialog;
|
13
|
import android.content.Context;
|
14
|
import android.content.DialogInterface;
|
15
|
import android.os.Bundle;
|
16
|
import androidx.annotation.NonNull;
|
17
|
import androidx.fragment.app.FragmentActivity;
|
18
|
import androidx.viewpager.widget.ViewPager;
|
19
|
import androidx.appcompat.app.AlertDialog;
|
20
|
import androidx.appcompat.app.AppCompatDialogFragment;
|
21
|
import com.google.android.material.tabs.TabLayout;
|
22
|
import android.util.DisplayMetrics;
|
23
|
import android.util.TypedValue;
|
24
|
import android.view.LayoutInflater;
|
25
|
import android.view.View;
|
26
|
import android.view.ViewGroup;
|
27
|
import android.view.Window;
|
28
|
import android.view.WindowManager;
|
29
|
import android.widget.Button;
|
30
|
import android.widget.ImageView;
|
31
|
import android.widget.TextView;
|
32
|
|
33
|
import org.distorted.main.R;
|
34
|
import org.distorted.main.RubikActivity;
|
35
|
import org.distorted.objects.RubikObject;
|
36
|
import org.distorted.objects.RubikObjectList;
|
37
|
import org.distorted.patterns.RubikPatternList;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class RubikDialogPattern extends AppCompatDialogFragment
|
42
|
{
|
43
|
private RubikDialogPatternPagerAdapter mPagerAdapter;
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
@NonNull
|
48
|
@Override
|
49
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
50
|
{
|
51
|
final FragmentActivity act = getActivity();
|
52
|
AlertDialog.Builder builder = new AlertDialog.Builder(act);
|
53
|
|
54
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
55
|
act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
|
56
|
|
57
|
int h = displaymetrics.heightPixels;
|
58
|
final float titleSize= h*RubikActivity.TAB_TEXT_SIZE;
|
59
|
final float okSize = h*RubikActivity.TAB_BUTTON_SIZE;
|
60
|
final int tabHeight= (int)(h*RubikActivity.TAB_HEIGHT);
|
61
|
final int tabWidth = (int)(h*RubikActivity.TAB_WIDTH);
|
62
|
|
63
|
LayoutInflater layoutInflater = act.getLayoutInflater();
|
64
|
TextView tv = (TextView) layoutInflater.inflate(R.layout.dialog_title, null);
|
65
|
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
|
66
|
tv.setText(R.string.choose_pattern);
|
67
|
builder.setCustomTitle(tv);
|
68
|
|
69
|
builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
|
70
|
{
|
71
|
@Override
|
72
|
public void onClick(DialogInterface dialog, int which)
|
73
|
{
|
74
|
|
75
|
}
|
76
|
});
|
77
|
|
78
|
LayoutInflater inflater = act.getLayoutInflater();
|
79
|
final View view = inflater.inflate(R.layout.dialog_tabbed, null);
|
80
|
builder.setView(view);
|
81
|
|
82
|
ViewPager viewPager = view.findViewById(R.id.viewpager);
|
83
|
TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
|
84
|
mPagerAdapter = new RubikDialogPatternPagerAdapter(act, viewPager, this);
|
85
|
tabLayout.setupWithViewPager(viewPager);
|
86
|
viewPager.setCurrentItem(getPatternOrdinal());
|
87
|
|
88
|
ViewGroup.LayoutParams paramsView = new ViewGroup.LayoutParams( tabWidth,tabHeight );
|
89
|
|
90
|
for(int i=0; i< RubikPatternList.NUM_OBJECTS; i++)
|
91
|
{
|
92
|
int ordinal = RubikPatternList.getObject(i);
|
93
|
RubikObject object = RubikObjectList.getObject(ordinal);
|
94
|
ImageView imageView = new ImageView(act);
|
95
|
if( object!=null ) object.setIconTo(act,imageView);
|
96
|
imageView.setLayoutParams(paramsView);
|
97
|
TabLayout.Tab tab = tabLayout.getTabAt(i);
|
98
|
if(tab!=null) tab.setCustomView(imageView);
|
99
|
}
|
100
|
|
101
|
Dialog dialog = builder.create();
|
102
|
dialog.setCanceledOnTouchOutside(false);
|
103
|
Window window = dialog.getWindow();
|
104
|
|
105
|
if( window!=null )
|
106
|
{
|
107
|
window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
|
108
|
}
|
109
|
|
110
|
dialog.setOnShowListener(new DialogInterface.OnShowListener()
|
111
|
{
|
112
|
@Override
|
113
|
public void onShow(DialogInterface dialog)
|
114
|
{
|
115
|
Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
|
116
|
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
|
117
|
}
|
118
|
});
|
119
|
|
120
|
return dialog;
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
private int getPatternOrdinal()
|
126
|
{
|
127
|
int obj = RubikObjectList.getCurrObject();
|
128
|
int ret = RubikPatternList.getOrdinal(obj);
|
129
|
|
130
|
if( ret<0 )
|
131
|
{
|
132
|
ret = RubikPatternList.getOrdinal(RubikObjectList.DEF_OBJECT);
|
133
|
}
|
134
|
|
135
|
return ret;
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
@Override
|
141
|
public void onResume()
|
142
|
{
|
143
|
super.onResume();
|
144
|
|
145
|
Window window = getDialog().getWindow();
|
146
|
Context context = getContext();
|
147
|
|
148
|
if( window!=null && context!=null )
|
149
|
{
|
150
|
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
151
|
WindowManager.LayoutParams params = window.getAttributes();
|
152
|
params.width = (int)Math.min( 0.65f*metrics.heightPixels,0.98f*metrics.widthPixels );
|
153
|
params.height = (int)Math.min( 0.85f*metrics.heightPixels,1.30f*metrics.widthPixels );
|
154
|
window.setAttributes(params);
|
155
|
}
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void rememberState()
|
161
|
{
|
162
|
mPagerAdapter.rememberState();
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public static String getDialogTag()
|
168
|
{
|
169
|
return "DialogPattern";
|
170
|
}
|
171
|
}
|