Advertisement
smc_gamer

IrisEffect.cs

Feb 18th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using SMLimitless.Extensions;
  10. using SMLimitless.Graphics;
  11. using SMLimitless.Interfaces;
  12.  
  13. namespace SMLimitless.Screens.Effects
  14. {
  15.     public sealed class IrisEffect : IEffect
  16.     {
  17.         private float fadeDelta;
  18.         private float currentFadeLevel;
  19.         private Vector2 irisCenter;
  20.         private float initialIrisRadius;
  21.         private float irisRadius;
  22.         private bool isRunning;
  23.         private bool isInitialized;
  24.         private EffectDirection dir;
  25.         private Color color;
  26.         private QuadRenderer quadRenderer;
  27.  
  28.         public event EffectCompletedEventHandler EffectCompletedEvent;
  29.  
  30.         public IrisEffect(Vector2 center)
  31.         {
  32.             irisCenter = center;
  33.  
  34.             Vector2 screenSize = GameServices.ScreenSize;
  35.             irisRadius = (float)Math.Sqrt((screenSize.X * screenSize.X) + (screenSize.Y + screenSize.Y));
  36.             initialIrisRadius = irisRadius;
  37.  
  38.             isInitialized = true;
  39.         }
  40.        
  41.         public void Draw()
  42.         {
  43.             if (isInitialized && (isRunning || dir == EffectDirection.Backward))
  44.             {
  45.                 //GameServices.SpriteBatch.End();
  46.  
  47.                 //GameServices.SpriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, null);
  48.                 var irisEffect = GameServices.Effects["IrisEffect"];
  49.                 irisEffect.Parameters["irisCenter"].SetValue(irisCenter);
  50.                 irisEffect.Parameters["radius"].SetValue(irisRadius);
  51.                 irisEffect.Parameters["backColor"].SetValue(new Vector4(color.R, color.G, color.B, color.A * currentFadeLevel));
  52.                 //irisEffect.CurrentTechnique.Passes[0].Apply();
  53.  
  54.                 quadRenderer.Render(irisEffect);
  55.  
  56.                 //GameServices.SpriteBatch.End();
  57.  
  58.                 //GameServices.SpriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, GameServices.Camera.GetTransformation());
  59.             }
  60.         }
  61.  
  62.         public void LoadContent()
  63.         {
  64.             GameServices.Effects.Add("IrisEffect", GameServices.GetService<ContentManager>().Load<Effect>("IrisEffect"));
  65.             quadRenderer = new QuadRenderer();
  66.            
  67.         }
  68.  
  69.         public void Set(EffectDirection direction, Color color)
  70.         {
  71.             this.color = color;
  72.             if (direction == EffectDirection.Forward)
  73.             {
  74.                 currentFadeLevel = 1f;
  75.             }
  76.             else
  77.             {
  78.                 currentFadeLevel = 0f;
  79.             }
  80.         }
  81.  
  82.         public void Start(int length, EffectDirection direction, Vector2 position, Color color)
  83.         {
  84.             if ((direction == EffectDirection.Forward && currentFadeLevel == 1f) || (direction == EffectDirection.Backward && currentFadeLevel == 0f))
  85.             {
  86.                 return;
  87.             }
  88.  
  89.             isRunning = true;
  90.             fadeDelta = 1.0f / length;
  91.             dir = direction;
  92.             this.color = color;
  93.  
  94.             if (dir == EffectDirection.Backward)
  95.             {
  96.                 currentFadeLevel = 1.0f;
  97.             }
  98.         }
  99.  
  100.         public void Stop()
  101.         {
  102.             isRunning = false;
  103.             currentFadeLevel = 0f;
  104.             fadeDelta = 0f;
  105.         }
  106.  
  107.         public void Update()
  108.         {
  109.             if (isRunning && isInitialized)
  110.             {
  111.                 switch (dir)
  112.                 {
  113.                     case EffectDirection.Forward:
  114.                         // fade to black, iris in
  115.                         if (currentFadeLevel < 1.0f)
  116.                         {
  117.                             currentFadeLevel += fadeDelta;
  118.                             irisRadius = initialIrisRadius * (1f - currentFadeLevel);
  119.                         }
  120.                         else FadeFinished();
  121.                         break;
  122.                     case EffectDirection.Backward:
  123.                         // fade from black, iris out
  124.                         if (currentFadeLevel > 0f)
  125.                         {
  126.                             currentFadeLevel -= fadeDelta;
  127.                             irisRadius = initialIrisRadius * (1f - currentFadeLevel);
  128.                         }
  129.                         else FadeFinished();
  130.                         break;
  131.                 }
  132.             }
  133.             else if (!isInitialized)
  134.             {
  135.                 throw new InvalidOperationException("The iris effect was not properly initialized. Please set the screen size.");
  136.             }
  137.         }
  138.  
  139.         private void FadeFinished()
  140.         {
  141.             isRunning = false;
  142.             fadeDelta = 0f;
  143.             if (EffectCompletedEvent != null)
  144.             {
  145.                 EffectCompletedEvent(this, dir);
  146.             }
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement