Project

General

Profile

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

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

1 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fcd5b990 Leszek Koltunski
package org.distorted.screens;
21 211b48f2 Leszek Koltunski
22 179f7189 Leszek Koltunski
import java.lang.ref.WeakReference;
23
24
import android.app.Activity;
25 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
26 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
27 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
28
import android.view.View;
29 85038b84 Leszek Koltunski
import android.view.ViewGroup;
30 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
31 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
32
import android.widget.LinearLayout;
33 d6e7c7fb Leszek Koltunski
import android.widget.RelativeLayout;
34 85038b84 Leszek Koltunski
import android.widget.ScrollView;
35 10373dc7 Leszek Koltunski
import android.widget.TextView;
36 211b48f2 Leszek Koltunski
37 5f3801d3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogUpdates;
38 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
39
import org.distorted.external.RubikUpdates;
40 3f7a4363 Leszek Koltunski
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
44 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
45 c5fca790 Leszek Koltunski
import org.distorted.objects.MainEntry;
46
import org.distorted.objects.MainEntryList;
47 211b48f2 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 85038b84 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenAbstract implements RubikNetwork.Updatee
51 211b48f2 Leszek Koltunski
  {
52 6e3fcb91 Leszek Koltunski
  public static final int NUM_COLUMNS  = 5;
53 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
54 211b48f2 Leszek Koltunski
55 113d9d67 Leszek Koltunski
  private TransparentImageButton mAboutButton, mExitButton;
56 10373dc7 Leszek Koltunski
  private TextView mBubbleUpdates;
57 85038b84 Leszek Koltunski
  private int mCurrentBubbleNumber;
58 85b09df4 Leszek Koltunski
  private int mLevelValue;
59 c5fca790 Leszek Koltunski
  private int mColCount, mRowCount, mNumObjects;
60 cc8ab8e3 Leszek Koltunski
  private float mScreenWidth;
61 179f7189 Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
62 255492a0 Leszek Koltunski
63 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
66 211b48f2 Leszek Koltunski
    {
67 85038b84 Leszek Koltunski
    act.switchRenderingOn();
68
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
69
    hiddenView.removeAllViews();
70 211b48f2 Leszek Koltunski
    }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
75 211b48f2 Leszek Koltunski
    {
76 85038b84 Leszek Koltunski
    act.switchRenderingOff();
77 179f7189 Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
78 9c39179e Leszek Koltunski
    mScreenWidth = act.getScreenWidthInPixels();
79 cc8ab8e3 Leszek Koltunski
    int screenHeight= act.getScreenHeightInPixels();
80
    int upperBarHeight = act.getHeightUpperBar();
81
    int lowerBarHeight = act.getHeightLowerBar();
82 e3c74c0f Leszek Koltunski
83 c5fca790 Leszek Koltunski
    computeRowAndColCounts();
84 e07c48a2 Leszek Koltunski
85 85038b84 Leszek Koltunski
    float titleSize = mScreenWidth*RubikActivity.TITLE_TEXT_SIZE;
86
    LayoutInflater inflater = act.getLayoutInflater();
87
88 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
89 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
90 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
91 85038b84 Leszek Koltunski
    TextView label = (TextView)inflater.inflate(R.layout.upper_text, null);
92
    label.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
93 876803e6 Leszek Koltunski
    label.setText(R.string.app_name);
94 85038b84 Leszek Koltunski
    layoutTop.addView(label);
95
96
    // BOTTOM /////////////////////////
97
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
98
    layoutBot.removeAllViews();
99
100 113d9d67 Leszek Koltunski
    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 85038b84 Leszek Koltunski
104 113d9d67 Leszek Koltunski
    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 85038b84 Leszek Koltunski
123
    // POPUP //////////////////////////
124
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
125
    hiddenView.removeAllViews();
126 85b09df4 Leszek Koltunski
127 85038b84 Leszek Koltunski
    ViewGroup.LayoutParams paramsMid = hiddenView.getLayoutParams();
128 cc8ab8e3 Leszek Koltunski
    paramsMid.height = (int)(screenHeight-upperBarHeight-lowerBarHeight);
129 85038b84 Leszek Koltunski
    hiddenView.setLayoutParams(paramsMid);
130 0254cfd7 Leszek Koltunski
131 85038b84 Leszek Koltunski
    ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
132
    hiddenView.addView(scroll);
133 211b48f2 Leszek Koltunski
134 cc8ab8e3 Leszek Koltunski
    setupScrollingObjects(act,scroll,mScreenWidth);
135 769d7b9f Leszek Koltunski
    }
136 211b48f2 Leszek Koltunski
137 8ab435b9 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
138 4c0cd600 Leszek Koltunski
139 113d9d67 Leszek Koltunski
  private void setupAboutButton(final RubikActivity act)
140 85038b84 Leszek Koltunski
    {
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 113d9d67 Leszek Koltunski
    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 85038b84 Leszek Koltunski
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 113d9d67 Leszek Koltunski
  private void setupExitButton(final RubikActivity act)
159 85038b84 Leszek Koltunski
    {
160 113d9d67 Leszek Koltunski
    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 85038b84 Leszek Koltunski
164 113d9d67 Leszek Koltunski
    mExitButton.setOnClickListener( new View.OnClickListener()
165 85038b84 Leszek Koltunski
      {
166
      @Override
167
      public void onClick(View v)
168
        {
169 113d9d67 Leszek Koltunski
        act.finish();
170 85038b84 Leszek Koltunski
        }
171
      });
172
    }
173
174
//////////////////////////////////////////////////////////////////////////////////////////////////
175
176 113d9d67 Leszek Koltunski
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout)
177 85038b84 Leszek Koltunski
    {
178 113d9d67 Leszek Koltunski
    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 cc8ab8e3 Leszek Koltunski
    ImageButton updatesButton = relLayout.findViewById(R.id.buttonUpdates);
182
    updatesButton.setImageResource(icon);
183
    updatesButton.setBackgroundResource(value.resourceId);
184 85038b84 Leszek Koltunski
185 cc8ab8e3 Leszek Koltunski
    updatesButton.setOnClickListener( new View.OnClickListener()
186 85038b84 Leszek Koltunski
      {
187
      @Override
188
      public void onClick(View v)
189
        {
190 113d9d67 Leszek Koltunski
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
191
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
192 85038b84 Leszek Koltunski
        }
193
      });
194 113d9d67 Leszek Koltunski
195
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
196
    mBubbleUpdates.setVisibility(View.INVISIBLE);
197
198
    RubikNetwork network = RubikNetwork.getInstance();
199
    network.signUpForUpdates(this);
200 85038b84 Leszek Koltunski
    }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204 cc8ab8e3 Leszek Koltunski
  private void setupScrollingObjects(final RubikActivity act, final ScrollView view, final float width)
205 85038b84 Leszek Koltunski
    {
206 cc8ab8e3 Leszek Koltunski
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
207
    int cubeSize = (int)(width/NUM_COLUMNS) - 2*margin;
208 85038b84 Leszek Koltunski
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 c5fca790 Leszek Koltunski
    MainEntryList list = MainEntryList.getInstance();
230 85038b84 Leszek Koltunski
231 c5fca790 Leszek Koltunski
    for(int index=0; index<mNumObjects; index++)
232 85038b84 Leszek Koltunski
      {
233 c5fca790 Leszek Koltunski
      MainEntry entry = list.getEntry(index);
234
      int row = index/NUM_COLUMNS;
235 85038b84 Leszek Koltunski
      ImageButton button = new ImageButton(act);
236 c5fca790 Leszek Koltunski
      if( entry!=null ) entry.setIconTo(act,button);
237 85038b84 Leszek Koltunski
238
      button.setOnClickListener( new View.OnClickListener()
239
        {
240
        @Override
241
        public void onClick(View v)
242
          {
243 c5fca790 Leszek Koltunski
          entry.onClick(act);
244 85038b84 Leszek Koltunski
          }
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 cc8ab8e3 Leszek Koltunski
      params.width = cubeSize;
256
      params.height= cubeSize;
257 6e3fcb91 Leszek Koltunski
258 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
259
260
      objectGrid.addView(button, params);
261 769d7b9f Leszek Koltunski
      }
262 4c0cd600 Leszek Koltunski
    }
263
264 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266
  public void savePreferences(SharedPreferences.Editor editor)
267
    {
268 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
269 211b48f2 Leszek Koltunski
    }
270
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273
  public void restorePreferences(SharedPreferences preferences)
274
    {
275 d38a302b Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
276 211b48f2 Leszek Koltunski
    }
277
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
280 dca3888a Leszek Koltunski
  public int getLevel()
281 211b48f2 Leszek Koltunski
    {
282 85b09df4 Leszek Koltunski
    return mLevelValue;
283 211b48f2 Leszek Koltunski
    }
284
285 84d746d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
286
287 cc8ab8e3 Leszek Koltunski
  public void recreateListOfObjects()
288 84d746d7 Leszek Koltunski
    {
289 c5fca790 Leszek Koltunski
    computeRowAndColCounts();
290 85038b84 Leszek Koltunski
    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 cc8ab8e3 Leszek Koltunski
        setupScrollingObjects(act,scroll,mScreenWidth);
303 85038b84 Leszek Koltunski
304
        mCurrentBubbleNumber--;
305
        updateNumberOfNewObjects();
306
        }
307
      });
308 84d746d7 Leszek Koltunski
    }
309
310 c5fca790 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 85038b84 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
  public void receiveUpdate(RubikUpdates updates)
341
    {
342 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
343 10373dc7 Leszek Koltunski
344 179f7189 Leszek Koltunski
    if( act!=null )
345 903c7bbc Leszek Koltunski
      {
346 85038b84 Leszek Koltunski
      mCurrentBubbleNumber = updates.getCompletedNumber();
347
348 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
349
        {
350
        @Override
351
        public void run()
352
          {
353 85038b84 Leszek Koltunski
          updateNumberOfNewObjects();
354 179f7189 Leszek Koltunski
          }
355
        });
356 903c7bbc Leszek Koltunski
      }
357 63dd19c4 Leszek Koltunski
    }
358
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
361
  public void errorUpdate()
362
    {
363 c99db493 Leszek Koltunski
    android.util.Log.e("D", "Screen: Error receiving update");
364 63dd19c4 Leszek Koltunski
    }
365 211b48f2 Leszek Koltunski
  }