Project

General

Profile

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

magiccube / src / main / java / org / distorted / control / RubikControlWhole.java @ ac2ee4b3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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
package org.distorted.control;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24

    
25
import org.distorted.library.effect.MatrixEffectMove;
26
import org.distorted.library.effect.MatrixEffectScale;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedNode;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshQuad;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.library.type.Dynamic;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
38

    
39
import java.io.IOException;
40
import java.io.InputStream;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class RubikControlWhole implements EffectListener
45
  {
46
  private static final int NUM_NODE = 4;
47
  private static final int NUM_EFFE = 4;
48

    
49
  private final RubikControl mControl;
50
  private DistortedEffects[] mEffects;
51
  private DistortedNode[] mNodes;
52
  private long mEffectID;
53
  private int mStageFinished, mWidth, mHeight;
54

    
55
  private MeshQuad mQuad;
56
  private DistortedTexture mTextureHand, mTextureShad, mTextureCirc;
57

    
58
  private Dynamic3D mDynMoveHand1, mDynMoveShad1;
59
  private Dynamic3D mDynScaleHand1, mDynScaleShad1;
60
  private MatrixEffectMove mMoveHand1, mMoveShad1;
61
  private MatrixEffectScale mScaleHand1, mScaleShad1;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   private Bitmap openBitmap(RubikActivity act, int resource)
66
     {
67
     try( InputStream is = act.getResources().openRawResource(resource) )
68
       {
69
       return BitmapFactory.decodeStream(is);
70
       }
71
     catch( IOException e )
72
       {
73
       // ignore
74
       }
75

    
76
     return null;
77
     }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  private void createEffectsStage1()
82
    {
83
    float x0 = mWidth*0.55f;
84
    float x1 = mWidth*0.45f;
85
    float x2 = mWidth*0.35f;
86
    float y1 = mHeight*0.28f;
87
    float z  = 0;
88
    float d  = mWidth*0.01f;
89
    float s  = mWidth*0.33f;
90

    
91
    Static3D point0h = new Static3D(-x0    ,-y1    , z);
92
    Static3D point1h = new Static3D(-x1    ,-y1    , z);
93
    Static3D point2h = new Static3D(-x2    ,-y1    , z);
94
    Static3D point3h = new Static3D(-x2  +d,-y1  +d, z);
95
    Static3D point0s = new Static3D(-x0+2*d,-y1+2*d, z);
96
    Static3D point1s = new Static3D(-x1+2*d,-y1+2*d, z);
97
    Static3D point2s = new Static3D(-x2+2*d,-y1+2*d, z);
98
    Static3D point3s = new Static3D(-x2  +d,-y1  +d, z);
99

    
100
    Static3D pointSc = new Static3D(s,s,s);
101

    
102
    mDynMoveHand1.add(point0h);
103
    mDynMoveHand1.add(point1h);
104
    mDynMoveHand1.add(point2h);
105
    mDynMoveHand1.add(point2h);
106
    mDynMoveHand1.add(point3h);
107
    mDynMoveShad1.add(point0s);
108
    mDynMoveShad1.add(point1s);
109
    mDynMoveShad1.add(point2s);
110
    mDynMoveShad1.add(point2s);
111
    mDynMoveShad1.add(point3s);
112
    mDynScaleHand1.add(pointSc);
113
    mDynScaleShad1.add(pointSc);
114

    
115
    mStageFinished = 1;
116
    }
117

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

    
120
  private void createEffectsStage2()
121
    {
122
    float x2 = mWidth*0.35f;
123
    float y1 = mHeight*0.28f;
124
    float z  = 0;
125
    float d  = mWidth*0.01f;
126
    float sS = mWidth*0.0001f;
127
    float sF = mWidth*0.16f;
128

    
129
    Static3D point3h = new Static3D(-x2  +d,-y1  +d, z);
130
    Static3D scaleS  = new Static3D(sS,sS,sS);
131
    Static3D scaleF  = new Static3D(sF,sF,sF);
132

    
133
    mDynMoveHand1.removeAll();
134
    mDynMoveHand1.add(point3h);
135
    mDynMoveShad1.removeAll();
136
    mDynMoveShad1.add(point3h);
137
    mDynScaleShad1.removeAll();
138
    mDynScaleShad1.add(scaleS);
139
    mDynScaleShad1.add(scaleF);
140
    mDynScaleShad1.resetToBeginning();
141

    
142
    mNodes[0].changeInputSurface(mTextureCirc);
143

    
144
    mStageFinished = 2;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void reloadEffectsStage1()
150
    {
151
    mDynMoveHand1.resetToBeginning();
152
    mDynMoveShad1.resetToBeginning();
153
    mMoveHand1.notifyWhenFinished(this);
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private void createEffects()
159
    {
160
    if( mEffects==null )
161
      {
162
      mEffects = new DistortedEffects[NUM_EFFE];
163
      for(int i=0; i<NUM_EFFE; i++) mEffects[i]= new DistortedEffects();
164

    
165
      int time = 3000;
166

    
167
      mDynMoveHand1 = new Dynamic3D(time,0.5f);
168
      mDynMoveHand1.setMode(Dynamic.MODE_PATH);
169
      mDynMoveHand1.setConvexity(0.0f);
170
      mDynMoveShad1 = new Dynamic3D(time,0.5f);
171
      mDynMoveShad1.setMode(Dynamic.MODE_PATH);
172
      mDynMoveShad1.setConvexity(0.0f);
173
      mDynScaleHand1 = new Dynamic3D(time,0.5f);
174
      mDynScaleHand1.setMode(Dynamic.MODE_PATH);
175
      mDynScaleHand1.setConvexity(0.0f);
176
      mDynScaleShad1 = new Dynamic3D(time,0.5f);
177
      mDynScaleShad1.setMode(Dynamic.MODE_PATH);
178
      mDynScaleShad1.setConvexity(0.0f);
179

    
180
      mMoveHand1 = new MatrixEffectMove(mDynMoveHand1);
181
      mMoveShad1 = new MatrixEffectMove(mDynMoveShad1);
182
      mScaleHand1= new MatrixEffectScale(mDynScaleHand1);
183
      mScaleShad1= new MatrixEffectScale(mDynScaleShad1);
184

    
185
      mMoveHand1.notifyWhenFinished(this);
186

    
187
      mEffects[0].apply(mScaleShad1);
188
      mEffects[0].apply(mMoveShad1);
189
      mEffects[2].apply(mScaleHand1);
190
      mEffects[2].apply(mMoveHand1);
191

    
192
      DistortedScreen screen = mControl.getScreen();
193
      mWidth = screen.getWidth();
194
      mHeight= screen.getHeight();
195

    
196
      createEffectsStage1();
197
      }
198
    else reloadEffectsStage1();
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  private void createNodes()
204
    {
205
    if( mNodes==null )
206
      {
207
      mNodes = new DistortedNode[NUM_NODE];
208
      mQuad  = new MeshQuad();
209
      }
210

    
211
    RubikActivity act = mControl.getActivity();
212

    
213
    if( act!=null )
214
      {
215
      Bitmap bmpCirc = openBitmap(act, R.drawable.ui_fading_circle);
216
      Bitmap bmpShad = openBitmap(act, R.drawable.ui_hand_shadow);
217
      Bitmap bmpHand = openBitmap(act, R.drawable.ui_hand_pointer);
218

    
219
      mTextureCirc = new DistortedTexture();
220
      mTextureCirc.setTexture(bmpCirc);
221
      mTextureShad = new DistortedTexture();
222
      mTextureShad.setTexture(bmpShad);
223
      mTextureHand = new DistortedTexture();
224
      mTextureHand.setTexture(bmpHand);
225

    
226
      mNodes[0]= new DistortedNode(mTextureShad,mEffects[0],mQuad);
227
      mNodes[1]= new DistortedNode(mTextureShad,mEffects[1],mQuad);
228
      mNodes[2]= new DistortedNode(mTextureHand,mEffects[2],mQuad);
229
      mNodes[3]= new DistortedNode(mTextureHand,mEffects[3],mQuad);
230
      }
231
    else
232
      {
233
      android.util.Log.e("D", "Activity NULL!!");
234
      }
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  long getEffectID()
240
    {
241
    return mEffectID;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  DistortedNode[] getNodes()
247
    {
248
    createEffects();
249
    createNodes();
250

    
251
    return mNodes;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  DistortedNode[] returnNodes()
257
    {
258
    return mNodes;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  RubikControlWhole(RubikControl control)
264
    {
265
    mControl = control;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
// PUBLIC
270

    
271
  public void effectFinished(long effectID)
272
    {
273
    switch( mStageFinished )
274
      {
275
      case 1: createEffectsStage2();
276
              break;
277
      case 2: mEffectID = -1;
278
              mControl.effectFinished(mEffectID);
279
              break;
280
      }
281
    }
282
  }
(3-3/3)