Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePattern.java @ da768c35

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.states;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24
import androidx.fragment.app.FragmentManager;
25
import android.util.TypedValue;
26
import android.view.Gravity;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.widget.ImageButton;
30
import android.widget.LinearLayout;
31
import android.widget.TextView;
32

    
33
import org.distorted.dialogs.RubikDialogPattern;
34
import org.distorted.main.R;
35
import org.distorted.main.RubikActivity;
36
import org.distorted.main.RubikPreRender;
37
import org.distorted.objects.ObjectList;
38
import org.distorted.patterns.RubikPattern;
39
import org.distorted.patterns.RubikPatternList;
40

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

    
43
public class RubikStatePattern extends RubikStateAbstract
44
  {
45
  private TextView mText;
46
  private ImageButton mPrevButton, mNextButton, mBackButton;
47
  private TextView mMovesText;
48
  private int mNumMoves;
49
  private int mPatternOrdinal, mCategory, mPattern;
50
  private float mButtonSize;
51

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

    
54
  RubikStatePattern()
55
    {
56

    
57
    }
58

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

    
61
  void leaveState(RubikActivity act)
62
    {
63
    RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
64

    
65
    ObjectList object = RubikPatternList.getObject(mPatternOrdinal);
66
    int size = RubikPatternList.getSize(mPatternOrdinal);
67

    
68
    if( !play.setObjectAndSize(act,object,size) )
69
      {
70
      int objectPlay= play.getObject();
71
      int sizePlay  = play.getSize();
72

    
73
      act.changeObject(ObjectList.getObject(objectPlay),sizePlay, false);
74
      }
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  void enterState(final RubikActivity act)
80
    {
81
    float width = act.getScreenWidthInPixels();
82
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
83
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
84

    
85
    RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
86
    int obj  = play.getObject();
87
    int size = play.getSize();
88

    
89
    mPatternOrdinal = RubikPatternList.getOrdinal(obj,size);
90

    
91
    if( mPatternOrdinal<0 )
92
      {
93
      mPatternOrdinal = ObjectList.getSizeIndex(RubikStatePlay.DEF_OBJECT,RubikStatePlay.DEF_SIZE);
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.upperBar);
105
    layoutTop.removeAllViews();
106
    mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
107
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
108
    mText.setText(R.string.patterns);
109
    layoutTop.addView(mText);
110

    
111
    // BOT ////////////////////////////
112
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
113
    layoutBot.removeAllViews();
114

    
115
    setupPrevButton(act,width);
116
    setupNextButton(act,width);
117
    setupTextView(act,width);
118

    
119
    setTrioState(false);
120

    
121
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
122
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
123
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
124

    
125
    LinearLayout layoutLeft = new LinearLayout(act);
126
    layoutLeft.setLayoutParams(paramsL);
127
    LinearLayout layoutMid = new LinearLayout(act);
128
    layoutMid.setLayoutParams(paramsM);
129
    LinearLayout layoutRight = new LinearLayout(act);
130
    layoutRight.setLayoutParams(paramsR);
131

    
132
    layoutLeft.addView(mPrevButton);
133
    layoutLeft.addView(mMovesText);
134
    layoutLeft.addView(mNextButton);
135

    
136
    setupBackButton(act,width);
137

    
138
    layoutRight.addView(mBackButton);
139

    
140
    layoutBot.addView(layoutLeft);
141
    layoutBot.addView(layoutMid);
142
    layoutBot.addView(layoutRight);
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  private void showDialog(FragmentManager manager)
148
    {
149
    RubikDialogPattern diag = new RubikDialogPattern();
150
    Bundle bundle = new Bundle();
151
    bundle.putInt("tab", mPatternOrdinal );
152
    diag.setArguments(bundle);
153
    diag.show( manager, RubikDialogPattern.getDialogTag() );
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private void setTrioState(boolean enable)
159
    {
160
    int state = enable ? View.VISIBLE : View.INVISIBLE;
161

    
162
    if( mPrevButton!=null ) mPrevButton.setVisibility(state);
163
    if( mNextButton!=null ) mNextButton.setVisibility(state);
164
    if( mMovesText !=null ) mMovesText.setVisibility(state);
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void setupBackButton(final RubikActivity act, final float width)
170
    {
171
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
172
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
173

    
174
    mBackButton.setOnClickListener( new View.OnClickListener()
175
      {
176
      @Override
177
      public void onClick(View v)
178
        {
179
        FragmentManager mana = act.getSupportFragmentManager();
180
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
181

    
182
        if( diag==null )
183
          {
184
          showDialog(mana);
185
          setTrioState(false);
186
          }
187
        else
188
          {
189
          diag.rememberState();
190
          diag.dismiss();
191
          StateList.goBack(act);
192
          }
193
        }
194
      });
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  private void setupPrevButton(final RubikActivity act, final float width)
200
    {
201
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
202
    mPrevButton = new TransparentImageButton(act,icon,width,0);
203

    
204
    mPrevButton.setOnClickListener( new View.OnClickListener()
205
      {
206
      @Override
207
      public void onClick(View v)
208
        {
209
        RubikPattern pattern = RubikPattern.getInstance();
210
        RubikPreRender pre = act.getPreRender();
211
        pattern.backMove( pre, mPatternOrdinal, mCategory, mPattern);
212
        int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
213
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
214
        }
215
      });
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  private void setupNextButton(final RubikActivity act, final float width)
221
    {
222
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
223
    mNextButton = new TransparentImageButton(act,icon,width,0);
224

    
225
    mNextButton.setOnClickListener( new View.OnClickListener()
226
      {
227
      @Override
228
      public void onClick(View v)
229
        {
230
        RubikPattern pattern = RubikPattern.getInstance();
231
        RubikPreRender pre = act.getPreRender();
232
        pattern.makeMove( pre, mPatternOrdinal, mCategory, mPattern);
233
        int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
234
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
235
        }
236
      });
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  private void setupTextView(final RubikActivity act, final float width)
242
    {
243
    int padding = (int)(width*RubikActivity.PADDING);
244
    int margin  = (int)(width*RubikActivity.MARGIN);
245

    
246
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
247
    params.topMargin    = margin;
248
    params.bottomMargin = margin;
249
    params.leftMargin   = margin;
250
    params.rightMargin  = margin;
251

    
252
    mMovesText = new TextView(act);
253
    mMovesText.setTextSize(20);
254
    mMovesText.setLayoutParams(params);
255
    mMovesText.setPadding(padding,0,padding,0);
256
    mMovesText.setGravity(Gravity.CENTER);
257
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
258
    mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public void setPattern(final RubikActivity act, int ordinal, int category, int pattern)
264
    {
265
    mPatternOrdinal = ordinal;
266
    mCategory       = category;
267
    mPattern        = pattern;
268

    
269
    setTrioState(true);
270

    
271
    RubikPattern patt = RubikPattern.getInstance();
272
    String patternName = patt.getPatternName(ordinal,category,pattern);
273
    mText.setText(patternName);
274

    
275
    mNumMoves   = patt.getNumMoves(ordinal,category,pattern);
276
    int currMove= patt.getCurMove(ordinal,category,pattern);
277

    
278
    mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public void savePreferences(SharedPreferences.Editor editor)
284
    {
285

    
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public void restorePreferences(SharedPreferences preferences)
291
    {
292

    
293
    }
294
  }
(3-3/9)