import os
import json
import random
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QTextEdit, QPushButton, QFileDialog, QComboBox, QMessageBox
)
from PyQt5.QtGui import QTextCursor, QTextCharFormat, QColor
class LightConeEditor(QWidget):
def init(self):
super().init()
self.setWindowTitle("Light Cone Editor")
self.setGeometry(300, 300, 800, 800)
self.initUI()
def initUI(self):
layout = QVBoxLayout(self)
# Available Paths
self.available_paths = [
"The Hunt", "Destruction", "Nihility", "Harmony",
"Preservation", "Abundance", "Erudition", "Remembrance"
]
# 光锥 ID 输入
id_layout = QHBoxLayout()
id_label = QLabel("光锥ID:")
self.id_input = QLineEdit()
id_layout.addWidget(id_label)
id_layout.addWidget(self.id_input)
layout.addLayout(id_layout)
# 光锥名称输入
name_layout = QHBoxLayout()
name_label = QLabel("光锥名称:")
self.name_input = QLineEdit()
name_layout.addWidget(name_label)
name_layout.addWidget(self.name_input)
layout.addLayout(name_layout)
# 光锥命途选择
path_layout = QHBoxLayout()
path_label = QLabel("光锥命途:")
self.path_input = QComboBox()
self.path_input.addItems(self.available_paths)
path_layout.addWidget(path_label)
path_layout.addWidget(self.path_input)
layout.addLayout(path_layout)
# 星级输入
star_level_layout = QHBoxLayout()
star_level_label = QLabel("光锥星级:")
self.star_level_input = QLineEdit()
star_level_layout.addWidget(star_level_label)
star_level_layout.addWidget(self.star_level_input)
layout.addLayout(star_level_layout)
# 技能名称输入
skill_name_layout = QHBoxLayout()
skill_name_label = QLabel("技能名称:")
self.skill_name_input = QLineEdit()
skill_name_layout.addWidget(skill_name_label)
skill_name_layout.addWidget(self.skill_name_input)
layout.addLayout(skill_name_layout)
# 技能描述输入
combined_skill_layout = QHBoxLayout()
combined_skill_label = QLabel("技能描述(带标记):")
self.combined_skill_input = QTextEdit()
combined_skill_layout.addWidget(combined_skill_label)
combined_skill_layout.addWidget(self.combined_skill_input)
layout.addLayout(combined_skill_layout)
# 连接文本更改信号以实现高亮显示
self.combined_skill_input.textChanged.connect(self.highlight_combined_skill_text)
# 光锥描述
light_cone_desc_layout = QHBoxLayout()
light_cone_desc_label = QLabel("光锥描述:")
self.light_cone_desc_input = QTextEdit()
light_cone_desc_layout.addWidget(light_cone_desc_label)
light_cone_desc_layout.addWidget(self.light_cone_desc_input)
layout.addLayout(light_cone_desc_layout)
# 属性输入: 基础
self.basic_attributes = {}
basic_attrs_layout = QHBoxLayout()
for attr in ["生命值", "攻击力", "防御力"]:
label = QLabel(f"基础{attr}:")
input_field = QLineEdit()
self.basic_attributes[attr] = input_field
basic_attrs_layout.addWidget(label)
basic_attrs_layout.addWidget(input_field)
layout.addLayout(basic_attrs_layout)
# 突破材料
self.general_materials = {}
self.path_materials = {}
for level in range(1, 4):
general_mat_layout = QHBoxLayout()
general_mat_label = QLabel(f"等级 {level} 通用突破材料 ID:")
general_mat_input = QLineEdit()
self.general_materials[level] = general_mat_input
general_mat_layout.addWidget(general_mat_label)
general_mat_layout.addWidget(general_mat_input)
layout.addLayout(general_mat_layout)
path_mat_layout = QHBoxLayout()
path_mat_label = QLabel(f"等级 {level} 命途突破材料 ID:")
path_mat_input = QLineEdit()
self.path_materials[level] = path_mat_input
path_mat_layout.addWidget(path_mat_label)
path_mat_layout.addWidget(path_mat_input)
layout.addLayout(path_mat_layout)
# 保存和加载按钮
button_layout = QHBoxLayout()
save_button = QPushButton("保存")
load_button = QPushButton("加载")
save_button.clicked.connect(self.save_lightcone)
load_button.clicked.connect(self.load_lightcone)
button_layout.addWidget(save_button)
button_layout.addWidget(load_button)
layout.addLayout(button_layout)
def highlight_combined_skill_text(self):
try:
# 获取文本并重置格式
text = self.combined_skill_input.toPlainText()
cursor = self.combined_skill_input.textCursor()
cursor.select(QTextCursor.Document)
self.combined_skill_input.blockSignals(True)
cursor.setCharFormat(QTextCharFormat()) # 重置所有文本格式
self.combined_skill_input.blockSignals(False)
highlight_format = QTextCharFormat()
highlight_format.setForeground(QColor("orange"))
# 查找所有符合条件的部分进行高亮
start_marker = '[['
end_marker = ']]'
start = 0
while start != -1:
start = text.find(start_marker, start)
if start != -1:
end = text.find(end_marker, start + len(start_marker))
if end != -1:
content = text[start + len(start_marker):end]
elements = content.split(',')
if len(elements) == 5:
self.combined_skill_input.blockSignals(True)
cursor.setPosition(start + len(start_marker), QTextCursor.MoveAnchor)
cursor.setPosition(end, QTextCursor.KeepAnchor)
cursor.setCharFormat(highlight_format)
self.combined_skill_input.blockSignals(False)
start = end + len(end_marker)
else:
start = -1
except Exception as e:
print(f"An error occurred while highlighting: {e}")
def save_lightcone(self):
lightcone_data = {
"light_cone_id": self.id_input.text(),
"light_cone_name": self.name_input.text(),
"light_cone_path": self.path_input.currentText(),
"star_level": self.star_level_input.text(),
"skill_name": self.skill_name_input.text(),
"combined_skill_description": self.combined_skill_input.toPlainText(),
"light_cone_description": self.light_cone_desc_input.toPlainText(),
"general_materials": {str(level): self.general_materials[level].text() for level in range(1, 4)},
"path_materials": {str(level): self.path_materials[level].text() for level in range(1, 4)},
"basic_attributes": {attr: self.basic_attributes[attr].text() for attr in ["生命值", "攻击力", "防御力"]}
}
lightcone_id = self.id_input.text().strip().replace(" ", "_").lower()
if not lightcone_id:
QMessageBox.warning(self, "错误", "请输入光锥ID!")
return
try:
file_path = os.path.join("config", "lightcone", f"{lightcone_id}.json")
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(lightcone_data, file, ensure_ascii=False, indent=4)
QMessageBox.information(self, "保存成功", f"保存成功到 {file_path}")
except Exception as e:
QMessageBox.critical(self, "保存错误", f"保存文件时出错: {e}")
def load_lightcone(self):
options = QFileDialog.Options()
file_path, _ = QFileDialog.getOpenFileName(self, "选择光锥 JSON 文件", os.path.join("config", "lightcone"), "JSON 文件 (*.json)", options=options)
if file_path:
try:
with open(file_path, 'r', encoding='utf-8') as file:
lightcone_data = json.load(file)
self.id_input.setText(lightcone_data.get("light_cone_id", ""))
self.name_input.setText(lightcone_data.get("light_cone_name", ""))
loaded_path = lightcone_data.get("light_cone_path", "")
if loaded_path in self.available_paths:
self.path_input.setCurrentText(loaded_path)
else:
self.path_input.setCurrentText(random.choice(self.available_paths))
self.star_level_input.setText(lightcone_data.get("star_level", ""))
self.skill_name_input.setText(lightcone_data.get("skill_name", ""))
self.combined_skill_input.setPlainText(lightcone_data.get("combined_skill_description", ""))
self.highlight_combined_skill_text()
self.light_cone_desc_input.setPlainText(lightcone_data.get("light_cone_description", ""))
for level in range(1, 4):
self.general_materials[level].setText(lightcone_data.get("general_materials", {}).get(str(level), ""))
self.path_materials[level].setText(lightcone_data.get("path_materials", {}).get(str(level), ""))
for attr in ["生命值", "攻击力", "防御力"]:
self.basic_attributes[attr].setText(lightcone_data.get("basic_attributes", {}).get(attr, ""))
except Exception as e:
QMessageBox.critical(self, "加载错误", f"无法加载文件: {file_path}\n错误详情: {e}")
if name == "main":
import sys
app = QApplication(sys.argv)
editor = LightConeEditor()
editor.show()
sys.exit(app.exec_())