Project

General

Profile

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

magiccube / src / main / java / org / distorted / overlays / OverlayStars.java @ 97201782

1 d7f0c07d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.overlays;
11
12
import android.content.res.Resources;
13
import android.graphics.Bitmap;
14
import android.graphics.BitmapFactory;
15 8feb68c2 Leszek Koltunski
import android.graphics.Canvas;
16
import android.graphics.Paint;
17 d7f0c07d Leszek Koltunski
18 8feb68c2 Leszek Koltunski
import org.distorted.library.effect.EffectQuality;
19 0da4feff Leszek Koltunski
import org.distorted.library.effect.FragmentEffectAlpha;
20 fc6d738d Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
21
import org.distorted.library.effect.MatrixEffectScale;
22 8feb68c2 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffectGlow;
23 b4cbe056 Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
24
import org.distorted.library.effect.VertexEffectScale;
25 d7f0c07d Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
26
import org.distorted.library.main.DistortedNode;
27
import org.distorted.library.main.DistortedScreen;
28
import org.distorted.library.main.DistortedTexture;
29 b4cbe056 Leszek Koltunski
import org.distorted.library.main.InternalOutputSurface;
30
import org.distorted.library.mesh.MeshJoined;
31 d7f0c07d Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
32
import org.distorted.library.message.EffectListener;
33 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Dynamic;
34 0da4feff Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
36 b4cbe056 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 305f368e Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
38 0da4feff Leszek Koltunski
import org.distorted.library.type.Static1D;
39 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Static2D;
40 d7f0c07d Leszek Koltunski
import org.distorted.library.type.Static3D;
41 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Static4D;
42 d7f0c07d Leszek Koltunski
import org.distorted.main.R;
43
44 b4cbe056 Leszek Koltunski
import java.util.Random;
45
46 d7f0c07d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
public class OverlayStars extends OverlayGeneric implements EffectListener
49
{
50 ec4987d4 Leszek Koltunski
   private static final int DUR_APP =  1500;
51 5b60eb06 Leszek Koltunski
   private static final int DUR_MOV =  3000;
52 ec4987d4 Leszek Koltunski
   private static final int DUR_GLO =   600;
53 b4cbe056 Leszek Koltunski
54 f8e8a08e Leszek Koltunski
   private static final int MAX_FALLING = 50;
55
56 d7f0c07d Leszek Koltunski
   private ListenerOverlay mListener;
57 fc6d738d Leszek Koltunski
   private DistortedNode mNodeFalling, mNodeCentral;
58 d7f0c07d Leszek Koltunski
   private DistortedScreen mScreen;
59 0da4feff Leszek Koltunski
   private DistortedTexture mTexture;
60
   private Bitmap mCountBitmap, mStarBitmap;
61 8feb68c2 Leszek Koltunski
   private Canvas mCountCanvas;
62
   private Paint mPaint;
63
   private int mBmpW, mBmpH;
64 b4cbe056 Leszek Koltunski
   private float mWidth, mHeight;
65
   private Random mRandom;
66 ec4987d4 Leszek Koltunski
   private long mMoveID, mGlow1ID, mGlow2ID, mAlphaID;
67 8feb68c2 Leszek Koltunski
   private int mTotalStars, mNewStars;
68 0da4feff Leszek Koltunski
   private FragmentEffectAlpha mAlpha;
69
   private Dynamic1D mAlphaStrength;
70 0c67ea13 Leszek Koltunski
   private boolean mIncrease;
71 ec4987d4 Leszek Koltunski
   private float mTextHeight;
72 b4cbe056 Leszek Koltunski
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 0c67ea13 Leszek Koltunski
   private Dynamic3D createRandomMove(boolean increase)
76 b4cbe056 Leszek Koltunski
      {
77
      Dynamic3D move = new Dynamic3D();
78 8feb68c2 Leszek Koltunski
      move.setMode(Dynamic.MODE_PATH);
79 b4cbe056 Leszek Koltunski
      move.setDuration(DUR_MOV);
80 f4a6e8ce Leszek Koltunski
      move.setCount( increase ? 0.40f : 0.50f );
81 b4cbe056 Leszek Koltunski
82 8feb68c2 Leszek Koltunski
      float widthS = (mRandom.nextFloat()-0.5f)*mWidth*1.10f;
83 b4cbe056 Leszek Koltunski
      float widthM = widthS + (mRandom.nextFloat()-0.5f)*mWidth*0.2f;
84 8feb68c2 Leszek Koltunski
      float heighS = mRandom.nextFloat()*mHeight*0.2f;
85
      float heighM = (mRandom.nextFloat()-0.5f)*mHeight*0.2f;
86 b4cbe056 Leszek Koltunski
87 7654a99d Leszek Koltunski
      Static3D pointS = new Static3D(widthS,mHeight*0.60f+heighS,0);
88 8feb68c2 Leszek Koltunski
      Static3D pointM = new Static3D(widthM,mHeight*0.25f+heighM,0);
89
      Static3D pointE = new Static3D(0,0,0);
90 702d6752 Leszek Koltunski
      Static3D pointF = new Static3D(0,0,-10000);
91 b4cbe056 Leszek Koltunski
92 0c67ea13 Leszek Koltunski
      if( increase )
93
         {
94
         move.add(pointS);
95
         move.add(pointM);
96
         move.add(pointE);
97
         move.add(pointF);
98
         }
99
      else
100
         {
101
         move.add(pointF);
102
         move.add(pointE);
103
         move.add(pointM);
104
         move.add(pointS);
105
         }
106 b4cbe056 Leszek Koltunski
107
      return move;
108
      }
109 d7f0c07d Leszek Koltunski
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 0da4feff Leszek Koltunski
   private void createAlphaEffect(boolean appear)
113
      {
114
      if( mAlpha==null )
115
         {
116
         mAlphaStrength = new Dynamic1D();
117
         mAlphaStrength.setMode(Dynamic.MODE_PATH);
118 6828c498 Leszek Koltunski
         mAlphaStrength.setDuration( mNewStars==0 ? 3*DUR_APP : DUR_APP);
119 0da4feff Leszek Koltunski
         mAlphaStrength.setCount(0.5f);
120
         equipAlpha(mAlphaStrength,appear);
121
         mAlpha = new FragmentEffectAlpha(mAlphaStrength);
122 063ae851 Leszek Koltunski
123
         if( mNewStars==0 )
124
            {
125
            mMoveID = mAlpha.getID();
126
            mAlpha.notifyWhenFinished(this);
127
            }
128 0da4feff Leszek Koltunski
         }
129
      else
130
         {
131
         mAlphaStrength.resetToBeginning();
132
         equipAlpha(mAlphaStrength,appear);
133
         }
134
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
   private void equipAlpha(Dynamic1D alpha, boolean appear)
139
      {
140
      Static1D point0 = new Static1D(0.0f);
141
      Static1D point1 = new Static1D(1.0f);
142
143
      alpha.removeAll();
144
145
      if( appear )
146
        {
147
        alpha.add(point0);
148 6828c498 Leszek Koltunski
        if( mNewStars==0 ) alpha.add(point1);
149 0da4feff Leszek Koltunski
        alpha.add(point1);
150
        }
151
      else
152
        {
153
        alpha.add(point1);
154 6828c498 Leszek Koltunski
        if( mNewStars==0 ) alpha.add(point1);
155 0da4feff Leszek Koltunski
        alpha.add(point0);
156
        }
157
      }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 ec4987d4 Leszek Koltunski
   private float computeQuotientAndTextHeight(int total)
162
      {
163
      if( total>=10000 )
164
         {
165
         mTextHeight = 0.610f;
166
         return 0.16f;
167
         }
168
      if( total>= 1000 )
169
         {
170
         mTextHeight = 0.625f;
171
         return 0.20f;
172
         }
173
      if( total>=  100 )
174
         {
175
         mTextHeight = 0.640f;
176
         return 0.26f;
177
         }
178
179
      mTextHeight = 0.655f;
180
      return 0.28f;
181
      }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185
   private void createBitmap(Resources res, int total)
186 d7f0c07d Leszek Koltunski
      {
187 fc6d738d Leszek Koltunski
      mStarBitmap = BitmapFactory.decodeResource(res, R.drawable.star);
188
      mBmpW = mStarBitmap.getWidth();
189
      mBmpH = mStarBitmap.getHeight();
190
      mCountBitmap = Bitmap.createBitmap(mBmpW,mBmpH,Bitmap.Config.ARGB_8888);
191
      mCountCanvas = new Canvas(mCountBitmap);
192 ec4987d4 Leszek Koltunski
193
      float quotient = computeQuotientAndTextHeight(total);
194
195 fc6d738d Leszek Koltunski
      mPaint = new Paint();
196
      mPaint.setColor(0xff000000);
197 ec4987d4 Leszek Koltunski
      mPaint.setTextSize(mBmpH*quotient);
198 fc6d738d Leszek Koltunski
      mPaint.setAntiAlias(true);
199
      mPaint.setTextAlign(Paint.Align.CENTER);
200
      }
201 d7f0c07d Leszek Koltunski
202 fc6d738d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
203 b4cbe056 Leszek Koltunski
204 fc6d738d Leszek Koltunski
   private DistortedNode createNodeFalling(boolean increase)
205
      {
206
      DistortedTexture texture = new DistortedTexture();
207
      texture.setTexture(mStarBitmap);
208 f8e8a08e Leszek Koltunski
      int numFalling = Math.min(mNewStars,MAX_FALLING);
209 0da4feff Leszek Koltunski
210 f8e8a08e Leszek Koltunski
      MeshQuad[] mesh = new MeshQuad[numFalling];
211 0da4feff Leszek Koltunski
212 f8e8a08e Leszek Koltunski
      for(int i=0; i<numFalling; i++)
213 b4cbe056 Leszek Koltunski
         {
214
         mesh[i] = new MeshQuad();
215 fc6d738d Leszek Koltunski
         mesh[i].setEffectAssociation(0,1,i+1);
216 b4cbe056 Leszek Koltunski
         }
217 d7f0c07d Leszek Koltunski
218 b4cbe056 Leszek Koltunski
      MeshJoined wholeMesh = new MeshJoined(mesh);
219
220
      DistortedEffects effects = new DistortedEffects();
221 b1178f5f Leszek Koltunski
      VertexEffectScale scaleE = new VertexEffectScale(mHeight*0.10f);
222 b4cbe056 Leszek Koltunski
      scaleE.setMeshAssociation(1,-1);
223
      effects.apply(scaleE);
224
225 f8e8a08e Leszek Koltunski
      for(int i=0; i<numFalling; i++)
226 b4cbe056 Leszek Koltunski
        {
227 0c67ea13 Leszek Koltunski
        Dynamic3D moveP = createRandomMove(increase);
228 b4cbe056 Leszek Koltunski
        VertexEffectMove moveE= new VertexEffectMove(moveP);
229 fc6d738d Leszek Koltunski
        moveE.setMeshAssociation(0,i+1);
230 b4cbe056 Leszek Koltunski
        effects.apply(moveE);
231 8feb68c2 Leszek Koltunski
232
        if( i==0 )
233
           {
234
           mMoveID = moveE.getID();
235
           moveE.notifyWhenFinished(this);
236
           }
237 b4cbe056 Leszek Koltunski
        }
238
239 fc6d738d Leszek Koltunski
      return new DistortedNode(texture,effects,wholeMesh);
240
      }
241 b4cbe056 Leszek Koltunski
242 fc6d738d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
243
244
   private DistortedNode createNodeCentral(int numStars)
245
      {
246
      mTexture = new DistortedTexture();
247
      renderStars(numStars);
248
249
      MeshQuad mesh = new MeshQuad();
250
251
      DistortedEffects effects = new DistortedEffects();
252 aa622165 Leszek Koltunski
      float scaleM  = mHeight*0.22f;
253 fc6d738d Leszek Koltunski
      Static3D moveM= new Static3D(0,0,1);
254
      MatrixEffectMove move  = new MatrixEffectMove(moveM);
255
      MatrixEffectScale scale= new MatrixEffectScale(scaleM);
256
      effects.apply(move);
257
      effects.apply(scale);
258 0da4feff Leszek Koltunski
      createAlphaEffect(true);
259
      effects.apply(mAlpha);
260
261 fc6d738d Leszek Koltunski
      return new DistortedNode(mTexture,effects,mesh);
262 8feb68c2 Leszek Koltunski
      }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266
   private void renderStars(int numStars)
267
      {
268
      String txt = ""+numStars;
269 0da4feff Leszek Koltunski
      mCountBitmap.eraseColor(0x00000000);
270
      mCountCanvas.drawBitmap(mStarBitmap,0,0,null);
271 ec4987d4 Leszek Koltunski
      mCountCanvas.drawText(txt,mBmpW*0.5f,mBmpH*mTextHeight,mPaint);
272 0da4feff Leszek Koltunski
      mTexture.setTexture(mCountBitmap);
273 d7f0c07d Leszek Koltunski
      }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
   public long startOverlay(DistortedScreen screen, ListenerOverlay listener, DataGeneric data)
278
      {
279 1eac2fbc Leszek Koltunski
      mRandom = new Random();
280 d7f0c07d Leszek Koltunski
      mScreen = screen;
281
      mListener= listener;
282
      DataStars stars = (DataStars)data;
283 305f368e Leszek Koltunski
      mTotalStars = stars.getTotal();
284 5b60eb06 Leszek Koltunski
      mNewStars   = stars.getNew();
285 d7f0c07d Leszek Koltunski
      Resources res = stars.getResources();
286 b4cbe056 Leszek Koltunski
      mWidth = mScreen.getWidth();
287
      mHeight= mScreen.getHeight();
288 0c67ea13 Leszek Koltunski
      mIncrease = mNewStars>0;
289
      if( !mIncrease ) mNewStars = -mNewStars;
290 fc6d738d Leszek Koltunski
291 ec4987d4 Leszek Koltunski
      createBitmap(res, mTotalStars);
292 063ae851 Leszek Koltunski
293
      if( mNewStars!=0 )
294
         {
295
         mNodeFalling = createNodeFalling(mIncrease);
296
         mNodeFalling.enableDepthStencil(InternalOutputSurface.NO_DEPTH_NO_STENCIL);
297
         mScreen.attach(mNodeFalling);
298
         }
299
300 fc6d738d Leszek Koltunski
      mNodeCentral = createNodeCentral(mTotalStars);
301
      mNodeCentral.glDepthMask(false);
302
      mScreen.attach(mNodeCentral);
303 d7f0c07d Leszek Koltunski
304
      return 0;
305
      }
306
307 ec4987d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
308
309
   private PostprocessEffectGlow constructGlow(boolean firstPhase)
310
      {
311 6828c498 Leszek Koltunski
      Dynamic2D haloRadius = new Dynamic2D( mNewStars==0 ? 2*DUR_GLO : DUR_GLO,0.5f);
312 305f368e Leszek Koltunski
      Static2D point20 = new Static2D( 0, 0);
313
      Static2D point21 = new Static2D(15,50);
314
      Dynamic4D color = new Dynamic4D(DUR_GLO, 0.5f);
315
      Static4D point40 = new Static4D(1,1,1,0.0f);
316
      Static4D point41 = new Static4D(1,1,1,0.8f);
317 ec4987d4 Leszek Koltunski
318
      if( firstPhase )
319
         {
320 305f368e Leszek Koltunski
         haloRadius.add(point20);
321
         haloRadius.add(point21);
322
         color.add(point40);
323
         color.add(point41);
324 ec4987d4 Leszek Koltunski
         }
325
      else
326
         {
327 305f368e Leszek Koltunski
         haloRadius.add(point21);
328
         haloRadius.add(point20);
329
         color.add(point41);
330
         color.add(point40);
331 ec4987d4 Leszek Koltunski
         }
332
333
      PostprocessEffectGlow glow = new PostprocessEffectGlow(haloRadius,color);
334
      glow.setQuality(EffectQuality.MEDIUM);
335
      glow.notifyWhenFinished(this);
336
337
      return glow;
338
      }
339
340 d7f0c07d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
341
342
   public void effectFinished(long id)
343
      {
344 8feb68c2 Leszek Koltunski
      if( id==mMoveID )
345
         {
346 ec4987d4 Leszek Koltunski
         PostprocessEffectGlow glow = constructGlow(true);
347
         mGlow1ID = glow.getID();
348 fc6d738d Leszek Koltunski
         DistortedEffects effects = mNodeCentral.getEffects();
349 8feb68c2 Leszek Koltunski
         effects.apply(glow);
350 ec4987d4 Leszek Koltunski
         }
351
      if( id==mGlow1ID )
352
         {
353 0c67ea13 Leszek Koltunski
         renderStars(mTotalStars+(mIncrease ? mNewStars : -mNewStars));
354 ec4987d4 Leszek Koltunski
         PostprocessEffectGlow glow = constructGlow(false);
355
         mGlow2ID = glow.getID();
356
         DistortedEffects effects = mNodeCentral.getEffects();
357
         effects.abortById(mGlow1ID);
358
         effects.apply(glow);
359 8feb68c2 Leszek Koltunski
         }
360 ec4987d4 Leszek Koltunski
      if( id==mGlow2ID )
361 8feb68c2 Leszek Koltunski
         {
362 ec4987d4 Leszek Koltunski
         DistortedEffects effects = mNodeCentral.getEffects();
363
         effects.abortById(mGlow2ID);
364 0da4feff Leszek Koltunski
         createAlphaEffect(false);
365
         mAlphaID = mAlpha.getID();
366
         mAlpha.notifyWhenFinished(this);
367 ec4987d4 Leszek Koltunski
         effects.apply(mAlpha);
368 0da4feff Leszek Koltunski
         }
369
      if( id==mAlphaID )
370
         {
371 fc6d738d Leszek Koltunski
         mScreen.detach(mNodeCentral);
372
         mNodeCentral.markForDeletion();
373
         mNodeCentral=null;
374 063ae851 Leszek Koltunski
375
         if( mNodeFalling!=null )
376
            {
377
            mScreen.detach(mNodeFalling);
378
            mNodeFalling.markForDeletion();
379
            mNodeFalling=null;
380
            }
381
382 d05e7629 Leszek Koltunski
         if( mListener!=null ) mListener.overlayFinished(id);
383 8feb68c2 Leszek Koltunski
         }
384 d7f0c07d Leszek Koltunski
      }
385 b4cbe056 Leszek Koltunski
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387
388
  @SuppressWarnings("unused")
389
  public static void enableEffects()
390
     {
391 0da4feff Leszek Koltunski
     FragmentEffectAlpha.enable();
392 b4cbe056 Leszek Koltunski
     VertexEffectMove.enable();
393
     VertexEffectScale.enable();
394 8feb68c2 Leszek Koltunski
     PostprocessEffectGlow.enable();
395 b4cbe056 Leszek Koltunski
     }
396 d7f0c07d Leszek Koltunski
}