Project

General

Profile

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

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

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
    Button b1 = layout.findViewById(R.id.objectSolver);
68
69
    if( object!=null && object.hasSolver() )
70
      {
71
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
72 4a6b3b53 leszek
      params.setMargins(marginH,marginH,marginH,marginV);
73 b42c8399 leszek
      b1.setLayoutParams(params);
74
      b1.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
75 4a6b3b53 leszek
76 b42c8399 leszek
      b1.setOnClickListener(new View.OnClickListener()
77
        {
78
        @Override
79
        public void onClick(View v)
80
          {
81 cb30e768 leszek
          mPopup.dismiss();
82
          act.switchToSolver(ordinal);
83 b42c8399 leszek
          }
84
        });
85 4a6b3b53 leszek
86
      firstButtonShown = true;
87 b42c8399 leszek
      }
88
    else b1.setVisibility(GONE);
89
90
    Button b2 = layout.findViewById(R.id.objectPattern);
91
92
    if( object!=null && object.hasPatterns() )
93
      {
94
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
95 4a6b3b53 leszek
      params.setMargins(marginH, firstButtonShown ? marginV : marginH ,marginH,marginV);
96 b42c8399 leszek
      b2.setLayoutParams(params);
97
      b2.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
98 4a6b3b53 leszek
99 b42c8399 leszek
      b2.setOnClickListener(new View.OnClickListener()
100
        {
101
        @Override
102
        public void onClick(View v)
103
          {
104 e9245b7b leszek
          mPopup.dismiss();
105
          act.switchToPattern(ordinal);
106 b42c8399 leszek
          }
107
        });
108 4a6b3b53 leszek
109
      firstButtonShown = true;
110 b42c8399 leszek
      }
111
    else b2.setVisibility(GONE);
112
113
    Button b3 = layout.findViewById(R.id.objectTutorial);
114
115
    if( object!=null && object.hasExtras() )
116
      {
117
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
118 4a6b3b53 leszek
      params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
119 b42c8399 leszek
      b3.setLayoutParams(params);
120
      b3.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
121 4a6b3b53 leszek
122 b42c8399 leszek
      b3.setOnClickListener(new View.OnClickListener()
123
        {
124
        @Override
125
        public void onClick(View v)
126
          {
127 b710a574 leszek
          mPopup.dismiss();
128
          act.switchToTutorial(ordinal);
129 b42c8399 leszek
          }
130
        });
131 4a6b3b53 leszek
132
      firstButtonShown = true;
133 b42c8399 leszek
      }
134
    else b3.setVisibility(GONE);
135
136
    Button b4 = layout.findViewById(R.id.objectInfo);
137
138
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
139 4a6b3b53 leszek
    params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
140 b42c8399 leszek
    b4.setLayoutParams(params);
141
    b4.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
142 4a6b3b53 leszek
143 b42c8399 leszek
    b4.setOnClickListener( new View.OnClickListener()
144
      {
145
      @Override
146
      public void onClick(View v)
147
        {
148 b45b986a leszek
        mPopup.dismiss();
149
        act.switchToConfig(ordinal);
150 b42c8399 leszek
        }
151
      });
152
153
    TextView levels = layout.findViewById(R.id.objectLevels);
154
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
155
156
    setupLevelButtons(act,object,layout,width,padding,marginH);
157
    }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 7ee8337b leszek
  private void setupLevelButtons(MainActivity act, RubikObject object, View layout, int width,int padding, int margin)
162 b42c8399 leszek
    {
163 72d3d383 leszek
    RubikScores scores = RubikScores.getInstance();
164
    Resources res = act.getResources();
165
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
166
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
167
168 b42c8399 leszek
    int layoutWidth = (int)(width*MENU_WIDTH);
169
    int levelHeight = (int)(width*BUTTON_HEIGHT);
170
    int levelWidth  = (layoutWidth-4*padding)/3;
171
172
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(levelWidth,levelHeight);
173
    params.setMargins(margin,-margin,margin,-margin);
174
175 72d3d383 leszek
    Button[] level = new Button[LEVELS_SHOWN+1];
176 b42c8399 leszek
177 72d3d383 leszek
    level[0] = layout.findViewById(R.id.level0);
178
    level[1] = layout.findViewById(R.id.level1);
179
    level[2] = layout.findViewById(R.id.level2);
180
    level[3] = layout.findViewById(R.id.level3);
181
    level[4] = layout.findViewById(R.id.level4);
182
    level[5] = layout.findViewById(R.id.level5);
183
    level[6] = layout.findViewById(R.id.level6);
184
    level[7] = layout.findViewById(R.id.level7);
185
    level[8] = layout.findViewById(R.id.levelM);
186 b42c8399 leszek
187 7ee8337b leszek
    int numScramble  = object==null ? 1 : object.getNumScramble();
188 b42c8399 leszek
    int min = Math.min(numScramble,LEVELS_SHOWN);
189
190
    if( numScramble>=1 && numScramble<=7 )
191
      {
192 72d3d383 leszek
      level[numScramble].setText(R.string.levelM);
193
      for(int i=numScramble+1; i<=8; i++) level[i].setVisibility(GONE);
194 b42c8399 leszek
      }
195
196 72d3d383 leszek
    for(int l=0; l<=min; l++)
197 b42c8399 leszek
      {
198 72d3d383 leszek
      level[l].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
199
      level[l].setLayoutParams(params);
200
      level[l].setPadding(0,0,0,0);
201
202 97201782 leszek
      final int ll=l-1;
203
      boolean isSolved = scores.isSolved(mObjectOrdinal,ll);
204 72d3d383 leszek
      level[l].setBackgroundTintList( isSolved ? colorG : colorD);
205
      int scrambles = (l>0 && l<min) ? l : numScramble;
206 7ee8337b leszek
207 72d3d383 leszek
      level[l].setOnClickListener( new View.OnClickListener()
208 b42c8399 leszek
        {
209
        @Override
210
        public void onClick(View v)
211
          {
212 7ee8337b leszek
          mPopup.dismiss();
213 97201782 leszek
          if( ll<0 ) scores.setRecord(mObjectOrdinal,ll,0); // remember we've entered the 'Free'
214 72d3d383 leszek
                                                            // so that the button turns green
215 97201782 leszek
          act.switchToPlay(object,scrambles,ll);
216 b42c8399 leszek
          }
217
        });
218
      }
219 4a6b3b53 leszek
220
    int index = (numScramble>=1 && numScramble<=7) ? numScramble : LEVELS_SHOWN;
221
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(levelWidth,levelHeight);
222
    params2.setMargins(margin,-margin,margin,margin);
223 72d3d383 leszek
    level[index].setLayoutParams(params2);
224 b42c8399 leszek
    }
225
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228
  void show(View v)
229
    {
230
    View popupView = mPopup.getContentView();
231
    popupView.setSystemUiVisibility(MainActivity.FLAGS);
232
    mPopup.showAtLocation(v, Gravity.CENTER, 0, 0);
233
    }
234
  }