Project

General

Profile

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

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

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