Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorial.java @ 988e3d33

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
      int colorB = getResources().getColor(R.color.light_grey);
112
      int colorT = getResources().getColor(R.color.white);
113

    
114
      int numTuts = tutorials.length;
115

    
116
      for( int t=0; t<numTuts; t++)
117
        {
118
        String[] tutorial = tutorials[t];
119

    
120
        String coun = tutorial[0];
121
        String url  = tutorial[1];
122
        String desc = tutorial[2];
123
        String auth = tutorial[3];
124

    
125
        int countryID = res.getIdentifier(coun, "drawable", packageName);
126

    
127
        View row = createRow(act, countryID, desc, url, auth, paneSize, colorB, colorT, t==(numTuts-1) );
128
        layout.addView(row);
129
        }
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private View createRow(final FragmentActivity act, int countryID, final String desc, final String url,
136
                         final String auth, int size, int colorB, int colorT, boolean last)
137
    {
138
    float textSize = size*TEXT_SIZE;
139
    View row = inflate( act, R.layout.dialog_tutorial_row, null);
140

    
141
    LinearLayout layout = row.findViewById(R.id.tutorialLayout);
142
    layout.setMinimumHeight(size);
143

    
144
    Button butt = row.findViewById(R.id.tutorialButton);
145
    butt.setText(R.string.view);
146
    butt.setTextColor(colorT);
147
    butt.setBackgroundColor(colorB);
148
    butt.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
149
    butt.setHeight(size);
150

    
151
    final TutorialActivity tact = (TutorialActivity)getContext();
152

    
153
    butt.setOnClickListener( new View.OnClickListener()
154
      {
155
      @Override
156
      public void onClick(View v)
157
        {
158
        mDialog.dismiss();
159
        if( tact!=null ) tact.loadTutorial(url);
160
        }
161
      });
162

    
163
    ImageView image = row.findViewById(R.id.tutorialCountry);
164
    int id = countryID!=0 ? countryID : org.distorted.flags.R.drawable.unknown;
165
    image.setImageResource(id);
166

    
167
    TextView author = row.findViewById(R.id.tutorialAuthor);
168
    author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
169
    author.setText(auth);
170

    
171
    TextView title  = row.findViewById(R.id.tutorialTitle);
172
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
173
    title.setText(desc);
174

    
175
    if( last )
176
      {
177
      int m = (int)convertDpToPixel(10,act);
178
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
179
      params.bottomMargin = m;
180
      params.leftMargin   = m;
181
      params.rightMargin  = m;
182
      layout.setLayoutParams(params);
183
      }
184

    
185
    return row;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  private static float convertDpToPixel(float dp, Context context)
191
    {
192
    return dp*((float) context.getResources().getDisplayMetrics().densityDpi/DisplayMetrics.DENSITY_DEFAULT);
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public static String getDialogTag()
198
    {
199
    return "DialogTutorialSingle";
200
    }
201
  }
(22-22/24)