Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePlay.java @ 0333d81e

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

    
47
  private HorizontalNumberPicker mPicker;
48
  private int mPickerValue;
49
  private int mButton = RubikObject.CUBE3.ordinal();
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

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

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

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

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

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

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

    
103
    markButton(act,mButton);
104

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

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

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  public void restorePreferences(SharedPreferences preferences)
128
    {
129
    mPickerValue= preferences.getInt("scramble", DEF_SCRAMBLE);
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public int getPicker()
135
    {
136
    return mPicker!=null ? mPicker.getValue() : 1;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  public int getButton()
142
    {
143
    return mButton;
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  public void markButton(RubikActivity act, int button)
149
    {
150
    mButton = button;
151

    
152
    for(int b = 0; b< RubikObject.LENGTH; b++)
153
      {
154
      Drawable d = act.findViewById(b).getBackground();
155

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