Project

General

Profile

« Previous | Next » 

Revision be803eb3

Added by Leszek Koltunski 13 days ago

Add possibility to select different solvers.

View differences:

src/main/java/org/distorted/phasedsolver/SolverActivity.java
15 15
import android.content.SharedPreferences;
16 16
import android.os.Bundle;
17 17
import android.view.View;
18
import android.widget.AdapterView;
19
import android.widget.ArrayAdapter;
20
import android.widget.Spinner;
18 21
import android.widget.TextView;
19 22
import android.widget.Toast;
20 23

  
......
22 25

  
23 26
import org.distorted.library.main.DistortedLibrary;
24 27
import org.distorted.objectlib.algsolvers.*;
28
import org.distorted.objectlib.algsolvers.implemented.Solver3x3Beginner;
29
import org.distorted.objectlib.algsolvers.implemented.SolverAbstract;
25 30
import org.distorted.objectlib.algsolvers.implemented.SolverKilominx;
26 31
import org.distorted.objectlib.main.InitAssets;
27 32
import org.distorted.objectlib.main.ObjectControl;
......
31 36

  
32 37
///////////////////////////////////////////////////////////////////////////////////////////////////
33 38

  
34
public class SolverActivity extends Activity implements SolutionListener
39
public class SolverActivity extends Activity implements SolutionListener, AdapterView.OnItemSelectedListener
35 40
{
36 41
  private InitAssets mAsset;
37 42
  private SharedPreferences mPreferences;
......
40 45
  private SolvedObject mObject;
41 46
  private String mMoves;
42 47
  private int mNumMoves;
43
  private SolverKilominx mBeginner;
48
  private int mCurrSolver;
49
  private SolverAbstract mSolver;
44 50
  private long mTime;
45 51

  
46 52
///////////////////////////////////////////////////////////////////////////////////////////////////
......
58 64
    mText = findViewById(R.id.solverMoves);
59 65
    mMoves = "";
60 66
    mNumMoves = 0;
67
    Spinner objectSpinner = findViewById(R.id.solverSpinner);
68
    objectSpinner.setOnItemSelectedListener(this);
61 69

  
62
    mBeginner = new SolverKilominx();
63
    mObject = mBeginner.getObject();
70
    mCurrSolver = 0;
71
    String[] solverNames = createSolverNames();
72
    ArrayAdapter<String> objAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, solverNames);
73
    objAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
74
    objectSpinner.setAdapter(objAdapter);
64 75

  
65
    int numPhases = mBeginner.getNumPhases();
66
    String[] names = new String[numPhases];
67
    for(int p=0; p<numPhases; p++) names[p] = mBeginner.getPhaseName(p);
76
    String[] phaseNames = createSolver();
68 77

  
69
    mLowerPane = new SolverLowerPane(this,names);
78
    mLowerPane = new SolverLowerPane(this,phaseNames);
70 79
    }
71 80

  
72 81
///////////////////////////////////////////////////////////////////////////////////////////////////
......
94 103

  
95 104
    restorePreferences();
96 105

  
106
    int ordinal = getObjectOrdinal();
97 107
    ObjectControl control = getControl();
98
    control.changeObject(ListObjects.KILO_3.ordinal(), MODE_NORM, mAsset);
108
    control.changeObject(ordinal, MODE_NORM, mAsset);
99 109
    }
100 110

  
101 111
///////////////////////////////////////////////////////////////////////////////////////////////////
......
107 117
    super.onDestroy();
108 118
    }
109 119

  
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

  
122
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
123
    {
124
    if( parent.getId()==R.id.solverSpinner && mCurrSolver!=pos )
125
      {
126
      mCurrSolver = pos;
127
      String[] phaseNames = createSolver();
128
      mLowerPane.updateNames(this,phaseNames);
129
      int ordinal = getObjectOrdinal();
130
      ObjectControl control = getControl();
131
      control.changeObject(ordinal, MODE_NORM, mAsset);
132
      }
133
    }
134

  
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

  
137
    public void onNothingSelected(AdapterView<?> parent) { }
138

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

  
141
  private String[] createSolverNames()
142
    {
143
    return new String[] {"3x3", "Kilo"};
144
    }
145

  
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

  
148
  private int getObjectOrdinal()
149
    {
150
    if( mCurrSolver==1 ) return ListObjects.KILO_3.ordinal();
151
    return ListObjects.CUBE_3.ordinal();
152
    }
153

  
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

  
156
  private String[] createSolver()
157
    {
158
    switch(mCurrSolver)
159
      {
160
      case 0 : mSolver = new Solver3x3Beginner(); break;
161
      case 1 : mSolver = new SolverKilominx(); break;
162
      default: android.util.Log.e("D", "unknown solver: "+mCurrSolver);
163
               mSolver = new Solver3x3Beginner(); break;
164
      }
165

  
166
    mObject = mSolver.getObject();
167

  
168
    int numPhases = mSolver.getNumPhases();
169
    String[] names = new String[numPhases];
170
    for(int p=0; p<numPhases; p++) names[p] = mSolver.getPhaseName(p);
171

  
172
    return names;
173
    }
174

  
110 175
///////////////////////////////////////////////////////////////////////////////////////////////////
111 176

  
112 177
  public void Left(View v)       { mLowerPane.backMove(this); }
......
189 254
      @Override
190 255
      public void run()
191 256
        {
192
        mBeginner.solution(listener, qi);
257
        mSolver.solution(listener, qi);
193 258
        }
194 259
      });
195 260
    }
src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
22 22
  private static final int MOVES_PLACE_1 = 101;
23 23
  private static final int MILLIS_PER_DEGREE = 6;
24 24

  
25
  private final String[] mPhaseNames;
26 25
  private final TextView mText, mPhase;
27
  private final int mNumPhases;
28
  private final int[][][] mMoves;
29

  
26
  private String[] mPhaseNames;
27
  private int mNumPhases;
28
  private int[][][] mMoves;
30 29
  private int mNumMoves,mCurrMove,mCurrPhase;
31 30
  private boolean mCanMove;
32 31
  private ObjectControl mControl;
......
45 44
    setMoves(act,null,0);
46 45
    }
47 46

  
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

  
49
  void updateNames(SolverActivity act, String[] names)
50
    {
51
    mPhaseNames = names;
52
    mNumPhases = names.length;
53
    mMoves = new int[mNumPhases][][];
54
    mCanMove = true;
55
    setMoves(act,null,0);
56
    }
57

  
48 58
///////////////////////////////////////////////////////////////////////////////////////////////////
49 59

  
50 60
  void backPhase(SolverActivity act)
src/main/res/layout/mainlayout.xml
55 55

  
56 56
    </LinearLayout>
57 57

  
58
    <TextView
59
        android:id="@+id/solverMoves"
58
    <LinearLayout
59
        android:id="@+id/middleBar"
60 60
        android:layout_below="@id/upperBar"
61 61
        android:layout_width="match_parent"
62 62
        android:layout_height="50dp"
63
        android:background="@color/white"
64
        android:textColor="@color/black"
65
        android:layout_marginStart="4dp"
66
        android:layout_marginEnd="4dp"
67
        android:textAppearance="?android:attr/textAppearanceLarge"
68
        android:gravity="center_vertical|start"/>
63
        android:gravity="center"
64
        android:orientation="horizontal"
65
        android:background="@android:color/transparent">
66

  
67
        <TextView
68
            android:id="@+id/solverMoves"
69
            android:layout_width="0dp"
70
            android:layout_height="match_parent"
71
            android:layout_weight="2"
72
            android:background="@color/white"
73
            android:textColor="@color/black"
74
            android:layout_marginStart="4dp"
75
            android:layout_marginEnd="4dp"
76
            android:layout_marginTop="3dp"
77
            android:layout_marginBottom="5dp"
78
            android:textAppearance="?android:attr/textAppearanceLarge"
79
            android:gravity="center_vertical|start"/>
80
        <Spinner
81
            android:id="@+id/solverSpinner"
82
            android:layout_width="0dp"
83
            android:layout_height="match_parent"
84
            android:layout_weight="1"/>
85

  
86
    </LinearLayout>
69 87

  
70 88
    <LinearLayout
71 89
        android:id="@+id/phaseBar"

Also available in: Unified diff