Revision 27a70eae
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/object/RubikObject.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.object; |
21 | 21 |
|
22 |
import org.distorted.magic.R; |
|
22 |
import android.content.SharedPreferences; |
|
23 |
|
|
24 |
import org.distorted.library.effect.Effect; |
|
25 |
import org.distorted.library.effect.MatrixEffectMove; |
|
26 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
|
27 |
import org.distorted.library.effect.MatrixEffectScale; |
|
28 |
import org.distorted.library.effect.VertexEffectSink; |
|
29 |
import org.distorted.library.main.DistortedEffects; |
|
30 |
import org.distorted.library.main.DistortedNode; |
|
31 |
import org.distorted.library.main.DistortedTexture; |
|
32 |
import org.distorted.library.mesh.MeshFlat; |
|
33 |
import org.distorted.library.message.EffectListener; |
|
34 |
import org.distorted.library.type.Static1D; |
|
35 |
import org.distorted.library.type.Static3D; |
|
36 |
import org.distorted.library.type.Static4D; |
|
23 | 37 |
|
24 | 38 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
25 | 39 |
|
26 |
public enum RubikObject
|
|
40 |
public abstract class RubikObject extends DistortedNode
|
|
27 | 41 |
{ |
28 |
CUBE2 ( 2, R.drawable.button2 ), |
|
29 |
CUBE3 ( 3, R.drawable.button3 ), |
|
30 |
CUBE4 ( 4, R.drawable.button4 ), |
|
31 |
CUBE5 ( 5, R.drawable.button5 ), |
|
32 |
; |
|
42 |
static final float OBJECT_SCREEN_RATIO = 0.5f; |
|
43 |
static final int POST_ROTATION_MILLISEC = 500; |
|
44 |
static final int TEXTURE_SIZE = 100; |
|
45 |
|
|
46 |
private Static3D mMove, mScale, mNodeMove, mNodeScale; |
|
47 |
private Static4D mQuatAccumulated; |
|
48 |
private DistortedTexture mNodeTexture; |
|
49 |
|
|
50 |
int mSize, mRotAxis, mRotRow; |
|
33 | 51 |
|
34 |
public static final int LENGTH = values().length; |
|
35 |
private final int mObjectSize, mIconID; |
|
36 |
private static final RubikObject[] objects; |
|
52 |
Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal; |
|
53 |
DistortedTexture mTexture; |
|
54 |
|
|
55 |
VertexEffectSink mSinkEffect; |
|
56 |
MatrixEffectMove mMoveEffect; |
|
57 |
MatrixEffectScale mScaleEffect; |
|
58 |
MatrixEffectQuaternion mQuatCEffect; |
|
59 |
MatrixEffectQuaternion mQuatAEffect; |
|
60 |
|
|
61 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
37 | 62 |
|
38 |
static
|
|
63 |
RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshFlat mesh, DistortedEffects effects)
|
|
39 | 64 |
{ |
40 |
int i = 0; |
|
41 |
objects = new RubikObject[LENGTH]; |
|
65 |
super(texture,effects,mesh); |
|
42 | 66 |
|
43 |
for(RubikObject size: RubikObject.values()) |
|
44 |
{ |
|
45 |
objects[i] = size; |
|
46 |
i++; |
|
47 |
} |
|
67 |
mNodeTexture = texture; |
|
68 |
|
|
69 |
mSize = size; |
|
70 |
|
|
71 |
mRotationAngleStatic = new Static1D(0); |
|
72 |
mRotationAngleMiddle = new Static1D(0); |
|
73 |
mRotationAngleFinal = new Static1D(0); |
|
74 |
|
|
75 |
mMove = new Static3D(0,0,0); |
|
76 |
mScale = new Static3D(1,1,1); |
|
77 |
mNodeMove = new Static3D(0,0,0); |
|
78 |
mNodeScale= new Static3D(1,1,1); |
|
79 |
|
|
80 |
mQuatAccumulated = quatAcc; |
|
81 |
|
|
82 |
Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f); |
|
83 |
Static3D matrCenter = new Static3D(0,0,0); |
|
84 |
Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f); |
|
85 |
|
|
86 |
mSinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), sinkCenter, region ); |
|
87 |
mMoveEffect = new MatrixEffectMove(mMove); |
|
88 |
mScaleEffect = new MatrixEffectScale(mScale); |
|
89 |
mQuatCEffect = new MatrixEffectQuaternion(quatCur, matrCenter); |
|
90 |
mQuatAEffect = new MatrixEffectQuaternion(quatAcc, matrCenter); |
|
91 |
|
|
92 |
MatrixEffectMove nodeMoveEffect = new MatrixEffectMove(mNodeMove); |
|
93 |
MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale); |
|
94 |
|
|
95 |
effects.apply(nodeScaleEffect); |
|
96 |
effects.apply(nodeMoveEffect); |
|
97 |
} |
|
98 |
|
|
99 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
100 |
|
|
101 |
int computeNearestAngle(float angle) |
|
102 |
{ |
|
103 |
final int NEAREST = 90; |
|
104 |
|
|
105 |
int tmp = (int)((angle+NEAREST/2)/NEAREST); |
|
106 |
if( angle< -(NEAREST*0.5) ) tmp-=1; |
|
107 |
|
|
108 |
return NEAREST*tmp; |
|
48 | 109 |
} |
49 | 110 |
|
50 | 111 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
51 | 112 |
|
52 |
public static RubikObject getObject(int ordinal)
|
|
113 |
private float getSinkStrength()
|
|
53 | 114 |
{ |
54 |
return objects[ordinal]; |
|
115 |
switch(mSize) |
|
116 |
{ |
|
117 |
case 1 : return 1.1f; |
|
118 |
case 2 : return 1.5f; |
|
119 |
case 3 : return 1.8f; |
|
120 |
case 4 : return 2.0f; |
|
121 |
default: return 3.0f - 4.0f/mSize; |
|
122 |
} |
|
55 | 123 |
} |
56 | 124 |
|
57 | 125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
58 | 126 |
|
59 |
RubikObject(int size, int iconID)
|
|
127 |
public int getSize()
|
|
60 | 128 |
{ |
61 |
mObjectSize = size; |
|
62 |
mIconID = iconID; |
|
129 |
return mSize; |
|
63 | 130 |
} |
64 | 131 |
|
65 | 132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
66 | 133 |
|
67 |
public int getIconID()
|
|
134 |
public void continueRotation(float angleInDegrees)
|
|
68 | 135 |
{ |
69 |
return mIconID;
|
|
136 |
mRotationAngleStatic.set0(angleInDegrees);
|
|
70 | 137 |
} |
71 | 138 |
|
72 | 139 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
73 | 140 |
|
74 |
public int getObjectSize() |
|
141 |
public Static4D getRotationQuat() |
|
142 |
{ |
|
143 |
return mQuatAccumulated; |
|
144 |
} |
|
145 |
|
|
146 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
147 |
|
|
148 |
public void recomputeScaleFactor(int screenWidth, int screenHeight) |
|
75 | 149 |
{ |
76 |
return mObjectSize; |
|
150 |
int texW = mNodeTexture.getWidth(); |
|
151 |
int texH = mNodeTexture.getHeight(); |
|
152 |
|
|
153 |
if( (float)texH/texW > (float)screenHeight/screenWidth ) |
|
154 |
{ |
|
155 |
int w = (screenHeight*texW)/texH; |
|
156 |
float factor = (float)screenHeight/texH; |
|
157 |
mNodeMove.set((screenWidth-w)*0.5f ,0, 0); |
|
158 |
mNodeScale.set(factor,factor,factor); |
|
159 |
} |
|
160 |
else |
|
161 |
{ |
|
162 |
int h = (screenWidth*texH)/texW; |
|
163 |
float factor = (float)screenWidth/texW; |
|
164 |
mNodeMove.set(0,(screenHeight-h)*0.5f,0); |
|
165 |
mNodeScale.set(factor,factor,factor); |
|
166 |
} |
|
167 |
|
|
168 |
float scaleFactor = (OBJECT_SCREEN_RATIO*texW/(TEXTURE_SIZE*mSize)); |
|
169 |
|
|
170 |
mMove.set( texW*0.5f , texH*0.5f , 0.0f ); |
|
171 |
mScale.set(scaleFactor,scaleFactor,scaleFactor); |
|
77 | 172 |
} |
173 |
|
|
174 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
175 |
|
|
176 |
public abstract void savePreferences(SharedPreferences.Editor editor); |
|
177 |
public abstract void restorePreferences(SharedPreferences preferences); |
|
178 |
|
|
179 |
public abstract void beginNewRotation(int vector, int row ); |
|
180 |
public abstract long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener ); |
|
181 |
public abstract long finishRotationNow(EffectListener listener); |
|
182 |
public abstract void removeRotationNow(); |
|
183 |
|
|
184 |
public abstract void releaseResources(); |
|
185 |
public abstract void createTexture(); |
|
186 |
public abstract void apply(Effect effect, int position); |
|
187 |
public abstract void remove(long effectID); |
|
188 |
public abstract void solve(); |
|
189 |
public abstract boolean isSolved(); |
|
78 | 190 |
} |
Also available in: Unified diff
Make RubikCube and RubikCubeMovement generic and not visible outside of their package.