Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / MainObjectPopup.java @ 4a6b3b53

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.app.Activity;
15
import android.content.Context;
16
import android.util.TypedValue;
17
import android.view.Gravity;
18
import android.view.LayoutInflater;
19
import android.view.View;
20
import android.widget.Button;
21
import android.widget.LinearLayout;
22
import android.widget.PopupWindow;
23
import android.widget.TextView;
24

    
25
import org.distorted.objects.RubikObject;
26
import org.distorted.objects.RubikObjectList;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class MainObjectPopup
31
  {
32
  private static final float MENU_TEXT_SIZE= 0.024f;
33
  private static final float MENU_MARGIN   = 0.008f;
34
  private static final float BUTTON_HEIGHT = 0.150f;
35
  private static final float MENU_WIDTH    = 0.550f;
36

    
37
  public static final int LEVELS_SHOWN = 8;
38

    
39
  private final PopupWindow mPopup;
40
  private final int mMenuTextSize;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  MainObjectPopup(MainActivity act, int ordinal, int width, int height)
45
    {
46
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
47
    final View layout = layoutInflater.inflate(R.layout.object_popup, null);
48
    mPopup = new PopupWindow(act);
49
    mPopup.setContentView(layout);
50
    mPopup.setFocusable(true);
51

    
52
    boolean firstButtonShown = false;
53

    
54
    mMenuTextSize = (int)(height*MENU_TEXT_SIZE);
55
    int padding   = (int)(height*MENU_MARGIN);
56
    int marginH   = padding/2;
57
    int marginV   =-padding/4;
58
    layout.setPadding(padding,0,padding,0);
59
    int levelHeight = (int)(width*BUTTON_HEIGHT);
60

    
61
    RubikObject object = RubikObjectList.getObject(ordinal);
62

    
63
    Button b1 = layout.findViewById(R.id.objectSolver);
64

    
65
    if( object!=null && object.hasSolver() )
66
      {
67
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
68
      params.setMargins(marginH,marginH,marginH,marginV);
69
      b1.setLayoutParams(params);
70
      b1.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
71

    
72
      b1.setOnClickListener(new View.OnClickListener()
73
        {
74
        @Override
75
        public void onClick(View v)
76
          {
77
          mPopup.dismiss();
78
          act.switchToSolver(ordinal);
79
          }
80
        });
81

    
82
      firstButtonShown = true;
83
      }
84
    else b1.setVisibility(GONE);
85

    
86
    Button b2 = layout.findViewById(R.id.objectPattern);
87

    
88
    if( object!=null && object.hasPatterns() )
89
      {
90
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
91
      params.setMargins(marginH, firstButtonShown ? marginV : marginH ,marginH,marginV);
92
      b2.setLayoutParams(params);
93
      b2.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
94

    
95
      b2.setOnClickListener(new View.OnClickListener()
96
        {
97
        @Override
98
        public void onClick(View v)
99
          {
100
          mPopup.dismiss();
101
          act.switchToPattern(ordinal);
102
          }
103
        });
104

    
105
      firstButtonShown = true;
106
      }
107
    else b2.setVisibility(GONE);
108

    
109
    Button b3 = layout.findViewById(R.id.objectTutorial);
110

    
111
    if( object!=null && object.hasExtras() )
112
      {
113
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
114
      params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
115
      b3.setLayoutParams(params);
116
      b3.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
117

    
118
      b3.setOnClickListener(new View.OnClickListener()
119
        {
120
        @Override
121
        public void onClick(View v)
122
          {
123
          mPopup.dismiss();
124
          act.switchToTutorial(ordinal);
125
          }
126
        });
127

    
128
      firstButtonShown = true;
129
      }
130
    else b3.setVisibility(GONE);
131

    
132
    Button b4 = layout.findViewById(R.id.objectInfo);
133

    
134
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,levelHeight);
135
    params.setMargins(marginH,firstButtonShown ? marginV : marginH,marginH,marginV);
136
    b4.setLayoutParams(params);
137
    b4.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
138

    
139
    b4.setOnClickListener( new View.OnClickListener()
140
      {
141
      @Override
142
      public void onClick(View v)
143
        {
144
        mPopup.dismiss();
145
        act.switchToConfig(ordinal);
146
        }
147
      });
148

    
149
    TextView levels = layout.findViewById(R.id.objectLevels);
150
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
151

    
152
    setupLevelButtons(act,object,layout,width,padding,marginH);
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private void setupLevelButtons(Activity act, RubikObject object, View layout, int width,int padding, int margin)
158
    {
159
    int layoutWidth = (int)(width*MENU_WIDTH);
160
    int levelHeight = (int)(width*BUTTON_HEIGHT);
161
    int levelWidth  = (layoutWidth-4*padding)/3;
162

    
163
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(levelWidth,levelHeight);
164
    params.setMargins(margin,-margin,margin,-margin);
165

    
166
    Button[] level = new Button[LEVELS_SHOWN+1];
167

    
168
    level[0] = layout.findViewById(R.id.level0);
169
    level[1] = layout.findViewById(R.id.level1);
170
    level[2] = layout.findViewById(R.id.level2);
171
    level[3] = layout.findViewById(R.id.level3);
172
    level[4] = layout.findViewById(R.id.level4);
173
    level[5] = layout.findViewById(R.id.level5);
174
    level[6] = layout.findViewById(R.id.level6);
175
    level[7] = layout.findViewById(R.id.level7);
176
    level[8] = layout.findViewById(R.id.levelM);
177

    
178
    int numScramble = object==null ? 1 : object.getNumScramble();
179
    int min = Math.min(numScramble,LEVELS_SHOWN);
180

    
181
    if( numScramble>=1 && numScramble<=7 )
182
      {
183
      level[numScramble].setText(R.string.levelM);
184
      for(int i=numScramble+1; i<=8; i++) level[i].setVisibility(GONE);
185
      }
186

    
187
    for(int i=0; i<=min; i++)
188
      {
189
      final int ii = i;
190
      level[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
191
      level[i].setLayoutParams(params);
192
      level[i].setPadding(0,0,0,0);
193

    
194
      level[i].setOnClickListener( new View.OnClickListener()
195
        {
196
        @Override
197
        public void onClick(View v)
198
          {
199

    
200
          }
201
        });
202
      }
203

    
204
    int index = (numScramble>=1 && numScramble<=7) ? numScramble : LEVELS_SHOWN;
205
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(levelWidth,levelHeight);
206
    params2.setMargins(margin,-margin,margin,margin);
207
    level[index].setLayoutParams(params2);
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  void show(View v)
213
    {
214
    View popupView = mPopup.getContentView();
215
    popupView.setSystemUiVisibility(MainActivity.FLAGS);
216
    mPopup.showAtLocation(v, Gravity.CENTER, 0, 0);
217
    }
218
  }
219

    
(2-2/3)