Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePlay.java @ ea6ee91b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.uistate;
21

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

    
34
import org.distorted.component.HorizontalNumberPicker;
35
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37
import org.distorted.object.RubikObjectList;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class RubikStatePlay extends RubikStateAbstract
42
  {
43
  private static final int MIN_SCRAMBLE =  1;
44
  private static final int DEF_SCRAMBLE =  1;
45
  public  static final int MAX_SCRAMBLE = 18;
46
  private static final int DEF_OBJECT   = RubikObjectList.CUBE.ordinal();
47
  private static final int DEF_SIZE     =  1;  // i.e. the second from the list of CUBE's sizes
48

    
49
  private HorizontalNumberPicker mPicker;
50
  private int mPickerValue;
51
  private int mObject = DEF_OBJECT;
52
  private int mSize   = DEF_SIZE;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  void leaveState(RubikActivity act)
57
    {
58
    mPickerValue = mPicker.getValue();
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  void enterState(RubikActivity act)
64
    {
65
    LayoutInflater inflater = act.getLayoutInflater();
66

    
67
    // TOP ////////////////////////////
68
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
69
    layoutTop.removeAllViews();
70

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

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

    
78
    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);
83

    
84
    for(int i=0; i< RubikObjectList.NUM_OBJECTS; i++)
85
      {
86
      int[] iconIDs = RubikObjectList.getObject(i).getIconIDs();
87
      int len = iconIDs.length;
88

    
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
        }
99
      }
100

    
101
    ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
102

    
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);
110

    
111
    markButton(act,mObject,mSize);
112

    
113
    mPicker = act.findViewById(R.id.rubikNumberPicker);
114

    
115
    if( mPicker!=null )
116
      {
117
      mPicker.setMin(MIN_SCRAMBLE);
118
      mPicker.setMax(MAX_SCRAMBLE);
119
      mPicker.setValue(mPickerValue);
120
      }
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  public void savePreferences(SharedPreferences.Editor editor)
126
    {
127
    if( mPicker!=null )
128
      {
129
      editor.putInt("statePlay_scramble", mPicker.getValue() );
130
      }
131

    
132
    editor.putInt("statePlay_object", mObject);
133
    editor.putInt("statePlay_size"  , mSize);
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public void restorePreferences(SharedPreferences preferences)
139
    {
140
    mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
141
    mObject     = preferences.getInt("statePlay_object"  , DEF_OBJECT  );
142
    mSize       = preferences.getInt("statePlay_size"    , DEF_SIZE    );
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public int getPicker()
148
    {
149
    return mPicker!=null ? mPicker.getValue() : 1;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public int getObject()
155
    {
156
    return mObject;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public int getSize()
162
    {
163
    return mSize;
164
    }
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
  }
(4-4/5)