Project

General

Profile

« Previous | Next » 

Revision 4c0cd600

Added by Leszek Koltunski about 4 years ago

Change the series of Object buttons in the Play state into a PopupWindow.

View differences:

src/main/java/org/distorted/uistate/RubikStatePlay.java
19 19

  
20 20
package org.distorted.uistate;
21 21

  
22
import android.content.Context;
22 23
import android.content.SharedPreferences;
23
import android.graphics.PorterDuff;
24
import android.graphics.drawable.Drawable;
25
import android.support.v4.content.ContextCompat;
24
import android.graphics.drawable.BitmapDrawable;
26 25
import android.util.DisplayMetrics;
26
import android.view.Gravity;
27 27
import android.view.LayoutInflater;
28 28
import android.view.View;
29
import android.view.ViewGroup;
30 29
import android.widget.Button;
31 30
import android.widget.ImageButton;
32 31
import android.widget.LinearLayout;
32
import android.widget.PopupWindow;
33 33

  
34 34
import org.distorted.component.HorizontalNumberPicker;
35 35
import org.distorted.magic.R;
......
46 46
  private static final int DEF_OBJECT   = RubikObjectList.CUBE.ordinal();
47 47
  private static final int DEF_SIZE     =  1;  // i.e. the second from the list of CUBE's sizes
48 48

  
49
  private ImageButton mObjButton;
50
  private Button mBackButton;
51
  private PopupWindow mPopup;
49 52
  private HorizontalNumberPicker mPicker;
50 53
  private int mPickerValue;
51 54
  private int mObject = DEF_OBJECT;
52 55
  private int mSize   = DEF_SIZE;
56
  private int mLayoutWidth, mLayoutHeight;
57
  private LinearLayout mLayout;
53 58

  
54 59
///////////////////////////////////////////////////////////////////////////////////////////////////
55 60

  
......
60 65

  
61 66
///////////////////////////////////////////////////////////////////////////////////////////////////
62 67

  
63
  void enterState(RubikActivity act)
68
  void enterState(final RubikActivity act)
64 69
    {
65 70
    LayoutInflater inflater = act.getLayoutInflater();
66 71

  
67 72
    // TOP ////////////////////////////
73
    final View viewTop = inflater.inflate(R.layout.play_title, null);
74

  
68 75
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
69 76
    layoutTop.removeAllViews();
70

  
71
    final View viewTop = inflater.inflate(R.layout.play_title, null);
72 77
    layoutTop.addView(viewTop);
73 78

  
74 79
    // BOT ////////////////////////////
75
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
76
    layoutBot.removeAllViews();
77

  
78 80
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
79
    float scale = metrics.density;
80
    int size    = (int)(60*scale + 0.5f);
81
    int padding = (int)( 3*scale + 0.5f);
82
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
81
    final float scale = metrics.density;
82
    int padding = (int)(3*scale + 0.5f);
83 83

  
84
    for(int i=0; i< RubikObjectList.NUM_OBJECTS; i++)
84
    if( mObjButton==null )
85 85
      {
86
      int[] iconIDs = RubikObjectList.getObject(i).getIconIDs();
87
      int len = iconIDs.length;
86
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
87
      mObjButton = new ImageButton(act);
88
      mObjButton.setLayoutParams(objectParams);
89
      mObjButton.setId(BUTTON_ID_OBJECT);
90
      mObjButton.setPadding(padding,0,padding,0);
91
      mObjButton.setImageResource(R.drawable.cube_menu);
92
      mObjButton.setOnClickListener(act);
93
      }
94
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
95
    layoutLeft.removeAllViews();
96
    layoutLeft.addView(mObjButton);
88 97

  
89
      for(int s=0; s<len; s++)
90
        {
91
        ImageButton button = new ImageButton(act);
92
        button.setLayoutParams(params);
93
        button.setId(RubikObjectList.pack(i,s));
94
        button.setPadding(padding,0,padding,0);
95
        button.setImageResource(iconIDs[s]);
96
        button.setOnClickListener(act);
97
        layoutBot.addView(button);
98
        }
98
    if( mBackButton==null )
99
      {
100
      LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
101
      mBackButton = new Button(act);
102
      mBackButton.setLayoutParams(backParams);
103
      mBackButton.setId(BUTTON_ID_BACK);
104
      mBackButton.setPadding(padding,0,padding,0);
105
      mBackButton.setText(R.string.back);
106
      mBackButton.setOnClickListener(act);
99 107
      }
108
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
109
    layoutRight.removeAllViews();
110
    layoutRight.addView(mBackButton);
100 111

  
101
    ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
112
    mPicker = act.findViewById(R.id.rubikNumberPicker);
113
    mPicker.setMin(MIN_SCRAMBLE);
114
    mPicker.setMax(MAX_SCRAMBLE);
115
    mPicker.setValue(mPickerValue);
102 116

  
103
    Button button = new Button(act);
104
    button.setLayoutParams(params2);
105
    button.setId(BUTTON_ID_BACK);
106
    button.setPadding(padding,0,padding,0);
107
    button.setText(R.string.back);
108
    button.setOnClickListener(act);
109
    layoutBot.addView(button);
117
    if( mPopup==null )
118
      {
119
      LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
120
      final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
121
      mLayout = layout.findViewById(R.id.popup);
110 122

  
111
    markButton(act,mObject,mSize);
123
      mPopup = new PopupWindow(act);
124
      mPopup.setContentView(layout);
125
      mPopup.setFocusable(true);
126
      int margin = (int)(5*scale + 0.5f);
112 127

  
113
    mPicker = act.findViewById(R.id.rubikNumberPicker);
128
      BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
129
      int cubeWidth  = bd.getIntrinsicWidth();
130
      int cubeHeight = bd.getIntrinsicHeight();
114 131

  
115
    if( mPicker!=null )
116
      {
117
      mPicker.setMin(MIN_SCRAMBLE);
118
      mPicker.setMax(MAX_SCRAMBLE);
119
      mPicker.setValue(mPickerValue);
132
      mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
133
      mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f);
134

  
135
      for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
136
        {
137
        RubikObjectList list = RubikObjectList.getObject(object);
138
        int[] sizes = list.getSizes();
139
        int[] icons = list.getIconIDs();
140
        int len = sizes.length;
141
        final int obj = object;
142

  
143
        for(int i=0; i<len; i++)
144
          {
145
          final int size = i;
146

  
147
          LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
148
          p.setMargins(margin, margin, margin, margin);
149

  
150
          ImageButton button = new ImageButton(act);
151
          button.setLayoutParams(p);
152

  
153
          button.setBackgroundResource(icons[i]);
154
          button.setOnClickListener( new View.OnClickListener()
155
            {
156
            @Override
157
            public void onClick(View v)
158
              {
159
              mObject = obj;
160
              mSize   = size;
161
              act.changeObject(obj,size);
162
              mPopup.dismiss();
163
              }
164
            });
165

  
166
          mLayout.addView(button);
167
          }
168
        }
120 169
      }
121 170
    }
122 171

  
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

  
174
  public void bringUpPopup(RubikActivity act, View view)
175
    {
176
    int total = RubikObjectList.getTotal();
177
    boolean vertical = act.isVertical();
178
    mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
179

  
180
    int height = view.getHeight();
181
    int width  = view.getWidth();
182
    int laywid = mLayoutWidth * (vertical? 1:total);
183
    int layhei = mLayoutHeight* (vertical? total:1);
184

  
185
    mPopup.showAsDropDown(view, (width-laywid)/2, -height-layhei, Gravity.LEFT);
186
    }
187

  
123 188
///////////////////////////////////////////////////////////////////////////////////////////////////
124 189

  
125 190
  public void savePreferences(SharedPreferences.Editor editor)
......
131 196

  
132 197
    editor.putInt("statePlay_object", mObject);
133 198
    editor.putInt("statePlay_size"  , mSize);
199

  
200
    mObjButton = null;
201
    mBackButton= null;
202

  
203
    if( mPopup!=null )
204
      {
205
      mPopup.dismiss();
206
      mPopup     = null;
207
      }
134 208
    }
135 209

  
136 210
///////////////////////////////////////////////////////////////////////////////////////////////////
......
162 236
    {
163 237
    return mSize;
164 238
    }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
  public void markButton(RubikActivity act, int object, int size)
169
    {
170
    mObject = object;
171
    mSize   = size;
172

  
173
    int lookingFor = RubikObjectList.pack(object,size);
174
    int len = RubikObjectList.getTotal();
175

  
176
    for(int button=0; button<len; button++)
177
      {
178
      Drawable d = act.findViewById(button).getBackground();
179

  
180
      if( button==lookingFor )
181
        {
182
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
183
        }
184
      else
185
        {
186
        d.clearColorFilter();
187
        }
188
      }
189
    }
190 239
  }

Also available in: Unified diff