Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ fe1d7818

1 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.config;
11
12
import android.os.Bundle;
13 9881dc03 leszek
import android.content.SharedPreferences;
14 58fd2ec0 leszek
15 b6606976 leszek
import androidx.preference.PreferenceManager;
16
17 8477cf44 leszek
import org.distorted.dialogs.DialogError;
18
import org.distorted.dialogs.DialogMessage;
19 a5972f92 leszek
import org.distorted.helpers.BaseActivity;
20 58fd2ec0 leszek
import org.distorted.library.main.DistortedLibrary;
21
import org.distorted.main.R;
22
import org.distorted.objectlib.main.InitAssets;
23
import org.distorted.objectlib.main.ObjectControl;
24
import org.distorted.objectlib.main.TwistyObject;
25
import org.distorted.objects.RubikObject;
26
import org.distorted.objects.RubikObjectList;
27
28
import java.io.InputStream;
29
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 a5972f92 leszek
public class ConfigActivity extends BaseActivity
33 58fd2ec0 leszek
{
34
    private static final int ACTIVITY_NUMBER = 7;
35
    private ConfigScreen mScreen;
36 14da3188 leszek
    private ConfigScreenPane mPane;
37 58fd2ec0 leszek
    private int mObjectOrdinal;
38 988e3d33 leszek
    private boolean mSupportsAdjColors;
39 e1637420 leszek
    private boolean mDisplayMessageDialog;
40 9b739a74 LeszekKoltunski
    private float mBorders, mCorners;
41 58fd2ec0 leszek
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
    @Override
45
    protected void onCreate(Bundle savedState)
46
      {
47
      super.onCreate(savedState);
48
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
49
      setContentView(R.layout.config);
50
51
      Bundle b = getIntent().getExtras();
52
      if(b != null) mObjectOrdinal = b.getInt("obj");
53
54 e1637420 leszek
      mDisplayMessageDialog = true;
55
56 a5972f92 leszek
      computeScreenDimensions();
57 58fd2ec0 leszek
      hideNavigationBar();
58
      cutoutHack();
59 a5972f92 leszek
      computeLowerBarHeight(RATIO_BAR);
60 58fd2ec0 leszek
      }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
    
64
    @Override
65
    protected void onPause() 
66
      {
67
      super.onPause();
68
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
69
      view.onPause();
70 8cf17ca7 leszek
      savePreferences();
71 58fd2ec0 leszek
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
72
      }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
    
76
    @Override
77
    protected void onResume() 
78
      {
79
      super.onResume();
80
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
81
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
82
      view.onResume();
83 9211042f leszek
      String key ="";
84 988e3d33 leszek
      mSupportsAdjColors = false;
85 58fd2ec0 leszek
86
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
87
        {
88
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
89
        changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
90 988e3d33 leszek
91
        if( object!=null )
92
          {
93
          key = object.getUpperName();
94
          mSupportsAdjColors = object.supportsAdjustableColors();
95
          }
96 58fd2ec0 leszek
        }
97 9211042f leszek
98 b6606976 leszek
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
99 9b739a74 LeszekKoltunski
      restorePreferences(key,preferences);
100 9211042f leszek
101
      if( mScreen==null ) mScreen = new ConfigScreen();
102
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
103
104 9b739a74 LeszekKoltunski
      mPane = new ConfigScreenPane(this,mSupportsAdjColors,mBorders,mCorners);
105 58fd2ec0 leszek
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108 e91771e4 leszek
109
    void repaintPuzzleFace(int cubit, int face)
110
      {
111 988e3d33 leszek
      if( mPane!=null && mSupportsAdjColors )
112 e91771e4 leszek
        {
113 4bd09fe2 leszek
        int color  = mPane.getCurrentColor();
114 e91771e4 leszek
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
115
        ObjectControl control = view.getObjectControl();
116 5b5b958a leszek
        control.repaintPuzzleFace(cubit,face,color);
117 e6dbe81a LeszekKoltunski
118
        if( mDisplayMessageDialog )
119
          {
120
          mDisplayMessageDialog = false;
121
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
122
          SharedPreferences.Editor editor = preferences.edit();
123
          editor.putBoolean("configDisplayDialog", mDisplayMessageDialog );
124
          editor.apply();
125
          }
126 e91771e4 leszek
        }
127
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 58fd2ec0 leszek
    @Override
132
    protected void onDestroy() 
133
      {
134
      super.onDestroy();
135
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
136
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
    void OpenGLError()
141
      {
142 8477cf44 leszek
      DialogError errDiag = new DialogError();
143 58fd2ec0 leszek
      errDiag.show(getSupportFragmentManager(), null);
144
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
149
      {
150
      if( object!=null )
151
        {
152
        int iconMode           = TwistyObject.MODE_NORM;
153
        InputStream jsonStream = object.getObjectStream(this);
154
        InputStream meshStream = object.getMeshStream(this);
155
        String name            = object.getUpperName();
156
        InitAssets asset       = new InitAssets(jsonStream,meshStream,null);
157
        control.changeIfDifferent(ordinal,name,iconMode,asset);
158
        }
159
      }
160
161 8cf17ca7 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163
    private void savePreferences()
164
      {
165
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
166
      view.getObjectControl().savePreferences();
167
      }
168
169 9211042f leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 9b739a74 LeszekKoltunski
    private void restorePreferences(String key, SharedPreferences preferences)
172 9211042f leszek
      {
173
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
174 15b600fd leszek
      view.getObjectControl().restoreStickers();
175 e1637420 leszek
176 b6606976 leszek
      mDisplayMessageDialog = preferences.getBoolean("configDisplayDialog",true);
177 9b739a74 LeszekKoltunski
      mBorders              = preferences.getFloat(key+"_border",1.0f);
178
      mCorners              = preferences.getFloat(key+"_corner",1.0f);
179 e1637420 leszek
180
      if( mDisplayMessageDialog )
181
        {
182
        Bundle bundle = new Bundle();
183
        bundle.putString("argument", getString(R.string.config_message) );
184 8477cf44 leszek
        DialogMessage diag = new DialogMessage();
185 e1637420 leszek
        diag.setArguments(bundle);
186 8477cf44 leszek
        diag.show( getSupportFragmentManager(), DialogMessage.getDialogTag());
187 e1637420 leszek
        }
188 9211042f leszek
      }
189
190 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
191
// PUBLIC API
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
    public ConfigRenderer getRenderer()
195
      {
196
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
197
      return view.getRenderer();
198
      }
199
200 427ba5bf leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
    public void recreateColors()
203
      {
204
      ObjectControl control = getControl();
205
      control.recreateColors();
206
      }
207
208 14da3188 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
    public void recreateStickers(int borders, int corners)
211
      {
212
      ObjectControl control = getControl();
213
214
      float border_step = ConfigScreenPane.BORDER_STEPS[borders];
215
      float corner_step = ConfigScreenPane.CORNER_STEPS[corners];
216
217
      control.recreateSticker(border_step,corner_step);
218
      }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222
    public void resetUI()
223
      {
224 4bd09fe2 leszek
      mPane.resetUI(this);
225 14da3188 leszek
      }
226
227 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229
    public ObjectControl getControl()
230
      {
231
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
232
      return view.getObjectControl();
233
      }
234
}