Commit 4c71c8a4 authored by hex's avatar hex

- Optimized DeckManager

parent 020eee2a
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using YGOSharp.OCGWrapper.Enums;
......@@ -189,7 +190,7 @@ public class DeckManager : ServantWithCardDescription
Program.camera_main_2d.ScreenToWorldPoint(
new Vector3(Screen.width + 600, Screen.height / 2, 600)
),
1.2f
0.6f
);
refreshDetail();
}
......@@ -205,7 +206,7 @@ public class DeckManager : ServantWithCardDescription
Program.camera_main_2d.ScreenToWorldPoint(
new Vector3(Screen.width - MAIN_PANEL_MARGIN_RIGHT, Screen.height / 2, 0)
),
1.2f
0.6f
);
refreshDetail();
}
......@@ -298,51 +299,54 @@ public class DeckManager : ServantWithCardDescription
if (deck.IMain.Count <= 60 && deck.IExtra.Count <= 15 && deck.ISide.Count <= 15)
{
string deckInUse = Config.Get("deckInUse", "miaowu");
StringBuilder sb = new StringBuilder(2048); // 预估最大容量,避免多次扩容
if (canSave)
{
ArrangeObjectDeck();
FromObjectDeckToCodedDeck(true);
string value = "#created by ygopro2\r\n#main\r\n";
sb.AppendLine("#created by ygopro2");
sb.AppendLine("#main");
for (int i = 0; i < deck.Main.Count; i++)
{
value += deck.Main[i].ToString() + "\r\n";
sb.AppendLine(deck.Main[i].ToString());
}
value += "#extra\r\n";
sb.AppendLine("#extra");
for (int i = 0; i < deck.Extra.Count; i++)
{
value += deck.Extra[i].ToString() + "\r\n";
sb.AppendLine(deck.Extra[i].ToString());
}
value += "!side\r\n";
sb.AppendLine("!side");
for (int i = 0; i < deck.Side.Count; i++)
{
value += deck.Side[i].ToString() + "\r\n";
sb.AppendLine(deck.Side[i].ToString());
}
System.IO.File.WriteAllText(
"deck/" + deckInUse + ".ydk",
value,
sb.ToString(),
System.Text.Encoding.UTF8
);
}
else
{
string value = "#created by ygopro2\r\n#main\r\n";
sb.AppendLine("#created by ygopro2");
sb.AppendLine("#main");
for (int i = 0; i < deck.Deck_O.Main.Count; i++)
{
value += deck.Deck_O.Main[i].ToString() + "\r\n";
sb.AppendLine(deck.Deck_O.Main[i].ToString());
}
value += "#extra\r\n";
sb.AppendLine("#extra");
for (int i = 0; i < deck.Deck_O.Extra.Count; i++)
{
value += deck.Deck_O.Extra[i].ToString() + "\r\n";
sb.AppendLine(deck.Deck_O.Extra[i].ToString());
}
value += "!side\r\n";
sb.AppendLine("!side");
for (int i = 0; i < deck.Deck_O.Side.Count; i++)
{
value += deck.Deck_O.Side[i].ToString() + "\r\n";
sb.AppendLine(deck.Deck_O.Side[i].ToString());
}
System.IO.File.WriteAllText(
"deck/" + deckInUse + ".ydk",
value,
sb.ToString(),
System.Text.Encoding.UTF8
);
}
......@@ -1292,7 +1296,7 @@ public class DeckManager : ServantWithCardDescription
return UIPopupList_pack.value;
}
// [新增] 封装的添加卡片副本的逻辑,用于复用
// 封装的添加卡片副本的逻辑,用于复用
private void AddCopyToDeck(MonoCardInDeckManager sourceCard)
{
// 安全检查:如果不在编辑模式,或卡片已满编,则不执行任何操作
......@@ -1311,6 +1315,7 @@ public class DeckManager : ServantWithCardDescription
deckDirty = true;
ArrangeObjectDeck(true); // 重新整理数据
ShowObjectDeck(); // 重新排列场上的卡片
UpdateDeckCounts(); // 事件驱动更新UI
}
#endregion
......@@ -1512,9 +1517,23 @@ public class DeckManager : ServantWithCardDescription
cameraDistance * Mathf.Sin(3.1415926f / 180f * cameraAngle),
-cameraDistance * Mathf.Cos(3.1415926f / 180f * cameraAngle)
);
if (Program.TimePassed() - lastRefreshTime > 80)
if (Program.InputEnterDown)
{
if (condition == Condition.editDeck)
{
onClickSearch();
}
}
if (Input.GetKeyDown(KeyCode.Tab) || Input.GetMouseButtonDown(2))
{
onClickDetail();
}
}
// 独立的卡组数量更新方法,在卡片增删时调用
public void UpdateDeckCounts()
{
lastRefreshTime = Program.TimePassed();
FromObjectDeckToCodedDeck();
main_unmber.set_number(deck.Main.Count, 3);
side_number.set_number(deck.Side.Count, 4);
......@@ -1535,18 +1554,6 @@ public class DeckManager : ServantWithCardDescription
s_number.set_number(s, 2);
t_unmber.set_number(t, 5);
}
if (Program.InputEnterDown)
{
if (condition == Condition.editDeck)
{
onClickSearch();
}
}
if (Input.GetKeyDown(KeyCode.Tab) || Input.GetMouseButtonDown(2))
{
onClickDetail();
}
}
private void camrem()
{
......@@ -1593,7 +1600,7 @@ public class DeckManager : ServantWithCardDescription
cardPicLoader cardInSearchResult = gameObject.GetComponent<cardPicLoader>();
if (cardInDeck != null && !cardInDeck.dying)
{
// [修改] 初始化长按/拖拽状态
// 初始化长按/拖拽状态
cardForLongPress = cardInDeck;
pressStartTime = Time.time;
isContinuouslyAdding = false; // 每次按下都重置持续添加状态
......@@ -1614,6 +1621,7 @@ public class DeckManager : ServantWithCardDescription
deck.IMain.Add(card);
cardInDragging = card;
card.beginDrag();
UpdateDeckCounts(); // 事件驱动更新UI
}
}
}
......@@ -1639,6 +1647,8 @@ public class DeckManager : ServantWithCardDescription
// 第四步:更新卡片在场景中的位置与角度(含Tween)
ShowObjectDeck();
UpdateDeckCounts(); // 事件驱动更新UI
// 第五步:清除引用
cardInDragging = null;
}
......@@ -1648,24 +1658,30 @@ public class DeckManager : ServantWithCardDescription
public override void ES_mouseUpRight()
{
if (Program.pointedGameObject != null)
{
if (Program.pointedGameObject == null)
return;
bool deckChanged = false;
if (condition == Condition.editDeck)
{
MonoCardInDeckManager cardInDeck =
Program.pointedGameObject.GetComponent<MonoCardInDeckManager>();
if (cardInDeck != null)
{
cardInDeck.killIt();
ArrangeObjectDeck(true);
ShowObjectDeck();
cardInDeck.killIt(); // 标记为待删除
// 从数据结构中移除
deck.IMain.Remove(cardInDeck);
deck.IExtra.Remove(cardInDeck);
deck.ISide.Remove(cardInDeck);
deckChanged = true;
}
cardPicLoader cardInSearchResult =
Program.pointedGameObject.GetComponent<cardPicLoader>();
if (cardInSearchResult != null)
{
CreateMonoCard(cardInSearchResult.data);
ShowObjectDeck();
deckChanged = true;
}
}
else
......@@ -1674,36 +1690,28 @@ public class DeckManager : ServantWithCardDescription
Program.pointedGameObject.GetComponent<MonoCardInDeckManager>();
if (cardInDeck != null)
{
bool isSide = false;
for (int i = 0; i < deck.ISide.Count; i++)
{
if (cardInDeck == deck.ISide[i])
{
isSide = true;
}
}
if (isSide)
if (deck.ISide.Contains(cardInDeck))
{
deck.ISide.Remove(cardInDeck);
if (cardInDeck.cardData.IsExtraCard())
{
deck.IExtra.Add(cardInDeck);
deck.ISide.Remove(cardInDeck);
}
else
{
deck.IMain.Add(cardInDeck);
deck.ISide.Remove(cardInDeck);
}
}
else
{
deck.ISide.Add(cardInDeck);
deck.IMain.Remove(cardInDeck);
deck.IExtra.Remove(cardInDeck);
deck.ISide.Add(cardInDeck);
}
ShowObjectDeck();
deckChanged = true;
}
}
if (deckChanged)
{
ArrangeObjectDeck(true);
ShowObjectDeck();
UpdateDeckCounts(); // 事件驱动更新UI
}
}
......@@ -1726,6 +1734,7 @@ public class DeckManager : ServantWithCardDescription
deck.Main.Add(card.cardData.Id);
}
deckDirty = true;
UpdateDeckCounts(); // [优化] 事件驱动更新UI
}
}
......
......@@ -35,13 +35,13 @@ public class selectReplay : WindowServantSP
"onValue",
InterString.Get("您确定要删除所有未命名的录像?"),
new messageSystemValue { hint = "yes", value = "yes" },
new messageSystemValue { hint = "no", value = "no" });
new messageSystemValue { hint = "no", value = "no" }
);
}
private void setSortLable()
{
if (Config.Get(sort,"1") == "1")
if (Config.Get(sort, "1") == "1")
{
UIHelper.trySetLableText(gameObject, "sort_", InterString.Get("时间排序"));
}
......@@ -76,12 +76,12 @@ public class selectReplay : WindowServantSP
{
return;
}
KF_replay(superScrollView.selectedString,true);
KF_replay(superScrollView.selectedString, true);
}
private void onSort()
{
if (Config.Get(sort,"1") == "1")
if (Config.Get(sort, "1") == "1")
{
Config.Set(sort, "0");
}
......@@ -105,7 +105,11 @@ public class selectReplay : WindowServantSP
if (name.Length > 4 && name.Substring(name.Length - 4, 4) == ".yrp")
{
opYRP = true;
RMSshow_input("onRename", InterString.Get("请输入重命名后的录像名"), name.Substring(0, name.Length - 4));
RMSshow_input(
"onRename",
InterString.Get("请输入重命名后的录像名"),
name.Substring(0, name.Length - 4)
);
}
else
{
......@@ -122,10 +126,10 @@ public class selectReplay : WindowServantSP
}
RMSshow_yesOrNo(
"onDelete",
InterString.Get("删除[?],@n请确认。",
superScrollView.selectedString),
InterString.Get("删除[?],@n请确认。", superScrollView.selectedString),
new messageSystemValue { hint = "yes", value = "yes" },
new messageSystemValue { hint = "no", value = "no" });
new messageSystemValue { hint = "no", value = "no" }
);
}
byte[] getYRPbuffer(string path)
......@@ -146,7 +150,6 @@ public class selectReplay : WindowServantSP
break;
}
}
}
catch (Exception e)
{
......@@ -168,20 +171,26 @@ public class selectReplay : WindowServantSP
try
{
BinaryReader reader = new BinaryReader(new MemoryStream(buffer));
returnValue.ID= reader.ReadInt32();
returnValue.Version= reader.ReadInt32();
returnValue.Flag= reader.ReadInt32();
returnValue.Seed= reader.ReadUInt32();
returnValue.ID = reader.ReadInt32();
returnValue.Version = reader.ReadInt32();
returnValue.Flag = reader.ReadInt32();
returnValue.Seed = reader.ReadUInt32();
returnValue.DataSize = reader.ReadInt32();
returnValue.Hash = reader.ReadInt32();
returnValue.Props= reader.ReadBytes(8);
returnValue.Props = reader.ReadBytes(8);
byte[] raw = reader.ReadToEnd();
if ((returnValue.Flag & 0x1) > 0)
{
SevenZip.Compression.LZMA.Decoder lzma = new SevenZip.Compression.LZMA.Decoder();
lzma.SetDecoderProperties(returnValue.Props);
MemoryStream decompressed = new MemoryStream();
lzma.Code(new MemoryStream(raw), decompressed, raw.LongLength, returnValue.DataSize, null);
lzma.Code(
new MemoryStream(raw),
decompressed,
raw.LongLength,
returnValue.DataSize,
null
);
raw = decompressed.ToArray();
}
reader = new BinaryReader(new MemoryStream(raw));
......@@ -281,7 +290,8 @@ public class selectReplay : WindowServantSP
{
value += yrp.playerData[i].extra[i2].ToString() + "\r\n";
}
string name = "deck/" + superScrollView.selectedString + "_" + (i + 1).ToString() + ".ydk";
string name =
"deck/" + superScrollView.selectedString + "_" + (i + 1).ToString() + ".ydk";
File.WriteAllText(name, value);
RMSshow_none(InterString.Get("卡组入库:[?]", name));
}
......@@ -306,8 +316,16 @@ public class selectReplay : WindowServantSP
{
if (File.Exists("replay/" + superScrollView.selectedString + ".yrp3d"))
{
File.WriteAllBytes("replay/" + superScrollView.selectedString + ".yrp", getYRPbuffer("replay/" + superScrollView.selectedString + ".yrp3d"));
RMSshow_none(InterString.Get("录像入库:[?]", "replay/" + superScrollView.selectedString + ".yrp"));
File.WriteAllBytes(
"replay/" + superScrollView.selectedString + ".yrp",
getYRPbuffer("replay/" + superScrollView.selectedString + ".yrp3d")
);
RMSshow_none(
InterString.Get(
"录像入库:[?]",
"replay/" + superScrollView.selectedString + ".yrp"
)
);
printFile();
}
else
......@@ -330,12 +348,17 @@ public class selectReplay : WindowServantSP
{
if (opYRP)
{
System.IO.File.Move("replay/" + superScrollView.selectedString, "replay/" + result[0].value + ".yrp");
}else
System.IO.File.Move(
"replay/" + superScrollView.selectedString,
"replay/" + result[0].value + ".yrp"
);
}
else
{
System.IO.File.Move("replay/" + superScrollView.selectedString + ".yrp3d", "replay/" + result[0].value + ".yrp3d");
System.IO.File.Move(
"replay/" + superScrollView.selectedString + ".yrp3d",
"replay/" + result[0].value + ".yrp3d"
);
}
printFile();
RMSshow_none(InterString.Get("重命名成功。"));
......@@ -353,20 +376,24 @@ public class selectReplay : WindowServantSP
{
if (File.Exists("replay/" + superScrollView.selectedString + ".yrp3d"))
{
System.IO.File.Delete("replay/" + superScrollView.selectedString + ".yrp3d");
RMSshow_none(InterString.Get("[?]已经被删除。", superScrollView.selectedString));
System.IO.File.Delete(
"replay/" + superScrollView.selectedString + ".yrp3d"
);
RMSshow_none(
InterString.Get("[?]已经被删除。", superScrollView.selectedString)
);
printFile();
}
if (File.Exists("replay/" + superScrollView.selectedString))
{
System.IO.File.Delete("replay/" + superScrollView.selectedString);
RMSshow_none(InterString.Get("[?]已经被删除。", superScrollView.selectedString));
RMSshow_none(
InterString.Get("[?]已经被删除。", superScrollView.selectedString)
);
printFile();
}
}
catch (Exception)
{
}
catch (Exception) { }
}
}
if (hashCode == "onValue")
......@@ -388,9 +415,7 @@ public class selectReplay : WindowServantSP
{
File.Delete("replay/" + fileInfos[i].Name);
}
catch (Exception)
{
}
catch (Exception) { }
}
}
}
......@@ -403,6 +428,7 @@ public class selectReplay : WindowServantSP
}
string selectedTrace = "";
void onSelected()
{
if (selectedTrace == superScrollView.selectedString)
......@@ -426,11 +452,17 @@ public class selectReplay : WindowServantSP
{
if (god)
{
RMSshow_none(InterString.Get("您正在观看旧版的录像(上帝视角),不保证稳定性。"));
RMSshow_none(
InterString.Get("您正在观看旧版的录像(上帝视角),不保证稳定性。")
);
if (precy != null)
precy.dispose();
precy = new PrecyOcg();
var collections = TcpHelper.getPackages(precy.ygopro.getYRP3dBuffer(getYRP(getYRPbuffer("replay/" + name + ".yrp3d"))));
var collections = TcpHelper.getPackages(
precy.ygopro.getYRP3dBuffer(
getYRP(getYRPbuffer("replay/" + name + ".yrp3d"))
)
);
pushCollection(collections);
}
else
......@@ -441,15 +473,19 @@ public class selectReplay : WindowServantSP
}
else
{
if (name.Length>4&&name.Substring(name.Length - 4, 4) == ".yrp")
if (name.Length > 4 && name.Substring(name.Length - 4, 4) == ".yrp")
{
if (File.Exists("replay/" + name))
{
RMSshow_none(InterString.Get("您正在观看旧版的录像(上帝视角),不保证稳定性。"));
RMSshow_none(
InterString.Get("您正在观看旧版的录像(上帝视角),不保证稳定性。")
);
if (precy != null)
precy.dispose();
precy = new PrecyOcg();
var collections = TcpHelper.getPackages(precy.ygopro.getYRP3dBuffer(getYRP(File.ReadAllBytes("replay/" + name))));
var collections = TcpHelper.getPackages(
precy.ygopro.getYRP3dBuffer(getYRP(File.ReadAllBytes("replay/" + name)))
);
pushCollection(collections);
}
}
......@@ -502,11 +538,19 @@ public class selectReplay : WindowServantSP
{
if (fileInfos[i].Name.Length > 6)
{
if (fileInfos[i].Name.Length > 6 && fileInfos[i].Name.Substring(fileInfos[i].Name.Length - 6, 6) == ".yrp3d")
if (
fileInfos[i].Name.Length > 6
&& fileInfos[i].Name.Substring(fileInfos[i].Name.Length - 6, 6) == ".yrp3d"
)
{
superScrollView.add(fileInfos[i].Name.Substring(0, fileInfos[i].Name.Length - 6));
superScrollView.add(
fileInfos[i].Name.Substring(0, fileInfos[i].Name.Length - 6)
);
}
if (fileInfos[i].Name.Length > 4 && fileInfos[i].Name.Substring(fileInfos[i].Name.Length - 4, 4) == ".yrp")
if (
fileInfos[i].Name.Length > 4
&& fileInfos[i].Name.Substring(fileInfos[i].Name.Length - 4, 4) == ".yrp"
)
{
superScrollView.add(fileInfos[i].Name);
}
......@@ -518,5 +562,4 @@ public class selectReplay : WindowServantSP
{
Program.I().shiftToServant(Program.I().menu);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment