Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePattern.java @ aa171dee

1 4f470e00 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e108b57e Leszek Koltunski
import android.os.Bundle;
24
import android.support.v4.app.FragmentManager;
25 4f470e00 Leszek Koltunski
import android.util.DisplayMetrics;
26 11877284 Leszek Koltunski
import android.view.Gravity;
27 4f470e00 Leszek Koltunski
import android.view.LayoutInflater;
28 a6d3b158 Leszek Koltunski
import android.view.View;
29 4f470e00 Leszek Koltunski
import android.widget.Button;
30 0a57000c Leszek Koltunski
import android.widget.ImageButton;
31 4f470e00 Leszek Koltunski
import android.widget.LinearLayout;
32
import android.widget.TextView;
33
34 e108b57e Leszek Koltunski
import org.distorted.dialog.RubikDialogPattern;
35 4f470e00 Leszek Koltunski
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37 aa171dee Leszek Koltunski
import org.distorted.object.RubikObject;
38 53f23b64 Leszek Koltunski
import org.distorted.object.RubikObjectList;
39
import org.distorted.patterns.RubikPattern;
40 4f470e00 Leszek Koltunski
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43
public class RubikStatePattern extends RubikStateAbstract
44
  {
45 e0e84674 Leszek Koltunski
  // TODO: read this from upper_text.xml;  this is the height of the TextView there.
46
  private static final int DEFAULT_TEXT_SIZE = 30;
47
48 3a9d19ed Leszek Koltunski
  private TextView mText;
49 53f23b64 Leszek Koltunski
  private Button mBackButton;
50 0a57000c Leszek Koltunski
  private ImageButton mPrevButton, mNextButton;
51
  private TextView mMovesText;
52 aa171dee Leszek Koltunski
  private int mNumMoves, mCurrMove;
53
  private int mSizeIndex, mCategory, mPattern;
54 4f470e00 Leszek Koltunski
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
  RubikStatePattern()
58
    {
59
60
    }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
  void leaveState(RubikActivity act)
65
    {
66 53f23b64 Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
67 aa171dee Leszek Koltunski
    int s = RubikObjectList.CUBE.getSizes()[mSizeIndex];
68 53f23b64 Leszek Koltunski
69 aa171dee Leszek Koltunski
    if( !play.setObjectAndSize(RubikObjectList.CUBE, s) )
70 53f23b64 Leszek Koltunski
      {
71
      int object= play.getObject();
72
      int size  = play.getSize();
73
74 aa171dee Leszek Koltunski
      act.changeObject(RubikObjectList.getObject(object),size,null);
75 53f23b64 Leszek Koltunski
      }
76 4f470e00 Leszek Koltunski
    }
77
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
81 4f470e00 Leszek Koltunski
    {
82 53f23b64 Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
83
    int obj  = play.getObject();
84
    int size = play.getSize();
85
86
    if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() )
87
      {
88 aa171dee Leszek Koltunski
      mSizeIndex = RubikObjectList.getSizeIndex(obj,size);
89 53f23b64 Leszek Koltunski
      }
90
    else
91
      {
92 aa171dee Leszek Koltunski
      mSizeIndex = RubikObjectList.getSizeIndex(RubikObjectList.CUBE.ordinal(),RubikStatePlay.DEF_SIZE);
93
      act.changeObject(RubikObjectList.CUBE,RubikStatePlay.DEF_SIZE,null);
94 53f23b64 Leszek Koltunski
      }
95 a6d3b158 Leszek Koltunski
96 e108b57e Leszek Koltunski
    FragmentManager mana = act.getSupportFragmentManager();
97
    RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
98
99
    if( diag==null ) showDialog(mana);
100
101 4f470e00 Leszek Koltunski
    LayoutInflater inflater = act.getLayoutInflater();
102
103
    // TOP ////////////////////////////
104
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
105
    layoutTop.removeAllViews();
106 3a9d19ed Leszek Koltunski
    mText = (TextView)inflater.inflate(R.layout.upper_text, null);
107
    mText.setText(R.string.patterns);
108
    layoutTop.addView(mText);
109 4f470e00 Leszek Koltunski
110
    // BOT ////////////////////////////
111 53f23b64 Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
112
    final float scale = metrics.density;
113
114 0a57000c Leszek Koltunski
    if( mPrevButton==null ) setupPrevButton(act,scale);
115
    if( mNextButton==null ) setupNextButton(act,scale);
116
    if( mMovesText ==null ) setupTextView(act,scale);
117
118
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
119
    layoutLeft.removeAllViews();
120
    layoutLeft.addView(mPrevButton);
121
    layoutLeft.addView(mMovesText);
122
    layoutLeft.addView(mNextButton);
123
124 53f23b64 Leszek Koltunski
    if( mBackButton==null ) setupBackButton(act,scale);
125
126 4f470e00 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
127
    layoutRight.removeAllViews();
128 53f23b64 Leszek Koltunski
    layoutRight.addView(mBackButton);
129
    }
130 4f470e00 Leszek Koltunski
131 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132 a6d3b158 Leszek Koltunski
133 e108b57e Leszek Koltunski
  private void showDialog(FragmentManager manager)
134 53f23b64 Leszek Koltunski
    {
135 e108b57e Leszek Koltunski
    Bundle bundle = new Bundle();
136
    int object = RubikObjectList.CUBE.ordinal();
137 aa171dee Leszek Koltunski
    bundle.putInt("tab", RubikObjectList.pack(object,mSizeIndex) );
138 e108b57e Leszek Koltunski
139
    RubikDialogPattern diag = new RubikDialogPattern();
140
    diag.setArguments(bundle);
141
    diag.show( manager, RubikDialogPattern.getDialogTag() );
142 53f23b64 Leszek Koltunski
    }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
  private void setupBackButton(final RubikActivity act, final float scale)
147
    {
148
    int padding = (int)(3*scale + 0.5f);
149
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
150
    mBackButton = new Button(act);
151
    mBackButton.setLayoutParams(backParams);
152
    mBackButton.setPadding(padding,0,padding,0);
153
    mBackButton.setText(R.string.back);
154
155
    mBackButton.setOnClickListener( new View.OnClickListener()
156 a6d3b158 Leszek Koltunski
      {
157
      @Override
158
      public void onClick(View v)
159
        {
160 e108b57e Leszek Koltunski
        FragmentManager mana = act.getSupportFragmentManager();
161
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
162 53f23b64 Leszek Koltunski
163 e108b57e Leszek Koltunski
        if( diag==null )
164 53f23b64 Leszek Koltunski
          {
165 e0e84674 Leszek Koltunski
          mText.setTextSize(DEFAULT_TEXT_SIZE);
166
          mText.setText(R.string.patterns);
167 e108b57e Leszek Koltunski
          showDialog(mana);
168 53f23b64 Leszek Koltunski
          }
169 e108b57e Leszek Koltunski
        else
170
          {
171 044529c1 Leszek Koltunski
          diag.rememberCategories();
172 e108b57e Leszek Koltunski
          diag.dismiss();
173
          RubikState.goBack(act);
174
          }
175
        }
176
      });
177 4f470e00 Leszek Koltunski
    }
178
179 0a57000c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
  private void setupPrevButton(final RubikActivity act, final float scale)
182
    {
183
    int padding = (int)(3*scale + 0.5f);
184 11877284 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
185 0a57000c Leszek Koltunski
    mPrevButton = new ImageButton(act);
186
    mPrevButton.setLayoutParams(params);
187
    mPrevButton.setPadding(padding,0,padding,0);
188
    mPrevButton.setImageResource(R.drawable.left);
189
190
    mPrevButton.setOnClickListener( new View.OnClickListener()
191
      {
192
      @Override
193
      public void onClick(View v)
194
        {
195 aa171dee Leszek Koltunski
        if( --mCurrMove< 0 ) mCurrMove=mNumMoves;
196
197
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
198
        RubikObject object = act.getObject();
199
        RubikPattern.getInstance().backMove( object, mSizeIndex, mCategory, mPattern);
200 0a57000c Leszek Koltunski
        }
201
      });
202
    }
203
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206
  private void setupNextButton(final RubikActivity act, final float scale)
207
    {
208
    int padding = (int)( 3*scale + 0.5f);
209 11877284 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
210 0a57000c Leszek Koltunski
    mNextButton = new ImageButton(act);
211
    mNextButton.setLayoutParams(params);
212
    mNextButton.setPadding(padding,0,padding,0);
213
    mNextButton.setImageResource(R.drawable.right);
214
215
    mNextButton.setOnClickListener( new View.OnClickListener()
216
      {
217
      @Override
218
      public void onClick(View v)
219
        {
220 aa171dee Leszek Koltunski
        if( ++mCurrMove> mNumMoves ) mCurrMove=0;
221
222
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
223
        RubikObject object = act.getObject();
224
        RubikPattern.getInstance().makeMove( object, mSizeIndex, mCategory, mPattern);
225 0a57000c Leszek Koltunski
        }
226
      });
227
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  private void setupTextView(final RubikActivity act, final float scale)
232
    {
233
    int padding = (int)( 3*scale + 0.5f);
234 11877284 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
235 0a57000c Leszek Koltunski
236
    mMovesText = new TextView(act);
237 aa171dee Leszek Koltunski
    mMovesText.setTextSize(20);
238 0a57000c Leszek Koltunski
    mMovesText.setLayoutParams(params);
239
    mMovesText.setPadding(padding,0,padding,0);
240 11877284 Leszek Koltunski
    mMovesText.setGravity(Gravity.CENTER);
241 0a57000c Leszek Koltunski
242 aa171dee Leszek Koltunski
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
243 0a57000c Leszek Koltunski
    }
244
245 3a9d19ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247 aa171dee Leszek Koltunski
  public void setPattern(final RubikActivity act, int sizeIndex, int category, int pattern)
248 3a9d19ed Leszek Koltunski
    {
249 aa171dee Leszek Koltunski
    mSizeIndex = sizeIndex;
250
    mCategory  = category;
251
    mPattern   = pattern;
252 044529c1 Leszek Koltunski
253 e0e84674 Leszek Koltunski
    RubikPattern patt = RubikPattern.getInstance();
254
    String patternName = patt.getPatternName(sizeIndex,category,pattern);
255
    mText.setText(patternName);
256 aa171dee Leszek Koltunski
257
    mNumMoves = patt.getNumMoves(sizeIndex,category,pattern);
258
    mCurrMove = patt.getCurMove(sizeIndex,category,pattern);
259
260
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
261 3a9d19ed Leszek Koltunski
    }
262
263 4f470e00 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265
  public void savePreferences(SharedPreferences.Editor editor)
266
    {
267 53f23b64 Leszek Koltunski
    mBackButton= null;
268 0a57000c Leszek Koltunski
    mPrevButton= null;
269
    mNextButton= null;
270
    mMovesText = null;
271 4f470e00 Leszek Koltunski
    }
272
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
  public void restorePreferences(SharedPreferences preferences)
276
    {
277
278
    }
279
  }