2026-02-23 01:14:16 +08:00

372 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: 白哉
* @Description:
* @Date: 2026年01月26日 星期一 16:01:27
* @Modify:
*/
using System.Collections.Generic;
using Logic.Multilingual;
using MemoryPack;
using UnityEngine.UI;
namespace TH1_Logic.Comic
{
[MemoryPackable]
public partial class ComicAsset
{
public List<ComicSheet> ComicSheets;
public List<ComicDialogLayoutParam> DialogLayoutParams;
[MemoryPackConstructor]
public ComicAsset()
{
ComicSheets = new List<ComicSheet>();
DialogLayoutParams = new List<ComicDialogLayoutParam>();
}
public void AddComicSheet(string name)
{
var sheet = new ComicSheet();
sheet.Name = name;
ComicSheets.Add(sheet);
}
public ComicSheet GetComicSheetByName(string name)
{
foreach (var sheet in ComicSheets)
{
if (sheet.Name == name) return sheet;
}
return null;
}
public ComicDialogLayoutParam GetDialogLayoutParamByImageName(string imageName)
{
if (DialogLayoutParams == null) DialogLayoutParams = new List<ComicDialogLayoutParam>();
foreach (var param in DialogLayoutParams)
{
if (param.ImageName == imageName) return param;
}
var defaultParam = new ComicDialogLayoutParam
{
ImageName = imageName,
Left = 20,
Right = 20,
Top = 20,
Bottom = 20,
};
DialogLayoutParams.Add(defaultParam);
return defaultParam;
}
// IL2CPP 保护确保反序列化后集合不为null
public void EnsureCollectionsInitialized()
{
if (ComicSheets == null) ComicSheets = new List<ComicSheet>();
if (DialogLayoutParams == null) DialogLayoutParams = new List<ComicDialogLayoutParam>();
foreach (var sheet in ComicSheets)
{
sheet?.EnsureCollectionsInitialized();
}
}
}
[MemoryPackable]
public partial class ComicSheet
{
public string Name;
public List<ComicData> ComicDatas;
[MemoryPackConstructor]
public ComicSheet()
{
ComicDatas = new List<ComicData>();
}
// IL2CPP 保护确保反序列化后集合不为null
public void EnsureCollectionsInitialized()
{
if (ComicDatas == null) ComicDatas = new List<ComicData>();
foreach (var data in ComicDatas)
{
data?.EnsureCollectionsInitialized();
}
}
public void AddComicData()
{
var data = new ComicData();
ComicDatas.Add(data);
}
// 将某个 ComicData 前移或者后移
public bool MoveComicData(ComicData data, int offset)
{
int currentIndex = ComicDatas.IndexOf(data);
if (currentIndex < 0) return false;
int newIndex = currentIndex + offset;
if (newIndex < 0 || newIndex >= ComicDatas.Count) return false;
ComicDatas.RemoveAt(currentIndex);
ComicDatas.Insert(newIndex, data);
return true;
}
}
[MemoryPackable]
public partial class ComicData
{
public ComicBackground Background;
public List<ComicSubItem> SubItems;
public List<ComicAction> Actions;
[MemoryPackIgnore]
public ComicMono Mono;
[MemoryPackIgnore] public ComicAction FadeInAction;
[MemoryPackIgnore] public ComicAction FadeOutAction;
[MemoryPackConstructor]
public ComicData()
{
Background = new ComicBackground();
SubItems = new List<ComicSubItem>();
Actions = new List<ComicAction>();
}
// IL2CPP 保护确保反序列化后集合不为null
public void EnsureCollectionsInitialized()
{
if (SubItems == null) SubItems = new List<ComicSubItem>();
if (Actions == null) Actions = new List<ComicAction>();
if (Background == null) Background = new ComicBackground();
}
public void SaveItemsMono()
{
foreach (var item in SubItems)
{
item.SaveItemMonoInfo();
}
}
public void ApplyToMono()
{
var backgroundImage = ComicManager.Instance.GetImage(Background.ImageName);
if (backgroundImage) Mono.BackgroundImage.sprite = backgroundImage;
}
public void AddSubItem()
{
var item = new ComicSubItem();
if (SubItems.Count == 0) item.Id = 1;
else item.Id = SubItems[^1].Id + 1;
SubItems.Add(item);
}
public void RemoveSubItem(ComicSubItem item)
{
for (int i = Actions.Count - 1; i >= 0; i--)
{
if (Actions[i].TargetId == item.Id) Actions.RemoveAt(i);
}
SubItems.Remove(item);
}
public void AddAction()
{
var action = new ComicAction();
Actions.Add(action);
}
public void RemoveAction(ComicAction action)
{
Actions.Remove(action);
}
public ComicSubItem GetSubItemById(int id)
{
foreach (var item in SubItems)
{
if (item.Id == id) return item;
}
return null;
}
public ComicAction GetActionById(uint id)
{
foreach (var action in Actions)
{
if (action.TargetId == id) return action;
}
return null;
}
public void InitFadeInAction(ComicActionType fadeInType)
{
FadeInAction = new ComicAction
{
ActionType = fadeInType,
TargetId = -1,
Index = 0,
Duration = 1f,
};
}
public void InitFadeOutAction(ComicActionType fadeOutType)
{
FadeOutAction = new ComicAction
{
ActionType = fadeOutType,
TargetId = -1,
Index = Actions.Count,
Duration = 1f,
};
}
}
// 漫画底图
[MemoryPackable]
public partial class ComicBackground
{
public string ImageName;
[MemoryPackConstructor]
public ComicBackground()
{
}
}
public enum ComicSubItemType
{
None = 0,
Dialog = 1,
Image = 2,
}
// 漫画子 Item
[MemoryPackable]
public partial class ComicSubItem
{
public int Id;
public ComicSubItemType ItemType;
public float PositionX;
public float PositionY;
public float Width;
public float Height;
public string ImageName;
public string Content;
public string Desc;
[MemoryPackIgnore]
public ComicItemMono Mono;
public float DialogWidth;
[MemoryPackConstructor]
public ComicSubItem()
{
Id = 0;
ItemType = ComicSubItemType.None;
PositionX = 0f;
PositionY = 0f;
Width = 100f;
Height = 100f;
DialogWidth = 500;
ImageName = string.Empty;
Content = string.Empty;
}
public void SaveItemMonoInfo()
{
PositionX = Mono.ItemRect.anchoredPosition.x;
PositionY = Mono.ItemRect.anchoredPosition.y;
Width = Mono.ItemRect.sizeDelta.x;
Height = Mono.ItemRect.sizeDelta.y;
if(ItemType == ComicSubItemType.Dialog)
{
var layout = Mono.ItemRect.GetComponent<VerticalLayoutGroup>();
if (layout)
{
var param = ComicManager.Instance.GetDialogLayoutParamByImageName(ImageName);
if (param != null)
{
if (layout.padding.left > 20) param.Left = layout.padding.left;
if (layout.padding.right > 20) param.Right = layout.padding.right;
if (layout.padding.top > 20) param.Top = layout.padding.top;
if (layout.padding.bottom > 20) param.Bottom = layout.padding.bottom;
}
}
}
}
public void ApplyToItemMono()
{
if (ItemType == ComicSubItemType.Image)
{
var image = ComicManager.Instance.GetImage(ImageName);
if (image) Mono.ItemImage.sprite = image;
var pos = new UnityEngine.Vector2(PositionX, PositionY);
Mono.ItemRect.anchoredPosition = pos;
Mono.ItemRect.sizeDelta = new UnityEngine.Vector2(Width, Height);
}
if (ItemType == ComicSubItemType.Dialog)
{
if (DialogWidth == 0) DialogWidth = 500;
var layout = Mono.TextMono.GetComponent<LayoutElement>();
if (layout) layout.preferredWidth = DialogWidth;
var verticalLayout = Mono.ItemRect.GetComponent<VerticalLayoutGroup>();
var dialogLayoutParam = ComicManager.Instance.GetDialogLayoutParamByImageName(ImageName);
if (verticalLayout && dialogLayoutParam != null)
{
verticalLayout.padding.left = dialogLayoutParam.Left;
verticalLayout.padding.right = dialogLayoutParam.Right;
verticalLayout.padding.top = dialogLayoutParam.Top;
verticalLayout.padding.bottom = dialogLayoutParam.Bottom;
}
var dialogImage = ComicManager.Instance.GetImage(ImageName);
if (dialogImage)
{
Mono.DialogImage.sprite = dialogImage;
Mono.DialogImage.type = UnityEngine.UI.Image.Type.Sliced;
}
MultilingualManager.Instance.SetUIText(Mono.TextMono, Content);
// 设置文字下边距为20px
Mono.TextMono.margin = new UnityEngine.Vector4(0, 0, 0, 20);
var pos = new UnityEngine.Vector2(PositionX, PositionY);
Mono.ItemRect.anchoredPosition = pos;
Mono.ItemRect.sizeDelta = new UnityEngine.Vector2(Width, Height);
}
}
}
// 对话框参数
[MemoryPackable]
public partial class ComicDialogLayoutParam
{
public string ImageName;
public int Left;
public int Right;
public int Top;
public int Bottom;
[MemoryPackConstructor]
public ComicDialogLayoutParam()
{
}
}
}