1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 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 static android.view.View.inflate;
|
13
|
|
14
|
import android.app.Dialog;
|
15
|
import android.content.Context;
|
16
|
import android.content.res.Resources;
|
17
|
import android.util.DisplayMetrics;
|
18
|
import android.util.TypedValue;
|
19
|
import android.view.View;
|
20
|
import android.view.Window;
|
21
|
import android.view.WindowManager;
|
22
|
import android.widget.Button;
|
23
|
import android.widget.ImageView;
|
24
|
import android.widget.LinearLayout;
|
25
|
import android.widget.TextView;
|
26
|
|
27
|
import androidx.fragment.app.FragmentActivity;
|
28
|
|
29
|
import org.distorted.main.R;
|
30
|
import org.distorted.objectlib.json.JsonReader;
|
31
|
import org.distorted.objects.RubikObject;
|
32
|
import org.distorted.objects.RubikObjectList;
|
33
|
import org.distorted.tutorials.TutorialActivity;
|
34
|
|
35
|
import java.io.InputStream;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public class RubikDialogTutorial extends RubikDialogAbstract
|
40
|
{
|
41
|
public static final float PANE_HEIGHT= 0.06f;
|
42
|
public static final float TEXT_SIZE = 0.41f;
|
43
|
|
44
|
private Dialog mDialog;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
@Override
|
49
|
public void onResume()
|
50
|
{
|
51
|
super.onResume();
|
52
|
|
53
|
Window window = getDialog().getWindow();
|
54
|
|
55
|
if( window!=null )
|
56
|
{
|
57
|
WindowManager.LayoutParams params = window.getAttributes();
|
58
|
params.width = (int)Math.min( mHeight*0.67f,mWidth*0.98f );
|
59
|
//params.height = (int)Math.min( mHeight*0.85f,mWidth*1.30f );
|
60
|
window.setAttributes(params);
|
61
|
}
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
public int getResource() { return R.layout.dialog_tutorial_single; }
|
67
|
public int getTitleResource() { return R.string.tutorials; }
|
68
|
public boolean hasArgument() { return true; }
|
69
|
public int getPositive() { return -1; }
|
70
|
public int getNegative() { return -1; }
|
71
|
public void positiveAction() { }
|
72
|
public void negativeAction() { }
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
|
77
|
{
|
78
|
mDialog = dialog;
|
79
|
|
80
|
int objectOrdinal = Integer.parseInt(mArgument);
|
81
|
|
82
|
if( objectOrdinal<0 )
|
83
|
{
|
84
|
android.util.Log.e("D", "object "+mArgument+" not found");
|
85
|
return;
|
86
|
}
|
87
|
|
88
|
RubikObject robject = RubikObjectList.getObject(objectOrdinal);
|
89
|
InputStream jsonStream = robject==null ? null : robject.getExtrasStream(act);
|
90
|
String[][] tutorials=null;
|
91
|
|
92
|
if( jsonStream!=null )
|
93
|
{
|
94
|
try
|
95
|
{
|
96
|
JsonReader reader = new JsonReader();
|
97
|
reader.parseJsonTutorial(jsonStream);
|
98
|
tutorials = reader.getTutorials();
|
99
|
}
|
100
|
catch(Exception ignored) { }
|
101
|
}
|
102
|
|
103
|
if( tutorials!=null )
|
104
|
{
|
105
|
int paneSize = (int)(mHeight*PANE_HEIGHT);
|
106
|
Resources res = act.getResources();
|
107
|
String packageName = act.getPackageName();
|
108
|
|
109
|
LinearLayout layout = view.findViewById(R.id.tutLayout);
|
110
|
|
111
|
TutorialActivity mact = (TutorialActivity)act;
|
112
|
int backgroundC = mact.getVeryDarkColor();
|
113
|
|
114
|
int colorB = res.getColor(backgroundC);
|
115
|
int colorT = res.getColor(R.color.white);
|
116
|
|
117
|
int numTuts = tutorials.length;
|
118
|
|
119
|
for( int t=0; t<numTuts; t++)
|
120
|
{
|
121
|
String[] tutorial = tutorials[t];
|
122
|
|
123
|
String coun = tutorial[0];
|
124
|
String url = tutorial[1];
|
125
|
String desc = tutorial[2];
|
126
|
String auth = tutorial[3];
|
127
|
|
128
|
int countryID = res.getIdentifier(coun, "drawable", packageName);
|
129
|
|
130
|
View row = createRow(act, countryID, desc, url, auth, paneSize, colorB, colorT, t==(numTuts-1) );
|
131
|
layout.addView(row);
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
private View createRow(final FragmentActivity act, int countryID, final String desc, final String url,
|
139
|
final String auth, int size, int colorB, int colorT, boolean last)
|
140
|
{
|
141
|
float textSize = size*TEXT_SIZE;
|
142
|
View row = inflate( act, R.layout.dialog_tutorial_row, null);
|
143
|
|
144
|
LinearLayout layout = row.findViewById(R.id.tutorialLayout);
|
145
|
layout.setMinimumHeight(size);
|
146
|
|
147
|
Button butt = row.findViewById(R.id.tutorialButton);
|
148
|
butt.setText(R.string.view);
|
149
|
butt.setTextColor(colorT);
|
150
|
butt.setBackgroundColor(colorB);
|
151
|
butt.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
152
|
butt.setHeight(size);
|
153
|
|
154
|
final TutorialActivity tact = (TutorialActivity)getContext();
|
155
|
|
156
|
butt.setOnClickListener( new View.OnClickListener()
|
157
|
{
|
158
|
@Override
|
159
|
public void onClick(View v)
|
160
|
{
|
161
|
mDialog.dismiss();
|
162
|
if( tact!=null ) tact.loadTutorial(url);
|
163
|
}
|
164
|
});
|
165
|
|
166
|
ImageView image = row.findViewById(R.id.tutorialCountry);
|
167
|
int id = countryID!=0 ? countryID : org.distorted.flags.R.drawable.unknown;
|
168
|
image.setImageResource(id);
|
169
|
|
170
|
TextView author = row.findViewById(R.id.tutorialAuthor);
|
171
|
author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
172
|
author.setText(auth);
|
173
|
|
174
|
TextView title = row.findViewById(R.id.tutorialTitle);
|
175
|
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
176
|
title.setText(desc);
|
177
|
|
178
|
if( last )
|
179
|
{
|
180
|
int m = (int)convertDpToPixel(10,act);
|
181
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
|
182
|
params.bottomMargin = m;
|
183
|
params.leftMargin = m;
|
184
|
params.rightMargin = m;
|
185
|
layout.setLayoutParams(params);
|
186
|
}
|
187
|
|
188
|
return row;
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
private static float convertDpToPixel(float dp, Context context)
|
194
|
{
|
195
|
return dp*((float) context.getResources().getDisplayMetrics().densityDpi/DisplayMetrics.DENSITY_DEFAULT);
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
public static String getDialogTag()
|
201
|
{
|
202
|
return "DialogTutorialSingle";
|
203
|
}
|
204
|
}
|