Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialSingle.java @ 4a6b3b53

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 RubikDialogTutorialSingle 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 = RubikObjectList.getOrdinal(mArgument);
81

    
82
    android.util.Log.e("D", "dialog tutorial: object "+mArgument);
83

    
84
    if( objectOrdinal<0 )
85
      {
86
      android.util.Log.e("D", "object "+mArgument+" not found");
87
      return;
88
      }
89
    RubikObject robject = RubikObjectList.getObject(objectOrdinal);
90
    InputStream jsonStream = robject==null ? null : robject.getExtrasStream(act);
91
    String[][] tutorials=null;
92

    
93
    if( jsonStream!=null )
94
      {
95
      try
96
        {
97
        JsonReader reader = new JsonReader();
98
        reader.parseJsonTutorial(jsonStream);
99
        tutorials = reader.getTutorials();
100
        }
101
      catch(Exception ignored) { }
102
      }
103

    
104
    if( tutorials!=null )
105
      {
106
      int paneSize = (int)(mHeight*PANE_HEIGHT);
107
      Resources res = act.getResources();
108
      String packageName = act.getPackageName();
109

    
110
      LinearLayout layout = view.findViewById(R.id.tutLayout);
111

    
112
      int colorB = getResources().getColor(R.color.light_grey);
113
      int colorT = getResources().getColor(R.color.white);
114

    
115
      int numTuts = tutorials.length;
116

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

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

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

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

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

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

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

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

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

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

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

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

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

    
186
    return row;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  public static String getDialogTag()
199
    {
200
    return "DialogTutorialSingle";
201
    }
202
  }
(30-30/33)