Attempt to serialize node data.

尝试序列化节点数据。
This commit is contained in:
Cold-Mint 2024-05-14 22:26:33 +08:00
parent 968fa7d723
commit 2fded22910
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
12 changed files with 239 additions and 17 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.2.1"> <Project Sdk="Godot.NET.Sdk/4.2.2">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework> <TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>

View File

@ -19,3 +19,7 @@ name,名称,Name,めいしょう
describe,描述,Describe,ないよう describe,描述,Describe,ないよう
creation,创建,Creation,創建 creation,创建,Creation,創建
default_room_name,房间{0},Room{0},部屋{0}です default_room_name,房间{0},Room{0},部屋{0}です
room_template_collection_prompt,房间模板集(每一行被认为是一条房间模板地址。),Room template set (Each row is considered to be one room template address.),お部屋テンプレートセット(各行がお部屋テンプレートアドレスとされています。)
error_specifying_room_template_line,位于{0}错误,文件不存在。,"Located at {0} error, file does not exist.",{0}エラーに位置し、ファイルは存在しません。
line_errors_must_start_with_res,位于{0}错误必须以res://开头。,"Located at {0} error, must start with res://.",{0}エラーに位置し、res://で始めなければなりません。
display_current_page_data,显示当前页面数据,Displays the current page data,現在のページデータを表示します
1 id zh en jp
19 describe 描述 Describe ないよう
20 creation 创建 Creation 創建
21 default_room_name 房间{0} Room{0} 部屋{0}です
22 room_template_collection_prompt 房间模板集(每一行被认为是一条房间模板地址。) Room template set (Each row is considered to be one room template address.) お部屋テンプレートセット(各行がお部屋テンプレートアドレスとされています。)
23 error_specifying_room_template_line 位于{0}错误,文件不存在。 Located at {0} error, file does not exist. {0}エラーに位置し、ファイルは存在しません。
24 line_errors_must_start_with_res 位于{0}错误,必须以res://开头。 Located at {0} error, must start with res://. {0}エラーに位置し、res://で始めなければなりません。
25 display_current_page_data 显示当前页面数据 Displays the current page data 現在のページデータを表示します

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -63,6 +63,22 @@ offset_right = 72.0
offset_bottom = 131.0 offset_bottom = 131.0
text = "describe" text = "describe"
[node name="Label4" type="Label" parent="CreateOrEditorPanel"]
layout_mode = 1
offset_left = 15.0
offset_top = 152.0
offset_right = 75.0
offset_bottom = 177.0
text = "room_template_collection_prompt"
[node name="RoomTemplateTipsLabel" type="Label" parent="CreateOrEditorPanel"]
layout_mode = 1
offset_left = 18.0
offset_top = 319.0
offset_right = 265.0
offset_bottom = 344.0
text = "errorTip"
[node name="RoomNameLineEdit" type="LineEdit" parent="CreateOrEditorPanel"] [node name="RoomNameLineEdit" type="LineEdit" parent="CreateOrEditorPanel"]
layout_mode = 1 layout_mode = 1
anchors_preset = 10 anchors_preset = 10
@ -118,6 +134,13 @@ grow_horizontal = 0
grow_vertical = 0 grow_vertical = 0
text = "creation" text = "creation"
[node name="RoomTemplateCollectionTextEdit" type="TextEdit" parent="CreateOrEditorPanel"]
layout_mode = 0
offset_left = 16.0
offset_top = 187.0
offset_right = 1111.0
offset_bottom = 303.0
[node name="HBoxContainer" type="HBoxContainer" parent="."] [node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 1 layout_mode = 1
anchors_preset = 1 anchors_preset = 1
@ -129,6 +152,10 @@ offset_right = -15.0
offset_bottom = 47.0 offset_bottom = 47.0
grow_horizontal = 0 grow_horizontal = 0
[node name="DisplayCurrentPageData" type="Button" parent="HBoxContainer"]
layout_mode = 2
text = "display_current_page_data"
[node name="ShowCreateRoomPanelButton" type="Button" parent="HBoxContainer"] [node name="ShowCreateRoomPanelButton" type="Button" parent="HBoxContainer"]
layout_mode = 2 layout_mode = 2
text = "create_room" text = "create_room"

View File

@ -0,0 +1,9 @@
namespace ColdMint.scripts.levelGraphEditor;
public class ConnectionData
{
public IRoomNodeData? From { get; set; }
public IRoomNodeData? To { get; set; }
public int FromPort { get; set; }
public int ToPort { get; set; }
}

View File

@ -0,0 +1,12 @@
using System.Collections.Generic;
namespace ColdMint.scripts.levelGraphEditor;
/// <summary>
/// <para>Level map editor saves data</para>
/// <para>关卡图编辑器保存的数据</para>
/// </summary>
public class LevelGraphEditorSaveData
{
public List<ConnectionData>? ConnectionData { get; set; }
}

View File

@ -16,11 +16,10 @@ public partial class RoomNode : GraphNode
Title = value.Title; Title = value.Title;
if (value.Description.Length == 0) if (value.Description.Length == 0)
{ {
_describeLabel.Visible = false; _describeLabel.Text = string.Empty;
} }
else else
{ {
_describeLabel.Visible = true;
_describeLabel.Text = value.Description; _describeLabel.Text = value.Description;
} }
@ -33,5 +32,4 @@ public partial class RoomNode : GraphNode
base._Ready(); base._Ready();
_describeLabel = GetNode<Label>("DescribeLabel"); _describeLabel = GetNode<Label>("DescribeLabel");
} }
} }

View File

@ -1,5 +1,11 @@
using ColdMint.scripts.levelGraphEditor; using System.Collections.Generic;
using System.Text.Json;
using ColdMint.scripts.debug;
using ColdMint.scripts.levelGraphEditor;
using ColdMint.scripts.serialization;
using ColdMint.scripts.utils;
using Godot; using Godot;
using Godot.Collections;
namespace ColdMint.scripts.loader.uiLoader; namespace ColdMint.scripts.loader.uiLoader;
@ -26,6 +32,9 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate
private Button? _returnButton; private Button? _returnButton;
private string? _defaultRoomName; private string? _defaultRoomName;
private int _roomIndex = 1; private int _roomIndex = 1;
private TextEdit? _roomTemplateCollectionTextEdit;
private Label? _roomTemplateTipsLabel;
private Button? _displayCurrentPageData;
public override void InitializeData() public override void InitializeData()
{ {
@ -38,6 +47,14 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate
public override void InitializeUi() public override void InitializeUi()
{ {
base.InitializeUi(); base.InitializeUi();
_roomTemplateTipsLabel = GetNode<Label>("CreateOrEditorPanel/RoomTemplateTipsLabel");
if (_roomTemplateTipsLabel != null)
{
_roomTemplateTipsLabel.Text = string.Empty;
}
_displayCurrentPageData = GetNode<Button>("HBoxContainer/DisplayCurrentPageData");
_roomTemplateCollectionTextEdit = GetNode<TextEdit>("CreateOrEditorPanel/RoomTemplateCollectionTextEdit");
_graphEdit = GetNode<GraphEdit>("GraphEdit"); _graphEdit = GetNode<GraphEdit>("GraphEdit");
_showCreateRoomPanelButton = GetNode<Button>("HBoxContainer/ShowCreateRoomPanelButton"); _showCreateRoomPanelButton = GetNode<Button>("HBoxContainer/ShowCreateRoomPanelButton");
_returnButton = GetNode<Button>("HBoxContainer/ReturnButton"); _returnButton = GetNode<Button>("HBoxContainer/ReturnButton");
@ -81,6 +98,63 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate
public override void LoadUiActions() public override void LoadUiActions()
{ {
base.LoadUiActions(); base.LoadUiActions();
if (_roomTemplateCollectionTextEdit != null)
{
_roomTemplateCollectionTextEdit.TextChanged += () =>
{
if (_roomTemplateTipsLabel == null)
{
return;
}
var text = _roomTemplateCollectionTextEdit.Text;
if (string.IsNullOrEmpty(text))
{
_roomTemplateTipsLabel.Text = string.Empty;
return;
}
var lastLine = StrUtils.GetLastLine(text);
if (string.IsNullOrEmpty(lastLine))
{
_roomTemplateTipsLabel.Text = string.Empty;
return;
}
//Parse the last line
//解析最后一行
if (lastLine.Length > 0)
{
if (!lastLine.StartsWith("res://"))
{
var lineError = TranslationServer.Translate("line_errors_must_start_with_res");
if (lineError == null)
{
return;
}
_roomTemplateTipsLabel.Text = string.Format(lineError, lastLine);
return;
}
var exists = FileAccess.FileExists(lastLine);
if (!exists)
{
var lineError = TranslationServer.Translate("error_specifying_room_template_line");
if (lineError == null)
{
return;
}
_roomTemplateTipsLabel.Text = string.Format(lineError, lastLine);
return;
}
_roomTemplateTipsLabel.Text = string.Empty;
}
};
}
if (_graphEdit != null) if (_graphEdit != null)
{ {
_graphEdit.ConnectionRequest += (fromNode, fromPort, toNode, toPort) => _graphEdit.ConnectionRequest += (fromNode, fromPort, toNode, toPort) =>
@ -155,6 +229,76 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate
} }
}; };
} }
if (_displayCurrentPageData != null)
{
_displayCurrentPageData.Pressed += () =>
{
if (_graphEdit == null)
{
return;
}
Array<Dictionary> connectionList = _graphEdit.GetConnectionList();
var levelGraphEditorSaveData = new LevelGraphEditorSaveData();
var connectionDataList = new List<ConnectionData>();
levelGraphEditorSaveData.ConnectionData = connectionDataList;
if (connectionList.Count <= 0) return;
foreach (var dictionary in connectionList)
{
if (dictionary == null)
{
continue;
}
var keys = dictionary.Keys;
if (keys.Count == 0)
{
continue;
}
var connectionData = new ConnectionData();
foreach (var variant in keys)
{
var typeStr = variant.ToString();
switch (typeStr)
{
case "from_node":
// LogCat.Log("查找"+dictionary[variant].Obj);
// var obj = dictionary[variant].Obj;
// if (obj == null)
// {
// LogCat.Log("obj is null");
// continue;
// }
//
// LogCat.Log("obj is " + obj.ToString());
// var roomNode = dictionary[variant]. as RoomNode;
// LogCat.Log("roomNode is" + (roomNode == null));
// var roomNode = dictionary[variant].As<RoomNode>();
// LogCat.Log("空的?" + (roomNode == null)+"类为"+dictionary[variant].ToString()+"路径为"+dictionary[variant].AsNodePath()+"对于C#"+dictionary[variant].Obj );
// connectionData.From = dictionary[variant].Obj as RoomNode;
// LogCat.Log("类型" + dictionary[variant]);
break;
case "from_port":
connectionData.FromPort = dictionary[variant].AsInt32();
break;
case "to_node":
connectionData.To = dictionary[variant].Obj as IRoomNodeData;
break;
case "to_port":
connectionData.ToPort = dictionary[variant].AsInt32();
break;
}
LogCat.Log(variant.ToString());
connectionDataList.Add(connectionData);
}
}
LogCat.Log(JsonSerialization.Serialize(connectionDataList));
};
}
} }

View File

@ -26,6 +26,17 @@ public static class JsonSerialization
return await JsonSerializer.DeserializeAsync<T>(openStream, _options); return await JsonSerializer.DeserializeAsync<T>(openStream, _options);
} }
/// <summary>
/// <para>Serialize the object to Json</para>
/// <para>将对象序列化为Json</para>
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string Serialize(object obj)
{
return JsonSerializer.Serialize(obj, _options);
}
public static async Task<T?> ReadJsonFileToObj<T>(Stream openStream) public static async Task<T?> ReadJsonFileToObj<T>(Stream openStream)
{ {
return await JsonSerializer.DeserializeAsync<T>(openStream, _options); return await JsonSerializer.DeserializeAsync<T>(openStream, _options);

17
scripts/utils/StrUtils.cs Normal file
View File

@ -0,0 +1,17 @@
namespace ColdMint.scripts.utils;
public class StrUtils
{
/// <summary>
/// <para>Gets the last line of a string</para>
/// <para>获取某个字符串的最后一行</para>
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetLastLine(string str)
{
var index = str.LastIndexOf('\n');
return index == -1 ? str : str[(index + 1)..];
}
}