Project

General

Profile

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

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

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