Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePattern.java @ 044529c1

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.LayoutInflater;
27
import android.view.View;
28
import android.widget.Button;
29
import android.widget.LinearLayout;
30
import android.widget.TextView;
31

    
32
import org.distorted.dialog.RubikDialogPattern;
33
import org.distorted.magic.R;
34
import org.distorted.magic.RubikActivity;
35
import org.distorted.object.RubikObjectList;
36
import org.distorted.patterns.RubikPattern;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikStatePattern extends RubikStateAbstract
41
  {
42
  // TODO: read this from upper_text.xml;  this is the height of the TextView there.
43
  private static final int DEFAULT_TEXT_SIZE = 30;
44

    
45
  private TextView mText;
46
  private Button mBackButton;
47
  private int mSize;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  RubikStatePattern()
52
    {
53

    
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  void leaveState(RubikActivity act)
59
    {
60
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
61

    
62
    if( !play.setObjectAndSize(RubikObjectList.CUBE, mSize) )
63
      {
64
      int object= play.getObject();
65
      int size  = play.getSize();
66

    
67
      act.changeObject(RubikObjectList.getObject(object),size);
68
      }
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  void enterState(final RubikActivity act)
74
    {
75
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
76
    int obj  = play.getObject();
77
    int size = play.getSize();
78

    
79
    if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() )
80
      {
81
      mSize = size;
82
      }
83
    else
84
      {
85
      mSize = RubikStatePlay.DEF_SIZE;
86
      act.changeObject(RubikObjectList.CUBE,mSize);
87
      }
88

    
89
    FragmentManager mana = act.getSupportFragmentManager();
90
    RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
91

    
92
    if( diag==null ) showDialog(mana);
93

    
94
    LayoutInflater inflater = act.getLayoutInflater();
95

    
96
    // TOP ////////////////////////////
97
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
98
    layoutTop.removeAllViews();
99
    mText = (TextView)inflater.inflate(R.layout.upper_text, null);
100
    mText.setText(R.string.patterns);
101
    layoutTop.addView(mText);
102

    
103
    // BOT ////////////////////////////
104
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
105
    final float scale = metrics.density;
106

    
107
    if( mBackButton==null ) setupBackButton(act,scale);
108

    
109
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
110
    layoutRight.removeAllViews();
111
    layoutRight.addView(mBackButton);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private void showDialog(FragmentManager manager)
117
    {
118
    Bundle bundle = new Bundle();
119
    int object = RubikObjectList.CUBE.ordinal();
120
    int sizeIndex = RubikObjectList.getSizeIndex(object,mSize);
121
    bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
122

    
123
    RubikDialogPattern diag = new RubikDialogPattern();
124
    diag.setArguments(bundle);
125
    diag.show( manager, RubikDialogPattern.getDialogTag() );
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  private void setupBackButton(final RubikActivity act, final float scale)
131
    {
132
    int padding = (int)(3*scale + 0.5f);
133
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
134
    mBackButton = new Button(act);
135
    mBackButton.setLayoutParams(backParams);
136
    mBackButton.setId(BUTTON_ID_BACK);
137
    mBackButton.setPadding(padding,0,padding,0);
138
    mBackButton.setText(R.string.back);
139

    
140
    mBackButton.setOnClickListener( new View.OnClickListener()
141
      {
142
      @Override
143
      public void onClick(View v)
144
        {
145
        FragmentManager mana = act.getSupportFragmentManager();
146
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
147

    
148
        if( diag==null )
149
          {
150
          mText.setTextSize(DEFAULT_TEXT_SIZE);
151
          mText.setText(R.string.patterns);
152
          showDialog(mana);
153
          }
154
        else
155
          {
156
          diag.rememberCategories();
157
          diag.dismiss();
158
          RubikState.goBack(act);
159
          }
160
        }
161
      });
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public void setPattern(int sizeIndex, int category, int pattern)
167
    {
168
    mSize = RubikObjectList.CUBE.getSizes()[sizeIndex];
169

    
170
    RubikPattern patt = RubikPattern.getInstance();
171
    String patternName = patt.getPatternName(sizeIndex,category,pattern);
172
    mText.setText(patternName);
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  public void savePreferences(SharedPreferences.Editor editor)
178
    {
179
    mBackButton= null;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  public void restorePreferences(SharedPreferences preferences)
185
    {
186

    
187
    }
188
  }
(4-4/6)