Project

General

Profile

« Previous | Next » 

Revision 2536c8da

Added by Leszek Koltunski about 5 hours ago

  • ID 2536c8da542b9480573183db79342be925386e84
  • Parent 884984d8

progress with remembering solves.

View differences:

src/main/java/org/distorted/helpers/RubikRememberedSolves.java
9 9

  
10 10
package org.distorted.helpers;
11 11

  
12
import android.app.Activity;
13

  
12 14
import org.distorted.library.type.Static4D;
13 15
import org.distorted.main.MainObjectPopup;
14 16
import org.json.JSONArray;
......
64 66

  
65 67
///////////////////////////////////////////////////////////////////////////////////////////////////
66 68

  
67
  public String addInfo(InputStream stream, int level, long time, Static4D rot, int[] quats)
69
  public JSONArray readFile(Activity act, String filename)
70
    {
71
    RubikFiles files = RubikFiles.getInstance();
72
    InputStream file = files.openFile(act, filename);
73

  
74
    try
75
      {
76
      String contents = readContents(file);
77
      return new JSONArray(contents);
78
      }
79
    catch(IOException iex)    { android.util.Log.e("D", "readFile: failed to read file" ); }
80
    catch(JSONException jex)  { android.util.Log.e("D", "readFile: failed to parse file"); }
81

  
82
    return null;
83
    }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

  
87
  private String readContents(InputStream stream) throws IOException
88
    {
89
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
90
    StringBuilder contents = new StringBuilder();
91
    String tmp;
92

  
93
    while( (tmp = br.readLine()) != null) contents.append(tmp);
94
    br.close();
95
    stream.close();
96

  
97
    return contents.toString();
98
    }
99

  
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

  
102
  private void debug(int level, long time, Static4D rot, int[] quats)
68 103
    {
69
    /*
70 104
    StringBuilder quatStr = new StringBuilder();
71 105

  
72 106
    for(int c : quats)
......
79 113
                                    "time: "+time+"\n"+
80 114
                                    "rotQuat: " +rot.get0()+" "+rot.get1()+" "+rot.get2()+" "+rot.get3()+"\n"+
81 115
                                    "quats: "+quatStr );
82
*/
116
    }
117

  
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

  
120
  public String addInfo(InputStream stream, int level, long time, Static4D rot, int[] quats)
121
    {
83 122
    if( stream!=null )
84 123
      {
85 124
      try
86 125
        {
87
        BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
88
        StringBuilder contents = new StringBuilder();
89
        String tmp;
90

  
91
        while( (tmp = br.readLine()) != null) contents.append(tmp);
92
        br.close();
93
        stream.close();
94

  
95
        JSONArray levels = new JSONArray(contents.toString());
126
        String contents = readContents(stream);
127
        JSONArray levels = new JSONArray(contents);
96 128
        JSONArray lvl = levels.getJSONArray(level);
97 129
        JSONObject data = createData(time,rot,quats);
98 130
        if( lvl.length()>MAXSOLVES ) lvl.remove(0);
99 131
        lvl.put(data);
100

  
101 132
        return levels.toString();
102 133
        }
103
      catch(IOException iex)    { android.util.Log.e("D", "addInfo: failed to read file"); }
134
      catch(IOException iex)    { android.util.Log.e("D", "addInfo: failed to read file" ); }
104 135
      catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file"); }
105 136
      }
106 137
    else
src/main/java/org/distorted/main/MainObjectPopup.java
79 79
      public void run()
80 80
        {
81 81
        RubikRememberedSolves solves = RubikRememberedSolves.getInstance();
82
        mRememberedSolves = solves.readFile(objname);
82
        mRememberedSolves = solves.readFile(act,objname);
83 83
        }
84 84
      };
85 85

  
......
205 205
    TextView levels = layout.findViewById(R.id.objectLevels);
206 206
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
207 207

  
208
    setupLevelButtons(act,object,layout,width,padding,marginH,darkC,passedC);
208
    setupLevelButtons(act,object,layout,width,height,padding,marginH,darkC,passedC);
209 209
    }
210 210

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

  
213
  private void setupLevelButtons(MainActivity act, RubikObject object, View layout, int width,int padding, int margin, int darkC, int passedC)
213
  private void setupLevelButtons(MainActivity act, RubikObject object, View layout, int width, int height, int padding, int margin, int darkC, int passedC)
214 214
    {
215 215
    RubikScores scores = RubikScores.getInstance(act);
216 216
    Resources res = act.getResources();
......
265 265
          if( ll<0 ) scores.setRecord(mObjectOrdinal,ll,0); // remember we've entered the 'Free'
266 266
                                                            // so that the button turns green
267 267

  
268
          JSONArray json = null;
268
          JSONArray json= null;
269 269
          int numSolves = 0;
270 270

  
271 271
          if( mRememberedSolves!=null )
......
277 277
              }
278 278
            catch(JSONException jex)
279 279
              {
280
              android.util.Log.e("D", "execption trying to parse file: "+jex.getMessage() );
280
              android.util.Log.e("D", "exception trying to parse file: "+jex.getMessage() );
281 281
              }
282 282
            }
283 283

  
284
          if( numSolves>0 ) act
285
          act.switchToPlay(object,mObjectOrdinal,scrambles,ll);
284
          if( numSolves>0 )
285
            {
286
            MainSolvesPopup popup = new MainSolvesPopup(act,object,json,width,height,darkC);
287
            popup.show(v);
288
            }
289
          else act.switchToPlay(object,mObjectOrdinal,scrambles,ll);
286 290
          }
287 291
        });
288 292
      }
src/main/java/org/distorted/main/MainSolvesPopup.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2025 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.main;
11

  
12
import android.view.Gravity;
13
import android.view.View;
14
import android.widget.PopupWindow;
15

  
16
import org.distorted.objects.RubikObject;
17
import org.json.JSONArray;
18

  
19
import java.lang.ref.WeakReference;
20

  
21
///////////////////////////////////////////////////////////////////////////////////////////////////
22

  
23
public class MainSolvesPopup
24
  {
25
  private final int mWidth, mHeight;
26
  private PopupWindow mPopup;
27
  private WeakReference<MainActivity> mAct;
28

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
  MainSolvesPopup(MainActivity act, RubikObject object, JSONArray array, int popupWidth, int popupHeight, int darkC)
32
    {
33
    mAct = new WeakReference<>(act);
34

  
35
    mWidth = popupWidth;
36
    mHeight= popupHeight;
37

  
38
    }
39

  
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

  
42
  void show(View v)
43
    {
44
    View popupView = mPopup.getContentView();
45
    popupView.setSystemUiVisibility(MainActivity.FLAGS);
46
    mPopup.showAtLocation(v, Gravity.CENTER, 0, 0);
47
    }
48
  }
49

  

Also available in: Unified diff