Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
EditMemoryBankDialogAPI.cs
浏览该文件的文档.
1using System.Text;
2using System.Xml.Linq;
3using Engine;
4
5namespace Game {
8 public DynamicArray<byte> Data = [];
10 public Action onCancel;
11 public int clickpos;
12 public bool isSetPos; //是否为设定位置模式
13 public int setPosN; //第几位数
14 public int lastvalue;
15 public bool isclick = true;
16 public List<ClickTextWidget> list = [];
17 public const string fName = "EditMemoryBankDialogAPI";
18
19 public byte LastOutput { get; set; }
20
21 public EditMemoryBankDialogAPI(MemoryBankData memoryBankData, Action onCancel) {
22 //Keyboard.CharacterEntered += ChangeNumber;
23 memory = memoryBankData;
24 Data.Clear();
25 Data.AddRange(memory.Data);
26 CanvasWidget canvasWidget = new() {
28 };
29 BevelledRectangleWidget rectangleWidget = new() { Style = ContentManager.Get<XElement>("Styles/DialogArea") };
30 StackPanelWidget stackPanel = new() { Direction = LayoutDirection.Vertical };
31 LabelWidget labelWidget = new() {
33 };
34 StackPanelWidget stackPanelWidget = new() {
35 Direction = LayoutDirection.Horizontal,
38 Margin = new Vector2(10f, 10f)
39 };
40 Children.Add(canvasWidget);
41 canvasWidget.Children.Add(rectangleWidget);
42 canvasWidget.Children.Add(stackPanel);
43 stackPanel.Children.Add(labelWidget);
44 stackPanel.Children.Add(stackPanelWidget);
45 stackPanelWidget.Children.Add(initData());
46 stackPanelWidget.Children.Add(InitButton());
47 MainView = stackPanel;
48 this.onCancel = onCancel;
49 lastvalue = memory.Read(0);
50 }
51
52 public byte Read(int address) => address >= 0 && address < Data.Count ? Data.Array[address] : (byte)0;
53
54 public void Write(int address, byte data) {
55 if (address >= 0
56 && address < Data.Count) {
57 Data.Array[address] = data;
58 }
59 else if (address >= 0
60 && address < 256
61 && data != 0) {
62 Data.Count = Math.Max(Data.Count, address + 1);
63 Data.Array[address] = data;
64 }
65 }
66
67 public void LoadString(string data) {
68 string[] array = data.Split(';', StringSplitOptions.RemoveEmptyEntries);
69 if (array.Length >= 1) {
70 string text = array[0];
71 text = text.TrimEnd('0');
72 Data.Clear();
73 for (int i = 0; i < Math.Min(text.Length, 256); i++) {
74 int num = MemoryBankData.m_hexChars.IndexOf(char.ToUpperInvariant(text[i]));
75 if (num < 0) {
76 num = 0;
77 }
78 Data.Add((byte)num);
79 }
80 }
81 if (array.Length >= 2) {
82 string text2 = array[1];
83 int num2 = MemoryBankData.m_hexChars.IndexOf(char.ToUpperInvariant(text2[0]));
84 if (num2 < 0) {
85 num2 = 0;
86 }
87 LastOutput = (byte)num2;
88 }
89 }
90
91 public string SaveString() => SaveString(true);
92
93 public string SaveString(bool saveLastOutput) {
94 StringBuilder stringBuilder = new();
95 int num = Data.Count;
96 for (int j = 0; j < num; j++) {
97 int index = Math.Clamp((int)Data.Array[j], 0, 15);
98 stringBuilder.Append(MemoryBankData.m_hexChars[index]);
99 }
100 if (saveLastOutput) {
101 stringBuilder.Append(';');
102 stringBuilder.Append(MemoryBankData.m_hexChars[Math.Clamp((int)LastOutput, 0, 15)]);
103 }
104 return stringBuilder.ToString();
105 }
106
107 public Widget initData() {
108 StackPanelWidget stack = new() {
109 Direction = LayoutDirection.Vertical,
112 Margin = new Vector2(10, 0)
113 };
114 for (int i = 0; i < 17; i++) {
115 StackPanelWidget line = new() { Direction = LayoutDirection.Horizontal };
116 for (int j = 0; j < 17; j++) {
117 int addr = (i - 1) * 16 + (j - 1);
118 if (j > 0
119 && i > 0) {
120 ClickTextWidget clickTextWidget = new(
121 new Vector2(22),
122 $"{MemoryBankData.m_hexChars[Read(addr)]}",
123 delegate {
124 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
125 clickpos = addr;
126 isclick = true;
127 }
128 );
129 list.Add(clickTextWidget);
130 line.Children.Add(clickTextWidget);
131 }
132 else {
133 int p;
134 if (i == 0
135 && j > 0) {
136 p = j - 1;
137 }
138 else if (j == 0
139 && i > 0) {
140 p = i - 1;
141 }
142 else {
143 ClickTextWidget click = new(new Vector2(22), "", null);
144 line.Children.Add(click);
145 continue;
146 }
147 ClickTextWidget clickTextWidget = new(new Vector2(22), MemoryBankData.m_hexChars[p].ToString(), delegate { });
148 clickTextWidget.labelWidget.Color = Color.DarkGray;
149 line.Children.Add(clickTextWidget);
150 }
151 }
152 stack.Children.Add(line);
153 }
154 return stack;
155 }
156
157 public Widget makeFuncButton(string txt, Action func) {
158 ClickTextWidget clickText = new(new Vector2(40), txt, func, true) { BorderColor = Color.White, Margin = new Vector2(2) };
159 clickText.labelWidget.FontScale = txt.Length > 1 ? 0.7f : 1f;
160 clickText.labelWidget.Color = Color.White;
161 return clickText;
162 }
163
164 // ReSharper disable UnusedMember.Local
165 void ChangeNumber(char pp)
166 // ReSharper restore UnusedMember.Local
167 {
168 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
169 Write(clickpos, (byte)pp); //写入数据
170 lastvalue = pp;
171 clickpos += 1; //自动加1
172 if (clickpos > 255) {
173 clickpos = 0;
174 }
175 isclick = true;
176 }
177
179 StackPanelWidget stack = new() {
180 Direction = LayoutDirection.Vertical,
183 Margin = new Vector2(10, 10)
184 };
185 for (int i = 0; i < 6; i++) {
186 StackPanelWidget stackPanelWidget = new() { Direction = LayoutDirection.Horizontal };
187 for (int j = 0; j < 3; j++) {
188 int cc = i * 3 + j;
189 if (cc < 15) {
190 int pp = cc + 1;
191 stackPanelWidget.Children.Add(
193 $"{MemoryBankData.m_hexChars[pp]}",
194 delegate {
195 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
196 if (!isSetPos) {
197 Write(clickpos, (byte)pp); //写入数据
198 lastvalue = pp;
199 clickpos += 1; //自动加1
200 if (clickpos > 255) {
201 clickpos = 0;
202 }
203 isclick = true;
204 }
205 else { //处于设定位置模式
206 if (setPosN == 0) {
207 clickpos = 16 * pp;
208 }
209 else if (setPosN == 1) {
210 clickpos += pp;
211 }
212 setPosN += 1;
213 if (setPosN == 2) {
214 if (clickpos > 0xff) {
215 clickpos = 0;
216 }
217 setPosN = 0;
218 isclick = true;
219 isSetPos = false;
220 }
221 }
222 }
223 )
224 );
225 }
226 else if (cc == 15) {
227 stackPanelWidget.Children.Add(
229 $"{MemoryBankData.m_hexChars[0]}",
230 delegate {
231 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
232 if (!isSetPos) {
233 Write(clickpos, 0); //写入数据
234 lastvalue = 0;
235 clickpos += 1; //自动加1
236 if (clickpos >= 255) {
237 clickpos = 0;
238 }
239 isclick = true;
240 }
241 else { //处于设定位置模式
242 if (setPosN == 0) {
243 clickpos = 0;
244 }
245 else if (setPosN == 1) {
246 clickpos += 0;
247 }
248 setPosN += 1;
249 if (setPosN == 2) {
250 if (clickpos > 0xff) {
251 clickpos = 0;
252 }
253 setPosN = 0;
254 isclick = true;
255 isSetPos = false;
256 }
257 }
258 }
259 )
260 );
261 }
262 else if (cc == 16) {
263 stackPanelWidget.Children.Add(
266 delegate {
267 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
268 for (int ai = 0; ai < Data.Count; ai++) {
269 Write(ai, 0);
270 }
271 isclick = true;
272 }
273 )
274 );
275 }
276 else if (cc == 17) {
277 stackPanelWidget.Children.Add(
280 delegate {
281 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
282 DynamicArray<byte> tmp = new();
283 tmp.AddRange(Data);
284 tmp.Count = 256;
285 for (int c = 0; c < 16; c++) {
286 for (int d = 0; d < 16; d++) {
287 Write(c + d * 16, tmp[c * 16 + d]);
288 }
289 }
290 clickpos = 0;
291 isclick = true;
292 }
293 )
294 );
295 }
296 }
297 stack.Children.Add(stackPanelWidget);
298 }
299 LabelWidget labelWidget = new() {
300 FontScale = 0.8f,
301 Text = LanguageControl.GetContentWidgets(fName, 3),
302 HorizontalAlignment = WidgetAlignment.Center,
303 Margin = new Vector2(0f, 10f),
304 Color = Color.DarkGray
305 };
306 stack.Children.Add(labelWidget);
307 stack.Children.Add(
308 makeTextBox(
309 delegate(TextBoxWidget textBoxWidget) {
310 LoadString(textBoxWidget.Text);
311 isclick = true;
312 },
313 memory.SaveString(false)
314 )
315 );
316 stack.Children.Add(
317 MakeButton(
319 delegate {
320 for (int i = 0; i < Data.Count; i++) {
321 memory.Write(i, Data[i]);
322 }
323 onCancel?.Invoke();
324 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
326 }
327 )
328 );
329 stack.Children.Add(
330 MakeButton(
331 LanguageControl.GetContentWidgets(fName, 5),
332 delegate {
333 AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
334 DialogsManager.HideDialog(this);
335 isclick = true;
336 }
337 )
338 );
339 return stack;
340 }
341
342 public Widget makeTextBox(Action<TextBoxWidget> ac, string text = "") {
344 RectangleWidget rectangleWidget = new() { FillColor = Color.Black, OutlineColor = Color.White, Size = new Vector2(120, 30) };
345 StackPanelWidget stack = new() { Direction = LayoutDirection.Vertical };
346 TextBoxWidget textBox = new() {
348 Color = new Color(255, 255, 255),
349 Margin = new Vector2(4f, 0f),
350 Size = new Vector2(120, 30),
351 MaximumLength = 256
352 };
353 textBox.FontScale = 0.7f;
354 textBox.Text = text;
355 textBox.TextChanged += ac;
356 stack.Children.Add(textBox);
357 canvasWidget.Children.Add(rectangleWidget);
358 canvasWidget.Children.Add(stack);
359 return canvasWidget;
360 }
361
362 static Widget MakeButton(string txt, Action tas) {
363 ClickTextWidget clickTextWidget = new(new Vector2(120, 30), txt, tas) { BorderColor = Color.White, Margin = new Vector2(0, 3) };
364 clickTextWidget.labelWidget.FontScale = 0.7f;
365 clickTextWidget.labelWidget.Color = Color.Green;
366 return clickTextWidget;
367 }
368
369 public override void Update() {
370 if (Input.Back
371 || Input.Cancel) {
373 }
374 if (isSetPos) {
375 list[clickpos].BorderColor = Color.Red; //设定选择颜色
376 return;
377 }
378 if (!isclick) {
379 return;
380 }
381 for (int i = 0; i < list.Count; i++) {
382 if (i == clickpos) {
383 list[i].BorderColor = Color.Yellow; //设定选择颜色
384 }
385 else {
386 list[i].BorderColor = Color.Transparent; //设定选择颜色
387 }
388 list[i].labelWidget.Text = $"{MemoryBankData.m_hexChars[Read(i)]}";
389 }
390 isclick = false;
391 }
392 }
393}
Engine.Color Color
static void PlaySound(string name, float volume, float pitch, float pan)
readonly WidgetsList Children
static object Get(Type type, string name)
static void HideDialog(Dialog dialog)
EditMemoryBankDialogAPI(MemoryBankData memoryBankData, Action onCancel)
static Widget MakeButton(string txt, Action tas)
Widget makeFuncButton(string txt, Action func)
Widget makeTextBox(Action< TextBoxWidget > ac, string text="")
static string GetContentWidgets(string name, string prop)
static List< char > m_hexChars
virtual WidgetAlignment VerticalAlignment
virtual Vector2 Margin
virtual WidgetAlignment HorizontalAlignment
XElement Style
void Add(Widget widget)
static Color Transparent
定义 Color.cs:5
static Color Red
static Color White
static Color Yellow
static Color Green
static Color DarkGray
定义 Color.cs:9
static Color Black
定义 Color.cs:7