Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FilterManager.cs
Go to the documentation of this file.
1using System;
5using Terraria.IO;
6
8
9public class FilterManager : EffectManager<Filter>
10{
11 private const float OPACITY_RATE = 1f;
12
14
15 private int _filterLimit = 16;
16
18
19 private int _activeFilterCount;
20
21 private bool _captureThisFrame;
22
23 public event Action OnPostDraw;
24
26 {
27 preferences.OnSave += Configuration_OnSave;
28 preferences.OnLoad += Configuration_OnLoad;
29 }
30
32 {
33 preferences.Put("FilterLimit", _filterLimit);
34 preferences.Put("FilterPriorityThreshold", Enum.GetName(typeof(EffectPriority), _priorityThreshold));
35 }
36
38 {
39 _filterLimit = preferences.Get("FilterLimit", 16);
40 if (Enum.TryParse<EffectPriority>(preferences.Get("FilterPriorityThreshold", "VeryLow"), out var result))
41 {
42 _priorityThreshold = result;
43 }
44 }
45
46 public override void OnActivate(Filter effect, Vector2 position)
47 {
48 if (_activeFilters.Contains(effect))
49 {
50 if (effect.Active)
51 {
52 return;
53 }
54 if (effect.Priority >= _priorityThreshold)
55 {
57 }
58 _activeFilters.Remove(effect);
59 }
60 else
61 {
62 effect.Opacity = 0f;
63 }
64 if (effect.Priority >= _priorityThreshold)
65 {
67 }
68 if (_activeFilters.Count == 0)
69 {
70 _activeFilters.AddLast(effect);
71 return;
72 }
74 {
76 if (effect.Priority <= value.Priority)
77 {
78 _activeFilters.AddAfter(linkedListNode, effect);
79 return;
80 }
81 }
82 _activeFilters.AddLast(effect);
83 }
84
86 {
87 if (_activeFilterCount == 0 && this.OnPostDraw == null)
88 {
89 _captureThisFrame = false;
90 return;
91 }
92 _captureThisFrame = true;
93 Main.instance.GraphicsDevice.SetRenderTarget(screenTarget1);
94 Main.instance.GraphicsDevice.Clear(clearColor);
95 }
96
98 {
101 int num = 0;
102 while (linkedListNode != null)
103 {
106 bool flag = false;
107 if (value.Priority >= _priorityThreshold)
108 {
109 num++;
111 {
112 value.Update(gameTime);
113 flag = true;
114 }
115 }
116 if (value.Active && flag)
117 {
118 value.Opacity = Math.Min(value.Opacity + (float)gameTime.ElapsedGameTime.TotalSeconds * 1f, 1f);
119 }
120 else
121 {
122 value.Opacity = Math.Max(value.Opacity - (float)gameTime.ElapsedGameTime.TotalSeconds * 1f, 0f);
123 }
124 if (!value.Active && value.Opacity == 0f)
125 {
126 if (value.Priority >= _priorityThreshold)
127 {
129 }
131 }
132 linkedListNode = next;
133 }
134 }
135
137 {
139 {
140 return;
141 }
144 Filter filter = null;
147 GraphicsDevice graphicsDevice = Main.instance.GraphicsDevice;
148 int num = 0;
149 if (Main.player[Main.myPlayer].gravDir == -1f)
150 {
152 graphicsDevice.SetRenderTarget(renderTarget2D);
153 graphicsDevice.Clear(clearColor);
156 Main.spriteBatch.End();
158 }
159 while (linkedListNode != null)
160 {
163 if (value.Priority >= _priorityThreshold)
164 {
165 num++;
166 if (num > _activeFilterCount - _filterLimit && value.IsVisible())
167 {
168 if (filter != null)
169 {
171 graphicsDevice.SetRenderTarget(renderTarget2D);
172 graphicsDevice.Clear(clearColor);
174 filter.Apply();
176 Main.spriteBatch.End();
178 }
179 filter = value;
180 }
181 }
182 linkedListNode = next;
183 }
184 graphicsDevice.SetRenderTarget(finalTexture);
185 graphicsDevice.Clear(clearColor);
186 if (Main.player[Main.myPlayer].gravDir == -1f)
187 {
189 }
190 else
191 {
193 }
194 if (filter != null)
195 {
196 filter.Apply();
198 }
199 else
200 {
202 }
203 Main.spriteBatch.End();
204 for (int i = 0; i < 8; i++)
205 {
206 graphicsDevice.Textures[i] = null;
207 }
208 if (this.OnPostDraw != null)
209 {
210 this.OnPostDraw();
211 }
212 }
213
214 public bool HasActiveFilter()
215 {
216 return _activeFilters.Count != 0;
217 }
218
219 public bool CanCapture()
220 {
221 if (!HasActiveFilter())
222 {
223 return this.OnPostDraw != null;
224 }
225 return true;
226 }
227}
static readonly BlendState AlphaBlend
Definition BlendState.cs:36
void Clear(ClearOptions options, Vector4 color, float depth, int stencil)
unsafe void SetRenderTarget(RenderTargetCube renderTarget, CubeMapFace cubeMapFace)
static readonly SamplerState LinearClamp
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
static bool TryParse(Type enumType, string? value, out object? result)
Definition Enum.cs:416
static ? string GetName(Type enumType, object value)
Definition Enum.cs:281
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
void BeginCapture(RenderTarget2D screenTarget1, Color clearColor)
void BindTo(Preferences preferences)
void Configuration_OnLoad(Preferences preferences)
void Configuration_OnSave(Preferences preferences)
void EndCapture(RenderTarget2D finalTexture, RenderTarget2D screenTarget1, RenderTarget2D screenTarget2, Color clearColor)
override void OnActivate(Filter effect, Vector2 position)
static Microsoft.Xna.Framework.Color ColorOfTheSkies
Definition Main.cs:2577
static SpriteBatch spriteBatch
Definition Main.cs:974
static int myPlayer
Definition Main.cs:1801
static Main instance
Definition Main.cs:283
static SpriteViewMatrix GameViewMatrix
Definition Main.cs:227
static Player[] player
Definition Main.cs:1803
static Matrix Invert(Matrix matrix)
Definition Matrix.cs:1694