Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ScreensManager.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
5 public static class ScreensManager {
6 public class AnimationData {
8
10
11 public float Factor;
12
13 public float Speed;
14
15 public object[] Parameters;
16 }
17
18 public static Dictionary<string, Screen> m_screens = [];
19
21
22 public static PrimitivesRenderer2D m_pr2 = new();
23
24 public static PrimitivesRenderer3D m_pr3 = new();
25
26 public static Random Random = new(0);
27
29
31
32 public static Matrix m_vrQuadMatrix;
33
34 public static float DebugUiScale = 1f;
35
36 public static ContainerWidget RootWidget { get; set; }
37
38 public static bool IsAnimating => m_animationData != null;
39
40 public static Screen CurrentScreen { get; set; }
41
45 public static Screen PreviousScreen {
46 get;
47 set;
48 }
49
50 public static Stack<Screen> HistoryStack { get; } = [];
51
52 public static Screen TopOfHistoryScreen => HistoryStack.TryPeek(out Screen screen) ? screen : null;
53
54 public static float FinalUiScale { get; set; }
55
56 public static T FindScreen<T>(string name) where T : Screen {
57 m_screens.TryGetValue(name, out Screen value);
58 return (T)value;
59 }
60
61 public static void AddScreen(string name, Screen screen) {
62 m_screens.Add(name, screen);
63 }
64
65 public static void SwitchScreen(string name, params object[] parameters) {
66 SwitchScreen(string.IsNullOrEmpty(name) ? null : FindScreen<Screen>(name), parameters);
67 }
68
69 public static void SwitchScreen(Screen screen, params object[] parameters) {
70 if (screen == CurrentScreen) {
71 return;
72 }
73 if (screen == null) {
74 throw new ArgumentNullException(nameof(screen));
75 }
76 if (m_animationData != null) {
78 }
80 NewScreen = screen, OldScreen = CurrentScreen, Parameters = parameters, Speed = CurrentScreen == null ? float.MaxValue : 4f
81 };
82 if (CurrentScreen != null) {
83 RootWidget.IsUpdateEnabled = false;
84 CurrentScreen.Input.Clear();
85 }
87 if (screen == TopOfHistoryScreen) {
88 HistoryStack.Pop();
89 }
90 else if (CurrentScreen != null) {
92 }
93 CurrentScreen = screen;
95 if (CurrentScreen != null) {
96 Log.Verbose($"Entered screen \"{GetScreenName(CurrentScreen)}\"");
98 }
99 }
100
101 public static void GoBack(params object[] parameters) {
102 SwitchScreen(TopOfHistoryScreen, parameters);
103 }
104
105 public static void Initialize() {
106 RootWidget = new CanvasWidget();
107 RootWidget.WidgetsHierarchyInput = new WidgetInput();
108 InitScreens();
109 SwitchScreen("Loading");
111 }
112
113 public static void InitScreens() {
114 LoadingScreen loadingScreen = new();
115 AddScreen("Loading", loadingScreen);
116 }
117
118 public static void Update() {
119 if (m_animationData != null) {
121 }
123 }
124
125 public static void Draw() {
126 Utilities.Dispose(ref m_uiRenderTarget);
128 }
129
130 public static void UpdateAnimation() {
131 float num = MathUtils.Min(Time.FrameDuration, 0.1f);
132 float factor = m_animationData.Factor;
133 m_animationData.Factor = MathUtils.Min(m_animationData.Factor + m_animationData.Speed * num, 1f);
134 if (m_animationData.Factor < 0.5f) {
135 if (m_animationData.OldScreen != null) {
136 float num2 = 2f * (0.5f - m_animationData.Factor);
137 float scale = 1f;
138 m_animationData.OldScreen.ColorTransform = new Color(num2, num2, num2, num2);
139 m_animationData.OldScreen.RenderTransform =
141 (0f - m_animationData.OldScreen.ActualSize.X) / 2f,
142 (0f - m_animationData.OldScreen.ActualSize.Y) / 2f,
143 0f
144 )
145 * Matrix.CreateScale(scale)
146 * Matrix.CreateTranslation(m_animationData.OldScreen.ActualSize.X / 2f, m_animationData.OldScreen.ActualSize.Y / 2f, 0f);
147 }
148 }
149 else if (factor < 0.5f) {
150 if (m_animationData.OldScreen != null) {
151 m_animationData.OldScreen.Leave();
153 "OnScreenLeaved",
154 loader => {
155 loader.OnScreenLeaved(m_animationData.OldScreen);
156 return false;
157 }
158 );
159 RootWidget.Children.Remove(m_animationData.OldScreen);
160 }
161 if (m_animationData.NewScreen != null) {
162 RootWidget.Children.Insert(0, m_animationData.NewScreen);
163 m_animationData.NewScreen.Enter(m_animationData.Parameters);
164 m_animationData.NewScreen.ColorTransform = Color.Transparent;
166 "OnScreenEntered",
167 loader => {
168 loader.OnScreenEntered(m_animationData.NewScreen, m_animationData.Parameters);
169 return false;
170 }
171 );
172 RootWidget.IsUpdateEnabled = true;
173 }
174 }
175 else if (m_animationData.NewScreen != null) {
176 float num3 = 2f * (m_animationData.Factor - 0.5f);
177 float scale2 = 1f;
178 m_animationData.NewScreen.ColorTransform = new Color(num3, num3, num3, num3);
179 m_animationData.NewScreen.RenderTransform =
181 (0f - m_animationData.NewScreen.ActualSize.X) / 2f,
182 (0f - m_animationData.NewScreen.ActualSize.Y) / 2f,
183 0f
184 )
185 * Matrix.CreateScale(scale2)
186 * Matrix.CreateTranslation(m_animationData.NewScreen.ActualSize.X / 2f, m_animationData.NewScreen.ActualSize.Y / 2f, 0f);
187 }
188 if (m_animationData.Factor >= 1f) {
189 EndAnimation();
190 }
191 }
192
193 public static void EndAnimation() {
194 if (m_animationData.NewScreen != null) {
195 m_animationData.NewScreen.ColorTransform = Color.White;
196 m_animationData.NewScreen.RenderTransform = Matrix.CreateScale(1f);
197 }
198 m_animationData = null;
199 }
200
201 public static string GetScreenName(Screen screen) {
202 string key = m_screens.FirstOrDefault(kvp => kvp.Value == screen).Key;
203 if (key == null) {
204 return string.Empty;
205 }
206 return key;
207 }
208
209 public static void AnimateVrQuad() {
210 if (Time.FrameIndex >= 5) {
211 float num = 6f;
212 Matrix hmdMatrix = Matrix.Identity;
213 Vector3 vector = hmdMatrix.Translation
214 + num * (Vector3.Normalize(hmdMatrix.Forward * new Vector3(1f, 0f, 1f)) + new Vector3(0f, 0.1f, 0f));
216 m_vrQuadPosition = vector;
217 }
218 if (Vector3.Distance(m_vrQuadPosition, vector) > 0f) {
219 Vector3 v = vector * new Vector3(1f, 0f, 1f) - m_vrQuadPosition * new Vector3(1f, 0f, 1f);
220 Vector3 v2 = vector * new Vector3(0f, 1f, 0f) - m_vrQuadPosition * new Vector3(0f, 1f, 0f);
221 float num2 = v.Length();
222 float num3 = v2.Length();
223 m_vrQuadPosition += v * MathUtils.Min(0.75f * MathF.Pow(MathUtils.Max(num2 - 0.15f * num, 0f), 0.33f) * Time.FrameDuration, 1f);
224 m_vrQuadPosition += v2 * MathUtils.Min(1.5f * MathF.Pow(MathUtils.Max(num3 - 0.05f * num, 0f), 0.33f) * Time.FrameDuration, 1f);
225 }
226 Vector2 vector2 = new(m_uiRenderTarget.Width / (float)m_uiRenderTarget.Height, 1f);
227 vector2 /= MathUtils.Max(vector2.X, vector2.Y);
228 vector2 *= 7.5f;
229 m_vrQuadMatrix.Forward = Vector3.Normalize(hmdMatrix.Translation - m_vrQuadPosition);
230 m_vrQuadMatrix.Right = Vector3.Normalize(Vector3.Cross(Vector3.UnitY, m_vrQuadMatrix.Forward)) * vector2.X;
231 m_vrQuadMatrix.Up = Vector3.Normalize(Vector3.Cross(m_vrQuadMatrix.Forward, m_vrQuadMatrix.Right)) * vector2.Y;
232 m_vrQuadMatrix.Translation = m_vrQuadPosition - 0.5f * (m_vrQuadMatrix.Right + m_vrQuadMatrix.Up);
233 RootWidget.WidgetsHierarchyInput.VrQuadMatrix = m_vrQuadMatrix;
234 }
235 }
236
237 public static void DrawVrQuad() {
238 QueueQuad(
239 m_pr3.TexturedBatch(
241 false,
242 0,
247 ),
248 m_vrQuadMatrix.Translation,
249 m_vrQuadMatrix.Right,
252 );
253 }
254
255 public static void DrawVrBackground() {
256 Matrix hmdMatrix = Matrix.Identity;
257 TexturedBatch3D batch = m_pr3.TexturedBatch(ContentManager.Get<Texture2D>("Textures/Star"));
258 Random.Seed(0);
259 for (int i = 0; i < 1500; i++) {
260 float f = MathF.Pow(Random.Float(0f, 1f), 6f);
261 Color rGB = (MathUtils.Lerp(0.05f, 0.4f, f) * Color.White).RGB;
262 int num = 6;
263 Vector3 vector = Random.Vector3(500f);
264 Vector3 vector2 = Vector3.Normalize(Vector3.Cross(vector, Vector3.UnitY)) * num;
265 Vector3 up = Vector3.Normalize(Vector3.Cross(vector2, vector)) * num;
266 QueueQuad(batch, vector + hmdMatrix.Translation, vector2, up, rGB);
267 }
268 TexturedBatch3D batch2 = m_pr3.TexturedBatch(
269 ContentManager.Get<Texture2D>("Textures/Blocks"),
270 true,
271 1,
272 null,
273 null,
274 null,
276 );
277 for (int j = -8; j <= 8; j++) {
278 for (int k = -8; k <= 8; k++) {
279 float num2 = 1f;
280 float num3 = 1f;
281 Vector3 vector3 = new Vector3((j - 0.5f) * num2, 0f, (k - 0.5f) * num2)
282 + new Vector3(MathF.Round(hmdMatrix.Translation.X), 0f, MathF.Round(hmdMatrix.Translation.Z));
283 float num4 = Vector3.Distance(vector3, hmdMatrix.Translation);
284 float num5 = MathUtils.Lerp(1f, 0f, MathUtils.Saturate(num4 / 7f));
285 if (num5 > 0f) {
286 QueueQuad(
287 batch2,
288 vector3,
289 new Vector3(num3, 0f, 0f),
290 new Vector3(0f, 0f, num3),
291 Color.Gray * num5,
292 new Vector2(0.1875f, 0.25f),
293 new Vector2(0.25f, 0.3125f)
294 );
295 }
296 }
297 }
298 }
299
300 public static void LayoutAndDrawWidgets() {
301 if (m_animationData != null) {
302 Display.Clear(Color.Black, 1f, 0);
303 }
304 float num = 850f / Math.Clamp(SettingsManager.UIScale, 0.5f, 1.2f) * DebugUiScale;
306 float num2 = vector.X / num;
307 Vector2 availableSize = new(num, num / vector.X * vector.Y);
308 float num3 = num * 9f / 16f;
309 if (vector.Y / num2 < num3) {
310 num2 = vector.Y / num3;
311 availableSize = new Vector2(num3 / vector.Y * vector.X, num3);
312 }
313 FinalUiScale = 1f / num2;
314 RootWidget.LayoutTransform = Matrix.CreateScale(num2, num2, 1f);
316 RootWidget.LayoutTransform *= new Matrix(
317 -1f,
318 0f,
319 0f,
320 0f,
321 0f,
322 -1f,
323 0f,
324 0f,
325 0f,
326 0f,
327 1f,
328 0f,
329 0f,
330 0f,
331 0f,
332 1f
333 );
334 }
337 }
338
339 public static void QueueQuad(FlatBatch3D batch, Vector3 corner, Vector3 right, Vector3 up, Color color) {
340 Vector3 p = corner + right;
341 Vector3 p2 = corner + right + up;
342 Vector3 p3 = corner + up;
343 batch.QueueQuad(corner, p, p2, p3, color);
344 }
345
346 public static void QueueQuad(TexturedBatch3D batch, Vector3 center, Vector3 right, Vector3 up, Color color) {
347 QueueQuad(
348 batch,
349 center,
350 right,
351 up,
352 color,
353 new Vector2(0f, 0f),
354 new Vector2(1f, 1f)
355 );
356 }
357
358 public static void QueueQuad(TexturedBatch3D batch,
359 Vector3 corner,
360 Vector3 right,
361 Vector3 up,
362 Color color,
363 Vector2 tc1,
364 Vector2 tc2) {
365 Vector3 p = corner + right;
366 Vector3 p2 = corner + right + up;
367 Vector3 p3 = corner + up;
368 batch.QueueQuad(
369 corner,
370 p,
371 p2,
372 p3,
373 new Vector2(tc1.X, tc2.Y),
374 new Vector2(tc2.X, tc2.Y),
375 new Vector2(tc2.X, tc1.Y),
376 new Vector2(tc1.X, tc1.Y),
377 color
378 );
379 }
380
381 public static void UpdateTopBarMarginLeft() {
383 && CurrentScreen?.Children.Find<BevelledButtonWidget>("TopBar.Back", false)?.ParentWidget?.ParentWidget is CanvasWidget topBar
384 && topBar.Size.X == 64f) {
385 topBar.MarginLeft = Window.DisplayCutoutInsets.X * FinalUiScale;
386 }
387 }
388
389 public static void ResetAllTopBarMarginLeft() {
390 foreach (Screen screen in m_screens.Values) {
391 if (screen.Children.Find<BevelledButtonWidget>("TopBar.Back", false)?.ParentWidget?.ParentWidget is CanvasWidget topBar
392 && topBar.Size.X == 64f) {
393 topBar.MarginLeft = 0f;
394 }
395 }
396 }
397 }
398}
Engine.Color Color
Engine.Vector3 Vector3
static readonly BlendState Opaque
static readonly DepthStencilState Default
static void Clear(Vector4? color, float? depth=null, int? stencil=null)
static Viewport Viewport
void QueueQuad(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Color color1, Color color2, Color color3, Color color4)
绘制矩形(支持渐变)
static readonly RasterizerState CullNoneScissor
void QueueQuad(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Vector2 texCoord1, Vector2 texCoord2, Vector2 texCoord3, Vector2 texCoord4, Color color)
static void Verbose(object message)
定义 Log.cs:44
static int Min(int x1, int x2)
static float Saturate(float x)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
static int FrameIndex
定义 Time.cs:26
static float FrameDuration
定义 Time.cs:46
static Action< Vector4, bool > DisplayCutoutInsetsChanged
readonly WidgetsList Children
static object Get(Type type, string name)
static void LayoutAndDrawWidgets()
static Vector3 m_vrQuadPosition
static AnimationData m_animationData
static Screen PreviousScreen
上一个Screen
static void GoBack(params object[] parameters)
static void QueueQuad(TexturedBatch3D batch, Vector3 center, Vector3 right, Vector3 up, Color color)
static void ResetAllTopBarMarginLeft()
static T FindScreen< T >(string name)
static void QueueQuad(TexturedBatch3D batch, Vector3 corner, Vector3 right, Vector3 up, Color color, Vector2 tc1, Vector2 tc2)
static PrimitivesRenderer3D m_pr3
static PrimitivesRenderer2D m_pr2
static void SwitchScreen(string name, params object[] parameters)
static ContainerWidget RootWidget
static void AddScreen(string name, Screen screen)
static Stack< Screen > HistoryStack
static void SwitchScreen(Screen screen, params object[] parameters)
static Screen TopOfHistoryScreen
static string GetScreenName(Screen screen)
static Dictionary< string, Screen > m_screens
static RenderTarget2D m_uiRenderTarget
static void QueueQuad(FlatBatch3D batch, Vector3 corner, Vector3 right, Vector3 up, Color color)
static void UpdateTopBarMarginLeft()
static void DrawWidgetsHierarchy(Widget rootWidget)
ContainerWidget ParentWidget
static void LayoutWidgetsHierarchy(Widget rootWidget, Vector2 availableSize)
static void UpdateWidgetsHierarchy(Widget rootWidget)
Widget Find(string name, Type type, bool throwIfNotFound=true)
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static Color Transparent
定义 Color.cs:5
static Color White
static Color Gray
static Color Black
定义 Color.cs:7
static Matrix CreateTranslation(float x, float y, float z)
static readonly Matrix Identity
Vector3 Translation
Vector3 Forward
static Matrix CreateScale(float scale)
static Vector3 Cross(Vector3 v1, Vector3 v2)
static Vector3 Normalize(Vector3 v)
static readonly Vector3 Zero
static float Distance(Vector3 v1, Vector3 v2)
static readonly Vector3 UnitY