Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
MotdWidget.cs
浏览该文件的文档.
1using System.Text.Json;
2using Engine;
3using Engine.Input;
4
5namespace Game {
6 public class MotdWidget : CanvasWidget {
7 public class LineData {
8 public float Time;
9
10 public Widget Widget;
11 }
12
14
15 public List<LineData> m_lines = [];
16
18
19 public double m_lastLineChangeTime;
20
21 public int m_tapsCount;
22
23 public bool Noticed;
24
31
32 public override void Update() {
33 if (!Noticed
34 && MotdManager.UpdateResult != null) {
35 JsonElement jsonDocument = MotdManager.UpdateResult.RootElement;
36 if (jsonDocument.GetProperty("update").GetString() == "1") {
37 try {
39 ScreensManager.m_screens["MainMenu"],
40 new MessageDialog(
41 jsonDocument.GetProperty("title").GetString(),
42 jsonDocument.GetProperty("content").GetString(),
43 jsonDocument.GetProperty("btn").GetString(),
45 btn => {
46 if (btn == MessageDialogButton.Button1) {
47 WebBrowserManager.LaunchBrowser(jsonDocument.GetProperty("url").GetString());
48 }
49 }
50 )
51 );
52 }
53 catch (Exception e) {
54 Log.Error($"Failed processing Update check. Reason: {e.Message}");
55 }
56 finally {
57 Noticed = true;
58 }
59 }
60 }
61 if (Input.Tap.HasValue) {
62 Widget widget = HitTestGlobal(Input.Tap.Value);
63 if (widget != null
64 && (widget == this || widget.IsChildWidgetOf(this))) {
66 }
67 }
68 if (m_tapsCount >= 5) {
69 m_tapsCount = 0;
70 MotdManager.ForceRedownload();
71 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
72 }
73 if (Input.IsKeyDownOnce(Key.PageUp)) {
75 }
76 if (Input.IsKeyDownOnce(Key.PageDown)) {
78 }
79 if (m_lines.Count > 0) {
81 double realTime = Time.RealTime;
82 if (m_lastLineChangeTime == 0.0
83 || realTime - m_lastLineChangeTime >= m_lines[m_currentLineIndex].Time) {
85 }
86 float num2 = (float)(realTime - m_lastLineChangeTime);
87 float num3 = (float)(m_lastLineChangeTime + m_lines[m_currentLineIndex].Time - 0.33000001311302185 - realTime);
89 position: new Vector2(
90 !(num2 < num3)
91 ? ActualSize.X * (1f - MathUtils.PowSign(MathF.Sin(MathUtils.Saturate(1.5f * num3) * (float)Math.PI / 2f), 0.33f))
92 : ActualSize.X * (MathUtils.PowSign(MathF.Sin(MathUtils.Saturate(1.5f * num2) * (float)Math.PI / 2f), 0.33f) - 1f),
93 0f
94 ),
95 widget: m_containerWidget
96 );
97 m_containerWidget.Size = ActualSize;
98 }
99 else {
100 m_containerWidget.Children.Clear();
101 }
102 }
103
104 public void GotoLine(int index) {
105 if (m_lines.Count > 0) {
106 m_currentLineIndex = MathUtils.Max(index, 0) % m_lines.Count;
107 m_containerWidget.Children.Clear();
110 m_tapsCount = 0;
111 }
112 }
113
114 public void Restart() {
117 }
118
120 m_lines.Clear();
121 if (MotdManager.MessageOfTheDay != null) {
123 try {
124 LineData item = ParseLine(line);
125 m_lines.Add(item);
126 }
127 catch (Exception ex) {
128 Log.Warning($"Error loading MOTD line {MotdManager.MessageOfTheDay.Lines.IndexOf(line) + 1}. Reason: {ex.Message}");
129 }
130 }
131 }
132 Restart();
133 }
134
136 LineData lineData = new() { Time = line.Time };
137 if (line.Node != null) {
138 lineData.Widget = LoadWidget(null, line.Node, null);
139 }
140 else {
141 if (string.IsNullOrEmpty(line.Text)) {
142 throw new InvalidOperationException("Invalid MOTD line.");
143 }
144 StackPanelWidget stackPanelWidget = new() {
146 };
147 string[] array = line.Text.Replace("\r", "").Split(["\n"], StringSplitOptions.None);
148 for (int i = 0; i < array.Length; i++) {
149 string text = array[i].Trim();
150 if (!string.IsNullOrEmpty(text)) {
151 LabelWidget widget = new() {
152 Text = text, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center, DropShadow = true
153 };
154 stackPanelWidget.Children.Add(widget);
155 }
156 }
157 lineData.Widget = stackPanelWidget;
158 }
159 return lineData;
160 }
161 }
162}
static void Error(object message)
定义 Log.cs:80
static void Warning(object message)
定义 Log.cs:68
static int Max(int x1, int x2)
static double RealTime
定义 Time.cs:38
void SetWidgetPosition(Widget widget, Vector2? position)
readonly WidgetsList Children
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
static Action MessageOfTheDayUpdated
static JsonDocument UpdateResult
static Message MessageOfTheDay
LineData ParseLine(MotdManager.Line line)
List< LineData > m_lines
void GotoLine(int index)
override void Update()
CanvasWidget m_containerWidget
void MotdManager_MessageOfTheDayUpdated()
static Dictionary< string, Screen > m_screens
static Widget LoadWidget(object eventsTarget, XElement node, ContainerWidget parentWidget)
virtual Widget HitTestGlobal(Vector2 point, Func< Widget, bool > predicate=null)
WidgetInput Input
virtual WidgetAlignment VerticalAlignment
virtual WidgetAlignment HorizontalAlignment
Vector2 ActualSize
virtual bool IsChildWidgetOf(ContainerWidget containerWidget)
void Add(Widget widget)