Project

General

Profile

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

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

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