gBlendMode = GUI_Select(
   kBlendAdditiveAlpha,
   "Blend Mode",
   {
      { description="Normal", value=kBlendNormal},
      { description="Additive Alpha", value=kBlendAdditiveAlpha},
      { description="Subtractive", value=kBlendSubtractive},
      { description="Multiplicative", value=kBlendMultiplicative},
      { description="Opaque", value=kBlendOpaque}
   }
);

gShape = GUI_Select(
   1,
   "Emitter Shape",
   {
      { description="Point", value=0},
      { description="Rectangle", value=1}
   }
);


if gShape == 1 then
   gWidth = GUI_Slider(
   35.00,
   "Emitter Width",
   0,500);

   gHeight = GUI_Slider(
   35.00,
   "Emitter Height",
   0,500);
end


mMaxParticles = GUI_Slider(
   1200.00,
   "Max Particles",
   0,1000);

gParticlesLife = GUI_Slider(
   2655.47,
   "Life (ms)",
   1,5000);

gSystemLife = GUI_Slider(
   -1,
   "System Life (s)",
   -1,5000);

gInitialScale = GUI_Slider(
   0.3,
   "Init Scale",
   0,3);

gSpinSpeedRange = GUI_Slider(
   0.00,
   "Spin Speed",
   0,10);

gSpinInit  = GUI_Slider(
   1.57,
   "Spin Init",
   0,6.28);


gFinalScale = GUI_Slider(
   0.3,
   "Final Scale",
   0,3);

gGravity = GUI_Slider(
   0.00,
   "Gravity",
   -100,1000);

gTexture = "fx_interface" --GUI_PickTexture("fx_interface","Texture");


gParticlesPerSecond = GUI_Slider(
   50,
   "Particle Creation Rate",
   1,200);

gAngleMax = GUI_Slider(
   0.00,
   "Angle Max",
   0,180);

gAngleOffset = GUI_Slider(
   0.00,
   "Angle Offset",
   -180,180);

gVelocity = GUI_Slider(
   0.00,
   "Max Init Speed",
   0,1000);

gVelocity2 = GUI_Slider(
   -35.00,
   "Min Init Speed",
   0,1000);

--[[gFadeSpeed1 = GUI_Slider(
   1000.00,
   "White Fade Speed",
   0,1000);

gFadeSpeed2 = GUI_Slider(
   1000.00,
   "Yellow Fade Speed",
   0,1000);

gFadeSpeed3 = GUI_Slider(
   1000.00,
   "Orange Fade Speed",
   0,1000);]]--

--Step configure
gNumberStep = GUI_Select(
   3,
   "Number of Steps",
   {
      { description="1 -- Only the Initial Color is used", value=1},
      { description="2 -- Initial to Color 2 is used", value=2},
      { description="3 -- Initial to Color 3 is used", value=3},
      { description="4 -- Initial to Color 4 is used", value=4},
      { description="5 -- Initial to Color 5 is used", value=5}
   }
);

----------------------------------------
gRedInit = GUI_Slider(
	1.00,
   "Red Canal for Initial Color",
   0,1);
gGreenInit = GUI_Slider(
	0.86,
   "Green Canal for Initial Color",
   0,1);
gBlueInit = GUI_Slider(
	0.49,
   "Blue Canal for Initial Color",
   0,1);
gAlphaInit = GUI_Slider(
   1.00,
   "Alpha Canal for Initial Color",
   0,1);
----------------------------
gTimeto2 = GUI_Slider(
   1295.00,
   "Time to Step 2",
   0,5000);
gRed2 = GUI_Slider(
   0.47,
   "Red Canal for Color step 2",
   0,1);
gGreen2 = GUI_Slider(
   0.40,
   "Green Canal for Color step 2",
   0,1);
gBlue2 = GUI_Slider(
   0.21,
   "Blue Canal for Color step 2",
   0,1);
gAlpha2 = GUI_Slider(
   1.00,
   "Alpha Canal for Color step 2",
   0,1);
-----------------------------
gTimeto3 = GUI_Slider(
   430.00,
   "Time to Step 3",
   0,5000);
gRed3 = GUI_Slider(
   1.00,
   "Red Canal for Color step 3",
   0,1);
gGreen3 = GUI_Slider(
   1.00,
   "Green Canal for Color step 3",
   0,1);
gBlue3 = GUI_Slider(
   0.80,
   "Blue Canal for Color step 3",
   0,1);
gAlpha3 = GUI_Slider(
   0.00,
   "Alpha Canal for Color step 3",
   0,1);
---------------------------------------
gTimeto4 = GUI_Slider(
   0.00,
   "Time to Step 4",
   0,5000);
gRed4 = GUI_Slider(
   0.00,
   "Red Canal for Color step 4",
   0,1);
gGreen4 = GUI_Slider(
   0.00,
   "Green Canal for Color step 4",
   0,1);
gBlue4 = GUI_Slider(
   0.00,
   "Blue Canal for Color step 4",
   0,1);
gAlpha4 = GUI_Slider(
   1.00,
   "Alpha Canal for Color step 4",
   0,1);
------------------------------------------
gTimeto5 = GUI_Slider(
   1.00,
   "Time to Step 5",
   0,5000);
gRed5 = GUI_Slider(
   1.00,
   "Red Canal for Color step 5",
   0,1);
gGreen5 = GUI_Slider(
   1.00,
   "Green Canal for Color step 5",
   0,1);
gBlue5 = GUI_Slider(
   1.00,
   "Blue Canal for Color step 5",
   0,1);
gAlpha5 = GUI_Slider(
   1.00,
   "Alpha Canal for Color step 5",
   0,1);
---------------------------------------


-- ------------------------------------------------------------
-- Initialization phase

-- Define our custom particle properties
pVelocity = Allocate(2) ; -- Allocate a Vec2 velocity member
pAge = Allocate(1) ; -- Allocate a TReal age member
pSpin = Allocate(1); -- Allocate a TReal spin member
pSpinSpeed = Allocate(1); -- Allocate a TReal spin member

-- dLocus is a data source: An external connection to the particle
-- system. Here we define dLocus for FluidFX, since FluidFX doesn't know
-- to provide the data source; in the game it's defined in
-- swarm.cpp as a data source, and it gets the mouse position.
if not dLocus then
   dLocus= Vec2(0,0) ;
end

if not pScaleFactor then
pScaleFactor = 1;
end

-- Set the particle texture
SetTexture(gTexture);

-- Set the blend mode
SetBlendMode(gBlendMode);

-- LAST, we set the total number of particles in the particle
-- system. This doesn't 'create' the particles, but rather
-- allocates a block of 'dead' particles that can be created
-- below.
SetNumParticles(mMaxParticles);

-- ------------------------------------------------------------
-- Action Phase

-- Particle Initialization Rules.
--
-- The following rules define how particles are created.
--
-- Any parameter below in initialization or animation rules that
-- is a scalar, Color(), or Vec2() can alternaly take a function
-- that returns the same type--and most functions can take any type
-- and will return that same type. The exception is any function
-- that's expecting a scalar in certain parameter positions.

-- Pick one of two positions randomly, and add dLocus.
-- dLocus is defined in swarm.cpp and added to the random
-- range
if gShape == 0 then
   
   pPosition:Init(dLocus);

elseif gShape == 1 then
   
   pPosition:Init( fRange( Vec2( pScaleFactor*( -gWidth/2 ), pScaleFactor*( -gHeight/2 ) ),Vec2( pScaleFactor*( gWidth/2 ), pScaleFactor*( gHeight/2 ) ) ) + dLocus );

end

-- Pick a velocity from a range
--pVelocity:Init( fRange( (gVelocity*Vec2(math.cos(gAngleOffset + gAngleMax),math.sin(gAngleOffset + gAngleMax))), (gVelocity*Vec2(math.cos(gAngleOffset - gAngleMax),math.sin(gAngleOffset - gAngleMax) )) ) );
Speed = pScaleFactor*fRange( gVelocity2, gVelocity);
--Angle = fRange( Vec2(1000*math.cos(math.rad(gAngleMax+ gAngleOffset -90)), 1000*math.sin(math.rad(gAngleMax+ gAngleOffset -90))), Vec2(1000*math.cos(math.rad(-gAngleMax+ gAngleOffset -90)), 1000*math.sin(math.rad(-gAngleMax+ gAngleOffset -90))) ) ;
Angle = fRange( math.rad( gAngleMax+ gAngleOffset ), math.rad( -gAngleMax+ gAngleOffset ) )+math.rad(gAngleOffset -90) ;

--Angle = gAngleOffset - 90; 

--pVelocity:Init( Angle );
--pVelocity:Init( Vec2( math.cos( Angle ), math.sin( Angle ) ) );
pVelocity:Init( f2dRotation( Angle ) * Speed  );
--pVelocity:Init( Vec2( Speed*math.cos(RandomRange( math.rad(-gAngleMax), math.rad(gAngleMax) ) + math.rad(gAngleOffset -90)), Speed*math.sin(RandomRange( math.rad(-gAngleMax), math.rad(gAngleMax) ) + math.rad(gAngleOffset -90)) ) );
--pVelocity:Init( fRange(Vec2(Xmax,Ymax), Vec2(Xmin,Ymin)));
-- Start color (tint) off as white (natural color of image)
pColor:Init( Color(gRedInit,gGreenInit,gBlueInit,gAlphaInit) );

-- Start scale out as Initial Scale
pScale:Init( pScaleFactor*gInitialScale );

-- Start age as 0 milliseconds
pAge:Init(0);

-- Start initial rotation as a random angle
pSpin:Init( gSpinInit );

-- Start spin velocity random from -10 to 10
pSpinSpeed:Init( fRange( -gSpinSpeedRange, gSpinSpeedRange ) );

-- ------------------------------------------------------------
-- Particle Parameter Animation Functions
--
-- The following rules define how particles animate.
-- Each rule is applied on each frame of animation.

-- Add velocity scaled by time to position
pVelocity:Anim( pVelocity + fTimeScale(Vec2(0,pScaleFactor*gGravity)) );
pPosition:Anim( pPosition + fTimeScale(pVelocity) );

pScale:Anim( pScale + fTimeScale(pScaleFactor*(gFinalScale-gInitialScale)*1000/gParticlesLife) );

if gNumberStep==1 then
pColor:Anim( pColor);

elseif gNumberStep==2 then
pColor:Anim( 
   fFade( 
      pAge,
      Color(gRedInit,gGreenInit,gBlueInit,gAlphaInit), gTimeto2, 
      Color(gRed2,gGreen2,gBlue2,gAlpha2) ) );

elseif gNumberStep==3 then
pColor:Anim( 
   fFade( 
      pAge,
      Color(gRedInit,gGreenInit,gBlueInit,gAlphaInit), gTimeto2, 
      Color(gRed2,gGreen2,gBlue2,gAlpha2), gTimeto3, 
      Color(gRed3,gGreen3,gBlue3,gAlpha3) ) );

elseif gNumberStep==4 then
pColor:Anim( 
   fFade( 
      pAge,
      Color(gRedInit,gGreenInit,gBlueInit,gAlphaInit), gTimeto2, 
      Color(gRed2,gGreen2,gBlue2,gAlpha2), gTimeto3, 
      Color(gRed3,gGreen3,gBlue3,gAlpha3), gTimeto4, 
      Color(gRed4,gGreen4,gBlue4,gAlpha4) ) );

elseif gNumberStep==5 then
pColor:Anim( 
   fFade( 
      pAge,
      Color(gRedInit,gGreenInit,gBlueInit,gAlphaInit), gTimeto2, 
      Color(gRed2,gGreen2,gBlue2,gAlpha2), gTimeto3, 
      Color(gRed3,gGreen3,gBlue3,gAlpha3), gTimeto4, 
      Color(gRed4,gGreen4,gBlue4,gAlpha4), gTimeto5,
      Color(gRed5,gGreen5,gBlue5,gAlpha5) ) );
elseif gNumberStep==6 then
pColor:Anim( 
   fFade( 
      pAge,
      Color(gRedInit,gGreenInit,gBlueInit,gAlphaInit), gTimeto2, 
      Color(gRed2,gGreen2,gBlue2,gAlpha2), gTimeto3, 
      Color(gRed3,gGreen3,gBlue3,gAlpha3), gTimeto4, 
      Color(gRed4,gGreen4,gBlue4,gAlpha4), gTimeto5,
      Color(gRed5,gGreen5,gBlue5,gAlpha5) ) );
end

--[[pColor:Anim(
   fFade(
      pAge,
      Color(1,0,0,1), 200,
      Color(0,1,0,1), 200,
      Color(0,0,1,1), 200,
      Color(1,0,0,1), 200,
      Color(0,1,0,1)
   ) );]]--

pSpin:Anim( pSpin + fTimeScale( pSpinSpeed ) );
pUp:Anim( f2dRotation( pSpin ) );


-- Add time to the age
pAge:Anim( pAge+fAge() );

-- Expire particles over 2500 ms old
Anim( fExpire( fGreater(pAge,gParticlesLife) ) );

-- Create an initial particle (optional)
-- CreateParticles( 1 );

-- A global variable that we can set from outside
gActive = true

gWasActive = false

gUseTimer = false

if gSystemLife ~= -1 then
   gUseTimer = true;
   timeout = NewTimeout(gSystemLife);
end

-- A function to run as we're executing
-- seconds - how many seconds have elapsed
function Update(seconds)
   if gActive then
	  if gWasActive then
		CreateParticles( seconds * gParticlesPerSecond );
	  else
		gWasActive = true
		CreateParticles( 50 );
	  end
     if gUseTimer then
      if timeout(seconds) then
         Done();
      end
     end
	else
		if gWasActive then
			CreateParticles( 30 );
			gWasActive = false
		end
   end
end

 
