Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObject.java @ 70b76549

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 4f9f99a2 Leszek Koltunski
package org.distorted.object;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
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 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
33 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
34 f16ff19d Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38 4f9f99a2 Leszek Koltunski
39 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 27a70eae Leszek Koltunski
public abstract class RubikObject extends DistortedNode
42 fdec60a3 Leszek Koltunski
  {
43 27a70eae Leszek Koltunski
  static final float OBJECT_SCREEN_RATIO = 0.5f;
44
  static final int TEXTURE_SIZE = 100;
45
46 f16ff19d Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
47 70b76549 Leszek Koltunski
  final float[] LEGAL_QUATS;
48 f16ff19d Leszek Koltunski
49 27a70eae Leszek Koltunski
  private Static3D mMove, mScale, mNodeMove, mNodeScale;
50
  private Static4D mQuatAccumulated;
51
  private DistortedTexture mNodeTexture;
52
53
  int mSize, mRotAxis, mRotRow;
54 fdec60a3 Leszek Koltunski
55 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
56
  DistortedTexture mTexture;
57
58
  VertexEffectSink mSinkEffect;
59
  MatrixEffectMove mMoveEffect;
60
  MatrixEffectScale mScaleEffect;
61
  MatrixEffectQuaternion mQuatCEffect;
62
  MatrixEffectQuaternion mQuatAEffect;
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65 fdec60a3 Leszek Koltunski
66 97c012ae Leszek Koltunski
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
67 fdec60a3 Leszek Koltunski
    {
68 27a70eae Leszek Koltunski
    super(texture,effects,mesh);
69 fdec60a3 Leszek Koltunski
70 f16ff19d Leszek Koltunski
    LEGAL_QUATS = getLegalQuats();
71 27a70eae Leszek Koltunski
    mNodeTexture = texture;
72
    mSize = size;
73
74
    mRotationAngleStatic = new Static1D(0);
75
    mRotationAngleMiddle = new Static1D(0);
76
    mRotationAngleFinal  = new Static1D(0);
77
78
    mMove     = new Static3D(0,0,0);
79
    mScale    = new Static3D(1,1,1);
80
    mNodeMove = new Static3D(0,0,0);
81
    mNodeScale= new Static3D(1,1,1);
82
83
    mQuatAccumulated = quatAcc;
84
85
    Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
86
    Static3D matrCenter = new Static3D(0,0,0);
87
    Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
88
89
    mSinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), sinkCenter, region );
90
    mMoveEffect = new MatrixEffectMove(mMove);
91
    mScaleEffect = new MatrixEffectScale(mScale);
92
    mQuatCEffect = new MatrixEffectQuaternion(quatCur, matrCenter);
93
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, matrCenter);
94
95
    MatrixEffectMove  nodeMoveEffect  = new MatrixEffectMove(mNodeMove);
96
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
97
98
    effects.apply(nodeScaleEffect);
99
    effects.apply(nodeMoveEffect);
100
    }
101
102 f16ff19d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
  void resetRotationAngle(Dynamic1D rotationAngle)
105
    {
106
    rotationAngle.setDuration(POST_ROTATION_MILLISEC);
107
    rotationAngle.resetToBeginning();
108
    rotationAngle.removeAll();
109
    rotationAngle.add(mRotationAngleStatic);
110
    rotationAngle.add(mRotationAngleMiddle);
111
    rotationAngle.add(mRotationAngleFinal);
112
    }
113
114 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 27a70eae Leszek Koltunski
  private float getSinkStrength()
117 dd73fdab Leszek Koltunski
    {
118 27a70eae Leszek Koltunski
    switch(mSize)
119
      {
120
      case 1 : return 1.1f;
121
      case 2 : return 1.5f;
122
      case 3 : return 1.8f;
123
      case 4 : return 2.0f;
124
      default: return 3.0f - 4.0f/mSize;
125
      }
126 dd73fdab Leszek Koltunski
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 27a70eae Leszek Koltunski
  public int getSize()
131 fdec60a3 Leszek Koltunski
    {
132 27a70eae Leszek Koltunski
    return mSize;
133 fdec60a3 Leszek Koltunski
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
138 fdec60a3 Leszek Koltunski
    {
139 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
140 fdec60a3 Leszek Koltunski
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
145
      {
146
      return mQuatAccumulated;
147
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151
  public void recomputeScaleFactor(int screenWidth, int screenHeight)
152 fdec60a3 Leszek Koltunski
    {
153 27a70eae Leszek Koltunski
    int texW = mNodeTexture.getWidth();
154
    int texH = mNodeTexture.getHeight();
155
156
    if( (float)texH/texW > (float)screenHeight/screenWidth )
157
      {
158
      int w = (screenHeight*texW)/texH;
159
      float factor = (float)screenHeight/texH;
160
      mNodeMove.set((screenWidth-w)*0.5f ,0, 0);
161
      mNodeScale.set(factor,factor,factor);
162
      }
163
    else
164
      {
165
      int h = (screenWidth*texH)/texW;
166
      float factor = (float)screenWidth/texW;
167
      mNodeMove.set(0,(screenHeight-h)*0.5f,0);
168
      mNodeScale.set(factor,factor,factor);
169
      }
170
171
    float scaleFactor = (OBJECT_SCREEN_RATIO*texW/(TEXTURE_SIZE*mSize));
172
173
    mMove.set( texW*0.5f , texH*0.5f , 0.0f );
174
    mScale.set(scaleFactor,scaleFactor,scaleFactor);
175 fdec60a3 Leszek Koltunski
    }
176 27a70eae Leszek Koltunski
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179 f16ff19d Leszek Koltunski
  abstract float[] getLegalQuats();
180
181 27a70eae Leszek Koltunski
  public abstract void savePreferences(SharedPreferences.Editor editor);
182
  public abstract void restorePreferences(SharedPreferences preferences);
183
184
  public abstract void beginNewRotation(int vector, int row );
185
  public abstract long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener );
186
  public abstract long finishRotationNow(EffectListener listener);
187
  public abstract void removeRotationNow();
188
189
  public abstract void apply(Effect effect, int position);
190
  public abstract void remove(long effectID);
191 74686c71 Leszek Koltunski
192
  public abstract void releaseResources();
193
  public abstract void createTexture();
194
195 27a70eae Leszek Koltunski
  public abstract void solve();
196
  public abstract boolean isSolved();
197 fdec60a3 Leszek Koltunski
  }