Project

General

Profile

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

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

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

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

    
86
      firstButtonShown = true;
87
      }
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
      params.setMargins(marginH, firstButtonShown ? marginV : marginH ,marginH,marginV);
96
      b2.setLayoutParams(params);
97
      b2.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
98

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

    
109
      firstButtonShown = true;
110
      }
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
      params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
119
      b3.setLayoutParams(params);
120
      b3.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
121

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

    
132
      firstButtonShown = true;
133
      }
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
    params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
140
    b4.setLayoutParams(params);
141
    b4.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
142

    
143
    b4.setOnClickListener( new View.OnClickListener()
144
      {
145
      @Override
146
      public void onClick(View v)
147
        {
148
        mPopup.dismiss();
149
        act.switchToConfig(ordinal);
150
        }
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
  private void setupLevelButtons(MainActivity act, RubikObject object, View layout, int width,int padding, int margin)
162
    {
163
    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
    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
    Button[] level = new Button[LEVELS_SHOWN+1];
176

    
177
    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

    
187
    int numScramble  = object==null ? 1 : object.getNumScramble();
188
    int min = Math.min(numScramble,LEVELS_SHOWN);
189

    
190
    if( numScramble>=1 && numScramble<=7 )
191
      {
192
      level[numScramble].setText(R.string.levelM);
193
      for(int i=numScramble+1; i<=8; i++) level[i].setVisibility(GONE);
194
      }
195

    
196
    for(int l=0; l<=min; l++)
197
      {
198
      level[l].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
199
      level[l].setLayoutParams(params);
200
      level[l].setPadding(0,0,0,0);
201

    
202
      final int ll=l-1;
203
      boolean isSolved = scores.isSolved(mObjectOrdinal,ll);
204
      level[l].setBackgroundTintList( isSolved ? colorG : colorD);
205
      int scrambles = (l>0 && l<min) ? l : numScramble;
206

    
207
      level[l].setOnClickListener( new View.OnClickListener()
208
        {
209
        @Override
210
        public void onClick(View v)
211
          {
212
          mPopup.dismiss();
213
          if( ll<0 ) scores.setRecord(mObjectOrdinal,ll,0); // remember we've entered the 'Free'
214
                                                            // so that the button turns green
215
          act.switchToPlay(object,scrambles,ll);
216
          }
217
        });
218
      }
219

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

    
(2-2/3)