You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.0 KiB
GDScript

@tool
extends EditorPlugin
var menu_parent: Node
var skill_popup: PopupMenu
var short_cut_popup: PopupMenu
var skill_cfg_list: Array[SkillCfg]
var short_cut_path_list: Array[String]
var selected_skill_file: String
func _enter_tree():
var root: Node = EditorInterface.get_editor_main_screen()
while root.get_parent() != null:
root = root.get_parent()
menu_parent = root.get_child(0).get_child(4).get_child(0).get_child(0).get_child(0)
if not menu_parent:
print("menu_parent is null")
return
## 技能配置
skill_popup = PopupMenu.new()
skill_popup.name = "技能配置"
menu_parent.add_child(skill_popup)
skill_popup.about_to_popup.connect(skill_popup_about_to_popup)
skill_popup.index_pressed.connect(skill_popup_index_pressed)
## 快捷方式
short_cut_popup = PopupMenu.new()
short_cut_popup.name = "快捷方式"
menu_parent.add_child(short_cut_popup)
short_cut_popup.index_pressed.connect(short_cut_index_pressed)
add_short_cut("配置-角色", "res://config/character")
add_short_cut("配置-武器技能", "res://config/skill_player_weapon")
add_short_cut("资产-技能特效", "res://scene/effect/particle")
add_short_cut("资产-AI", "res://scene/ai")
add_short_cut("资产-场景活动物件", "res://scene/level_active")
add_short_cut("编辑场景-关卡", "res://scene/level/level.tscn")
add_short_cut("编辑场景-角色", "res://scene/character/character.tscn")
add_short_cut("打印meshlib-main", "res://resource/mesh_library/mesh_library_main.tres")
add_short_cut("打印meshlib-sub", "res://resource/mesh_library/mesh_library_sub.tres")
add_short_cut("打印meshlib-deco1", "res://resource/mesh_library/mesh_library_deco1.tres")
add_short_cut("打印meshlib-deco2", "res://resource/mesh_library/mesh_library_deco2.tres")
add_short_cut("打印meshlib-chara", "res://resource/mesh_library/mesh_library_chara.tres")
add_short_cut("功能-修复meshlib材质", "func_fix_mesh")
func add_short_cut(target_name: String, path: String):
short_cut_popup.add_item(target_name)
short_cut_path_list.append(path)
func skill_popup_about_to_popup():
skill_cfg_list.clear()
skill_popup.set_item_count(0)
skill_cfg_list = Util.get_all_skill()
for skill_cfg in skill_cfg_list:
skill_popup.add_item(skill_cfg.get_res_name())
func skill_popup_index_pressed(index: int):
EditorInterface.select_file(skill_cfg_list[index].resource_path)
func short_cut_index_pressed(index: int):
var short_cut_path: String = short_cut_path_list[index]
if short_cut_path.ends_with(".tscn"):
EditorInterface.select_file(short_cut_path)
EditorInterface.open_scene_from_path(short_cut_path)
return
elif short_cut_path.ends_with(".tres"):
#todo 目前只有meshlib
var mesh_library: MeshLibrary = ResourceLoader.load(short_cut_path)
print("[", short_cut_path, "]")
for item_id: int in mesh_library.get_item_list():
var mesh: Mesh = mesh_library.get_item_mesh(item_id)
var surface_count: int = mesh.get_surface_count()
var material_name: String = "null"
if surface_count > 0:
material_name = mesh.surface_get_material(0).resource_path
if material_name.begins_with("res://.godot/imported"):
material_name = "inner"
elif material_name.begins_with("res://render/material"):
material_name = material_name.trim_prefix("res://render/material/").trim_suffix(".tres")
else:
material_name = "null"
print("[", item_id, "]", mesh_library.get_item_name(item_id), "::", material_name)
elif short_cut_path == "func_fix_mesh":
var material_grid_block: Material = load("res://render/material/level_grid_block.tres") as Material
var material_grid_water: Material = load("res://render/material/level_grid_water.tres") as Material
var material_grass: Material = load("res://render/material/level_grass.tres") as Material
var dir_path: String = "res://resource/mesh_library"
var dir: DirAccess = DirAccess.open(dir_path)
for file: String in dir.get_files():
var path: String = dir_path + "/" + file
var res: Resource = load(path)
var fix_count: int = 0
if not res is MeshLibrary:
continue
var mesh_library: MeshLibrary = res as MeshLibrary
for item_id: int in mesh_library.get_item_list():
var mesh: Mesh = mesh_library.get_item_mesh(item_id)
var mesh_name: String = mesh_library.get_item_name(item_id)
var material_set: Material = material_grid_block
if mesh_name.contains("water"):
material_set = material_grid_water
if mesh_name.contains("grass"):
material_set = material_grass
for surface_id: int in range(mesh.get_surface_count()):
if not mesh.surface_get_material(surface_id) == material_set:
fix_count += 1
mesh.surface_set_material(surface_id, material_set)
ResourceSaver.save(mesh, mesh.get_path())
ResourceSaver.save(res, path)
print("修复meshlib材质(", fix_count, "):", path)
else:
EditorInterface.select_file(short_cut_path)
func _process(delta: float) -> void:
var selected_paths: PackedStringArray = EditorInterface.get_selected_paths()
if len(selected_paths) != 1:
return
var selected_path: String = selected_paths[0]
if selected_skill_file == selected_path:
return
selected_skill_file = selected_path
if not selected_path.ends_with(".tres"):
return
var res := ResourceLoader.load(selected_path)
if not res is SkillCfg:
return
var skill_cfg: SkillCfg = res as SkillCfg
if not skill_cfg.skill_animation:
return
EditorInterface.open_scene_from_path("res://scene/character/character.tscn")
var root: Node = EditorInterface.get_edited_scene_root()
var character_skill: AnimationPlayer = root.find_child("Skill") as AnimationPlayer
var animation_name: String = "animation_library/%s" % Util.get_resource_name(res)
if not character_skill.has_animation(animation_name):
print("技能动画不存在:", animation_name)
return
character_skill.current_animation = animation_name
EditorInterface.edit_node(character_skill)
EditorInterface.edit_resource(res)
func _exit_tree():
remove_tool_menu_item("Test")
if skill_popup: skill_popup.queue_free()
if short_cut_popup: short_cut_popup.queue_free()