Project

General

Profile

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

magiccube / src / main / java / org / distorted / overlays / OverlayStars.java @ 8feb68c2

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
import org.distorted.library.effect.MatrixEffectMove;
20
import org.distorted.library.effect.MatrixEffectScale;
21
import org.distorted.library.effect.PostprocessEffectGlow;
22 b4cbe056 Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
23
import org.distorted.library.effect.VertexEffectScale;
24 d7f0c07d Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedNode;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.main.DistortedTexture;
28 b4cbe056 Leszek Koltunski
import org.distorted.library.main.InternalOutputSurface;
29
import org.distorted.library.mesh.MeshJoined;
30 d7f0c07d Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
31
import org.distorted.library.message.EffectListener;
32 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.Dynamic2D;
34 b4cbe056 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
35 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
36
import org.distorted.library.type.Static2D;
37 d7f0c07d Leszek Koltunski
import org.distorted.library.type.Static3D;
38 8feb68c2 Leszek Koltunski
import org.distorted.library.type.Static4D;
39 d7f0c07d Leszek Koltunski
import org.distorted.main.R;
40
41 b4cbe056 Leszek Koltunski
import java.util.Random;
42
43 d7f0c07d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
public class OverlayStars extends OverlayGeneric implements EffectListener
46
{
47 8feb68c2 Leszek Koltunski
   private static final int DUR_MOV =  4000;
48
   private static final int DUR_GLO =  2000;
49
50
   private static final int[] colors = new int[] {0,0,1,  1,0,1,  1,0,0,  1,1,0,  0,1,0,  1,1,1}; // blue, pink, red, yellow, green, white
51
   private static final int INDEX = 5;
52 b4cbe056 Leszek Koltunski
53 d7f0c07d Leszek Koltunski
   private ListenerOverlay mListener;
54 8feb68c2 Leszek Koltunski
   private DistortedNode mStarsNode, mCountNode;
55 d7f0c07d Leszek Koltunski
   private DistortedScreen mScreen;
56 8feb68c2 Leszek Koltunski
   private DistortedTexture mStarsTexture, mCountTexture;
57
   private Bitmap mCountBitmap;
58
   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 8feb68c2 Leszek Koltunski
   private long mMoveID, mGlowID;
64
   private int mTotalStars, mNewStars;
65 b4cbe056 Leszek Koltunski
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
   private Dynamic3D createRandomMove()
69
      {
70
      Dynamic3D move = new Dynamic3D();
71 8feb68c2 Leszek Koltunski
      move.setMode(Dynamic.MODE_PATH);
72 b4cbe056 Leszek Koltunski
      move.setDuration(DUR_MOV);
73
      move.setCount(0.5f);
74
75 8feb68c2 Leszek Koltunski
      float widthS = (mRandom.nextFloat()-0.5f)*mWidth*1.10f;
76 b4cbe056 Leszek Koltunski
      float widthM = widthS + (mRandom.nextFloat()-0.5f)*mWidth*0.2f;
77 8feb68c2 Leszek Koltunski
      float heighS = mRandom.nextFloat()*mHeight*0.2f;
78
      float heighM = (mRandom.nextFloat()-0.5f)*mHeight*0.2f;
79 b4cbe056 Leszek Koltunski
80 8feb68c2 Leszek Koltunski
      Static3D pointS = new Static3D(widthS,mHeight*0.55f+heighS,0);
81
      Static3D pointM = new Static3D(widthM,mHeight*0.25f+heighM,0);
82
      Static3D pointE = new Static3D(0,0,0);
83 b4cbe056 Leszek Koltunski
84
      move.add(pointS);
85
      move.add(pointM);
86 8feb68c2 Leszek Koltunski
      move.add(pointE);
87 b4cbe056 Leszek Koltunski
88
      return move;
89
      }
90 d7f0c07d Leszek Koltunski
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 8feb68c2 Leszek Koltunski
   private DistortedNode createStarsNode(Resources res)
94 d7f0c07d Leszek Koltunski
      {
95 b4cbe056 Leszek Koltunski
      // texture /////////////////////////////////////////////////////
96 8feb68c2 Leszek Koltunski
      if( mStarsTexture==null )
97
         {
98
         mStarsTexture = new DistortedTexture();
99
         Bitmap star = BitmapFactory.decodeResource(res, R.drawable.star);
100
         mBmpW = star.getWidth();
101
         mBmpH = star.getHeight();
102
         mStarsTexture.setTexture(star);
103
         }
104 d7f0c07d Leszek Koltunski
105 b4cbe056 Leszek Koltunski
      // mesh ////////////////////////////////////////////////////////
106 8feb68c2 Leszek Koltunski
      MeshQuad[] mesh = new MeshQuad[mNewStars+1];
107 b4cbe056 Leszek Koltunski
108 8feb68c2 Leszek Koltunski
      for(int i=0; i<mNewStars+1; i++)
109 b4cbe056 Leszek Koltunski
         {
110
         mesh[i] = new MeshQuad();
111 8feb68c2 Leszek Koltunski
         mesh[i].setEffectAssociation(0,(i>=mNewStars?2:1),i);
112 b4cbe056 Leszek Koltunski
         }
113 d7f0c07d Leszek Koltunski
114 b4cbe056 Leszek Koltunski
      MeshJoined wholeMesh = new MeshJoined(mesh);
115
116
      // effects: main ///////////////////////////////////////////////
117
      DistortedEffects effects = new DistortedEffects();
118
119 8feb68c2 Leszek Koltunski
      float scaleM  = mWidth*0.40f;
120
      float scaleP  = mWidth*0.15f;
121
      Static3D moveM= new Static3D(0,0,1);
122 d7f0c07d Leszek Koltunski
123 b4cbe056 Leszek Koltunski
      VertexEffectMove mainMove     = new VertexEffectMove(moveM);
124
      VertexEffectScale mainScale   = new VertexEffectScale(scaleM);
125 8feb68c2 Leszek Koltunski
      mainMove.setMeshAssociation(2,-1);
126
      mainScale.setMeshAssociation(2,-1);
127 b4cbe056 Leszek Koltunski
128
      // effects: moving stars ///////////////////////////////////////
129
      VertexEffectScale scaleE = new VertexEffectScale(scaleP);
130
      scaleE.setMeshAssociation(1,-1);
131
      effects.apply(scaleE);
132
133 8feb68c2 Leszek Koltunski
      for(int i=0; i<mNewStars; i++)
134 b4cbe056 Leszek Koltunski
        {
135
        Dynamic3D moveP = createRandomMove();
136
        VertexEffectMove moveE= new VertexEffectMove(moveP);
137 8feb68c2 Leszek Koltunski
        moveE.setMeshAssociation(0,i);
138 b4cbe056 Leszek Koltunski
        effects.apply(moveE);
139 8feb68c2 Leszek Koltunski
140
        if( i==0 )
141
           {
142
           mMoveID = moveE.getID();
143
           moveE.notifyWhenFinished(this);
144
           }
145 b4cbe056 Leszek Koltunski
        }
146
147
      // main effect queue ///////////////////////////////////////////
148
      effects.apply(mainMove);
149
      effects.apply(mainScale);
150
151 8feb68c2 Leszek Koltunski
      return new DistortedNode(mStarsTexture,effects,wholeMesh);
152
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
   private void renderStars(int numStars)
157
      {
158
      String txt = ""+numStars;
159
      mCountCanvas.drawText(txt,mBmpW*0.5f,mBmpH*0.64f,mPaint);
160
      mCountTexture.setTexture(mCountBitmap);
161
      }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165
   private DistortedNode createCountNode()
166
      {
167
      // texture /////////////////////////////////////////////////////
168
      if( mCountTexture==null )
169
         {
170
         mCountTexture = new DistortedTexture();
171
172
         mCountBitmap = Bitmap.createBitmap(mBmpW,mBmpH,Bitmap.Config.ARGB_8888);
173
         mCountCanvas = new Canvas(mCountBitmap);
174
         mPaint = new Paint();
175
         mPaint.setColor(0xff000000);
176
         mPaint.setTextSize(mBmpH*0.28f);
177
         mPaint.setAntiAlias(true);
178
         mPaint.setTextAlign(Paint.Align.CENTER);
179
         renderStars(mTotalStars);
180
         }
181
182
      // mesh ////////////////////////////////////////////////////////
183
      MeshQuad mesh = new MeshQuad();
184
185
      // effects: main ///////////////////////////////////////////////
186
      DistortedEffects effects = new DistortedEffects();
187
188
      float scaleM  = mWidth*0.40f;
189
      Static3D moveM= new Static3D(0,0,1);
190
191
      MatrixEffectMove mainMove  = new MatrixEffectMove(moveM);
192
      MatrixEffectScale mainScale= new MatrixEffectScale(scaleM);
193
194
      // main effect queue ///////////////////////////////////////////
195
      effects.apply(mainMove);
196
      effects.apply(mainScale);
197
198
      return new DistortedNode(mCountTexture,effects,mesh);
199 d7f0c07d Leszek Koltunski
      }
200
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
   public long startOverlay(DistortedScreen screen, ListenerOverlay listener, DataGeneric data)
204
      {
205 b4cbe056 Leszek Koltunski
      if( mRandom==null ) mRandom = new Random();
206
207 d7f0c07d Leszek Koltunski
      mScreen = screen;
208
      mListener= listener;
209
      DataStars stars = (DataStars)data;
210 8feb68c2 Leszek Koltunski
      mTotalStars = stars.getTotal();
211
      mNewStars   = stars.getNew();
212 d7f0c07d Leszek Koltunski
      Resources res = stars.getResources();
213
214 b4cbe056 Leszek Koltunski
      mWidth = mScreen.getWidth();
215
      mHeight= mScreen.getHeight();
216
217 8feb68c2 Leszek Koltunski
      mStarsNode = createStarsNode(res);
218
      mStarsNode.glDepthMask(false);
219
      mStarsNode.glStencilMask(0);
220
      mStarsNode.enableDepthStencil(InternalOutputSurface.NO_DEPTH_NO_STENCIL);
221
      mScreen.attach(mStarsNode);
222
223
      mCountNode = createCountNode();
224
      mCountNode.glDepthMask(false);
225
      mCountNode.glStencilMask(0);
226
      mCountNode.enableDepthStencil(InternalOutputSurface.NO_DEPTH_NO_STENCIL);
227
      mScreen.attach(mCountNode);
228 d7f0c07d Leszek Koltunski
229
      return 0;
230
      }
231
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234
   public void effectFinished(long id)
235
      {
236 8feb68c2 Leszek Koltunski
      if( id==mMoveID )
237
         {
238
         Dynamic2D haloRadius = new Dynamic2D(DUR_GLO,1.0f);
239
         haloRadius.add(new Static2D( 0, 0));
240
         haloRadius.add(new Static2D(15,50));
241
         haloRadius.add(new Static2D( 0, 0));
242
243
         Dynamic4D color= new Dynamic4D(DUR_GLO,1.0f);
244
         Static4D P1    = new Static4D(colors[3*INDEX],colors[3*INDEX+1], colors[3*INDEX+2], 0.0f);
245
         Static4D P2    = new Static4D(colors[3*INDEX],colors[3*INDEX+1], colors[3*INDEX+2], 0.5f);
246
         color.add(P1);
247
         color.add(P2);
248
         color.add(P1);
249
250
         PostprocessEffectGlow glow = new PostprocessEffectGlow(haloRadius,color);
251
         glow.setQuality(EffectQuality.MEDIUM);
252
         mGlowID = glow.getID();
253
         glow.notifyWhenFinished(this);
254
         DistortedEffects effects = mStarsNode.getEffects();
255
         effects.apply(glow);
256
257
         renderStars(mTotalStars+mNewStars);
258
         }
259
      if( id==mGlowID )
260
         {
261
         mScreen.detach(mStarsNode);
262
         mStarsNode.markForDeletion();
263
         mStarsNode=null;
264
         mScreen.detach(mCountNode);
265
         mCountNode.markForDeletion();
266
         mCountNode=null;
267
         mListener.overlayFinished(id);
268
         }
269 d7f0c07d Leszek Koltunski
      }
270 b4cbe056 Leszek Koltunski
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273
  @SuppressWarnings("unused")
274
  public static void enableEffects()
275
     {
276
     VertexEffectMove.enable();
277
     VertexEffectScale.enable();
278 8feb68c2 Leszek Koltunski
     PostprocessEffectGlow.enable();
279 b4cbe056 Leszek Koltunski
     }
280 d7f0c07d Leszek Koltunski
}