Project

General

Profile

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

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

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.os.Bundle;
24
import android.support.v4.app.FragmentManager;
25
import android.util.DisplayMetrics;
26
import android.view.Gravity;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.widget.Button;
30
import android.widget.ImageButton;
31
import android.widget.LinearLayout;
32
import android.widget.TextView;
33

    
34
import org.distorted.dialog.RubikDialogPattern;
35
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37
import org.distorted.magic.RubikPostRender;
38
import org.distorted.object.RubikObjectList;
39
import org.distorted.patterns.RubikPattern;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikStatePattern extends RubikStateAbstract
44
  {
45
  // 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
  private TextView mText;
49
  private Button mBackButton;
50
  private ImageButton mPrevButton, mNextButton;
51
  private TextView mMovesText;
52
  private int mNumMoves;
53
  private int mSizeIndex, mCategory, mPattern;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  RubikStatePattern()
58
    {
59

    
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  void leaveState(RubikActivity act)
65
    {
66
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
67
    int s = RubikObjectList.CUBE.getSizes()[mSizeIndex];
68

    
69
    if( !play.setObjectAndSize(RubikObjectList.CUBE, s) )
70
      {
71
      int object= play.getObject();
72
      int size  = play.getSize();
73

    
74
      act.changeObject(RubikObjectList.getObject(object),size,null);
75
      }
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  void enterState(final RubikActivity act)
81
    {
82
    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
      mSizeIndex = RubikObjectList.getSizeIndex(obj,size);
89
      }
90
    else
91
      {
92
      mSizeIndex = RubikObjectList.getSizeIndex(RubikObjectList.CUBE.ordinal(),RubikStatePlay.DEF_SIZE);
93
      act.changeObject(RubikObjectList.CUBE,RubikStatePlay.DEF_SIZE,null);
94
      }
95

    
96
    FragmentManager mana = act.getSupportFragmentManager();
97
    RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
98

    
99
    if( diag==null ) showDialog(mana);
100

    
101
    LayoutInflater inflater = act.getLayoutInflater();
102

    
103
    // TOP ////////////////////////////
104
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
105
    layoutTop.removeAllViews();
106
    mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
107
    mText.setText(R.string.patterns);
108
    layoutTop.addView(mText);
109

    
110
    // BOT ////////////////////////////
111
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
112
    final float scale = metrics.density;
113

    
114
    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
    if( mBackButton==null ) setupBackButton(act,scale);
125

    
126
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
127
    layoutRight.removeAllViews();
128
    layoutRight.addView(mBackButton);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private void showDialog(FragmentManager manager)
134
    {
135
    Bundle bundle = new Bundle();
136
    int object = RubikObjectList.CUBE.ordinal();
137
    bundle.putInt("tab", RubikObjectList.pack(object,mSizeIndex) );
138

    
139
    RubikDialogPattern diag = new RubikDialogPattern();
140
    diag.setArguments(bundle);
141
    diag.show( manager, RubikDialogPattern.getDialogTag() );
142
    }
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
      {
157
      @Override
158
      public void onClick(View v)
159
        {
160
        FragmentManager mana = act.getSupportFragmentManager();
161
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
162

    
163
        if( diag==null )
164
          {
165
          mText.setTextSize(DEFAULT_TEXT_SIZE);
166
          showDialog(mana);
167
          }
168
        else
169
          {
170
          diag.rememberState();
171
          diag.dismiss();
172
          RubikState.goBack(act);
173
          }
174
        }
175
      });
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private void setupPrevButton(final RubikActivity act, final float scale)
181
    {
182
    int padding = (int)(3*scale + 0.5f);
183
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
184
    mPrevButton = new ImageButton(act);
185
    mPrevButton.setLayoutParams(params);
186
    mPrevButton.setPadding(padding,0,padding,0);
187
    mPrevButton.setImageResource(R.drawable.left);
188

    
189
    mPrevButton.setOnClickListener( new View.OnClickListener()
190
      {
191
      @Override
192
      public void onClick(View v)
193
        {
194
        RubikPattern pattern = RubikPattern.getInstance();
195
        RubikPostRender post = act.getPostRender();
196
        pattern.backMove( post, mSizeIndex, mCategory, mPattern);
197
        int currMove = pattern.getCurMove(mSizeIndex, mCategory, mPattern);
198
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
199
        }
200
      });
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  private void setupNextButton(final RubikActivity act, final float scale)
206
    {
207
    int padding = (int)( 3*scale + 0.5f);
208
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
209
    mNextButton = new ImageButton(act);
210
    mNextButton.setLayoutParams(params);
211
    mNextButton.setPadding(padding,0,padding,0);
212
    mNextButton.setImageResource(R.drawable.right);
213

    
214
    mNextButton.setOnClickListener( new View.OnClickListener()
215
      {
216
      @Override
217
      public void onClick(View v)
218
        {
219
        RubikPattern pattern = RubikPattern.getInstance();
220
        RubikPostRender post = act.getPostRender();
221
        pattern.makeMove( post, mSizeIndex, mCategory, mPattern);
222
        int currMove = pattern.getCurMove(mSizeIndex, mCategory, mPattern);
223
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
224
        }
225
      });
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  private void setupTextView(final RubikActivity act, final float scale)
231
    {
232
    int padding = (int)( 3*scale + 0.5f);
233
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
234

    
235
    mMovesText = new TextView(act);
236
    mMovesText.setTextSize(20);
237
    mMovesText.setLayoutParams(params);
238
    mMovesText.setPadding(padding,0,padding,0);
239
    mMovesText.setGravity(Gravity.CENTER);
240

    
241
    RubikPattern pattern = RubikPattern.getInstance();
242
    int currMove = pattern.getCurMove(mSizeIndex, mCategory, mPattern);
243
    mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  public void setPattern(final RubikActivity act, int sizeIndex, int category, int pattern)
249
    {
250
    mSizeIndex = sizeIndex;
251
    mCategory  = category;
252
    mPattern   = pattern;
253

    
254
    RubikPattern patt = RubikPattern.getInstance();
255
    String patternName = patt.getPatternName(sizeIndex,category,pattern);
256
    mText.setText(patternName);
257

    
258
    mNumMoves   = patt.getNumMoves(sizeIndex,category,pattern);
259
    int currMove= patt.getCurMove(sizeIndex,category,pattern);
260

    
261
    mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public void savePreferences(SharedPreferences.Editor editor)
267
    {
268
    mBackButton= null;
269
    mPrevButton= null;
270
    mNextButton= null;
271
    mMovesText = null;
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public void restorePreferences(SharedPreferences preferences)
277
    {
278

    
279
    }
280
  }
(4-4/6)