Project

General

Profile

« Previous | Next » 

Revision 7c9910e5

Added by Leszek Koltunski over 2 years ago

IAP part 5: new 'Purchase' activity (cont'd)

View differences:

src/main/java/org/distorted/purchase/PurchaseScreen.java
9 9

  
10 10
package org.distorted.purchase;
11 11

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

  
23
import org.distorted.helpers.PopupCreator;
24 15
import org.distorted.helpers.TransparentImageButton;
25 16
import org.distorted.main.R;
26 17
import org.distorted.main.RubikActivity;
27 18
import org.distorted.objectlib.main.ObjectControl;
28
import org.distorted.objects.RubikObject;
29
import org.distorted.objects.RubikObjectList;
30

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

  
33 20
///////////////////////////////////////////////////////////////////////////////////////////////////
34 21

  
35 22
public class PurchaseScreen
36 23
{
37
  private static final int NUM_COLUMNS = 5;
38
  private static final int[] mLocation = new int[2];
39

  
40
  private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
41
  private TextView mMovesText;
42
  private PopupWindow mObjectPopup;
43
  private PurchaseScreenPane mPane;
44
  private int mObjectOrdinal;
45
  private int mColCount, mRowCount, mMaxRowCount;
46
  private int mObjectSize;
47
  private int mBarHeight;
48
  private float mButtonSize;
49

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

  
52
  private void setupObjectWindow(final PurchaseActivity act, final float width, final float height)
53
    {
54
    int numObjects= RubikObjectList.getNumObjects();
55
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
56
    mColCount = NUM_COLUMNS;
57

  
58
    int cubeSize  = (int)(width/9);
59
    int margin    = (int)(width*RubikActivity.LARGE_MARGIN);
60
    int padding   = (int)(width*RubikActivity.POPUP_PADDING);
61
    mObjectSize   = (int)(cubeSize + 2*margin + 0.5f);
62
    mMaxRowCount  = (int)((height-mBarHeight)/mObjectSize);
63

  
64
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
65
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
66

  
67
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
68

  
69
    for(int child=0; child<numObjects; child++)
70
      {
71
      final RubikObject obj = RubikObjectList.getObject(child);
72
      View v = objectGrid.getChildAt(child);
73
      ImageButton button = PopupCreator.getButton(obj,v);
74
      final int ordinal = child;
75

  
76
      button.setOnClickListener( new View.OnClickListener()
77
        {
78
        @Override
79
        public void onClick(View v)
80
          {
81
          if( act.getControl().isUINotBlocked() && mObjectOrdinal!=ordinal )
82
            {
83
            mObjectOrdinal = ordinal;
84
            act.changeObject(mObjectOrdinal);
85
            mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
86
            mPane.updatePane(act,mObjectOrdinal);
87
            }
88

  
89
          mObjectPopup.dismiss();
90
          }
91
        });
92
      }
93

  
94
    mObjectPopup = new PopupWindow(act);
95
    mObjectPopup.setFocusable(true);
96
    mObjectPopup.setContentView(view);
97
    }
24
  private TransparentImageButton mBackButton;
98 25

  
99 26
///////////////////////////////////////////////////////////////////////////////////////////////////
100 27

  
......
118 45

  
119 46
///////////////////////////////////////////////////////////////////////////////////////////////////
120 47

  
121
  private void setupObjectButton(final PurchaseActivity act, final int width)
122
    {
123
    final int margin= (int)(width*RubikActivity.SMALL_MARGIN);
124
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
125
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
126
    mObjectButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
127

  
128
    mObjectButton.setOnClickListener( new View.OnClickListener()
129
      {
130
      @Override
131
      public void onClick(View view)
132
        {
133
        if( mObjectPopup==null )
134
          {
135
          float height= act.getScreenHeightInPixels();
136
          setupObjectWindow(act,width,height);
137
          }
138

  
139
        if( act.getControl().isUINotBlocked())
140
          {
141
          int rowCount = Math.min(mMaxRowCount,mRowCount);
142
          View popupView = mObjectPopup.getContentView();
143
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
144
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
145
          }
146
        }
147
      });
148
    }
149

  
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
152

  
153
  private void displayPopup(PurchaseActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
154
    {
155
    View topLayout = act.findViewById(R.id.mainLayout);
156
    boolean isFullScreen;
157

  
158
    if( topLayout!=null )
159
      {
160
      topLayout.getLocationOnScreen(mLocation);
161
      isFullScreen = (mLocation[1]==0);
162
      }
163
    else
164
      {
165
      isFullScreen = true;
166
      }
167

  
168
    try
169
      {
170
      // if on Android 11 or we are fullscreen
171
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
172
        {
173
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
174
        window.update(view, w, h);
175
        }
176
      else  // Android 10 or below in pop-up mode or split-screen mode
177
        {
178
        view.getLocationOnScreen(mLocation);
179
        int width  = view.getWidth();
180
        int height = view.getHeight();
181
        int x = mLocation[0]+(width-w)/2;
182
        int y = mLocation[1]+height+yoff;
183

  
184
        window.showAsDropDown(view);
185
        window.update(x,y,w,h);
186
        }
187
      }
188
    catch( IllegalArgumentException iae )
189
      {
190
      // ignore, this means window is 'not attached to window manager' -
191
      // which most probably is because we are already exiting the app.
192
      }
193
    }
194

  
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

  
197
  private void prevObject(PurchaseActivity act, int numObjects)
198
    {
199
    mObjectOrdinal--;
200
    if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1;
201

  
202
    act.changeObject(mObjectOrdinal);
203
    mPane.updatePane(act,mObjectOrdinal);
204
    }
205

  
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

  
208
  private void nextObject(PurchaseActivity act, int numObjects)
209
    {
210
    mObjectOrdinal++;
211
    if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0;
212

  
213
    act.changeObject(mObjectOrdinal);
214
    mPane.updatePane(act,mObjectOrdinal);
215
    }
216

  
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

  
219
  private void setupPrevButton(final PurchaseActivity act)
220
    {
221
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
222
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
223
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
224

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

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

  
239
  private void setupNextButton(final PurchaseActivity act)
240
    {
241
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
242
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
243
    mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
244

  
245
    mNextButton.setOnClickListener( new View.OnClickListener()
246
      {
247
      @Override
248
      public void onClick(View v)
249
        {
250
        int numObjects = RubikObjectList.getNumObjects();
251
        nextObject(act,numObjects);
252
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
253
        }
254
      });
255
    }
256

  
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

  
259
  private void setupTextView(final PurchaseActivity act, final float width, int numObjects)
260
    {
261
    int padding = (int)(width*RubikActivity.PADDING);
262
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
263
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
264
    params.topMargin    = margin;
265
    params.bottomMargin = margin;
266
    params.leftMargin   = margin;
267
    params.rightMargin  = margin;
268

  
269
    mMovesText = new TextView(act);
270
    mMovesText.setTextSize(20);
271
    mMovesText.setLayoutParams(params);
272
    mMovesText.setPadding(padding,0,padding,0);
273
    mMovesText.setGravity(Gravity.CENTER);
274
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
275
    mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
276
    }
277

  
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

  
280
  void onAttachedToWindow(final PurchaseActivity act, final int objectOrdinal)
48
  void onAttachedToWindow(final PurchaseActivity act, final int ordinal)
281 49
    {
282
    int numObjects = RubikObjectList.getNumObjects();
283 50
    int width = act.getScreenWidthInPixels();
284
    mBarHeight = act.getHeightBar();
285
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
286
    mObjectOrdinal = objectOrdinal;
287 51

  
288 52
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
289 53
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
......
296 60
    LinearLayout layoutRight= new LinearLayout(act);
297 61
    layoutRight.setLayoutParams(paramsR);
298 62

  
299
    setupObjectButton(act,width);
300
    setupPrevButton(act);
301
    setupNextButton(act);
302
    setupTextView(act,width,numObjects);
303 63
    setupBackButton(act);
304 64

  
305
    layoutLeft.addView(mObjectButton);
306
    layoutMid.addView(mPrevButton);
307
    layoutMid.addView(mMovesText);
308
    layoutMid.addView(mNextButton);
309 65
    layoutRight.addView(mBackButton);
310 66

  
311 67
    LinearLayout layout = act.findViewById(R.id.lowerBar);
......
314 70
    layout.addView(layoutMid);
315 71
    layout.addView(layoutRight);
316 72

  
317
    mPane = new PurchaseScreenPane(act,mObjectOrdinal);
73
    PurchaseScreenPane pane = new PurchaseScreenPane(act,ordinal);
74
    pane.updatePane(act,ordinal);
318 75
    }
319 76
}

Also available in: Unified diff