Project

General

Profile

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

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

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