Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePattern.java @ 8becce57

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_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
          mText.setText(R.string.patterns);
167
          showDialog(mana);
168
          }
169
        else
170
          {
171
          diag.rememberCategories();
172
          diag.dismiss();
173
          RubikState.goBack(act);
174
          }
175
        }
176
      });
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private void setupPrevButton(final RubikActivity act, final float scale)
182
    {
183
    int padding = (int)(3*scale + 0.5f);
184
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
185
    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
        RubikPattern pattern = RubikPattern.getInstance();
196
        RubikPostRender post = act.getPostRender();
197
        pattern.backMove( post, mSizeIndex, mCategory, mPattern);
198
        int currMove = pattern.getCurMove(mSizeIndex, mCategory, mPattern);
199
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
200
        }
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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
210
    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
        RubikPattern pattern = RubikPattern.getInstance();
221
        RubikPostRender post = act.getPostRender();
222
        pattern.makeMove( post, mSizeIndex, mCategory, mPattern);
223
        int currMove = pattern.getCurMove(mSizeIndex, mCategory, mPattern);
224
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
225
        }
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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
235

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

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

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

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

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

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

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  public void restorePreferences(SharedPreferences preferences)
278
    {
279

    
280
    }
281
  }
(4-4/6)