Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / MainObjectPopup.java @ 58fd2ec0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.content.res.ColorStateList;
16
import android.content.res.Resources;
17
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
import org.distorted.external.RubikScores;
27
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
  private final int mObjectOrdinal;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  MainObjectPopup(MainActivity act, int ordinal, int width, int height)
48
    {
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
    mObjectOrdinal = ordinal;
56
    boolean firstButtonShown = false;
57

    
58
    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
    ///////// SOLVER //////////////////////////////////////////////////
68
    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
      params.setMargins(marginH,marginH,marginH,marginV);
74
      b1.setLayoutParams(params);
75
      b1.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
76

    
77
      b1.setOnClickListener(new View.OnClickListener()
78
        {
79
        @Override
80
        public void onClick(View v)
81
          {
82
          mPopup.dismiss();
83
          act.switchToSolver(ordinal);
84
          }
85
        });
86

    
87
      firstButtonShown = true;
88
      }
89
    else b1.setVisibility(GONE);
90

    
91
    ///////// PATTERN /////////////////////////////////////////////////
92
    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
      params.setMargins(marginH, firstButtonShown ? marginV : marginH ,marginH,marginV);
98
      b2.setLayoutParams(params);
99
      b2.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
100

    
101
      b2.setOnClickListener(new View.OnClickListener()
102
        {
103
        @Override
104
        public void onClick(View v)
105
          {
106
          mPopup.dismiss();
107
          act.switchToPattern(ordinal);
108
          }
109
        });
110

    
111
      firstButtonShown = true;
112
      }
113
    else b2.setVisibility(GONE);
114

    
115
    ///////// TUTORIALS ///////////////////////////////////////////////
116
    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
      params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
122
      b3.setLayoutParams(params);
123
      b3.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
124

    
125
      b3.setOnClickListener(new View.OnClickListener()
126
        {
127
        @Override
128
        public void onClick(View v)
129
          {
130
          mPopup.dismiss();
131
          act.switchToTutorial(ordinal);
132
          }
133
        });
134

    
135
      firstButtonShown = true;
136
      }
137
    else b3.setVisibility(GONE);
138

    
139
    ///////// INFO ////////////////////////////////////////////////////
140
    Button b4 = layout.findViewById(R.id.objectInfo);
141

    
142
    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
    b4.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
146

    
147
    b4.setOnClickListener( new View.OnClickListener()
148
      {
149
      @Override
150
      public void onClick(View v)
151
        {
152
        mPopup.dismiss();
153
        act.switchToInfo(ordinal);
154
        }
155
      });
156

    
157
    ///////// 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
    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
  private void setupLevelButtons(MainActivity act, RubikObject object, View layout, int width,int padding, int margin)
185
    {
186
    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
    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
    Button[] level = new Button[LEVELS_SHOWN+1];
199

    
200
    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

    
210
    int numScramble  = object==null ? 1 : object.getNumScramble();
211
    int min = Math.min(numScramble,LEVELS_SHOWN);
212

    
213
    if( numScramble>=1 && numScramble<=7 )
214
      {
215
      level[numScramble].setText(R.string.levelM);
216
      for(int i=numScramble+1; i<=8; i++) level[i].setVisibility(GONE);
217
      }
218

    
219
    for(int l=0; l<=min; l++)
220
      {
221
      level[l].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
222
      level[l].setLayoutParams(params);
223
      level[l].setPadding(0,0,0,0);
224

    
225
      final int ll= ( l==LEVELS_SHOWN ? l : l-1);  // the last level has to be upped by one
226
      boolean isSolved = scores.isSolved(mObjectOrdinal,ll);
227
      level[l].setBackgroundTintList( isSolved ? colorG : colorD);
228
      int scrambles = (l>0 && l<min) ? l : numScramble;
229

    
230
      level[l].setOnClickListener( new View.OnClickListener()
231
        {
232
        @Override
233
        public void onClick(View v)
234
          {
235
          mPopup.dismiss();
236
          if( ll<0 ) scores.setRecord(mObjectOrdinal,ll,0); // remember we've entered the 'Free'
237
                                                            // so that the button turns green
238
          act.switchToPlay(object,mObjectOrdinal,scrambles,ll);
239
          }
240
        });
241
      }
242

    
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
    level[index].setLayoutParams(params2);
247
    }
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
  }
258

    
(2-2/4)