Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.graphics.Canvas;
16
import android.graphics.Paint;
17

    
18
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
import org.distorted.library.effect.VertexEffectMove;
23
import org.distorted.library.effect.VertexEffectScale;
24
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
import org.distorted.library.main.InternalOutputSurface;
29
import org.distorted.library.mesh.MeshJoined;
30
import org.distorted.library.mesh.MeshQuad;
31
import org.distorted.library.message.EffectListener;
32
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.Dynamic2D;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Dynamic4D;
36
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.main.R;
40

    
41
import java.util.Random;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class OverlayStars extends OverlayGeneric implements EffectListener
46
{
47
   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

    
53
   private ListenerOverlay mListener;
54
   private DistortedNode mStarsNode, mCountNode;
55
   private DistortedScreen mScreen;
56
   private DistortedTexture mStarsTexture, mCountTexture;
57
   private Bitmap mCountBitmap;
58
   private Canvas mCountCanvas;
59
   private Paint mPaint;
60
   private int mBmpW, mBmpH;
61
   private float mWidth, mHeight;
62
   private Random mRandom;
63
   private long mMoveID, mGlowID;
64
   private int mTotalStars, mNewStars;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
   private Dynamic3D createRandomMove()
69
      {
70
      Dynamic3D move = new Dynamic3D();
71
      move.setMode(Dynamic.MODE_PATH);
72
      move.setDuration(DUR_MOV);
73
      move.setCount(0.5f);
74

    
75
      float widthS = (mRandom.nextFloat()-0.5f)*mWidth*1.10f;
76
      float widthM = widthS + (mRandom.nextFloat()-0.5f)*mWidth*0.2f;
77
      float heighS = mRandom.nextFloat()*mHeight*0.2f;
78
      float heighM = (mRandom.nextFloat()-0.5f)*mHeight*0.2f;
79

    
80
      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

    
84
      move.add(pointS);
85
      move.add(pointM);
86
      move.add(pointE);
87

    
88
      return move;
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   private DistortedNode createStarsNode(Resources res)
94
      {
95
      // texture /////////////////////////////////////////////////////
96
      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

    
105
      // mesh ////////////////////////////////////////////////////////
106
      MeshQuad[] mesh = new MeshQuad[mNewStars+1];
107

    
108
      for(int i=0; i<mNewStars+1; i++)
109
         {
110
         mesh[i] = new MeshQuad();
111
         mesh[i].setEffectAssociation(0,(i>=mNewStars?2:1),i);
112
         }
113

    
114
      MeshJoined wholeMesh = new MeshJoined(mesh);
115

    
116
      // effects: main ///////////////////////////////////////////////
117
      DistortedEffects effects = new DistortedEffects();
118

    
119
      float scaleM  = mWidth*0.40f;
120
      float scaleP  = mWidth*0.15f;
121
      Static3D moveM= new Static3D(0,0,1);
122

    
123
      VertexEffectMove mainMove     = new VertexEffectMove(moveM);
124
      VertexEffectScale mainScale   = new VertexEffectScale(scaleM);
125
      mainMove.setMeshAssociation(2,-1);
126
      mainScale.setMeshAssociation(2,-1);
127

    
128
      // effects: moving stars ///////////////////////////////////////
129
      VertexEffectScale scaleE = new VertexEffectScale(scaleP);
130
      scaleE.setMeshAssociation(1,-1);
131
      effects.apply(scaleE);
132

    
133
      for(int i=0; i<mNewStars; i++)
134
        {
135
        Dynamic3D moveP = createRandomMove();
136
        VertexEffectMove moveE= new VertexEffectMove(moveP);
137
        moveE.setMeshAssociation(0,i);
138
        effects.apply(moveE);
139

    
140
        if( i==0 )
141
           {
142
           mMoveID = moveE.getID();
143
           moveE.notifyWhenFinished(this);
144
           }
145
        }
146

    
147
      // main effect queue ///////////////////////////////////////////
148
      effects.apply(mainMove);
149
      effects.apply(mainScale);
150

    
151
      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
      }
200

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

    
203
   public long startOverlay(DistortedScreen screen, ListenerOverlay listener, DataGeneric data)
204
      {
205
      if( mRandom==null ) mRandom = new Random();
206

    
207
      mScreen = screen;
208
      mListener= listener;
209
      DataStars stars = (DataStars)data;
210
      mTotalStars = stars.getTotal();
211
      mNewStars   = stars.getNew();
212
      Resources res = stars.getResources();
213

    
214
      mWidth = mScreen.getWidth();
215
      mHeight= mScreen.getHeight();
216

    
217
      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

    
229
      return 0;
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
   public void effectFinished(long id)
235
      {
236
      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
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  @SuppressWarnings("unused")
274
  public static void enableEffects()
275
     {
276
     VertexEffectMove.enable();
277
     VertexEffectScale.enable();
278
     PostprocessEffectGlow.enable();
279
     }
280
}
(5-5/5)