Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePlay.java @ 8e1231ae

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.RubikObject;
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_BUTTON   = RubikObject.CUBE3.ordinal();
47

    
48
  private HorizontalNumberPicker mPicker;
49
  private int mPickerValue;
50
  private int mButton = DEF_BUTTON;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  public void leaveState(RubikActivity act)
55
    {
56
    mPickerValue = mPicker.getValue();
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public void enterState(RubikActivity act)
62
    {
63
    LayoutInflater inflater = act.getLayoutInflater();
64

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

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

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

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

    
82
    for(int i = 0; i< RubikObject.LENGTH; i++)
83
      {
84
      ImageButton button = new ImageButton(act);
85
      button.setLayoutParams(params);
86
      button.setId(i);
87
      button.setPadding(padding,0,padding,0);
88
      int iconID = RubikObject.getObject(i).getIconID();
89
      button.setImageResource(iconID);
90
      button.setOnClickListener(act);
91
      layoutBot.addView(button);
92
      }
93

    
94
    ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
95

    
96
    Button button = new Button(act);
97
    button.setLayoutParams(params2);
98
    button.setId(BUTTON_ID_BACK);
99
    button.setPadding(padding,0,padding,0);
100
    button.setText(R.string.back);
101
    button.setOnClickListener(act);
102
    layoutBot.addView(button);
103

    
104
    markButton(act,mButton);
105

    
106
    mPicker = act.findViewById(R.id.rubikNumberPicker);
107

    
108
    if( mPicker!=null )
109
      {
110
      mPicker.setMin(MIN_SCRAMBLE);
111
      mPicker.setMax(MAX_SCRAMBLE);
112
      mPicker.setValue(mPickerValue);
113
      }
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  public void savePreferences(SharedPreferences.Editor editor)
119
    {
120
    if( mPicker!=null )
121
      {
122
      editor.putInt("scramble", mPicker.getValue() );
123
      }
124

    
125
    editor.putInt("button", mButton);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public void restorePreferences(SharedPreferences preferences)
131
    {
132
    mPickerValue= preferences.getInt("scramble", DEF_SCRAMBLE);
133
    mButton     = preferences.getInt("button"  , DEF_BUTTON  );
134
    }
135

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

    
138
  public int getPicker()
139
    {
140
    return mPicker!=null ? mPicker.getValue() : 1;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public int getButton()
146
    {
147
    return mButton;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  public void markButton(RubikActivity act, int button)
153
    {
154
    mButton = button;
155

    
156
    for(int b = 0; b< RubikObject.LENGTH; b++)
157
      {
158
      Drawable d = act.findViewById(b).getBackground();
159

    
160
      if( b==button )
161
        {
162
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
163
        }
164
      else
165
        {
166
        d.clearColorFilter();
167
        }
168
      }
169
    }
170
  }
(4-4/5)