Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPattern.java @ 8ab435b9

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.screens;
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.objectlib.main.ObjectType;
34

    
35
import org.distorted.main.R;
36
import org.distorted.dialogs.RubikDialogPattern;
37
import org.distorted.helpers.TransparentImageButton;
38
import org.distorted.main.RubikActivity;
39
import org.distorted.objectlib.main.ObjectPreRender;
40
import org.distorted.patterns.RubikPattern;
41
import org.distorted.patterns.RubikPatternList;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikScreenPattern extends RubikScreenAbstract
46
  {
47
  private TextView mText;
48
  private ImageButton mPrevButton, mNextButton, mBackButton;
49
  private TextView mMovesText;
50
  private int mNumMoves;
51
  private int mPatternOrdinal, mCategory, mPattern;
52
  private float mButtonSize;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  RubikScreenPattern()
57
    {
58

    
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  void leaveScreen(RubikActivity act)
64
    {
65
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
66
    ObjectType object = RubikPatternList.getObject(mPatternOrdinal);
67

    
68
    if( !play.setObject(act,object) )
69
      {
70
      act.changeObject(play.getObject(),false);
71
      }
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  void enterScreen(final RubikActivity act)
77
    {
78
    float width = act.getScreenWidthInPixels();
79
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
80
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
81
    mPatternOrdinal = getPatternOrdinal();
82
    LayoutInflater inflater = act.getLayoutInflater();
83

    
84
    // TOP ////////////////////////////
85
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
86
    layoutTop.removeAllViews();
87
    mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
88
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
89
    mText.setText(R.string.patterns);
90
    layoutTop.addView(mText);
91

    
92
    // BOT ////////////////////////////
93
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
94
    layoutBot.removeAllViews();
95

    
96
    setupPrevButton(act,width);
97
    setupNextButton(act,width);
98
    setupTextView(act,width);
99

    
100
    setTrioState(false);
101

    
102
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
103
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
104
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
105

    
106
    LinearLayout layoutLeft = new LinearLayout(act);
107
    layoutLeft.setLayoutParams(paramsL);
108
    LinearLayout layoutMid = new LinearLayout(act);
109
    layoutMid.setLayoutParams(paramsM);
110
    LinearLayout layoutRight = new LinearLayout(act);
111
    layoutRight.setLayoutParams(paramsR);
112

    
113
    layoutLeft.addView(mPrevButton);
114
    layoutLeft.addView(mMovesText);
115
    layoutLeft.addView(mNextButton);
116

    
117
    setupBackButton(act,width);
118

    
119
    layoutRight.addView(mBackButton);
120

    
121
    layoutBot.addView(layoutLeft);
122
    layoutBot.addView(layoutMid);
123
    layoutBot.addView(layoutRight);
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void showDialog(FragmentManager manager)
129
    {
130
    RubikDialogPattern diag = new RubikDialogPattern();
131
    Bundle bundle = new Bundle();
132
    bundle.putInt("tab", mPatternOrdinal );
133
    diag.setArguments(bundle);
134
    diag.show( manager, RubikDialogPattern.getDialogTag() );
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void setTrioState(boolean enable)
140
    {
141
    int state = enable ? View.VISIBLE : View.INVISIBLE;
142

    
143
    if( mPrevButton!=null ) mPrevButton.setVisibility(state);
144
    if( mNextButton!=null ) mNextButton.setVisibility(state);
145
    if( mMovesText !=null ) mMovesText.setVisibility(state);
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private void setupBackButton(final RubikActivity act, final float width)
151
    {
152
    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);
153
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
154

    
155
    mBackButton.setOnClickListener( new View.OnClickListener()
156
      {
157
      @Override
158
      public void onClick(View v)
159
        {
160
        FragmentManager mana = act.getSupportFragmentManager();
161
        ScreenList.goBack(act);
162
        showDialog(mana);
163
        }
164
      });
165
    }
166

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

    
169
  private void setupPrevButton(final RubikActivity act, final float width)
170
    {
171
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
172
    mPrevButton = new TransparentImageButton(act,icon,width,0);
173

    
174
    mPrevButton.setOnClickListener( new View.OnClickListener()
175
      {
176
      @Override
177
      public void onClick(View v)
178
        {
179
        RubikPattern pattern = RubikPattern.getInstance();
180
        ObjectPreRender pre = act.getPreRender();
181
        pattern.backMove( pre, mPatternOrdinal, mCategory, mPattern);
182
        int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
183
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
184
        }
185
      });
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  private void setupNextButton(final RubikActivity act, final float width)
191
    {
192
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
193
    mNextButton = new TransparentImageButton(act,icon,width,0);
194

    
195
    mNextButton.setOnClickListener( new View.OnClickListener()
196
      {
197
      @Override
198
      public void onClick(View v)
199
        {
200
        RubikPattern pattern = RubikPattern.getInstance();
201
        ObjectPreRender pre = act.getPreRender();
202
        pattern.makeMove( pre, mPatternOrdinal, mCategory, mPattern);
203
        int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
204
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
205
        }
206
      });
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  private void setupTextView(final RubikActivity act, final float width)
212
    {
213
    int padding = (int)(width*RubikActivity.PADDING);
214
    int margin  = (int)(width*RubikActivity.MARGIN);
215

    
216
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
217
    params.topMargin    = margin;
218
    params.bottomMargin = margin;
219
    params.leftMargin   = margin;
220
    params.rightMargin  = margin;
221

    
222
    mMovesText = new TextView(act);
223
    mMovesText.setTextSize(20);
224
    mMovesText.setLayoutParams(params);
225
    mMovesText.setPadding(padding,0,padding,0);
226
    mMovesText.setGravity(Gravity.CENTER);
227
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
228
    mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public void setPattern(final RubikActivity act, int ordinal, int category, int pattern)
234
    {
235
    mPatternOrdinal = ordinal;
236
    mCategory       = category;
237
    mPattern        = pattern;
238

    
239
    setTrioState(true);
240

    
241
    RubikPattern patt = RubikPattern.getInstance();
242
    String patternName = patt.getPatternName(ordinal,category,pattern);
243
    mText.setText(patternName);
244

    
245
    mNumMoves   = patt.getNumMoves(ordinal,category,pattern);
246
    int currMove= patt.getCurMove(ordinal,category,pattern);
247

    
248
    mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  public void savePreferences(SharedPreferences.Editor editor)
254
    {
255

    
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public void restorePreferences(SharedPreferences preferences)
261
    {
262

    
263
    }
264
  }
(4-4/10)