[NUI] Change RendererTexture(texture) into TextureResourceUrl(imageUrl)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.ParticleSystem.Sample / ParticleSystemSample.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Threading;
4 using Tizen.NUI.BaseComponents;
5 using Tizen.NUI.Components;
6 using Tizen.NUI.ParticleSystem;
7
8 namespace Tizen.NUI.ParticleSystem.Sample
9 {
10     // SparkleEffectSource spawns particle from the middle of the
11     // window.
12     class SparkleEffectSource : ParticleSourceInterface
13     {
14         public override void Construct(params object[] list)
15         {
16             base.Construct(list);
17             mRadius = new Vector2(list[0] as Vector2);
18         }
19         
20         public override uint Update(ParticleEmitterProxy emitterProxy, uint count)
21         {
22             if(mStreamBasePos == 0 || mStreamBaseAngle == 0) // streams must exist 
23             {
24                 return 0u;
25             }
26
27             while(count > 0)
28             {
29                 // Create new particle (lifetime 5 seconds of each)
30                 var particle = emitterProxy.NewParticle(5.0f);
31                 if(particle == null)
32                 {
33                     return 0u;
34                 }
35                 
36                 UpdateParticle(ref particle);
37                 
38                 count--;
39             }
40             return 0;
41         }
42         
43         public override void Init()
44         {
45             // Add local stream of Vector3 type
46             mStreamBasePos = Emitter.AddLocalStreamVector3(Vector3.Zero);
47             
48             // Add local stream of float type
49             mStreamBaseAngle = Emitter.AddLocalStreamFloat(0.0f);
50         }
51
52         void UpdateParticle(ref Particle p)
53         {
54             float posRadians   = ((mRandom.Next() % 360) * (float)Math.PI) / 180.0f;
55             p.Position = new Vector3(mRadius.X * (float)Math.Sin(posRadians), mRadius.Y * (float)Math.Cos(posRadians), 0.0f);
56             p.SetStreamValue(p.Position, mStreamBasePos);
57             p.Color = Vector4.One;
58             p.SetStreamValue(mAngle, mStreamBaseAngle);
59             mAngle = ((mAngle+5)%360);
60             float rad   = ((mRandom.Next() % 360) * (float)Math.PI) / 180.0f;
61             float speed = ((mRandom.Next() % 5) + 5);
62             p.Velocity = new Vector3((float)Math.Sin(rad) * speed, (float)Math.Cos(rad) * speed, 0);
63
64             // Random initial scale
65             float initialScale = (float)(mRandom.Next() % 32) + 32;
66             p.Scale = new Vector3(initialScale, initialScale, 1.0f);
67         }
68         
69         private static float mAngle = 0;
70         private Random mRandom = new Random();
71         public uint mStreamBasePos = 0;
72         public uint mStreamBaseAngle = 0;
73         private Vector2 mRadius;
74     }
75
76     // SparkleEffectModifier spawns particle from the middle of the
77     // window.
78     class SparkleEffectModifier : ParticleModifierInterface
79     {
80         public override void Construct(params object[] list)
81         {
82             base.Construct(list);
83             mSource = list[0] as SparkleEffectSource;
84         }
85
86         public override void Update(ParticleEmitterProxy proxy, List<Particle> particles)
87         {
88             if (particles.Count == 0)
89             {
90                 return;
91             }
92
93             if (mStreamBasePos == 0)
94             {
95                 mStreamBasePos = mSource.mStreamBasePos;
96             }
97
98             if (mStreamBaseAngle == 0)
99             {
100                 mStreamBaseAngle = mSource.mStreamBaseAngle;
101             }
102
103             if (mStreamBasePos == 0)
104             {
105                 return;
106             }
107
108             for (uint i = 0; i < particles.Count; ++i)
109             {
110                 var p = particles[(int)i];
111                 
112                 float angle = p.GetStreamValue(mStreamBaseAngle);
113                 Vector3 basePos = p.GetStreamValue(mStreamBaseAngle);
114                 float radians  = (float)((angle * Math.PI)/180.0f);
115                 float lifetime = p.Lifetime;
116                 Vector3 pos = p.Position;
117                 var vel = p.Velocity;
118                 p.Position = new Vector3(pos.X + vel.X * (float)Math.Cos(radians),pos.Y + vel.Y * (float)Math.Sin(radians),
119                      pos.Z);
120
121                 p.Velocity = (vel * 0.990f);
122                 float normalizedTime = (lifetime / p.LifetimeBase);
123
124                 var color = new Vector4( 1, 1, 1, normalizedTime);
125                 p.Color = color;
126                 p.Scale = new Vector3(64.0f*(normalizedTime * normalizedTime * normalizedTime * normalizedTime), 64.0f*(normalizedTime * normalizedTime * normalizedTime * normalizedTime), 1.0f);
127                  
128             }
129         }
130
131         private uint mStreamBasePos = 0;
132         private uint mStreamBaseAngle = 0;
133         private SparkleEffectSource mSource;
134
135     }
136     
137     class ParticleSystemSample : NUIApplication
138     {
139         static string IMAGE_DIR = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "image/";
140
141         private Window mWindow;
142
143         private ParticleEmitter mEmitter;
144         private ParticleSource<SparkleEffectSource> mSource;
145         private ParticleModifier<SparkleEffectModifier> mModifier;
146
147         public void Activate()
148         {
149             mWindow = Window.Instance;
150             mWindow.BackgroundColor = Color.Black;
151
152             var view = new View();
153             view.BackgroundColor = Color.Wheat;
154             view.Size = new Size(mWindow.Size.Width, mWindow.Size.Height);
155             view.PivotPoint = PivotPoint.Center;
156             view.ParentOrigin = ParentOrigin.TopLeft;
157             view.Position2D = new Position2D(0, 0);
158             mWindow.Add(view);
159             // Attach emitter to view
160             mEmitter = new ParticleEmitter(view)
161             {
162                 ParticleCount = 10000,
163                 EmissionRate = 500,
164                 InitialParticleCount = 0,
165                 RendererBlendingMode = ParticleBlendingMode.Screen
166             };
167             
168             mSource = new ParticleSource<SparkleEffectSource>(new Vector2(50, 50));
169             mModifier = new ParticleModifier<SparkleEffectModifier>(mSource.Callback);
170             
171             mEmitter.SetSource(mSource);
172             mEmitter.AddModifier(mModifier);
173             
174             // Load texture
175             mEmitter.TextureResourceUrl = IMAGE_DIR + "/blue-part2.png";
176             mEmitter.Start();
177         }
178         
179         protected override void OnCreate()
180         {
181             // Up call to the Base class first
182             base.OnCreate();
183             Activate();
184         }
185         
186         /// <summary>
187         /// The main entry point for the application.
188         /// </summary>
189         [STAThread] // Forces app to use one thread to access NUI
190         static void Main(string[] args)
191         {
192             ParticleSystemSample example = new ParticleSystemSample();
193             example.Run(args);
194         }
195     }
196 }