Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 1f3bc259

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 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.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 acabdd83 Leszek Koltunski
import org.distorted.external.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 314e9ff0 Leszek Koltunski
      final int ordinal = object;
277
      final RubikObject rObject = RubikObjectList.getObject(object);
278 287e91a6 Leszek Koltunski
      int row = object/NUM_COLUMNS;
279 7ac0ee88 Leszek Koltunski
      ImageButton button = new ImageButton(act);
280 eb985085 Leszek Koltunski
      if( rObject!=null ) rObject.setIconTo(act,button);
281 314e9ff0 Leszek Koltunski
282 7ac0ee88 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
283 769d7b9f Leszek Koltunski
        {
284 7ac0ee88 Leszek Koltunski
        @Override
285
        public void onClick(View v)
286 769d7b9f Leszek Koltunski
          {
287 2afc6754 Leszek Koltunski
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
288 769d7b9f Leszek Koltunski
            {
289 400ff34d Leszek Koltunski
            RubikObjectList.setCurrObject(act,ordinal);
290
            act.changeObject(ordinal,true);
291 4c9947bd Leszek Koltunski
            if( mPlayLayout!=null ) adjustLevels(act);
292 dd1a65c1 Leszek Koltunski
            mMovesController.clearMoves(act);
293 769d7b9f Leszek Koltunski
            }
294
295 1f3bc259 Leszek Koltunski
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
296 7ac0ee88 Leszek Koltunski
          }
297
        });
298 e07c48a2 Leszek Koltunski
299 7ac0ee88 Leszek Koltunski
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
300
      params.bottomMargin = margin;
301
      params.topMargin    = margin;
302
      params.leftMargin   = margin;
303
      params.rightMargin  = margin;
304 fa679111 Leszek Koltunski
305 6e3fcb91 Leszek Koltunski
      params.width = cubeWidth;
306
      params.height= cubeWidth;
307
308 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
309
310
      objectGrid.addView(button, params);
311 769d7b9f Leszek Koltunski
      }
312 4c0cd600 Leszek Koltunski
    }
313
314 8d1da3f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316 d6e7c7fb Leszek Koltunski
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
317 8d1da3f1 Leszek Koltunski
    {
318
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
319 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);
320
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
321 8d1da3f1 Leszek Koltunski
322
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
323 d6e7c7fb Leszek Koltunski
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
324
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
325 8d1da3f1 Leszek Koltunski
326
    buttonTut.setImageResource(iconT);
327 d6e7c7fb Leszek Koltunski
    buttonDow.setImageResource(iconD);
328
    buttonInf.setImageResource(iconI);
329 8d1da3f1 Leszek Koltunski
330 6e3fcb91 Leszek Koltunski
    Resources res = act.getResources();
331 d6e7c7fb Leszek Koltunski
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
332 6e3fcb91 Leszek Koltunski
    mBottomHeight = bd.getIntrinsicHeight();
333
334 8d1da3f1 Leszek Koltunski
    TypedValue outValue = new TypedValue();
335
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
336
    buttonTut.setBackgroundResource(outValue.resourceId);
337 d6e7c7fb Leszek Koltunski
    buttonDow.setBackgroundResource(outValue.resourceId);
338
    buttonInf.setBackgroundResource(outValue.resourceId);
339 8d1da3f1 Leszek Koltunski
340
    buttonTut.setOnClickListener( new View.OnClickListener()
341
      {
342
      @Override
343
      public void onClick(View v)
344
        {
345
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
346
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
347
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
348
        }
349
      });
350
351 d6e7c7fb Leszek Koltunski
    buttonDow.setOnClickListener( new View.OnClickListener()
352
      {
353
      @Override
354
      public void onClick(View v)
355
        {
356 5f3801d3 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
357
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
358
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
359 d6e7c7fb Leszek Koltunski
        }
360
      });
361
362
    buttonInf.setOnClickListener( new View.OnClickListener()
363 8d1da3f1 Leszek Koltunski
      {
364
      @Override
365
      public void onClick(View v)
366
        {
367 f12e4de9 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
368 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
369
        act.switchConfig(currObject);
370 8d1da3f1 Leszek Koltunski
        }
371
      });
372 d6e7c7fb Leszek Koltunski
373 10373dc7 Leszek Koltunski
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
374
    mBubbleUpdates.setVisibility(View.INVISIBLE);
375
376 d6e7c7fb Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
377
    network.signUpForUpdates(this);
378 8d1da3f1 Leszek Koltunski
    }
379
380 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
381
382 ad0c8e0e Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width)
383 e03e0352 Leszek Koltunski
    {
384
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
385 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
386
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
387 e03e0352 Leszek Koltunski
388
    mMenuPopup = new PopupWindow(act);
389
    mMenuPopup.setContentView(layout);
390
    mMenuPopup.setFocusable(true);
391 c651024f Leszek Koltunski
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
392 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
393 e03e0352 Leszek Koltunski
394 0254cfd7 Leszek Koltunski
    mMenuLayoutWidth = (int)(width/2);
395
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
396
397 43162dfb Leszek Koltunski
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
398 e03e0352 Leszek Koltunski
399
    for(int i=0; i<NUM_BUTTONS; i++)
400
      {
401
      final int but = i;
402
      Button button = new Button(act);
403 43162dfb Leszek Koltunski
      button.setLayoutParams(p);
404 e03e0352 Leszek Koltunski
      button.setText(BUTTON_LABELS[i]);
405 88fb92ba Leszek Koltunski
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
406 e03e0352 Leszek Koltunski
407
      button.setOnClickListener( new View.OnClickListener()
408
        {
409
        @Override
410
        public void onClick(View v)
411
          {
412
          mMenuPopup.dismiss();
413 0254cfd7 Leszek Koltunski
          MenuAction(act,but);
414 e03e0352 Leszek Koltunski
          }
415
        });
416
417 e07c48a2 Leszek Koltunski
      menuLayout.addView(button);
418 e03e0352 Leszek Koltunski
      }
419 0254cfd7 Leszek Koltunski
    }
420 e03e0352 Leszek Koltunski
421 0254cfd7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
422
423
  private void setupPlayWindow(final RubikActivity act, final float width)
424
    {
425
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
426
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
427
    mPlayLayout = layout.findViewById(R.id.playGrid);
428
429
    mPlayLayoutWidth = (int)(width*0.4f);
430
431
    mPlayPopup = new PopupWindow(act);
432
    mPlayPopup.setContentView(layout);
433
    mPlayPopup.setFocusable(true);
434
435
    adjustLevels(act);
436 e03e0352 Leszek Koltunski
    }
437
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439
440 0254cfd7 Leszek Koltunski
  private void MenuAction(RubikActivity act, int button)
441 e03e0352 Leszek Koltunski
    {
442
    switch(button)
443
      {
444 8ab435b9 Leszek Koltunski
      case 0: Bundle sBundle = new Bundle();
445 400ff34d Leszek Koltunski
              int currObject = RubikObjectList.getCurrObject();
446
              sBundle.putInt("tab", currObject );
447 a8576d91 Leszek Koltunski
              sBundle.putBoolean("submitting", false);
448 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
449 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
450 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
451
              break;
452 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
453
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
454 e03e0352 Leszek Koltunski
              break;
455 b2a92941 Leszek Koltunski
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
456 e03e0352 Leszek Koltunski
              break;
457 b2a92941 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
458 234a7582 Leszek Koltunski
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
459 2971588c Leszek Koltunski
              break;
460 b2a92941 Leszek Koltunski
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
461 a8576d91 Leszek Koltunski
              aDiag.show(act.getSupportFragmentManager(), null);
462 e03e0352 Leszek Koltunski
              break;
463
      }
464
    }
465
466 46405bb4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
467
468 dd874ae8 Leszek Koltunski
  void setupSolveButton(final RubikActivity act)
469 46405bb4 Leszek Koltunski
    {
470 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);
471 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
472
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
473 46405bb4 Leszek Koltunski
474 a8576d91 Leszek Koltunski
    mSolveButton.setOnClickListener( new View.OnClickListener()
475 46405bb4 Leszek Koltunski
      {
476 a8576d91 Leszek Koltunski
      @Override
477
      public void onClick(View v)
478
        {
479 2afc6754 Leszek Koltunski
        act.getControl().solveObject();
480 dd1a65c1 Leszek Koltunski
        mMovesController.clearMoves(act);
481 a8576d91 Leszek Koltunski
        }
482
      });
483 46405bb4 Leszek Koltunski
    }
484
485 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
486
487
  private void setupScrambleButton(final RubikActivity act)
488
    {
489
    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);
490 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
491
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
492 dd874ae8 Leszek Koltunski
493
    mScrambleButton.setOnClickListener( new View.OnClickListener()
494
      {
495
      @Override
496
      public void onClick(View v)
497
        {
498 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
499
        RubikObject object = RubikObjectList.getObject(currObject);
500 d433b50e Leszek Koltunski
        int numScrambles = object==null ? 0 : object.getNumScramble();
501 dd874ae8 Leszek Koltunski
        mShouldReactToEndOfScrambling = false;
502
        act.getControl().scrambleObject(numScrambles);
503
        }
504
      });
505
    }
506
507 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
508 3398a606 Leszek Koltunski
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
509
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
510
// going to be called)
511
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
512 211b48f2 Leszek Koltunski
513
  public void savePreferences(SharedPreferences.Editor editor)
514
    {
515 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
516
517 3398a606 Leszek Koltunski
    if( mObjectPopup!=null )
518
      {
519
      mObjectPopup.dismiss();
520
      mObjectPopup = null;
521
      }
522 87d86e5f Leszek Koltunski
523 3398a606 Leszek Koltunski
    if( mMenuPopup!=null )
524
      {
525
      mMenuPopup.dismiss();
526
      mMenuPopup = null;
527
      }
528
529
    if( mPlayPopup!=null )
530
      {
531
      mPlayPopup.dismiss();
532
      mPlayPopup = null;
533
      }
534 211b48f2 Leszek Koltunski
    }
535
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537
538
  public void restorePreferences(SharedPreferences preferences)
539
    {
540 d38a302b Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
541 211b48f2 Leszek Koltunski
    }
542
543 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
544
545 400ff34d Leszek Koltunski
  public void setCurrObject(RubikActivity act)
546 53f23b64 Leszek Koltunski
    {
547 400ff34d Leszek Koltunski
    if( mPlayLayout!=null ) adjustLevels(act);
548 7b7d65ce Leszek Koltunski
    }
549
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
552
553
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
554
    {
555
    View topLayout = act.findViewById(R.id.relativeLayout);
556 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
557 00aa398a Leszek Koltunski
558
    if( topLayout!=null )
559
      {
560
      topLayout.getLocationOnScreen(mLocation);
561 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
562 00aa398a Leszek Koltunski
      }
563
    else
564
      {
565 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
566 00aa398a Leszek Koltunski
      }
567 255492a0 Leszek Koltunski
568 69a271f3 Leszek Koltunski
    try
569 255492a0 Leszek Koltunski
      {
570 69a271f3 Leszek Koltunski
      // if on Android 11 or we are fullscreen
571
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
572
        {
573
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
574
        window.update(view, w, h);
575
        }
576
      else  // Android 10 or below in pop-up mode or split-screen mode
577
        {
578
        view.getLocationOnScreen(mLocation);
579
        int width  = view.getWidth();
580
        int height = view.getHeight();
581
        int x = mLocation[0]+(width-w)/2;
582
        int y = mLocation[1]+height+yoff;
583
584
        window.showAsDropDown(view);
585
        window.update(x,y,w,h);
586
        }
587 255492a0 Leszek Koltunski
      }
588 69a271f3 Leszek Koltunski
    catch( IllegalArgumentException iae )
589 255492a0 Leszek Koltunski
      {
590 69a271f3 Leszek Koltunski
      // ignore, this means window is 'not attached to window manager' -
591
      // which most probably is because we are already exiting the app.
592 255492a0 Leszek Koltunski
      }
593
    }
594
595 ae77f661 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
596
597 d38a302b Leszek Koltunski
  private void adjustSolvedIcons()
598 ae77f661 Leszek Koltunski
    {
599 59cc16ae Leszek Koltunski
    if( mPlayLayout!=null )
600 ae77f661 Leszek Koltunski
      {
601 59cc16ae Leszek Koltunski
      int currObject = RubikObjectList.getCurrObject();
602
      int dbLevel = RubikObjectList.getDBLevel(currObject);
603
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
604
      RubikScores scores = RubikScores.getInstance();
605
606
      for(int i=0; i<numLevel; i++)
607
        {
608
        int level = i<numLevel-1 ? i+1 : dbLevel;
609
        Button button = (Button)mPlayLayout.getChildAt(i);
610
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
611
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
612
        }
613 ae77f661 Leszek Koltunski
      }
614
    }
615
616 255492a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
617 7b7d65ce Leszek Koltunski
618 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
619 7b7d65ce Leszek Koltunski
    {
620 400ff34d Leszek Koltunski
    int currObject = RubikObjectList.getCurrObject();
621
    int dbLevel = RubikObjectList.getDBLevel(currObject);
622
    RubikObject object = RubikObjectList.getObject(currObject);
623 d433b50e Leszek Koltunski
    int numScrambles = object==null ? 0 : object.getNumScramble();
624 0a7aa15b Leszek Koltunski
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
625 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
626 7b7d65ce Leszek Koltunski
627 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel-1; i++)
628 53f23b64 Leszek Koltunski
      {
629 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
630 53f23b64 Leszek Koltunski
      }
631
632 4fc7b1e6 Leszek Koltunski
    if( numLevel>0 )
633
      {
634
      levels[numLevel-1] = act.getString(R.string.level_full);
635
      }
636 00af5060 Leszek Koltunski
637 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
638
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
639 00af5060 Leszek Koltunski
      {
640
      mLevelValue=1;
641
      }
642 0254cfd7 Leszek Koltunski
643 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
644 c651024f Leszek Koltunski
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
645 de62316a Leszek Koltunski
    int padding  = (int)(width*RubikActivity.PADDING);
646
    int butWidth = mPlayLayoutWidth - 2*padding;
647
    int butHeight= (int)mMenuItemSize;
648
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
649 0254cfd7 Leszek Koltunski
650 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
651 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
652 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
653 0254cfd7 Leszek Koltunski
    pT.setMargins(margin, margin, margin, margin);
654 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
655 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
656
657
    mPlayLayout.removeAllViews();
658
659 d7e539d0 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
660
661 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
662 011fcfe0 Leszek Koltunski
      {
663 0a7aa15b Leszek Koltunski
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
664
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
665 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
666 00af5060 Leszek Koltunski
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
667 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
668
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
669
670 400ff34d Leszek Koltunski
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
671 d7e539d0 Leszek Koltunski
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
672
673 0254cfd7 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
674 011fcfe0 Leszek Koltunski
        {
675 0254cfd7 Leszek Koltunski
        @Override
676
        public void onClick(View v)
677
          {
678 2afc6754 Leszek Koltunski
          ObjectControl control = act.getControl();
679 6fad862b Leszek Koltunski
680 2afc6754 Leszek Koltunski
          if(control.isUINotBlocked())
681 6fad862b Leszek Koltunski
            {
682 598de3ee Leszek Koltunski
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
683 0a7aa15b Leszek Koltunski
            mLevelValue = level;
684 dd874ae8 Leszek Koltunski
            mShouldReactToEndOfScrambling = true;
685 2afc6754 Leszek Koltunski
            control.scrambleObject(scrambles);
686 6fad862b Leszek Koltunski
            }
687 0254cfd7 Leszek Koltunski
          }
688
        });
689 011fcfe0 Leszek Koltunski
690 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
691
      }
692 53f23b64 Leszek Koltunski
    }
693
694 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
695
696 dca3888a Leszek Koltunski
  public int getLevel()
697 211b48f2 Leszek Koltunski
    {
698 85b09df4 Leszek Koltunski
    return mLevelValue;
699 211b48f2 Leszek Koltunski
    }
700
701 84d746d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
702
703
  public void recreatePopup()
704
    {
705
    mObjectPopup = null;
706 f3f3cb09 Leszek Koltunski
707
    int numObjects = RubikObjectList.getNumObjects();
708
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
709
    mColCount = NUM_COLUMNS;
710 84d746d7 Leszek Koltunski
    }
711
712 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
713
714
  public boolean shouldReactToEndOfScrambling()
715
    {
716
    return mShouldReactToEndOfScrambling;
717
    }
718 63dd19c4 Leszek Koltunski
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720
721
  public void receiveUpdate(RubikUpdates updates)
722
    {
723 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
724 10373dc7 Leszek Koltunski
725 179f7189 Leszek Koltunski
    if( act!=null )
726 903c7bbc Leszek Koltunski
      {
727 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
728
        {
729
        @Override
730
        public void run()
731
          {
732 2c9ab085 Leszek Koltunski
          int num = updates.getCompletedNumber();
733 179f7189 Leszek Koltunski
734
          if( num>0 )
735
            {
736
            String shownNum = String.valueOf(num);
737
            mBubbleUpdates.setText(shownNum);
738
            mBubbleUpdates.setVisibility(View.VISIBLE);
739
            int height = (int)(0.05f*mScreenWidth);
740
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
741
            }
742
         else
743
            {
744
            mBubbleUpdates.setVisibility(View.INVISIBLE);
745
            }
746
          }
747
        });
748 903c7bbc Leszek Koltunski
      }
749 63dd19c4 Leszek Koltunski
    }
750
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752
753
  public void errorUpdate()
754
    {
755 c99db493 Leszek Koltunski
    android.util.Log.e("D", "Screen: Error receiving update");
756 63dd19c4 Leszek Koltunski
    }
757 211b48f2 Leszek Koltunski
  }