Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 46be3ddf

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 4c0cd600 Leszek Koltunski
import android.content.Context;
26 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
27 b600ccd9 Leszek Koltunski
import android.content.res.Resources;
28 4c0cd600 Leszek Koltunski
import android.graphics.drawable.BitmapDrawable;
29 255492a0 Leszek Koltunski
import android.os.Build;
30 e03e0352 Leszek Koltunski
import android.os.Bundle;
31 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
32 255492a0 Leszek Koltunski
import android.view.Gravity;
33 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.widget.Button;
36 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
37 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
38
import android.widget.LinearLayout;
39 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
40 d6e7c7fb Leszek Koltunski
import android.widget.RelativeLayout;
41 10373dc7 Leszek Koltunski
import android.widget.TextView;
42 211b48f2 Leszek Koltunski
43 5f3801d3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogUpdates;
44 63dd19c4 Leszek Koltunski
import org.distorted.network.RubikNetwork;
45
import org.distorted.network.RubikUpdates;
46 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
47 3f7a4363 Leszek Koltunski
48
import org.distorted.main.R;
49
import org.distorted.main.RubikActivity;
50 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
51 a8576d91 Leszek Koltunski
import org.distorted.dialogs.RubikDialogPattern;
52 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogScores;
53 234a7582 Leszek Koltunski
import org.distorted.dialogs.RubikDialogTutorial;
54 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentButton;
55
import org.distorted.helpers.TransparentImageButton;
56 6a083c6a Leszek Koltunski
import org.distorted.network.RubikScores;
57 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
58
import org.distorted.objects.RubikObjectList;
59 211b48f2 Leszek Koltunski
60 b600ccd9 Leszek Koltunski
import static android.view.View.inflate;
61 88d28110 Leszek Koltunski
62 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 63dd19c4 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
65 211b48f2 Leszek Koltunski
  {
66 6e3fcb91 Leszek Koltunski
  public static final int NUM_COLUMNS  = 5;
67 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
68 211b48f2 Leszek Koltunski
69 7cf2637d Leszek Koltunski
  private static final int[] BUTTON_LABELS = { R.string.scores,
70
                                               R.string.patterns,
71
                                               R.string.solver,
72
                                               R.string.tutorials,
73
                                               R.string.about };
74
75 e03e0352 Leszek Koltunski
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
76 de62316a Leszek Koltunski
  private static final float LAST_BUTTON = 1.5f;
77 dc78f395 Leszek Koltunski
  private static final int[] mLocation = new int[2];
78 e03e0352 Leszek Koltunski
79 0873a75a Leszek Koltunski
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
80 033100af Leszek Koltunski
  private TransparentButton mPlayButton;
81 0254cfd7 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
82 400ff34d Leszek Koltunski
  private LinearLayout mPlayLayout;
83 10373dc7 Leszek Koltunski
  private TextView mBubbleUpdates;
84 0254cfd7 Leszek Koltunski
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
85 85b09df4 Leszek Koltunski
  private int mLevelValue;
86 e07c48a2 Leszek Koltunski
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
87 598de3ee Leszek Koltunski
  private int mColCount, mRowCount, mMaxRowCount;
88 255492a0 Leszek Koltunski
  private int mUpperBarHeight;
89 dd874ae8 Leszek Koltunski
  private boolean mShouldReactToEndOfScrambling;
90 6e3fcb91 Leszek Koltunski
  private int mBottomHeight;
91 9c39179e Leszek Koltunski
  private float mScreenWidth;
92 179f7189 Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
93 255492a0 Leszek Koltunski
94 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
97 211b48f2 Leszek Koltunski
    {
98 85b09df4 Leszek Koltunski
99 211b48f2 Leszek Koltunski
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
104 211b48f2 Leszek Koltunski
    {
105 179f7189 Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
106 d433b50e Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
107 9c39179e Leszek Koltunski
    mScreenWidth = act.getScreenWidthInPixels();
108 255492a0 Leszek Koltunski
    mUpperBarHeight = act.getHeightUpperBar();
109 a8576d91 Leszek Koltunski
110 9c39179e Leszek Koltunski
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
111
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
112
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
113 e3c74c0f Leszek Koltunski
114 d433b50e Leszek Koltunski
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
115 287e91a6 Leszek Koltunski
    mColCount = NUM_COLUMNS;
116 e07c48a2 Leszek Koltunski
117 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
118 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
119 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
120 85b09df4 Leszek Koltunski
121 9c39179e Leszek Koltunski
    setupObjectButton(act,mScreenWidth);
122 85b09df4 Leszek Koltunski
    layoutTop.addView(mObjButton);
123 0254cfd7 Leszek Koltunski
124 9c39179e Leszek Koltunski
    setupMenuButton(act,mScreenWidth);
125 0254cfd7 Leszek Koltunski
    layoutTop.addView(mMenuButton);
126
127 9c39179e Leszek Koltunski
    setupPlayButton(act,mScreenWidth);
128 85b09df4 Leszek Koltunski
    layoutTop.addView(mPlayButton);
129 211b48f2 Leszek Koltunski
130 dd874ae8 Leszek Koltunski
    setupSolveButton(act);
131
    setupScrambleButton(act);
132
    createBottomPane(act,mSolveButton,mScrambleButton);
133 769d7b9f Leszek Koltunski
    }
134 211b48f2 Leszek Koltunski
135 8ab435b9 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
136 4c0cd600 Leszek Koltunski
137 ad0c8e0e Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float width)
138 769d7b9f Leszek Koltunski
    {
139 c651024f Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
140 6e3fcb91 Leszek Koltunski
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
141 e07c48a2 Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
142 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
143
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
144 769d7b9f Leszek Koltunski
145
    mObjButton.setOnClickListener( new View.OnClickListener()
146
      {
147
      @Override
148
      public void onClick(View view)
149 4c0cd600 Leszek Koltunski
        {
150 2da68298 Leszek Koltunski
        if( mObjectPopup==null )
151
          {
152
          float width = act.getScreenWidthInPixels();
153
          float height= act.getScreenHeightInPixels();
154
          setupObjectWindow(act,width,height);
155
          }
156
157
        if( act.getControl().isUINotBlocked())
158 a42e25a6 Leszek Koltunski
          {
159 598de3ee Leszek Koltunski
          int rowCount = Math.min(mMaxRowCount,mRowCount);
160 c5b4af4a Leszek Koltunski
          View popupView = mObjectPopup.getContentView();
161
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
162 1071fb69 Leszek Koltunski
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
163 c5b4af4a Leszek Koltunski
          }
164 769d7b9f Leszek Koltunski
        }
165
      });
166
    }
167 4c0cd600 Leszek Koltunski
168 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 2da68298 Leszek Koltunski
  private void setupPlayButton(final RubikActivity act, final float width)
171 85b09df4 Leszek Koltunski
    {
172 c651024f Leszek Koltunski
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
173 d90c55cc Leszek Koltunski
174 033100af Leszek Koltunski
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
175 85b09df4 Leszek Koltunski
176
    mPlayButton.setOnClickListener( new View.OnClickListener()
177
      {
178
      @Override
179 0254cfd7 Leszek Koltunski
      public void onClick(View view)
180 85b09df4 Leszek Koltunski
        {
181 2da68298 Leszek Koltunski
         if( mPlayPopup==null )
182
          {
183
          float width = act.getScreenWidthInPixels();
184
          setupPlayWindow(act,width);
185
          }
186
187
        if( act.getControl().isUINotBlocked())
188 6e194411 Leszek Koltunski
          {
189 d38a302b Leszek Koltunski
          adjustSolvedIcons();
190 2da68298 Leszek Koltunski
          float height= act.getScreenHeightInPixels();
191
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
192 c5b4af4a Leszek Koltunski
          View popupView = mPlayPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194 400ff34d Leszek Koltunski
          final int object  = RubikObjectList.getCurrObject();
195
          final int dbLevel = RubikObjectList.getDBLevel(object);
196 0a7aa15b Leszek Koltunski
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
197 d90c55cc Leszek Koltunski
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
198 255492a0 Leszek Koltunski
          final int realHeight = Math.min(popupHeight,maxHeight);
199
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
200 6fad862b Leszek Koltunski
          }
201 85b09df4 Leszek Koltunski
        }
202
      });
203
    }
204
205 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207 ad0c8e0e Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float width)
208 e31abc1e Leszek Koltunski
    {
209 c651024f Leszek Koltunski
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
210 0254cfd7 Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_menu,R.drawable.ui_medium_menu, R.drawable.ui_big_menu, R.drawable.ui_huge_menu);
211 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
212
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
213 e31abc1e Leszek Koltunski
214 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
215 e31abc1e Leszek Koltunski
      {
216
      @Override
217
      public void onClick(View view)
218
        {
219 2da68298 Leszek Koltunski
        if( mMenuPopup==null )
220
          {
221
          float width = act.getScreenWidthInPixels();
222
          setupMenuWindow(act,width);
223
          }
224
225
        if( act.getControl().isUINotBlocked())
226 e03e0352 Leszek Koltunski
          {
227 c5b4af4a Leszek Koltunski
          View popupView = mMenuPopup.getContentView();
228
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
229 255492a0 Leszek Koltunski
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
230 6fad862b Leszek Koltunski
          }
231 e31abc1e Leszek Koltunski
        }
232
      });
233
    }
234
235 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237 598de3ee Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
238 4c0cd600 Leszek Koltunski
    {
239 6e3fcb91 Leszek Koltunski
    int cubeWidth = (int)(width/9);
240 598de3ee Leszek Koltunski
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
241
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
242 0b162619 Leszek Koltunski
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
243 598de3ee Leszek Koltunski
244 8d1da3f1 Leszek Koltunski
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
245 b600ccd9 Leszek Koltunski
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
246 e07c48a2 Leszek Koltunski
247
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
248
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
249
250 92843d3b Leszek Koltunski
    objectGrid.setColumnCount(mColCount);
251
    objectGrid.setRowCount(mRowCount);
252 e07c48a2 Leszek Koltunski
253 d6e7c7fb Leszek Koltunski
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
254
    setupBottomLayout(act,bottomLayout);
255 0b162619 Leszek Koltunski
256 b600ccd9 Leszek Koltunski
    mObjectPopup = new PopupWindow(act);
257
    mObjectPopup.setFocusable(true);
258
    mObjectPopup.setContentView(view);
259
260 0501a4b8 Leszek Koltunski
    int[] nextInRow = new int[mRowCount];
261 fa679111 Leszek Koltunski
262 e07c48a2 Leszek Koltunski
    for(int row=0; row<mRowCount; row++)
263
      {
264
      rowSpecs[row] = GridLayout.spec(row);
265 0501a4b8 Leszek Koltunski
      nextInRow[row]= 0;
266 e07c48a2 Leszek Koltunski
      }
267
    for(int col=0; col<mColCount; col++)
268
      {
269
      colSpecs[col] = GridLayout.spec(col);
270
      }
271 769d7b9f Leszek Koltunski
272 d433b50e Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
273
274
    for(int object=0; object<numObjects; object++)
275 769d7b9f Leszek Koltunski
      {
276 d433b50e Leszek Koltunski
      final RubikObject robject = RubikObjectList.getObject(object);
277 6e3fcb91 Leszek Koltunski
      int icons = robject==null ? 0 : robject.getIconID();
278 287e91a6 Leszek Koltunski
      int row = object/NUM_COLUMNS;
279 400ff34d Leszek Koltunski
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
280 769d7b9f Leszek Koltunski
281 7ac0ee88 Leszek Koltunski
      ImageButton button = new ImageButton(act);
282
      button.setBackgroundResource(icons);
283
      button.setOnClickListener( new View.OnClickListener()
284 769d7b9f Leszek Koltunski
        {
285 7ac0ee88 Leszek Koltunski
        @Override
286
        public void onClick(View v)
287 769d7b9f Leszek Koltunski
          {
288 2afc6754 Leszek Koltunski
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
289 769d7b9f Leszek Koltunski
            {
290 400ff34d Leszek Koltunski
            RubikObjectList.setCurrObject(act,ordinal);
291
            act.changeObject(ordinal,true);
292 4c9947bd Leszek Koltunski
            if( mPlayLayout!=null ) adjustLevels(act);
293 dd1a65c1 Leszek Koltunski
            mMovesController.clearMoves(act);
294 769d7b9f Leszek Koltunski
            }
295
296 7ac0ee88 Leszek Koltunski
          mObjectPopup.dismiss();
297
          }
298
        });
299 e07c48a2 Leszek Koltunski
300 7ac0ee88 Leszek Koltunski
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
301
      params.bottomMargin = margin;
302
      params.topMargin    = margin;
303
      params.leftMargin   = margin;
304
      params.rightMargin  = margin;
305 fa679111 Leszek Koltunski
306 6e3fcb91 Leszek Koltunski
      params.width = cubeWidth;
307
      params.height= cubeWidth;
308
309 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
310
311
      objectGrid.addView(button, params);
312 769d7b9f Leszek Koltunski
      }
313 4c0cd600 Leszek Koltunski
    }
314
315 8d1da3f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
316
317 d6e7c7fb Leszek Koltunski
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
318 8d1da3f1 Leszek Koltunski
    {
319
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
320 d6e7c7fb Leszek Koltunski
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
321
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
322 8d1da3f1 Leszek Koltunski
323
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
324 d6e7c7fb Leszek Koltunski
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
325
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
326 8d1da3f1 Leszek Koltunski
327
    buttonTut.setImageResource(iconT);
328 d6e7c7fb Leszek Koltunski
    buttonDow.setImageResource(iconD);
329
    buttonInf.setImageResource(iconI);
330 8d1da3f1 Leszek Koltunski
331 6e3fcb91 Leszek Koltunski
    Resources res = act.getResources();
332 d6e7c7fb Leszek Koltunski
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
333 6e3fcb91 Leszek Koltunski
    mBottomHeight = bd.getIntrinsicHeight();
334
335 8d1da3f1 Leszek Koltunski
    TypedValue outValue = new TypedValue();
336
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
337
    buttonTut.setBackgroundResource(outValue.resourceId);
338 d6e7c7fb Leszek Koltunski
    buttonDow.setBackgroundResource(outValue.resourceId);
339
    buttonInf.setBackgroundResource(outValue.resourceId);
340 8d1da3f1 Leszek Koltunski
341
    buttonTut.setOnClickListener( new View.OnClickListener()
342
      {
343
      @Override
344
      public void onClick(View v)
345
        {
346
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
347
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
348
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
349
        }
350
      });
351
352 d6e7c7fb Leszek Koltunski
    buttonDow.setOnClickListener( new View.OnClickListener()
353
      {
354
      @Override
355
      public void onClick(View v)
356
        {
357 5f3801d3 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
358
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
359
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
360 d6e7c7fb Leszek Koltunski
        }
361
      });
362
363
    buttonInf.setOnClickListener( new View.OnClickListener()
364 8d1da3f1 Leszek Koltunski
      {
365
      @Override
366
      public void onClick(View v)
367
        {
368 f12e4de9 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
369 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
370
        act.switchConfig(currObject);
371 8d1da3f1 Leszek Koltunski
        }
372
      });
373 d6e7c7fb Leszek Koltunski
374 10373dc7 Leszek Koltunski
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
375
    mBubbleUpdates.setVisibility(View.INVISIBLE);
376
377 d6e7c7fb Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
378
    network.signUpForUpdates(this);
379 8d1da3f1 Leszek Koltunski
    }
380
381 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
382
383 ad0c8e0e Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width)
384 e03e0352 Leszek Koltunski
    {
385
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
386 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
387
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
388 e03e0352 Leszek Koltunski
389
    mMenuPopup = new PopupWindow(act);
390
    mMenuPopup.setContentView(layout);
391
    mMenuPopup.setFocusable(true);
392 c651024f Leszek Koltunski
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
393 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
394 e03e0352 Leszek Koltunski
395 0254cfd7 Leszek Koltunski
    mMenuLayoutWidth = (int)(width/2);
396
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
397
398 43162dfb Leszek Koltunski
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
399 e03e0352 Leszek Koltunski
400
    for(int i=0; i<NUM_BUTTONS; i++)
401
      {
402
      final int but = i;
403
      Button button = new Button(act);
404 43162dfb Leszek Koltunski
      button.setLayoutParams(p);
405 e03e0352 Leszek Koltunski
      button.setText(BUTTON_LABELS[i]);
406 88fb92ba Leszek Koltunski
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
407 e03e0352 Leszek Koltunski
408
      button.setOnClickListener( new View.OnClickListener()
409
        {
410
        @Override
411
        public void onClick(View v)
412
          {
413
          mMenuPopup.dismiss();
414 0254cfd7 Leszek Koltunski
          MenuAction(act,but);
415 e03e0352 Leszek Koltunski
          }
416
        });
417
418 e07c48a2 Leszek Koltunski
      menuLayout.addView(button);
419 e03e0352 Leszek Koltunski
      }
420 0254cfd7 Leszek Koltunski
    }
421 e03e0352 Leszek Koltunski
422 0254cfd7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
423
424
  private void setupPlayWindow(final RubikActivity act, final float width)
425
    {
426
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
427
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
428
    mPlayLayout = layout.findViewById(R.id.playGrid);
429
430
    mPlayLayoutWidth = (int)(width*0.4f);
431
432
    mPlayPopup = new PopupWindow(act);
433
    mPlayPopup.setContentView(layout);
434
    mPlayPopup.setFocusable(true);
435
436
    adjustLevels(act);
437 e03e0352 Leszek Koltunski
    }
438
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440
441 0254cfd7 Leszek Koltunski
  private void MenuAction(RubikActivity act, int button)
442 e03e0352 Leszek Koltunski
    {
443
    switch(button)
444
      {
445 8ab435b9 Leszek Koltunski
      case 0: Bundle sBundle = new Bundle();
446 400ff34d Leszek Koltunski
              int currObject = RubikObjectList.getCurrObject();
447
              sBundle.putInt("tab", currObject );
448 a8576d91 Leszek Koltunski
              sBundle.putBoolean("submitting", false);
449 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
450 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
451 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
452
              break;
453 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
454
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
455 e03e0352 Leszek Koltunski
              break;
456 b2a92941 Leszek Koltunski
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
457 e03e0352 Leszek Koltunski
              break;
458 b2a92941 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
459 234a7582 Leszek Koltunski
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
460 2971588c Leszek Koltunski
              break;
461 b2a92941 Leszek Koltunski
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
462 a8576d91 Leszek Koltunski
              aDiag.show(act.getSupportFragmentManager(), null);
463 e03e0352 Leszek Koltunski
              break;
464
      }
465
    }
466
467 46405bb4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
468
469 dd874ae8 Leszek Koltunski
  void setupSolveButton(final RubikActivity act)
470 46405bb4 Leszek Koltunski
    {
471 dd874ae8 Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
472 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
473
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
474 46405bb4 Leszek Koltunski
475 a8576d91 Leszek Koltunski
    mSolveButton.setOnClickListener( new View.OnClickListener()
476 46405bb4 Leszek Koltunski
      {
477 a8576d91 Leszek Koltunski
      @Override
478
      public void onClick(View v)
479
        {
480 2afc6754 Leszek Koltunski
        act.getControl().solveObject();
481 dd1a65c1 Leszek Koltunski
        mMovesController.clearMoves(act);
482 a8576d91 Leszek Koltunski
        }
483
      });
484 46405bb4 Leszek Koltunski
    }
485
486 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
487
488
  private void setupScrambleButton(final RubikActivity act)
489
    {
490
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble_new,R.drawable.ui_medium_cube_scramble_new, R.drawable.ui_big_cube_scramble_new, R.drawable.ui_huge_cube_scramble_new);
491 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
492
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
493 dd874ae8 Leszek Koltunski
494
    mScrambleButton.setOnClickListener( new View.OnClickListener()
495
      {
496
      @Override
497
      public void onClick(View v)
498
        {
499 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
500
        RubikObject object = RubikObjectList.getObject(currObject);
501 d433b50e Leszek Koltunski
        int numScrambles = object==null ? 0 : object.getNumScramble();
502 dd874ae8 Leszek Koltunski
        mShouldReactToEndOfScrambling = false;
503
        act.getControl().scrambleObject(numScrambles);
504
        }
505
      });
506
    }
507
508 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
509 3398a606 Leszek Koltunski
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
510
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
511
// going to be called)
512
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
513 211b48f2 Leszek Koltunski
514
  public void savePreferences(SharedPreferences.Editor editor)
515
    {
516 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
517
518 3398a606 Leszek Koltunski
    if( mObjectPopup!=null )
519
      {
520
      mObjectPopup.dismiss();
521
      mObjectPopup = null;
522
      }
523 87d86e5f Leszek Koltunski
524 3398a606 Leszek Koltunski
    if( mMenuPopup!=null )
525
      {
526
      mMenuPopup.dismiss();
527
      mMenuPopup = null;
528
      }
529
530
    if( mPlayPopup!=null )
531
      {
532
      mPlayPopup.dismiss();
533
      mPlayPopup = null;
534
      }
535 211b48f2 Leszek Koltunski
    }
536
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
539
  public void restorePreferences(SharedPreferences preferences)
540
    {
541 d38a302b Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
542 211b48f2 Leszek Koltunski
    }
543
544 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
545
546 400ff34d Leszek Koltunski
  public void setCurrObject(RubikActivity act)
547 53f23b64 Leszek Koltunski
    {
548 400ff34d Leszek Koltunski
    if( mPlayLayout!=null ) adjustLevels(act);
549 7b7d65ce Leszek Koltunski
    }
550
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
553
554
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
555
    {
556
    View topLayout = act.findViewById(R.id.relativeLayout);
557 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
558 00aa398a Leszek Koltunski
559
    if( topLayout!=null )
560
      {
561
      topLayout.getLocationOnScreen(mLocation);
562 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
563 00aa398a Leszek Koltunski
      }
564
    else
565
      {
566 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
567 00aa398a Leszek Koltunski
      }
568 255492a0 Leszek Koltunski
569 69a271f3 Leszek Koltunski
    try
570 255492a0 Leszek Koltunski
      {
571 69a271f3 Leszek Koltunski
      // if on Android 11 or we are fullscreen
572
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
573
        {
574
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
575
        window.update(view, w, h);
576
        }
577
      else  // Android 10 or below in pop-up mode or split-screen mode
578
        {
579
        view.getLocationOnScreen(mLocation);
580
        int width  = view.getWidth();
581
        int height = view.getHeight();
582
        int x = mLocation[0]+(width-w)/2;
583
        int y = mLocation[1]+height+yoff;
584
585
        window.showAsDropDown(view);
586
        window.update(x,y,w,h);
587
        }
588 255492a0 Leszek Koltunski
      }
589 69a271f3 Leszek Koltunski
    catch( IllegalArgumentException iae )
590 255492a0 Leszek Koltunski
      {
591 69a271f3 Leszek Koltunski
      // ignore, this means window is 'not attached to window manager' -
592
      // which most probably is because we are already exiting the app.
593 255492a0 Leszek Koltunski
      }
594
    }
595
596 ae77f661 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
597
598 d38a302b Leszek Koltunski
  private void adjustSolvedIcons()
599 ae77f661 Leszek Koltunski
    {
600 59cc16ae Leszek Koltunski
    if( mPlayLayout!=null )
601 ae77f661 Leszek Koltunski
      {
602 59cc16ae Leszek Koltunski
      int currObject = RubikObjectList.getCurrObject();
603
      int dbLevel = RubikObjectList.getDBLevel(currObject);
604
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
605
      RubikScores scores = RubikScores.getInstance();
606
607
      for(int i=0; i<numLevel; i++)
608
        {
609
        int level = i<numLevel-1 ? i+1 : dbLevel;
610
        Button button = (Button)mPlayLayout.getChildAt(i);
611
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
612
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
613
        }
614 ae77f661 Leszek Koltunski
      }
615
    }
616
617 255492a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
618 7b7d65ce Leszek Koltunski
619 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
620 7b7d65ce Leszek Koltunski
    {
621 400ff34d Leszek Koltunski
    int currObject = RubikObjectList.getCurrObject();
622
    int dbLevel = RubikObjectList.getDBLevel(currObject);
623
    RubikObject object = RubikObjectList.getObject(currObject);
624 d433b50e Leszek Koltunski
    int numScrambles = object==null ? 0 : object.getNumScramble();
625 0a7aa15b Leszek Koltunski
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
626 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
627 7b7d65ce Leszek Koltunski
628 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel-1; i++)
629 53f23b64 Leszek Koltunski
      {
630 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
631 53f23b64 Leszek Koltunski
      }
632
633 4fc7b1e6 Leszek Koltunski
    if( numLevel>0 )
634
      {
635
      levels[numLevel-1] = act.getString(R.string.level_full);
636
      }
637 00af5060 Leszek Koltunski
638 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
639
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
640 00af5060 Leszek Koltunski
      {
641
      mLevelValue=1;
642
      }
643 0254cfd7 Leszek Koltunski
644 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
645 c651024f Leszek Koltunski
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
646 de62316a Leszek Koltunski
    int padding  = (int)(width*RubikActivity.PADDING);
647
    int butWidth = mPlayLayoutWidth - 2*padding;
648
    int butHeight= (int)mMenuItemSize;
649
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
650 0254cfd7 Leszek Koltunski
651 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
652 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
653 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
654 0254cfd7 Leszek Koltunski
    pT.setMargins(margin, margin, margin, margin);
655 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
656 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
657
658
    mPlayLayout.removeAllViews();
659
660 d7e539d0 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
661
662 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
663 011fcfe0 Leszek Koltunski
      {
664 0a7aa15b Leszek Koltunski
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
665
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
666 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
667 00af5060 Leszek Koltunski
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
668 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
669
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
670
671 400ff34d Leszek Koltunski
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
672 d7e539d0 Leszek Koltunski
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
673
674 0254cfd7 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
675 011fcfe0 Leszek Koltunski
        {
676 0254cfd7 Leszek Koltunski
        @Override
677
        public void onClick(View v)
678
          {
679 2afc6754 Leszek Koltunski
          ObjectControl control = act.getControl();
680 6fad862b Leszek Koltunski
681 2afc6754 Leszek Koltunski
          if(control.isUINotBlocked())
682 6fad862b Leszek Koltunski
            {
683 598de3ee Leszek Koltunski
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
684 0a7aa15b Leszek Koltunski
            mLevelValue = level;
685 dd874ae8 Leszek Koltunski
            mShouldReactToEndOfScrambling = true;
686 2afc6754 Leszek Koltunski
            control.scrambleObject(scrambles);
687 6fad862b Leszek Koltunski
            }
688 0254cfd7 Leszek Koltunski
          }
689
        });
690 011fcfe0 Leszek Koltunski
691 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
692
      }
693 53f23b64 Leszek Koltunski
    }
694
695 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
696
697 dca3888a Leszek Koltunski
  public int getLevel()
698 211b48f2 Leszek Koltunski
    {
699 85b09df4 Leszek Koltunski
    return mLevelValue;
700 211b48f2 Leszek Koltunski
    }
701
702 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
703
704
  public boolean shouldReactToEndOfScrambling()
705
    {
706
    return mShouldReactToEndOfScrambling;
707
    }
708 63dd19c4 Leszek Koltunski
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710
711
  public void receiveUpdate(RubikUpdates updates)
712
    {
713 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
714 10373dc7 Leszek Koltunski
715 179f7189 Leszek Koltunski
    if( act!=null )
716 903c7bbc Leszek Koltunski
      {
717 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
718
        {
719
        @Override
720
        public void run()
721
          {
722 2c9ab085 Leszek Koltunski
          int num = updates.getCompletedNumber();
723 179f7189 Leszek Koltunski
724
          if( num>0 )
725
            {
726
            String shownNum = String.valueOf(num);
727
            mBubbleUpdates.setText(shownNum);
728
            mBubbleUpdates.setVisibility(View.VISIBLE);
729
            int height = (int)(0.05f*mScreenWidth);
730
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
731
            }
732
         else
733
            {
734
            mBubbleUpdates.setVisibility(View.INVISIBLE);
735
            }
736
          }
737
        });
738 903c7bbc Leszek Koltunski
      }
739 63dd19c4 Leszek Koltunski
    }
740
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742
743
  public void errorUpdate()
744
    {
745 c99db493 Leszek Koltunski
    android.util.Log.e("D", "Screen: Error receiving update");
746 63dd19c4 Leszek Koltunski
    }
747 211b48f2 Leszek Koltunski
  }