Project

General

Profile

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

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

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.RubikObject;
46
import org.distorted.objects.RubikObjectList;
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;
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
    int numObjects = RubikObjectList.getNumObjects();
79
    mScreenWidth = act.getScreenWidthInPixels();
80
    int screenHeight= act.getScreenHeightInPixels();
81
    int upperBarHeight = act.getHeightUpperBar();
82
    int lowerBarHeight = act.getHeightLowerBar();
83

    
84
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
85
    mColCount = NUM_COLUMNS;
86

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

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

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

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

    
106
    LinearLayout layoutLeft = new LinearLayout(act);
107

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

    
114
    setupAboutButton(act);
115
    setupUpdatesButtonAndBubble(act,layoutMid);
116
    setupExitButton(act);
117

    
118
    layoutLeft.addView(mAboutButton);
119
    layoutRight.addView(mExitButton);
120

    
121
    layoutBot.addView(layoutLeft);
122
    layoutBot.addView(layoutMid);
123
    layoutBot.addView(layoutRight);
124

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

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

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

    
136
    setupScrollingObjects(act,scroll,mScreenWidth);
137
    }
138

    
139
//////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private void setupAboutButton(final RubikActivity act)
142
    {
143
    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);
144
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
145
    mAboutButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
146

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

    
158
//////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  private void setupExitButton(final RubikActivity act)
161
    {
162
    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);
163
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
164
    mExitButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
165

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

    
176
//////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout)
179
    {
180
    TypedValue value = new TypedValue();
181
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, value, true);
182
    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);
183
    ImageButton updatesButton = relLayout.findViewById(R.id.buttonUpdates);
184
    updatesButton.setImageResource(icon);
185
    updatesButton.setBackgroundResource(value.resourceId);
186

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

    
197
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
198
    mBubbleUpdates.setVisibility(View.INVISIBLE);
199

    
200
    RubikNetwork network = RubikNetwork.getInstance();
201
    network.signUpForUpdates(this);
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

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

    
211
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
212

    
213
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
214
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
215

    
216
    objectGrid.setColumnCount(mColCount);
217
    objectGrid.setRowCount(mRowCount);
218

    
219
    int[] nextInRow = new int[mRowCount];
220

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

    
231
    int numObjects = RubikObjectList.getNumObjects();
232

    
233
    for(int object=0; object<numObjects; object++)
234
      {
235
      final int ordinal = object;
236
      RubikObject rObject = RubikObjectList.getObject(ordinal);
237
      int row = object/NUM_COLUMNS;
238
      ImageButton button = new ImageButton(act);
239
      if( rObject!=null ) rObject.setIconTo(act,button);
240

    
241
      button.setOnClickListener( new View.OnClickListener()
242
        {
243
        @Override
244
        public void onClick(View v)
245
          {
246
          // TODO
247
          android.util.Log.e("D", "object "+ordinal+" clicked");
248
          }
249
        });
250

    
251
      GridLayout.Spec rowS = rowSpecs[row];
252
      GridLayout.Spec colS = colSpecs[nextInRow[row]];
253
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowS,colS);
254
      params.bottomMargin = margin;
255
      params.topMargin    = margin;
256
      params.leftMargin   = margin;
257
      params.rightMargin  = margin;
258

    
259
      params.width = cubeSize;
260
      params.height= cubeSize;
261

    
262
      nextInRow[row]++;
263

    
264
      objectGrid.addView(button, params);
265
      }
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  public void savePreferences(SharedPreferences.Editor editor)
271
    {
272
    editor.putInt("play_LevelValue", mLevelValue );
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  public void restorePreferences(SharedPreferences preferences)
278
    {
279
    mLevelValue = preferences.getInt("play_LevelValue", 0);
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public int getLevel()
285
    {
286
    return mLevelValue;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  public void recreateListOfObjects()
292
    {
293
    int numObjects = RubikObjectList.getNumObjects();
294
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
295
    mColCount = NUM_COLUMNS;
296

    
297
    RubikActivity act = mWeakAct.get();
298
    LayoutInflater inflater = act.getLayoutInflater();
299

    
300
    act.runOnUiThread(new Runnable()
301
      {
302
      @Override
303
      public void run()
304
        {
305
        LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
306
        hiddenView.removeAllViews();
307
        ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
308
        hiddenView.addView(scroll);
309
        setupScrollingObjects(act,scroll,mScreenWidth);
310

    
311
        mCurrentBubbleNumber--;
312
        updateNumberOfNewObjects();
313
        }
314
      });
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public void receiveUpdate(RubikUpdates updates)
338
    {
339
    Activity act = mWeakAct.get();
340

    
341
    if( act!=null )
342
      {
343
      mCurrentBubbleNumber = updates.getCompletedNumber();
344

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

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