Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ c5fca790

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.screens;
21

    
22
import java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25
import android.content.SharedPreferences;
26
import android.util.TypedValue;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.view.ViewGroup;
30
import android.widget.GridLayout;
31
import android.widget.ImageButton;
32
import android.widget.LinearLayout;
33
import android.widget.RelativeLayout;
34
import android.widget.ScrollView;
35
import android.widget.TextView;
36

    
37
import org.distorted.dialogs.RubikDialogUpdates;
38
import org.distorted.external.RubikNetwork;
39
import org.distorted.external.RubikUpdates;
40

    
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.dialogs.RubikDialogAbout;
44
import org.distorted.helpers.TransparentImageButton;
45
import org.distorted.objects.MainEntry;
46
import org.distorted.objects.MainEntryList;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class RubikScreenPlay extends RubikScreenAbstract implements RubikNetwork.Updatee
51
  {
52
  public static final int NUM_COLUMNS  = 5;
53
  public static final int LEVELS_SHOWN = 10;
54

    
55
  private TransparentImageButton mAboutButton, mExitButton;
56
  private TextView mBubbleUpdates;
57
  private int mCurrentBubbleNumber;
58
  private int mLevelValue;
59
  private int mColCount, mRowCount, mNumObjects;
60
  private float mScreenWidth;
61
  private WeakReference<RubikActivity> mWeakAct;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  void leaveScreen(RubikActivity act)
66
    {
67
    act.switchRenderingOn();
68
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
69
    hiddenView.removeAllViews();
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  void enterScreen(final RubikActivity act)
75
    {
76
    act.switchRenderingOff();
77
    mWeakAct = new WeakReference<>(act);
78
    mScreenWidth = act.getScreenWidthInPixels();
79
    int screenHeight= act.getScreenHeightInPixels();
80
    int upperBarHeight = act.getHeightUpperBar();
81
    int lowerBarHeight = act.getHeightLowerBar();
82

    
83
    computeRowAndColCounts();
84

    
85
    float titleSize = mScreenWidth*RubikActivity.TITLE_TEXT_SIZE;
86
    LayoutInflater inflater = act.getLayoutInflater();
87

    
88
    // TOP ////////////////////////////
89
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
90
    layoutTop.removeAllViews();
91
    TextView label = (TextView)inflater.inflate(R.layout.upper_text, null);
92
    label.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
93
    label.setText(R.string.app_name);
94
    layoutTop.addView(label);
95

    
96
    // BOTTOM /////////////////////////
97
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
98
    layoutBot.removeAllViews();
99

    
100
    float D = 0.20f;
101
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( (int)(mScreenWidth*     D ),LinearLayout.LayoutParams.MATCH_PARENT);
102
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( (int)(mScreenWidth*(1-2*D)),LinearLayout.LayoutParams.MATCH_PARENT);
103

    
104
    LinearLayout layoutLeft = new LinearLayout(act);
105

    
106
    layoutLeft.setLayoutParams(params1);
107
    RelativeLayout layoutMid = (RelativeLayout)inflater.inflate(R.layout.play_bottom_bar, null);
108
    layoutMid.setLayoutParams(params2);
109
    LinearLayout layoutRight = new LinearLayout(act);
110
    layoutRight.setLayoutParams(params1);
111

    
112
    setupAboutButton(act);
113
    setupUpdatesButtonAndBubble(act,layoutMid);
114
    setupExitButton(act);
115

    
116
    layoutLeft.addView(mAboutButton);
117
    layoutRight.addView(mExitButton);
118

    
119
    layoutBot.addView(layoutLeft);
120
    layoutBot.addView(layoutMid);
121
    layoutBot.addView(layoutRight);
122

    
123
    // POPUP //////////////////////////
124
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
125
    hiddenView.removeAllViews();
126

    
127
    ViewGroup.LayoutParams paramsMid = hiddenView.getLayoutParams();
128
    paramsMid.height = (int)(screenHeight-upperBarHeight-lowerBarHeight);
129
    hiddenView.setLayoutParams(paramsMid);
130

    
131
    ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
132
    hiddenView.addView(scroll);
133

    
134
    setupScrollingObjects(act,scroll,mScreenWidth);
135
    }
136

    
137
//////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void setupAboutButton(final RubikActivity act)
140
    {
141
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
142
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
143
    mAboutButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
144

    
145
    mAboutButton.setOnClickListener( new View.OnClickListener()
146
      {
147
      @Override
148
      public void onClick(View v)
149
        {
150
        RubikDialogAbout aDiag = new RubikDialogAbout();
151
        aDiag.show(act.getSupportFragmentManager(), null);
152
        }
153
      });
154
    }
155

    
156
//////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private void setupExitButton(final RubikActivity act)
159
    {
160
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_exit,R.drawable.ui_medium_exit, R.drawable.ui_big_exit, R.drawable.ui_huge_exit);
161
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
162
    mExitButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
163

    
164
    mExitButton.setOnClickListener( new View.OnClickListener()
165
      {
166
      @Override
167
      public void onClick(View v)
168
        {
169
        act.finish();
170
        }
171
      });
172
    }
173

    
174
//////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout)
177
    {
178
    TypedValue value = new TypedValue();
179
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, value, true);
180
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
181
    ImageButton updatesButton = relLayout.findViewById(R.id.buttonUpdates);
182
    updatesButton.setImageResource(icon);
183
    updatesButton.setBackgroundResource(value.resourceId);
184

    
185
    updatesButton.setOnClickListener( new View.OnClickListener()
186
      {
187
      @Override
188
      public void onClick(View v)
189
        {
190
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
191
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
192
        }
193
      });
194

    
195
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
196
    mBubbleUpdates.setVisibility(View.INVISIBLE);
197

    
198
    RubikNetwork network = RubikNetwork.getInstance();
199
    network.signUpForUpdates(this);
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  private void setupScrollingObjects(final RubikActivity act, final ScrollView view, final float width)
205
    {
206
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
207
    int cubeSize = (int)(width/NUM_COLUMNS) - 2*margin;
208

    
209
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
210

    
211
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
212
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
213

    
214
    objectGrid.setColumnCount(mColCount);
215
    objectGrid.setRowCount(mRowCount);
216

    
217
    int[] nextInRow = new int[mRowCount];
218

    
219
    for(int row=0; row<mRowCount; row++)
220
      {
221
      rowSpecs[row] = GridLayout.spec(row);
222
      nextInRow[row]= 0;
223
      }
224
    for(int col=0; col<mColCount; col++)
225
      {
226
      colSpecs[col] = GridLayout.spec(col);
227
      }
228

    
229
    MainEntryList list = MainEntryList.getInstance();
230

    
231
    for(int index=0; index<mNumObjects; index++)
232
      {
233
      MainEntry entry = list.getEntry(index);
234
      int row = index/NUM_COLUMNS;
235
      ImageButton button = new ImageButton(act);
236
      if( entry!=null ) entry.setIconTo(act,button);
237

    
238
      button.setOnClickListener( new View.OnClickListener()
239
        {
240
        @Override
241
        public void onClick(View v)
242
          {
243
          entry.onClick(act);
244
          }
245
        });
246

    
247
      GridLayout.Spec rowS = rowSpecs[row];
248
      GridLayout.Spec colS = colSpecs[nextInRow[row]];
249
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowS,colS);
250
      params.bottomMargin = margin;
251
      params.topMargin    = margin;
252
      params.leftMargin   = margin;
253
      params.rightMargin  = margin;
254

    
255
      params.width = cubeSize;
256
      params.height= cubeSize;
257

    
258
      nextInRow[row]++;
259

    
260
      objectGrid.addView(button, params);
261
      }
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public void savePreferences(SharedPreferences.Editor editor)
267
    {
268
    editor.putInt("play_LevelValue", mLevelValue );
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public void restorePreferences(SharedPreferences preferences)
274
    {
275
    mLevelValue = preferences.getInt("play_LevelValue", 0);
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public int getLevel()
281
    {
282
    return mLevelValue;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public void recreateListOfObjects()
288
    {
289
    computeRowAndColCounts();
290
    RubikActivity act = mWeakAct.get();
291
    LayoutInflater inflater = act.getLayoutInflater();
292

    
293
    act.runOnUiThread(new Runnable()
294
      {
295
      @Override
296
      public void run()
297
        {
298
        LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
299
        hiddenView.removeAllViews();
300
        ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
301
        hiddenView.addView(scroll);
302
        setupScrollingObjects(act,scroll,mScreenWidth);
303

    
304
        mCurrentBubbleNumber--;
305
        updateNumberOfNewObjects();
306
        }
307
      });
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  private void computeRowAndColCounts()
313
    {
314
    MainEntryList list = MainEntryList.getInstance();
315
    mNumObjects = list.getNumOfEntries();
316
    mRowCount = (mNumObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
317
    mColCount = NUM_COLUMNS;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  private void updateNumberOfNewObjects()
323
    {
324
    if( mCurrentBubbleNumber>0 )
325
      {
326
      String shownNum = String.valueOf(mCurrentBubbleNumber);
327
      mBubbleUpdates.setText(shownNum);
328
      mBubbleUpdates.setVisibility(View.VISIBLE);
329
      int height = (int)(0.05f*mScreenWidth);
330
      mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
331
      }
332
    else
333
      {
334
      mBubbleUpdates.setVisibility(View.INVISIBLE);
335
      }
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  public void receiveUpdate(RubikUpdates updates)
341
    {
342
    Activity act = mWeakAct.get();
343

    
344
    if( act!=null )
345
      {
346
      mCurrentBubbleNumber = updates.getCompletedNumber();
347

    
348
      act.runOnUiThread(new Runnable()
349
        {
350
        @Override
351
        public void run()
352
          {
353
          updateNumberOfNewObjects();
354
          }
355
        });
356
      }
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  public void errorUpdate()
362
    {
363
    android.util.Log.e("D", "Screen: Error receiving update");
364
    }
365
  }
(6-6/11)