Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPattern.java @ 3f7a4363

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.ObjectList;
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.main.RubikPreRender;
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

    
67
    ObjectList object = RubikPatternList.getObject(mPatternOrdinal);
68
    int size = RubikPatternList.getSize(mPatternOrdinal);
69

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

    
75
      act.changeObject(ObjectList.getObject(objectPlay),sizePlay, false);
76
      }
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  void enterScreen(final RubikActivity act)
82
    {
83
    float width = act.getScreenWidthInPixels();
84
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
85
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
86
    mPatternOrdinal = getPatternOrdinal();
87
    LayoutInflater inflater = act.getLayoutInflater();
88

    
89
    // TOP ////////////////////////////
90
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
91
    layoutTop.removeAllViews();
92
    mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
93
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
94
    mText.setText(R.string.patterns);
95
    layoutTop.addView(mText);
96

    
97
    // BOT ////////////////////////////
98
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
99
    layoutBot.removeAllViews();
100

    
101
    setupPrevButton(act,width);
102
    setupNextButton(act,width);
103
    setupTextView(act,width);
104

    
105
    setTrioState(false);
106

    
107
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
108
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
109
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
110

    
111
    LinearLayout layoutLeft = new LinearLayout(act);
112
    layoutLeft.setLayoutParams(paramsL);
113
    LinearLayout layoutMid = new LinearLayout(act);
114
    layoutMid.setLayoutParams(paramsM);
115
    LinearLayout layoutRight = new LinearLayout(act);
116
    layoutRight.setLayoutParams(paramsR);
117

    
118
    layoutLeft.addView(mPrevButton);
119
    layoutLeft.addView(mMovesText);
120
    layoutLeft.addView(mNextButton);
121

    
122
    setupBackButton(act,width);
123

    
124
    layoutRight.addView(mBackButton);
125

    
126
    layoutBot.addView(layoutLeft);
127
    layoutBot.addView(layoutMid);
128
    layoutBot.addView(layoutRight);
129
    }
130

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

    
133
  private void showDialog(FragmentManager manager)
134
    {
135
    RubikDialogPattern diag = new RubikDialogPattern();
136
    Bundle bundle = new Bundle();
137
    bundle.putInt("tab", mPatternOrdinal );
138
    diag.setArguments(bundle);
139
    diag.show( manager, RubikDialogPattern.getDialogTag() );
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private void setTrioState(boolean enable)
145
    {
146
    int state = enable ? View.VISIBLE : View.INVISIBLE;
147

    
148
    if( mPrevButton!=null ) mPrevButton.setVisibility(state);
149
    if( mNextButton!=null ) mNextButton.setVisibility(state);
150
    if( mMovesText !=null ) mMovesText.setVisibility(state);
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  private void setupBackButton(final RubikActivity act, final float width)
156
    {
157
    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);
158
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
159

    
160
    mBackButton.setOnClickListener( new View.OnClickListener()
161
      {
162
      @Override
163
      public void onClick(View v)
164
        {
165
        FragmentManager mana = act.getSupportFragmentManager();
166
        ScreenList.goBack(act);
167
        showDialog(mana);
168
        }
169
      });
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  private void setupPrevButton(final RubikActivity act, final float width)
175
    {
176
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
177
    mPrevButton = new TransparentImageButton(act,icon,width,0);
178

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

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  private void setupNextButton(final RubikActivity act, final float width)
196
    {
197
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
198
    mNextButton = new TransparentImageButton(act,icon,width,0);
199

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

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private void setupTextView(final RubikActivity act, final float width)
217
    {
218
    int padding = (int)(width*RubikActivity.PADDING);
219
    int margin  = (int)(width*RubikActivity.MARGIN);
220

    
221
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
222
    params.topMargin    = margin;
223
    params.bottomMargin = margin;
224
    params.leftMargin   = margin;
225
    params.rightMargin  = margin;
226

    
227
    mMovesText = new TextView(act);
228
    mMovesText.setTextSize(20);
229
    mMovesText.setLayoutParams(params);
230
    mMovesText.setPadding(padding,0,padding,0);
231
    mMovesText.setGravity(Gravity.CENTER);
232
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
233
    mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public void setPattern(final RubikActivity act, int ordinal, int category, int pattern)
239
    {
240
    mPatternOrdinal = ordinal;
241
    mCategory       = category;
242
    mPattern        = pattern;
243

    
244
    setTrioState(true);
245

    
246
    RubikPattern patt = RubikPattern.getInstance();
247
    String patternName = patt.getPatternName(ordinal,category,pattern);
248
    mText.setText(patternName);
249

    
250
    mNumMoves   = patt.getNumMoves(ordinal,category,pattern);
251
    int currMove= patt.getCurMove(ordinal,category,pattern);
252

    
253
    mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public void savePreferences(SharedPreferences.Editor editor)
259
    {
260

    
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  public void restorePreferences(SharedPreferences preferences)
266
    {
267

    
268
    }
269
  }
(4-4/10)