Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 30667941

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.config;
11

    
12
import android.os.Build;
13
import android.util.TypedValue;
14
import android.view.Gravity;
15
import android.view.View;
16
import android.widget.GridLayout;
17
import android.widget.ImageButton;
18
import android.widget.LinearLayout;
19
import android.widget.PopupWindow;
20
import android.widget.ScrollView;
21
import android.widget.TextView;
22

    
23
import org.distorted.helpers.ObjectGridCreator;
24

    
25
import org.distorted.helpers.TransparentImageButton;
26
import org.distorted.main.MainActivity;
27
import org.distorted.main.R;
28
import org.distorted.objects.RubikObject;
29
import org.distorted.objects.RubikObjectList;
30

    
31
import static android.view.View.inflate;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class ConfigScreen
36
{
37
  private static final float TEXT_SIZE = 0.0310f;
38
  private static final float PADDING   = 0.0060f;
39
  private static final float MARGIN    = 0.0024f;
40
  private static final int[] mLocation = new int[2];
41
  private static final float POPUP_W  = 0.8f;
42

    
43
  private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
44
  private TextView mMovesText;
45
  private PopupWindow mObjectPopup;
46
  private ConfigScreenPane mPane;
47
  private int mObjectOrdinal;
48
  private float mButtonSize;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
53
    {
54
    int numObjects= RubikObjectList.getNumObjects();
55
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
56
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
57

    
58
    int[] objects = new int[numObjects];
59
    for(int i=0; i<numObjects; i++) objects[i] = i;
60

    
61
    ObjectGridCreator creator = new ObjectGridCreator(width);
62
    creator.createObjectGridClassic(objectGrid,act,objects);
63

    
64
    for(int child=0; child<numObjects; child++)
65
      {
66
      final RubikObject obj = RubikObjectList.getObject(child);
67
      View v = objectGrid.getChildAt(child);
68
      ImageButton button = creator.getButton(obj,v);
69
      final int ordinal = child;
70

    
71
      button.setOnClickListener( new View.OnClickListener()
72
        {
73
        @Override
74
        public void onClick(View v)
75
          {
76
          if( mObjectOrdinal!=ordinal )
77
            {
78
            mObjectOrdinal = ordinal;
79
            act.changeObject(mObjectOrdinal);
80
            mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
81
            mPane.updatePane(act,mObjectOrdinal);
82
            }
83

    
84
          mObjectPopup.dismiss();
85
          }
86
        });
87
      }
88

    
89
    mObjectPopup = new PopupWindow(act);
90
    mObjectPopup.setFocusable(true);
91
    mObjectPopup.setContentView(view);
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  private void setupBackButton(final ConfigActivity act)
97
    {
98
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
99
    mBackButton = new TransparentImageButton(act,R.drawable.ui_smallback,params);
100

    
101
    mBackButton.setOnClickListener( new View.OnClickListener()
102
      {
103
      @Override
104
      public void onClick(View v)
105
        {
106
        act.finish();
107
        }
108
      });
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void setupObjectButton(final ConfigActivity act, final int width, final int height)
114
    {
115
    final int margin= (int)(height*MARGIN);
116
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
117
    mObjectButton = new TransparentImageButton(act,R.drawable.ui_cube_menu,params);
118

    
119
    mObjectButton.setOnClickListener( new View.OnClickListener()
120
      {
121
      @Override
122
      public void onClick(View view)
123
        {
124
        if( mObjectPopup==null ) setupObjectWindow(act,width,height);
125
        View popupView = mObjectPopup.getContentView();
126
        popupView.setSystemUiVisibility(MainActivity.FLAGS);
127
        displayPopup(act,view,mObjectPopup,width,height,margin,margin);
128
        }
129
      });
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
134

    
135
  private void displayPopup(ConfigActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
136
    {
137
    View topLayout = act.findViewById(R.id.mainLayout);
138
    boolean isFullScreen;
139

    
140
    if( topLayout!=null )
141
      {
142
      topLayout.getLocationOnScreen(mLocation);
143
      isFullScreen = (mLocation[1]==0);
144
      }
145
    else
146
      {
147
      isFullScreen = true;
148
      }
149

    
150
    try
151
      {
152
      // if on Android 11 or we are fullscreen
153
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
154
        {
155
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
156
        window.update(view, w, h);
157
        }
158
      else  // Android 10 or below in pop-up mode or split-screen mode
159
        {
160
        view.getLocationOnScreen(mLocation);
161
        int width  = view.getWidth();
162
        int height = view.getHeight();
163
        int x = mLocation[0]+(width-w)/2;
164
        int y = mLocation[1]+height+yoff;
165

    
166
        window.showAsDropDown(view);
167
        window.update(x,y,w,h);
168
        }
169
      }
170
    catch( IllegalArgumentException iae )
171
      {
172
      // ignore, this means window is 'not attached to window manager' -
173
      // which most probably is because we are already exiting the app.
174
      }
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void prevObject(ConfigActivity act, int numObjects)
180
    {
181
    mObjectOrdinal--;
182
    if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1;
183

    
184
    act.changeObject(mObjectOrdinal);
185
    mPane.updatePane(act,mObjectOrdinal);
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  private void nextObject(ConfigActivity act, int numObjects)
191
    {
192
    mObjectOrdinal++;
193
    if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0;
194

    
195
    act.changeObject(mObjectOrdinal);
196
    mPane.updatePane(act,mObjectOrdinal);
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  private void setupPrevButton(final ConfigActivity act)
202
    {
203
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
204
    mPrevButton = new TransparentImageButton(act,R.drawable.ui_left,params);
205

    
206
    mPrevButton.setOnClickListener( new View.OnClickListener()
207
      {
208
      @Override
209
      public void onClick(View v)
210
        {
211
        int numObjects = RubikObjectList.getNumObjects();
212
        prevObject(act,numObjects);
213
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
214
        }
215
      });
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  private void setupNextButton(final ConfigActivity act)
221
    {
222
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
223
    mNextButton = new TransparentImageButton(act,R.drawable.ui_right,params);
224

    
225
    mNextButton.setOnClickListener( new View.OnClickListener()
226
      {
227
      @Override
228
      public void onClick(View v)
229
        {
230
        int numObjects = RubikObjectList.getNumObjects();
231
        nextObject(act,numObjects);
232
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
233
        }
234
      });
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  private void setupTextView(final ConfigActivity act, final float height, int numObjects)
240
    {
241
    int padding = (int)(height*PADDING);
242
    int margin  = (int)(height*MARGIN);
243
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
244
    params.topMargin    = margin;
245
    params.bottomMargin = margin;
246
    params.leftMargin   = margin;
247
    params.rightMargin  = margin;
248

    
249
    mMovesText = new TextView(act);
250
    mMovesText.setTextSize(20);
251
    mMovesText.setLayoutParams(params);
252
    mMovesText.setPadding(padding,0,padding,0);
253
    mMovesText.setGravity(Gravity.CENTER);
254
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
255
    mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  void onAttachedToWindow(final ConfigActivity act, final int objectOrdinal)
261
    {
262
    int numObjects = RubikObjectList.getNumObjects();
263
    int width = act.getScreenWidthInPixels();
264
    int height= act.getScreenHeightInPixels();
265
    int barHeight = act.getHeightBar();
266
    mButtonSize = height*TEXT_SIZE;
267
    mObjectOrdinal = objectOrdinal;
268

    
269
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
270
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
271
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
272

    
273
    LinearLayout layoutLeft = new LinearLayout(act);
274
    layoutLeft.setLayoutParams(paramsL);
275
    LinearLayout layoutMid  = new LinearLayout(act);
276
    layoutMid.setLayoutParams(paramsM);
277
    LinearLayout layoutRight= new LinearLayout(act);
278
    layoutRight.setLayoutParams(paramsR);
279

    
280
    int popupW = (int)(POPUP_W*width);
281
    int popupH = (int)(height-barHeight);
282

    
283
    setupObjectButton(act,popupW,popupH);
284
    setupPrevButton(act);
285
    setupNextButton(act);
286
    setupTextView(act,height,numObjects);
287
    setupBackButton(act);
288

    
289
    layoutLeft.addView(mObjectButton);
290
    layoutMid.addView(mPrevButton);
291
    layoutMid.addView(mMovesText);
292
    layoutMid.addView(mNextButton);
293
    layoutRight.addView(mBackButton);
294

    
295
    LinearLayout layout = act.findViewById(R.id.lowerBar);
296
    layout.removeAllViews();
297
    layout.addView(layoutLeft);
298
    layout.addView(layoutMid);
299
    layout.addView(layoutRight);
300

    
301
    mPane = new ConfigScreenPane(act,mObjectOrdinal);
302
    }
303
}
(4-4/6)