Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / MainObjectPopup.java @ a5972f92

1 b42c8399 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.main;
11
12
import static android.view.View.GONE;
13
14
import android.content.Context;
15 72d3d383 leszek
import android.content.res.ColorStateList;
16
import android.content.res.Resources;
17 b42c8399 leszek
import android.util.TypedValue;
18
import android.view.Gravity;
19
import android.view.LayoutInflater;
20
import android.view.View;
21
import android.widget.Button;
22
import android.widget.LinearLayout;
23
import android.widget.PopupWindow;
24
import android.widget.TextView;
25
26 72d3d383 leszek
import org.distorted.external.RubikScores;
27 b42c8399 leszek
import org.distorted.objects.RubikObject;
28
import org.distorted.objects.RubikObjectList;
29
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32
public class MainObjectPopup
33
  {
34
  private static final float MENU_TEXT_SIZE= 0.024f;
35
  private static final float MENU_MARGIN   = 0.008f;
36
  private static final float BUTTON_HEIGHT = 0.150f;
37
  private static final float MENU_WIDTH    = 0.550f;
38
39
  public static final int LEVELS_SHOWN = 8;
40
41
  private final PopupWindow mPopup;
42
  private final int mMenuTextSize;
43 72d3d383 leszek
  private final int mObjectOrdinal;
44 b42c8399 leszek
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47 b45b986a leszek
  MainObjectPopup(MainActivity act, int ordinal, int width, int height)
48 b42c8399 leszek
    {
49
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
50
    final View layout = layoutInflater.inflate(R.layout.object_popup, null);
51
    mPopup = new PopupWindow(act);
52
    mPopup.setContentView(layout);
53
    mPopup.setFocusable(true);
54
55 72d3d383 leszek
    mObjectOrdinal = ordinal;
56 4a6b3b53 leszek
    boolean firstButtonShown = false;
57
58 b42c8399 leszek
    mMenuTextSize = (int)(height*MENU_TEXT_SIZE);
59
    int padding   = (int)(height*MENU_MARGIN);
60
    int marginH   = padding/2;
61
    int marginV   =-padding/4;
62
    layout.setPadding(padding,0,padding,0);
63
    int levelHeight = (int)(width*BUTTON_HEIGHT);
64
65
    RubikObject object = RubikObjectList.getObject(ordinal);
66
67 58fd2ec0 leszek
    ///////// SOLVER //////////////////////////////////////////////////
68 b42c8399 leszek
    Button b1 = layout.findViewById(R.id.objectSolver);
69
70
    if( object!=null && object.hasSolver() )
71
      {
72
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
73 4a6b3b53 leszek
      params.setMargins(marginH,marginH,marginH,marginV);
74 b42c8399 leszek
      b1.setLayoutParams(params);
75
      b1.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
76 4a6b3b53 leszek
77 b42c8399 leszek
      b1.setOnClickListener(new View.OnClickListener()
78
        {
79
        @Override
80
        public void onClick(View v)
81
          {
82 cb30e768 leszek
          mPopup.dismiss();
83
          act.switchToSolver(ordinal);
84 b42c8399 leszek
          }
85
        });
86 4a6b3b53 leszek
87
      firstButtonShown = true;
88 b42c8399 leszek
      }
89
    else b1.setVisibility(GONE);
90
91 58fd2ec0 leszek
    ///////// PATTERN /////////////////////////////////////////////////
92 b42c8399 leszek
    Button b2 = layout.findViewById(R.id.objectPattern);
93
94
    if( object!=null && object.hasPatterns() )
95
      {
96
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
97 4a6b3b53 leszek
      params.setMargins(marginH, firstButtonShown ? marginV : marginH ,marginH,marginV);
98 b42c8399 leszek
      b2.setLayoutParams(params);
99
      b2.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
100 4a6b3b53 leszek
101 b42c8399 leszek
      b2.setOnClickListener(new View.OnClickListener()
102
        {
103
        @Override
104
        public void onClick(View v)
105
          {
106 e9245b7b leszek
          mPopup.dismiss();
107
          act.switchToPattern(ordinal);
108 b42c8399 leszek
          }
109
        });
110 4a6b3b53 leszek
111
      firstButtonShown = true;
112 b42c8399 leszek
      }
113
    else b2.setVisibility(GONE);
114
115 58fd2ec0 leszek
    ///////// TUTORIALS ///////////////////////////////////////////////
116 b42c8399 leszek
    Button b3 = layout.findViewById(R.id.objectTutorial);
117
118
    if( object!=null && object.hasExtras() )
119
      {
120
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
121 4a6b3b53 leszek
      params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
122 b42c8399 leszek
      b3.setLayoutParams(params);
123
      b3.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
124 4a6b3b53 leszek
125 b42c8399 leszek
      b3.setOnClickListener(new View.OnClickListener()
126
        {
127
        @Override
128
        public void onClick(View v)
129
          {
130 b710a574 leszek
          mPopup.dismiss();
131
          act.switchToTutorial(ordinal);
132 b42c8399 leszek
          }
133
        });
134 4a6b3b53 leszek
135
      firstButtonShown = true;
136 b42c8399 leszek
      }
137
    else b3.setVisibility(GONE);
138
139 58fd2ec0 leszek
    ///////// INFO ////////////////////////////////////////////////////
140 b42c8399 leszek
    Button b4 = layout.findViewById(R.id.objectInfo);
141
142 58fd2ec0 leszek
    LinearLayout.LayoutParams paramsInfo = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
143
    paramsInfo.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
144
    b4.setLayoutParams(paramsInfo);
145 b42c8399 leszek
    b4.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
146 4a6b3b53 leszek
147 b42c8399 leszek
    b4.setOnClickListener( new View.OnClickListener()
148
      {
149
      @Override
150
      public void onClick(View v)
151
        {
152 b45b986a leszek
        mPopup.dismiss();
153 5b22f901 leszek
        act.switchToInfo(ordinal);
154 b42c8399 leszek
        }
155
      });
156
157 58fd2ec0 leszek
    ///////// CONFIG //////////////////////////////////////////////////
158
    Button b5 = layout.findViewById(R.id.objectConfig);
159
160
    LinearLayout.LayoutParams paramsConfig = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
161
    paramsConfig.setMargins(marginH,marginV,marginH,marginV);
162
    b5.setLayoutParams(paramsConfig);
163
    b5.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
164
165
    b5.setOnClickListener( new View.OnClickListener()
166
      {
167
      @Override
168
      public void onClick(View v)
169
        {
170
        mPopup.dismiss();
171
        act.switchToConfig(ordinal);
172
        }
173
      });
174
175
    ///////// LEVELS //////////////////////////////////////////////////
176 b42c8399 leszek
    TextView levels = layout.findViewById(R.id.objectLevels);
177
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
178
179
    setupLevelButtons(act,object,layout,width,padding,marginH);
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184 7ee8337b leszek
  private void setupLevelButtons(MainActivity act, RubikObject object, View layout, int width,int padding, int margin)
185 b42c8399 leszek
    {
186 72d3d383 leszek
    RubikScores scores = RubikScores.getInstance();
187
    Resources res = act.getResources();
188
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
189
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
190
191 b42c8399 leszek
    int layoutWidth = (int)(width*MENU_WIDTH);
192
    int levelHeight = (int)(width*BUTTON_HEIGHT);
193
    int levelWidth  = (layoutWidth-4*padding)/3;
194
195
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(levelWidth,levelHeight);
196
    params.setMargins(margin,-margin,margin,-margin);
197
198 72d3d383 leszek
    Button[] level = new Button[LEVELS_SHOWN+1];
199 b42c8399 leszek
200 72d3d383 leszek
    level[0] = layout.findViewById(R.id.level0);
201
    level[1] = layout.findViewById(R.id.level1);
202
    level[2] = layout.findViewById(R.id.level2);
203
    level[3] = layout.findViewById(R.id.level3);
204
    level[4] = layout.findViewById(R.id.level4);
205
    level[5] = layout.findViewById(R.id.level5);
206
    level[6] = layout.findViewById(R.id.level6);
207
    level[7] = layout.findViewById(R.id.level7);
208
    level[8] = layout.findViewById(R.id.levelM);
209 b42c8399 leszek
210 7ee8337b leszek
    int numScramble  = object==null ? 1 : object.getNumScramble();
211 b42c8399 leszek
    int min = Math.min(numScramble,LEVELS_SHOWN);
212
213
    if( numScramble>=1 && numScramble<=7 )
214
      {
215 72d3d383 leszek
      level[numScramble].setText(R.string.levelM);
216
      for(int i=numScramble+1; i<=8; i++) level[i].setVisibility(GONE);
217 b42c8399 leszek
      }
218
219 72d3d383 leszek
    for(int l=0; l<=min; l++)
220 b42c8399 leszek
      {
221 72d3d383 leszek
      level[l].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
222
      level[l].setLayoutParams(params);
223
      level[l].setPadding(0,0,0,0);
224
225 c22e6ef7 leszek
      final int ll= ( l==LEVELS_SHOWN ? l : l-1);  // the last level has to be upped by one
226 97201782 leszek
      boolean isSolved = scores.isSolved(mObjectOrdinal,ll);
227 72d3d383 leszek
      level[l].setBackgroundTintList( isSolved ? colorG : colorD);
228
      int scrambles = (l>0 && l<min) ? l : numScramble;
229 7ee8337b leszek
230 72d3d383 leszek
      level[l].setOnClickListener( new View.OnClickListener()
231 b42c8399 leszek
        {
232
        @Override
233
        public void onClick(View v)
234
          {
235 7ee8337b leszek
          mPopup.dismiss();
236 97201782 leszek
          if( ll<0 ) scores.setRecord(mObjectOrdinal,ll,0); // remember we've entered the 'Free'
237 72d3d383 leszek
                                                            // so that the button turns green
238 1a206525 leszek
          act.switchToPlay(object,mObjectOrdinal,scrambles,ll);
239 b42c8399 leszek
          }
240
        });
241
      }
242 4a6b3b53 leszek
243
    int index = (numScramble>=1 && numScramble<=7) ? numScramble : LEVELS_SHOWN;
244
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(levelWidth,levelHeight);
245
    params2.setMargins(margin,-margin,margin,margin);
246 72d3d383 leszek
    level[index].setLayoutParams(params2);
247 b42c8399 leszek
    }
248
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
251
  void show(View v)
252
    {
253
    View popupView = mPopup.getContentView();
254
    popupView.setSystemUiVisibility(MainActivity.FLAGS);
255
    mPopup.showAtLocation(v, Gravity.CENTER, 0, 0);
256
    }
257
  }