Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPattern.java @ eaf46415

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
      int objectPlay= play.getObject();
71

    
72
      act.changeObject(ObjectType.getObject(objectPlay),false);
73
      }
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

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

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

    
98
    setupPrevButton(act,width);
99
    setupNextButton(act,width);
100
    setupTextView(act,width);
101

    
102
    setTrioState(false);
103

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

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

    
115
    layoutLeft.addView(mPrevButton);
116
    layoutLeft.addView(mMovesText);
117
    layoutLeft.addView(mNextButton);
118

    
119
    setupBackButton(act,width);
120

    
121
    layoutRight.addView(mBackButton);
122

    
123
    layoutBot.addView(layoutLeft);
124
    layoutBot.addView(layoutMid);
125
    layoutBot.addView(layoutRight);
126
    }
127

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

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

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

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

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

    
241
    setTrioState(true);
242

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

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

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  public void savePreferences(SharedPreferences.Editor editor)
256
    {
257

    
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  public void restorePreferences(SharedPreferences preferences)
263
    {
264

    
265
    }
266
  }
(4-4/10)