Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogTutorialSingle.java @ b710a574

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.res.Resources;
16
import android.util.TypedValue;
17
import android.view.View;
18
import android.view.Window;
19
import android.view.WindowManager;
20
import android.widget.Button;
21
import android.widget.ImageView;
22
import android.widget.LinearLayout;
23
import android.widget.TextView;
24

    
25
import androidx.fragment.app.FragmentActivity;
26

    
27
import org.distorted.main.R;
28
import org.distorted.objectlib.json.JsonReader;
29
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31
import org.distorted.tutorials.TutorialActivity;
32

    
33
import java.io.InputStream;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class RubikDialogTutorialSingle extends RubikDialogAbstract
38
  {
39
  public static final float PANE_HEIGHT  = 0.06f;
40
  private Dialog mDialog;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  @Override
45
  public void onResume()
46
    {
47
    super.onResume();
48

    
49
    Window window = getDialog().getWindow();
50

    
51
    if( window!=null )
52
      {
53
      WindowManager.LayoutParams params = window.getAttributes();
54
      params.width  = (int)Math.min( mHeight*0.65f,mWidth*0.98f );
55
      //params.height = (int)Math.min( mHeight*0.85f,mWidth*1.30f );
56
      window.setAttributes(params);
57
      }
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  public int getResource()      { return R.layout.dialog_tutorial_single; }
63
  public int getTitleResource() { return R.string.tutorials; }
64
  public boolean hasArgument()  { return true; }
65
  public int getPositive()      { return -1; }
66
  public int getNegative()      { return -1; }
67
  public void positiveAction()  { }
68
  public void negativeAction()  { }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
73
    {
74
    mDialog = dialog;
75

    
76
    int objectOrdinal = RubikObjectList.getOrdinal(mArgument);
77

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

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

    
89
    if( jsonStream!=null )
90
      {
91
      try
92
        {
93
        JsonReader reader = new JsonReader();
94
        reader.parseJsonTutorial(jsonStream);
95
        tutorials = reader.getTutorials();
96
        }
97
      catch(Exception ignored) { }
98
      }
99

    
100
    if( tutorials!=null )
101
      {
102
      int paneSize = (int)(mHeight*PANE_HEIGHT);
103
      Resources res = act.getResources();
104
      String packageName = act.getPackageName();
105

    
106
      LinearLayout layout = view.findViewById(R.id.tutLayout);
107

    
108
      int colorB = getResources().getColor(R.color.light_grey);
109
      int colorT = getResources().getColor(R.color.white);
110

    
111
      int numTuts = tutorials.length;
112

    
113
      for( int t=0; t<numTuts; t++)
114
        {
115
        String[] tutorial = tutorials[t];
116

    
117
        String coun = tutorial[0];
118
        String url  = tutorial[1];
119
        String desc = tutorial[2];
120
        String auth = tutorial[3];
121

    
122
        int countryID = res.getIdentifier(coun, "drawable", packageName);
123

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

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

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

    
138
    LinearLayout layout = row.findViewById(R.id.tutorialLayout);
139
    layout.setMinimumHeight(size);
140

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

    
148
    final TutorialActivity tact = (TutorialActivity)getContext();
149

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

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

    
164
    TextView author = row.findViewById(R.id.tutorialAuthor);
165
    author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
166
    author.setText(auth);
167

    
168
    TextView title  = row.findViewById(R.id.tutorialTitle);
169
    title.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
170
    title.setText(desc);
171

    
172
    if( last )
173
      {
174
      int m = 20;
175
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
176
      params.bottomMargin = m;
177
      params.leftMargin   = m;
178
      params.rightMargin  = m;
179
      layout.setLayoutParams(params);
180
      }
181

    
182
    return row;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public static String getDialogTag()
188
    {
189
    return "DialogTutorialSingle";
190
    }
191
  }
(30-30/33)