ASSISTANT
为了满足您的需求,我们可以扩展 `LightConeEditor` 界面,为基础生命值、攻击力、防御力,以及每级和每次突破增加的生命值、攻击力、防御力添加相关的输入框。以下是更新后的完整代码,用于实现这些功能:
```python
import os
import json
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QTextEdit, QPushButton, QFileDialog
)
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, 600)
self.initUI()
def initUI(self):
layout = QVBoxLayout(self)
# Light Cone ID input
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)
# Skill name input
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)
# Basic attributes inputs
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)
# Per level increase attributes inputs
self.per_level_increase = {}
per_level_layout = QHBoxLayout()
for attr in ["生命值", "攻击力", "防御力"]:
label = QLabel(f"每级增加{attr}:")
input_field = QLineEdit()
self.per_level_increase[attr] = input_field
per_level_layout.addWidget(label)
per_level_layout.addWidget(input_field)
layout.addLayout(per_level_layout)
# Per breakthrough increase attributes inputs
self.per_breakthrough_increase = {}
breakthrough_layout = QHBoxLayout()
for attr in ["生命值", "攻击力", "防御力"]:
label = QLabel(f"每次突破增加{attr}:")
input_field = QLineEdit()
self.per_breakthrough_increase[attr] = input_field
breakthrough_layout.addWidget(label)
breakthrough_layout.addWidget(input_field)
layout.addLayout(breakthrough_layout)
# Skill descriptions for levels 1-5 with text highlighting capabilities
self.skill_descriptions = {}
for level in range(1, 6):
level_layout = QHBoxLayout()
level_label = QLabel(f"等级 {level} 技能描述:")
description_input = QTextEdit()
self.skill_descriptions[level] = description_input
description_input.textChanged.connect(lambda _=None, level=level: self.highlight_text(level))
level_layout.addWidget(level_label)
level_layout.addWidget(description_input)
layout.addLayout(level_layout)
# Description of the light cone
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)
# Materials for breakthrough
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)
# Save, load and modify buttons
button_layout = QHBoxLayout()
save_button = QPushButton("保存")
load_button = QPushButton("加载")
modify_button = QPushButton("修改")
save_button.clicked.connect(self.save_lightcone)
load_button.clicked.connect(self.load_lightcone)
modify_button.clicked.connect(self.modify_lightcone)
button_layout.addWidget(save_button)
button_layout.addWidget(load_button)
button_layout.addWidget(modify_button)
layout.addLayout(button_layout)
def highlight_text(self, level):
description_input = self.skill_descriptions[level]
cursor = description_input.textCursor()
original_position = cursor.position() # 保存初始光标位置
text = description_input.toPlainText()
# Mark special text: [[highlighted]]
start_marker = '[['
end_marker = ']]'
# Clear previous formatting
cursor.movePosition(QTextCursor.Start)
plain_format = QTextCharFormat()
cursor.setCharFormat(plain_format)
# Block signals to prevent recursion
description_input.blockSignals(True)
# Reset text to include markers temporarily
cursor.select(QTextCursor.Document)
cursor.removeSelectedText()
cursor.insertText(text)
# Finding and marking text
start = text.find(start_marker)
while start != -1:
end = text.find(end_marker, start)
if end != -1:
actual_start = start + len(start_marker)
actual_end = end
# Format the text between the markers
cursor.setPosition(actual_start)
cursor.setPosition(actual_end, QTextCursor.KeepAnchor)
color_format = QTextCharFormat()
color_format.setForeground(QColor("orange"))
cursor.setCharFormat(color_format)
# Update positions to continue searching
start = text.find(start_marker, end + len(end_marker))
else:
break
# Unblock signals
description_input.blockSignals(False)
# 恢复原始光标位置
if original_position <= len(text):
cursor.setPosition(original_position)
else:
cursor.setPosition(len(text))
description_input.setTextCursor(cursor)
def save_lightcone(self):
lightcone_data = {
"skill_name": self.skill_name_input.text(),
"skill_descriptions": {level: self.skill_descriptions[level].toPlainText() for level in range(1, 6)},
"light_cone_description": self.light_cone_desc_input.toPlainText(),
"general_materials": {level: self.general_materials[level].text() for level in range(1, 4)},
"path_materials": {level: self.path_materials[level].text() for level in range(1, 4)},
"basic_attributes": {attr: self.basic_attributes[attr].text() for attr in ["生命值", "攻击力", "防御力"]},
"per_level_increase": {attr: self.per_level_increase[attr].text() for attr in ["生命值", "攻击力", "防御力"]},
"per_breakthrough_increase": {attr: self.per_breakthrough_increase[attr].text() for attr in ["生命值", "攻击力", "防御力"]}
}
lightcone_id = self.id_input.text().strip().replace(" ", "_").lower() # Use light cone ID as filename
if not lightcone_id:
return # Handle empty ID
file_path = os.path.join("config", "lightcone", f"{lightcone_id}.json")
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(lightcone_data, file, ensure_ascii=False, indent=4)
def load_lightcone(self):
options = QFileDialog.Options()
file_path, _ = QFileDialog.getOpenFileName(self, "Select Light Cone JSON", os.path.join("config", "lightcone"), "JSON Files (*.json)", options=options)
if file_path:
with open(file_path, 'r', encoding='utf-8') as file:
lightcone_data = json.load(file)
self.id_input.setText(os.path.basename(file_path).replace(".json", "")) # Extract ID from filename
self.skill_name_input.setText(lightcone_data.get("skill_name", ""))
for level in range(1, 6):
self.skill_descriptions[level].setPlainText(lightcone_data.get("skill_descriptions", {}).get(level, ""))
# Apply highlight after setting text
self.highlight_text(level)
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(level, ""))
self.path_materials[level].setText(lightcone_data.get("path_materials", {}).get(level, ""))
# Load additional attributes
for attr in ["生命值", "攻击力", "防御力"]:
self.basic_attributes[attr].setText(lightcone_data.get("basic_attributes", {}).get(attr, ""))
self.per_level_increase[attr].setText(lightcone_data.get("per_level_increase", {}).get(attr, ""))
self.per_breakthrough_increase[attr].setText(lightcone_data.get("per_breakthrough_increase", {}).get(attr, ""))
def modify_lightcone(self):
# Placeholder: Implement modification logic if desired
pass
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
editor = LightConeEditor()
editor.show()
sys.exit(app.exec_())
```
### 说明:
1. **新增输入框**:
- 添加了三个新的类别的属性输入框:基础属性、每级增加的属性、每次突破增加的属性。这些属性包括生命值、攻击力、防御力。
2. **数据保存与加载**:
- 在 `save_lightcone` 和 `load_lightcone` 方法中包含了对新属性的处理,确保这些属性可以被存储到 JSON 文件中并能够从 JSON 文件正确读取。
这个界面将允许用户输入各种与光锥相关的详细信息,同时支持高亮功能和 JSON 数据的存储与加载。您可以根据需要微调或扩展这些功能。