1313 lines
51 KiB
Python
1313 lines
51 KiB
Python
from panda3d.core import loadPrcFileData, WindowProperties, Point3
|
||
from math import pi, sin, cos
|
||
|
||
from direct.showbase.ShowBase import ShowBase
|
||
from direct.task import Task
|
||
from direct.actor.Actor import Actor
|
||
from direct.interval.IntervalGlobal import Sequence
|
||
|
||
import p3dimgui
|
||
|
||
from imgui_bundle import imgui, imgui_ctx
|
||
|
||
import sys
|
||
import os
|
||
import warnings
|
||
|
||
# 导入MyWorld类和必要的模块
|
||
from core.world import CoreWorld
|
||
from core.selection import SelectionSystem
|
||
from core.event_handler import EventHandler
|
||
from core.tool_manager import ToolManager
|
||
from core.script_system import ScriptManager
|
||
from core.patrol_system import PatrolSystem
|
||
from core.Command_System import CommandManager
|
||
from gui.gui_manager import GUIManager
|
||
from core.terrain_manager import TerrainManager
|
||
from scene.scene_manager import SceneManager
|
||
from project.project_manager import ProjectManager
|
||
from core.InfoPanelManager import InfoPanelManager
|
||
from core.collision_manager import CollisionManager
|
||
try:
|
||
# 尝试导入视频管理器,避免循环导入
|
||
import importlib.util
|
||
spec = importlib.util.spec_from_file_location("video_integration", "demo/video_integration.py")
|
||
video_module = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(video_module)
|
||
VideoManager = video_module.VideoManager
|
||
except:
|
||
# 如果video_integration模块不存在,则跳过
|
||
VideoManager = None
|
||
print("⚠ 视频管理器模块未找到,视频功能将不可用")
|
||
|
||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||
|
||
class MyWorld(CoreWorld):
|
||
def __init__(self):
|
||
super().__init__()
|
||
|
||
# 设置窗口为最大化模式
|
||
props = WindowProperties()
|
||
props.set_maximized(True)
|
||
self.win.request_properties(props)
|
||
print("✓ 窗口已设置为最大化模式")
|
||
|
||
# 初始化选择和变换系统
|
||
self.selection = SelectionSystem(self)
|
||
|
||
# 绑定F键用于聚焦选中节点
|
||
self.accept("f", self.onFocusKeyPressed)
|
||
self.accept("F", self.onFocusKeyPressed) # 大写F
|
||
|
||
#初始化巡检系统
|
||
self.patrol_system = PatrolSystem(self)
|
||
self.accept("p",self.onPatrolKeyPressed)
|
||
self.accept("P",self.onPatrolKeyPressed)
|
||
|
||
# 初始化事件处理系统
|
||
self.event_handler = EventHandler(self)
|
||
|
||
# 初始化工具管理系统
|
||
self.tool_manager = ToolManager(self)
|
||
|
||
# 初始化脚本管理系统
|
||
self.script_manager = ScriptManager(self)
|
||
|
||
# 初始化GUI管理系统
|
||
self.gui_manager = GUIManager(self)
|
||
|
||
# 初始化视频管理
|
||
if VideoManager is not None:
|
||
self.video_manager = VideoManager(self)
|
||
else:
|
||
self.video_manager = None
|
||
|
||
# 初始化场景管理系统
|
||
self.scene_manager = SceneManager(self)
|
||
|
||
# 初始化项目管理系统
|
||
self.project_manager = ProjectManager(self)
|
||
|
||
self.info_panel_manager = InfoPanelManager(self)
|
||
|
||
self.command_manager = CommandManager()
|
||
|
||
# 初始化碰撞管理器
|
||
self.collision_manager = CollisionManager(self)
|
||
|
||
# 初始化VR管理器
|
||
try:
|
||
from core.vr import VRManager
|
||
self.vr_manager = VRManager(self)
|
||
print("✓ VR管理器初始化完成")
|
||
except Exception as e:
|
||
print(f"⚠ VR管理器初始化失败: {e}")
|
||
self.vr_manager = None
|
||
|
||
# 调试选项
|
||
self.debug_collision = True # 是否显示碰撞体
|
||
|
||
# 默认启用模型间碰撞检测(可选)
|
||
self.enableModelCollisionDetection(enable=True, frequency=0.1, threshold=0.5)
|
||
|
||
# 启动脚本系统
|
||
self.script_manager.start_system()
|
||
|
||
self.terrain_manager = TerrainManager(self)
|
||
|
||
self.terrain_edit_radius = 3.0
|
||
self.terrain_edit_strength=0.3
|
||
self.terrain_edit_operation = "add"
|
||
|
||
# Install Dear ImGui
|
||
p3dimgui.init()
|
||
|
||
# 启用ImGui Docking功能
|
||
imgui.get_io().config_flags |= imgui.ConfigFlags_.docking_enable
|
||
print("✓ ImGui Docking功能已启用")
|
||
|
||
# 初始化样式管理器
|
||
from core.imgui_style_manager import ImGuiStyleManager
|
||
self.style_manager = ImGuiStyleManager(self.imgui)
|
||
|
||
# 尝试直接设置中文字体
|
||
try:
|
||
import platform
|
||
from pathlib import Path
|
||
|
||
# 获取中文字体路径
|
||
system = platform.system().lower()
|
||
if system == "linux":
|
||
font_path = "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"
|
||
elif system == "windows":
|
||
font_path = "C:/Windows/Fonts/msyh.ttc"
|
||
elif system == "darwin":
|
||
font_path = "/System/Library/Fonts/PingFang.ttc"
|
||
else:
|
||
font_path = None
|
||
|
||
if font_path and Path(font_path).exists():
|
||
# 先清除默认字体
|
||
self.imgui.io.fonts.clear()
|
||
|
||
# 创建中文字符范围(基本中文字符)
|
||
# 这个范围包含了常用中文字符的Unicode范围
|
||
chinese_ranges = []
|
||
# 基本拉丁字母
|
||
for i in range(0x0020, 0x00FF):
|
||
chinese_ranges.append(i)
|
||
# 中文字符范围
|
||
for i in range(0x4E00, 0x9FFF): # CJK统一汉字
|
||
chinese_ranges.append(i)
|
||
for i in range(0x3400, 0x4DBF): # CJK扩展A
|
||
chinese_ranges.append(i)
|
||
for i in range(0x20000, 0x2A6DF): # CJK扩展B
|
||
chinese_ranges.append(i)
|
||
for i in range(0x2A700, 0x2B73F): # CJK扩展C
|
||
chinese_ranges.append(i)
|
||
for i in range(0x2B740, 0x2B81F): # CJK扩展D
|
||
chinese_ranges.append(i)
|
||
for i in range(0x2B820, 0x2CEAF): # CJK扩展E
|
||
chinese_ranges.append(i)
|
||
for i in range(0x2CEB0, 0x2EBEF): # CJK扩展F
|
||
chinese_ranges.append(i)
|
||
for i in range(0x3000, 0x303F): # CJK符号和标点
|
||
chinese_ranges.append(i)
|
||
for i in range(0xFF00, 0xFFEF): # 全角字符
|
||
chinese_ranges.append(i)
|
||
|
||
# 添加中文字体(不指定字符范围,让ImGui自动处理)
|
||
font = self.imgui.io.fonts.add_font_from_file_ttf(font_path, 14.0)
|
||
print(f"✓ 直接设置中文字体成功: {font_path}")
|
||
print(f" 使用自动字符范围")
|
||
else:
|
||
print("⚠ 中文字体文件不存在")
|
||
except Exception as e:
|
||
print(f"⚠ 直接设置中文字体失败: {e}")
|
||
# 备用方案:使用默认字体
|
||
try:
|
||
self.imgui.io.fonts.add_font_default()
|
||
print("✓ 使用默认字体")
|
||
except:
|
||
pass
|
||
|
||
# Disable the camera trackball controls.
|
||
self.disableMouse()
|
||
|
||
self.showDemoWindow = False
|
||
|
||
# UI状态管理
|
||
self.showSceneTree = True
|
||
self.showPropertyPanel = True
|
||
self.showConsole = True
|
||
self.showScriptPanel = True
|
||
self.showToolbar = True
|
||
|
||
# 菜单状态管理
|
||
self.show_new_project_dialog = False
|
||
self.show_open_project_dialog = False
|
||
self.show_save_as_dialog = False
|
||
|
||
# 路径选择对话框状态
|
||
self.show_path_browser = False
|
||
self.path_browser_mode = "" # "new_project" 或 "open_project"
|
||
self.path_browser_current_path = os.getcwd()
|
||
self.path_browser_selected_path = ""
|
||
self.path_browser_items = []
|
||
|
||
# 快捷键映射
|
||
self.shortcut_keys = {
|
||
'Ctrl+N': self._on_new_project,
|
||
'Ctrl+O': self._on_open_project,
|
||
'Ctrl+S': self._on_save_project,
|
||
'Alt+F4': self._on_exit
|
||
}
|
||
|
||
# 键盘状态跟踪
|
||
self.ctrl_pressed = False
|
||
self.alt_pressed = False
|
||
|
||
# 消息系统
|
||
self.messages = []
|
||
self.max_messages = 5 # 最多显示5条消息
|
||
|
||
self.accept('imgui-new-frame', self.__newFrame)
|
||
self.accept('`', self.__toggleImgui)
|
||
|
||
# 添加键盘事件监听用于快捷键
|
||
self.accept('control', self._on_ctrl_pressed)
|
||
self.accept('control-up', self._on_ctrl_released)
|
||
self.accept('alt', self._on_alt_pressed)
|
||
self.accept('alt-up', self._on_alt_released)
|
||
self.accept('n', self._on_n_pressed)
|
||
self.accept('o', self._on_o_pressed)
|
||
self.accept('s', self._on_s_pressed)
|
||
self.accept('f4', self._on_f4_pressed)
|
||
|
||
self.testTexture = None
|
||
self.icons = {} # 初始化图标字典
|
||
|
||
# 添加初始化消息
|
||
self.add_success_message("MyWorld 初始化完成")
|
||
self.add_info_message("ImGui菜单系统已就绪")
|
||
self.add_info_message("快捷键已启用 (Ctrl+N, Ctrl+O, Ctrl+S, Alt+F4)")
|
||
|
||
print("✓ MyWorld 初始化完成")
|
||
|
||
# ==================== 兼容性属性 ====================
|
||
|
||
# 保留models属性以兼容现有代码
|
||
@property
|
||
def models(self):
|
||
"""模型列表的兼容性属性"""
|
||
return self.scene_manager.models
|
||
|
||
@models.setter
|
||
def models(self, value):
|
||
"""模型列表的兼容性设置器"""
|
||
self.scene_manager.models = value
|
||
|
||
# 保留gui_elements属性以兼容现有代码
|
||
@property
|
||
def gui_elements(self):
|
||
"""GUI元素列表的兼容性属性"""
|
||
return self.gui_manager.gui_elements
|
||
|
||
@gui_elements.setter
|
||
def gui_elements(self, value):
|
||
"""GUI元素列表的兼容性设置器"""
|
||
self.gui_manager.gui_elements = value
|
||
|
||
# 保留guiEditMode属性以兼容现有代码
|
||
@property
|
||
def guiEditMode(self):
|
||
"""GUI编辑模式的兼容性属性"""
|
||
return self.gui_manager.guiEditMode
|
||
|
||
@guiEditMode.setter
|
||
def guiEditMode(self, value):
|
||
"""GUI编辑模式的兼容性设置器"""
|
||
self.gui_manager.guiEditMode = value
|
||
|
||
# 保留currentTool属性以兼容现有代码
|
||
@property
|
||
def currentTool(self):
|
||
"""当前工具的兼容性属性"""
|
||
return self.tool_manager.currentTool
|
||
|
||
@currentTool.setter
|
||
def currentTool(self, value):
|
||
"""当前工具的兼容性设置器"""
|
||
self.tool_manager.currentTool = value
|
||
|
||
# 保留terrains属性以兼容现有代码
|
||
@property
|
||
def terrains(self):
|
||
"""地形列表的兼容性属性"""
|
||
return self.terrain_manager.terrains
|
||
|
||
@terrains.setter
|
||
def terrains(self,value):
|
||
"""地形列表的兼容性设置器"""
|
||
self.terrain_manager.terrains = value
|
||
|
||
def onFocusKeyPressed(self):
|
||
"""处理 F 键按下事件"""
|
||
try:
|
||
if hasattr(self, 'selection') and self.selection.selectedNode:
|
||
self.selection.focusCameraOnSelectedNodeAdvanced()
|
||
else:
|
||
print("当前没有选中任何节点")
|
||
except Exception as e:
|
||
print(f"处理 F 键事件失败: {e}")
|
||
|
||
def onPatrolKeyPressed(self):
|
||
"""处理 P 键按下事件 - 控制巡检系统"""
|
||
try:
|
||
print("检测到 P 键按下")
|
||
if not self.patrol_system.is_patrolling:
|
||
if not self.patrol_system.patrol_points:
|
||
self.createDefaultPatrolRoute()
|
||
if self.patrol_system.start_patrol():
|
||
print("✓ 巡检已开始")
|
||
else:
|
||
print("✗ 巡检启动失败")
|
||
else:
|
||
if self.patrol_system.stop_patrol():
|
||
print("✓ 巡检已停止")
|
||
else:
|
||
print("✗ 巡检停止失败")
|
||
except Exception as e:
|
||
print(f"处理 P 键事件失败: {e}")
|
||
|
||
def createDefaultPatrolRoute(self):
|
||
"""创建默认巡检路线"""
|
||
try:
|
||
self.patrol_system.clear_patrol_points()
|
||
self.patrol_system.add_patrol_point((0, -10, 2), (0,0,0), 1.5)
|
||
self.patrol_system.add_patrol_point((10, -10, 2), (0,0,0), 1.5)
|
||
self.patrol_system.add_patrol_point((10, 5, 2), (0,0,0), 1.5)
|
||
self.patrol_system.add_patrol_point((10, 0, 5), (0,0,0), 1.5)
|
||
self.patrol_system.add_patrol_point((0, -10, 2), None, 2.5)
|
||
print("✓ 默认自动朝向巡检路线已创建")
|
||
self.patrol_system.list_patrol_points()
|
||
except Exception as e:
|
||
print(f"创建默认自动朝向巡检路线失败: {e}")
|
||
|
||
def enableModelCollisionDetection(self, enable=True, frequency=0.1, threshold=0.5):
|
||
"""启用模型间碰撞检测"""
|
||
return self.collision_manager.enableModelCollisionDetection(enable, frequency, threshold)
|
||
|
||
def __toggleImgui(self):
|
||
if not self.imgui.isKeyboardCaptured():
|
||
self.imgui.toggle()
|
||
|
||
def __newFrame(self):
|
||
# Dear ImGui commands can be placed here.
|
||
|
||
# 创建全屏DockSpace(在第一帧之后创建)
|
||
if imgui.get_frame_count() > 0:
|
||
viewport = imgui.get_main_viewport()
|
||
imgui.dock_space_over_viewport(0, viewport, imgui.DockNodeFlags_.passthru_central_node)
|
||
|
||
# 在第一帧应用样式
|
||
if imgui.get_frame_count() == 0:
|
||
self.style_manager.apply_style()
|
||
# 加载图标
|
||
self.icons = {
|
||
'select': self.style_manager.load_icon('select_tool'),
|
||
'move': self.style_manager.load_icon('move_tool'),
|
||
'rotate': self.style_manager.load_icon('rotate_tool'),
|
||
'scale': self.style_manager.load_icon('scale_tool'),
|
||
'success': self.style_manager.load_icon('success_icon'),
|
||
'warning': self.style_manager.load_icon('warning_icon'),
|
||
'property_select_image': self.style_manager.load_icon('property_select_image'),
|
||
'delete_fail_icon': self.style_manager.load_icon('delete_fail_icon'),
|
||
}
|
||
|
||
# 确保中文字体被正确应用
|
||
try:
|
||
# 强制刷新字体
|
||
self.imgui.io.fonts.clear()
|
||
# 重新添加中文字体
|
||
import platform
|
||
from pathlib import Path
|
||
|
||
system = platform.system().lower()
|
||
if system == "linux":
|
||
font_path = "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"
|
||
elif system == "windows":
|
||
font_path = "C:/Windows/Fonts/msyh.ttc"
|
||
elif system == "darwin":
|
||
font_path = "/System/Library/Fonts/PingFang.ttc"
|
||
|
||
if font_path and Path(font_path).exists():
|
||
# 添加中文字体,包含中文字符集
|
||
try:
|
||
chinese_ranges = imgui.get_io().fonts.get_glyph_ranges_chinese_full()
|
||
self.imgui.io.fonts.add_font_from_file_ttf(font_path, 14.0, None, chinese_ranges)
|
||
print("✓ 第一帧重新设置中文字体(包含中文字符集)")
|
||
except:
|
||
# 如果获取字符集失败,使用简单方式
|
||
self.imgui.io.fonts.add_font_from_file_ttf(font_path, 14.0)
|
||
print("✓ 第一帧重新设置中文字体(简单方式)")
|
||
except Exception as e:
|
||
print(f"⚠ 第一帧设置中文字体失败: {e}")
|
||
|
||
# 获取窗口尺寸
|
||
display_size = imgui.get_io().display_size
|
||
window_width = display_size.x
|
||
window_height = display_size.y
|
||
|
||
# 绘制菜单栏
|
||
self._draw_menu_bar()
|
||
|
||
# 绘制停靠布局
|
||
self._draw_docked_layout(window_width, window_height)
|
||
|
||
# 绘制纹理测试窗口
|
||
if self.testTexture:
|
||
with self.style_manager.begin_styled_window("Texture Test", True,
|
||
imgui.WindowFlags_.always_auto_resize) as (_, windowOpen):
|
||
if not windowOpen:
|
||
self.imgui.removeTexture(self.testTexture)
|
||
self.testTexture = None
|
||
return
|
||
imgui.image(self.testTexture, (256, 256))
|
||
|
||
# 绘制ImGui演示窗口
|
||
if self.showDemoWindow:
|
||
imgui.show_demo_window()
|
||
|
||
# 绘制对话框
|
||
self._draw_new_project_dialog()
|
||
self._draw_open_project_dialog()
|
||
self._draw_path_browser()
|
||
|
||
def _draw_docked_layout(self, window_width, window_height):
|
||
"""绘制可停靠的布局(支持拖拽)"""
|
||
# 左侧场景树面板
|
||
if self.showSceneTree:
|
||
self._draw_scene_tree()
|
||
|
||
# 属性面板
|
||
if self.showPropertyPanel:
|
||
self._draw_property_panel()
|
||
|
||
# 脚本面板
|
||
if self.showScriptPanel:
|
||
self._draw_script_panel()
|
||
|
||
# 底部控制台
|
||
if self.showConsole:
|
||
self._draw_console()
|
||
|
||
# 顶部工具栏
|
||
if self.showToolbar:
|
||
self._draw_toolbar()
|
||
|
||
|
||
|
||
def _draw_menu_bar(self):
|
||
"""绘制菜单栏"""
|
||
with imgui_ctx.begin_main_menu_bar() as main_menu:
|
||
if main_menu:
|
||
# 文件菜单
|
||
with imgui_ctx.begin_menu("文件") as file_menu:
|
||
if file_menu:
|
||
if imgui.menu_item("新建项目", "Ctrl+N", False, True)[1]:
|
||
self._on_new_project()
|
||
if imgui.menu_item("打开项目", "Ctrl+O", False, True)[1]:
|
||
self._on_open_project()
|
||
imgui.separator()
|
||
if imgui.menu_item("保存", "Ctrl+S", False, True)[1]:
|
||
self._on_save_project()
|
||
if imgui.menu_item("另存为", "", False, True)[1]:
|
||
self._on_save_as_project()
|
||
imgui.separator()
|
||
if imgui.menu_item("退出", "Alt+F4", False, True)[1]:
|
||
self._on_exit()
|
||
|
||
# 编辑菜单
|
||
with imgui_ctx.begin_menu("编辑") as edit_menu:
|
||
if edit_menu:
|
||
imgui.menu_item("撤销", "Ctrl+Z", False, True)
|
||
imgui.menu_item("重做", "Ctrl+Y", False, True)
|
||
imgui.separator()
|
||
imgui.menu_item("复制", "Ctrl+C", False, True)
|
||
imgui.menu_item("粘贴", "Ctrl+V", False, True)
|
||
imgui.menu_item("删除", "Del", False, True)
|
||
|
||
# 视图菜单
|
||
with imgui_ctx.begin_menu("视图") as view_menu:
|
||
if view_menu:
|
||
_, self.showToolbar = imgui.menu_item("工具栏", "", self.showToolbar, True)
|
||
_, self.showSceneTree = imgui.menu_item("场景树", "", self.showSceneTree, True)
|
||
_, self.showPropertyPanel = imgui.menu_item("属性面板", "", self.showPropertyPanel, True)
|
||
_, self.showConsole = imgui.menu_item("控制台", "", self.showConsole, True)
|
||
_, self.showScriptPanel = imgui.menu_item("脚本管理", "", self.showScriptPanel, True)
|
||
|
||
# 工具菜单
|
||
with imgui_ctx.begin_menu("工具") as tools_menu:
|
||
if tools_menu:
|
||
imgui.menu_item("导入模型", "", False, True)
|
||
imgui.menu_item("地形编辑器", "", False, True)
|
||
imgui.menu_item("材质编辑器", "", False, True)
|
||
imgui.menu_item("脚本编辑器", "", False, True)
|
||
|
||
# 窗口菜单
|
||
with imgui_ctx.begin_menu("窗口") as window_menu:
|
||
if window_menu:
|
||
_, self.showDemoWindow = imgui.menu_item("ImGui演示", "", self.showDemoWindow, True)
|
||
if self.testTexture:
|
||
imgui.menu_item("关闭纹理测试", "", False, True)
|
||
else:
|
||
imgui.menu_item("显示纹理测试", "", False, True)
|
||
|
||
# 帮助菜单
|
||
with imgui_ctx.begin_menu("帮助") as help_menu:
|
||
if help_menu:
|
||
imgui.menu_item("关于", "", False, True)
|
||
imgui.menu_item("文档", "", False, True)
|
||
|
||
# 右侧显示FPS
|
||
imgui.set_cursor_pos_x(imgui.get_window_size().x - 140)
|
||
imgui.text("%.2f FPS (%.2f ms)" % (imgui.get_io().framerate, 1000.0 / imgui.get_io().framerate))
|
||
|
||
def _draw_toolbar(self):
|
||
"""绘制工具栏"""
|
||
# 工具栏可以保持无标题栏,但允许移动和调整大小
|
||
flags = self.style_manager.get_window_flags("toolbar")
|
||
|
||
with self.style_manager.begin_styled_window("工具栏", self.showToolbar, flags):
|
||
self.showToolbar = True # 确保窗口保持打开
|
||
|
||
# 工具按钮组
|
||
if self.icons.get('select'):
|
||
if self.style_manager.image_button(self.icons['select'], (24, 24)):
|
||
print("选择工具")
|
||
if imgui.is_item_hovered():
|
||
imgui.set_tooltip("选择工具 (Q)")
|
||
imgui.same_line()
|
||
else:
|
||
if imgui.button("选择"):
|
||
print("选择工具")
|
||
imgui.same_line()
|
||
|
||
if self.icons.get('move'):
|
||
if self.style_manager.image_button(self.icons['move'], (24, 24)):
|
||
print("移动工具")
|
||
if imgui.is_item_hovered():
|
||
imgui.set_tooltip("移动工具 (W)")
|
||
imgui.same_line()
|
||
else:
|
||
if imgui.button("移动"):
|
||
print("移动工具")
|
||
imgui.same_line()
|
||
|
||
if self.icons.get('rotate'):
|
||
if self.style_manager.image_button(self.icons['rotate'], (24, 24)):
|
||
print("旋转工具")
|
||
if imgui.is_item_hovered():
|
||
imgui.set_tooltip("旋转工具 (E)")
|
||
imgui.same_line()
|
||
else:
|
||
if imgui.button("旋转"):
|
||
print("旋转工具")
|
||
imgui.same_line()
|
||
|
||
if self.icons.get('scale'):
|
||
if self.style_manager.image_button(self.icons['scale'], (24, 24)):
|
||
print("缩放工具")
|
||
if imgui.is_item_hovered():
|
||
imgui.set_tooltip("缩放工具 (R)")
|
||
else:
|
||
if imgui.button("缩放"):
|
||
print("缩放工具")
|
||
|
||
imgui.same_line()
|
||
imgui.separator()
|
||
imgui.same_line()
|
||
|
||
# 其他工具按钮
|
||
if imgui.button("导入"):
|
||
print("导入模型")
|
||
imgui.same_line()
|
||
if imgui.button("保存"):
|
||
print("保存场景")
|
||
imgui.same_line()
|
||
if imgui.button("播放"):
|
||
print("播放动画")
|
||
|
||
def _draw_scene_tree(self):
|
||
"""绘制场景树面板"""
|
||
# 使用更少的限制性标志,允许docking
|
||
flags = (imgui.WindowFlags_.no_collapse)
|
||
|
||
with self.style_manager.begin_styled_window("场景树", self.showSceneTree, flags):
|
||
self.showSceneTree = True # 确保窗口保持打开
|
||
|
||
imgui.text("场景层级")
|
||
imgui.separator()
|
||
|
||
# 模拟场景树结构
|
||
if imgui.tree_node("渲染"):
|
||
if imgui.tree_node("环境光"):
|
||
imgui.text("环境光 #1")
|
||
imgui.tree_pop()
|
||
if imgui.tree_node("定向光"):
|
||
imgui.text("定向光 #1")
|
||
imgui.tree_pop()
|
||
if imgui.tree_node("地板"):
|
||
imgui.text("地板节点")
|
||
imgui.tree_pop()
|
||
imgui.tree_pop()
|
||
|
||
if imgui.tree_node("相机"):
|
||
imgui.text("主相机")
|
||
imgui.tree_pop()
|
||
|
||
if imgui.tree_node("模型"):
|
||
imgui.text("(空)")
|
||
imgui.tree_pop()
|
||
|
||
def _draw_property_panel(self):
|
||
"""绘制属性面板"""
|
||
# 使用面板类型的窗口标志,支持docking
|
||
flags = self.style_manager.get_window_flags("panel")
|
||
|
||
with self.style_manager.begin_styled_window("属性面板", self.showPropertyPanel, flags):
|
||
self.showPropertyPanel = True # 确保窗口保持打开
|
||
|
||
imgui.text("对象属性")
|
||
imgui.separator()
|
||
|
||
# 模拟属性设置
|
||
changed, pos_x = imgui.drag_float("位置 X", 0.0, 0.1)
|
||
changed, pos_y = imgui.drag_float("位置 Y", 0.0, 0.1)
|
||
changed, pos_z = imgui.drag_float("位置 Z", 0.0, 0.1)
|
||
|
||
imgui.separator()
|
||
|
||
changed, rot_x = imgui.drag_float("旋转 X", 0.0, 1.0)
|
||
changed, rot_y = imgui.drag_float("旋转 Y", 0.0, 1.0)
|
||
changed, rot_z = imgui.drag_float("旋转 Z", 0.0, 1.0)
|
||
|
||
imgui.separator()
|
||
|
||
changed, scale = imgui.drag_float("缩放", 1.0, 0.1)
|
||
|
||
imgui.separator()
|
||
|
||
if imgui.button("重置变换"):
|
||
print("重置变换")
|
||
|
||
def _draw_console(self):
|
||
"""绘制控制台面板"""
|
||
# 使用面板类型的窗口标志,支持docking
|
||
flags = self.style_manager.get_window_flags("panel")
|
||
|
||
with self.style_manager.begin_styled_window("控制台", self.showConsole, flags):
|
||
self.showConsole = True # 确保窗口保持打开
|
||
|
||
imgui.text("控制台输出")
|
||
imgui.separator()
|
||
|
||
# 显示消息系统中的消息
|
||
if hasattr(self, 'messages') and self.messages:
|
||
for message in self.messages:
|
||
# 显示时间戳
|
||
imgui.text_colored((0.7, 0.7, 0.7, 1.0), f"[{message['timestamp']}]")
|
||
imgui.same_line()
|
||
|
||
# 根据消息类型显示图标
|
||
if message['text'].startswith('✓'):
|
||
if self.icons.get('success'):
|
||
imgui.image(self.icons['success'], (12, 12))
|
||
imgui.same_line()
|
||
elif message['text'].startswith('✗'):
|
||
if self.icons.get('delete_fail_icon'):
|
||
imgui.image(self.icons['delete_fail_icon'], (12, 12))
|
||
imgui.same_line()
|
||
elif message['text'].startswith('⚠'):
|
||
if self.icons.get('warning'):
|
||
imgui.image(self.icons['warning'], (12, 12))
|
||
imgui.same_line()
|
||
|
||
# 显示消息文本
|
||
imgui.text_colored(message['color'], message['text'])
|
||
else:
|
||
# 默认消息
|
||
imgui.text_colored((0.157, 0.620, 1.0, 1.0), "[系统]")
|
||
imgui.same_line()
|
||
imgui.text("引擎已就绪")
|
||
|
||
# 输入框
|
||
imgui.separator()
|
||
changed, command = imgui.input_text(">", "", 256)
|
||
if changed and command:
|
||
self.add_info_message(f"执行命令: {command}")
|
||
# TODO: 实现命令执行逻辑
|
||
|
||
def _draw_script_panel(self):
|
||
"""绘制脚本管理面板"""
|
||
# 使用面板类型的窗口标志,支持docking
|
||
flags = self.style_manager.get_window_flags("panel")
|
||
|
||
with self.style_manager.begin_styled_window("脚本管理", self.showScriptPanel, flags):
|
||
self.showScriptPanel = True # 确保窗口保持打开
|
||
|
||
imgui.text("脚本列表")
|
||
imgui.separator()
|
||
|
||
# 模拟脚本列表
|
||
selected, _ = imgui.selectable("main.py", False)
|
||
if selected:
|
||
print("选择脚本: main.py")
|
||
selected, _ = imgui.selectable("player_controller.py", False)
|
||
if selected:
|
||
print("选择脚本: player_controller.py")
|
||
selected, _ = imgui.selectable("ui_manager.py", False)
|
||
if selected:
|
||
print("选择脚本: ui_manager.py")
|
||
|
||
imgui.separator()
|
||
|
||
# 脚本操作按钮
|
||
if imgui.button("新建脚本"):
|
||
print("新建脚本")
|
||
imgui.same_line()
|
||
if imgui.button("编辑"):
|
||
print("编辑脚本")
|
||
imgui.same_line()
|
||
if imgui.button("重载"):
|
||
print("重载脚本")
|
||
|
||
imgui.separator()
|
||
|
||
imgui.text("脚本输出:")
|
||
imgui.text("脚本引擎已启动")
|
||
imgui.text("热重载监控已启动")
|
||
|
||
# ==================== 菜单处理函数 ====================
|
||
|
||
def _on_new_project(self):
|
||
"""处理新建项目菜单项"""
|
||
self.add_info_message("打开新建项目对话框")
|
||
self.show_new_project_dialog = True
|
||
|
||
def _on_open_project(self):
|
||
"""处理打开项目菜单项"""
|
||
self.add_info_message("打开项目对话框")
|
||
self.show_open_project_dialog = True
|
||
|
||
def _on_save_project(self):
|
||
"""处理保存项目菜单项"""
|
||
if hasattr(self, 'project_manager') and self.project_manager:
|
||
try:
|
||
# 检查是否有当前项目路径
|
||
if not self.project_manager.current_project_path:
|
||
self.add_warning_message("没有当前项目路径,请先创建或打开项目")
|
||
self.show_save_as_dialog = True
|
||
return
|
||
|
||
# 直接调用保存逻辑,避免Qt依赖
|
||
if self._save_project_impl():
|
||
self.add_success_message("项目保存成功")
|
||
else:
|
||
self.add_error_message("项目保存失败")
|
||
except Exception as e:
|
||
self.add_error_message(f"项目保存失败: {e}")
|
||
else:
|
||
self.add_error_message("项目管理器未初始化")
|
||
|
||
def _on_save_as_project(self):
|
||
"""处理另存为项目菜单项"""
|
||
self.add_info_message("另存为项目(功能待实现)")
|
||
# TODO: 实现另存为对话框
|
||
# self.show_save_as_dialog = True
|
||
|
||
def _on_exit(self):
|
||
"""处理退出菜单项"""
|
||
self.add_info_message("退出应用程序")
|
||
self.userExit()
|
||
|
||
# ==================== 键盘事件处理函数 ====================
|
||
|
||
def _on_ctrl_pressed(self):
|
||
"""Ctrl键按下"""
|
||
self.ctrl_pressed = True
|
||
|
||
def _on_ctrl_released(self):
|
||
"""Ctrl键释放"""
|
||
self.ctrl_pressed = False
|
||
|
||
def _on_alt_pressed(self):
|
||
"""Alt键按下"""
|
||
self.alt_pressed = True
|
||
|
||
def _on_alt_released(self):
|
||
"""Alt键释放"""
|
||
self.alt_pressed = False
|
||
|
||
def _on_n_pressed(self):
|
||
"""N键按下 - 检查Ctrl+N组合键"""
|
||
if self.ctrl_pressed:
|
||
self._on_new_project()
|
||
|
||
def _on_o_pressed(self):
|
||
"""O键按下 - 检查Ctrl+O组合键"""
|
||
if self.ctrl_pressed:
|
||
self._on_open_project()
|
||
|
||
def _on_s_pressed(self):
|
||
"""S键按下 - 检查Ctrl+S组合键"""
|
||
if self.ctrl_pressed:
|
||
self._on_save_project()
|
||
|
||
def _on_f4_pressed(self):
|
||
"""F4键按下 - 检查Alt+F4组合键"""
|
||
if self.alt_pressed:
|
||
self._on_exit()
|
||
|
||
# ==================== 消息系统 ====================
|
||
|
||
def add_message(self, text, color=(1.0, 1.0, 1.0, 1.0)):
|
||
"""添加消息到消息列表"""
|
||
import datetime
|
||
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
|
||
self.messages.append({
|
||
'text': text,
|
||
'color': color,
|
||
'timestamp': timestamp
|
||
})
|
||
|
||
# 限制消息数量
|
||
if len(self.messages) > self.max_messages:
|
||
self.messages = self.messages[-self.max_messages:]
|
||
|
||
def add_success_message(self, text):
|
||
"""添加成功消息"""
|
||
self.add_message(f"✓ {text}", (0.176, 1.0, 0.769, 1.0))
|
||
|
||
def add_error_message(self, text):
|
||
"""添加错误消息"""
|
||
self.add_message(f"✗ {text}", (1.0, 0.3, 0.3, 1.0))
|
||
|
||
def add_warning_message(self, text):
|
||
"""添加警告消息"""
|
||
self.add_message(f"⚠ {text}", (0.953, 0.616, 0.471, 1.0))
|
||
|
||
def add_info_message(self, text):
|
||
"""添加信息消息"""
|
||
self.add_message(f"ℹ {text}", (0.157, 0.620, 1.0, 1.0))
|
||
|
||
# ==================== 对话框绘制函数 ====================
|
||
|
||
def _draw_new_project_dialog(self):
|
||
"""绘制新建项目对话框"""
|
||
if not self.show_new_project_dialog:
|
||
return
|
||
|
||
# 初始化默认值
|
||
if not hasattr(self, 'new_project_name'):
|
||
self.new_project_name = "新项目"
|
||
if not hasattr(self, 'new_project_path'):
|
||
self.new_project_path = "./projects/"
|
||
|
||
# 设置对话框标志
|
||
flags = (imgui.WindowFlags_.no_resize |
|
||
imgui.WindowFlags_.no_collapse |
|
||
imgui.WindowFlags_.modal)
|
||
|
||
# 获取屏幕尺寸,居中显示对话框
|
||
display_size = imgui.get_io().display_size
|
||
dialog_width = 400
|
||
dialog_height = 300
|
||
imgui.set_next_window_size((dialog_width, dialog_height))
|
||
imgui.set_next_window_pos(
|
||
((display_size.x - dialog_width) / 2, (display_size.y - dialog_height) / 2)
|
||
)
|
||
|
||
with imgui_ctx.begin("新建项目", True, flags) as window:
|
||
if not window.opened:
|
||
self.show_new_project_dialog = False
|
||
return
|
||
|
||
imgui.text("创建新项目")
|
||
imgui.separator()
|
||
|
||
# 项目名称输入
|
||
changed, project_name = imgui.input_text("项目名称", self.new_project_name, 256)
|
||
if changed:
|
||
self.new_project_name = project_name
|
||
|
||
# 项目路径输入
|
||
changed, project_path = imgui.input_text("项目路径", self.new_project_path, 256)
|
||
if changed:
|
||
self.new_project_path = project_path
|
||
|
||
imgui.same_line()
|
||
if imgui.button("浏览..."):
|
||
self.path_browser_mode = "new_project"
|
||
self.path_browser_current_path = os.path.dirname(self.new_project_path) if self.new_project_path else os.getcwd()
|
||
self.show_path_browser = True
|
||
self._refresh_path_browser()
|
||
|
||
imgui.separator()
|
||
|
||
# 按钮区域
|
||
if imgui.button("创建"):
|
||
if self.new_project_name and self.new_project_path:
|
||
self._create_new_project(self.new_project_name, self.new_project_path)
|
||
self.show_new_project_dialog = False
|
||
|
||
imgui.same_line()
|
||
if imgui.button("取消"):
|
||
self.show_new_project_dialog = False
|
||
|
||
def _draw_open_project_dialog(self):
|
||
"""绘制打开项目对话框"""
|
||
if not self.show_open_project_dialog:
|
||
return
|
||
|
||
# 初始化默认值
|
||
if not hasattr(self, 'open_project_path'):
|
||
self.open_project_path = "./projects/"
|
||
|
||
# 设置对话框标志
|
||
flags = (imgui.WindowFlags_.no_resize |
|
||
imgui.WindowFlags_.no_collapse |
|
||
imgui.WindowFlags_.modal)
|
||
|
||
# 获取屏幕尺寸,居中显示对话框
|
||
display_size = imgui.get_io().display_size
|
||
dialog_width = 500
|
||
dialog_height = 400
|
||
imgui.set_next_window_size((dialog_width, dialog_height))
|
||
imgui.set_next_window_pos(
|
||
((display_size.x - dialog_width) / 2, (display_size.y - dialog_height) / 2)
|
||
)
|
||
|
||
with imgui_ctx.begin("打开项目", True, flags) as window:
|
||
if not window.opened:
|
||
self.show_open_project_dialog = False
|
||
return
|
||
|
||
imgui.text("选择项目")
|
||
imgui.separator()
|
||
|
||
imgui.text("项目路径:")
|
||
changed, project_path = imgui.input_text("##project_path", self.open_project_path, 512)
|
||
if changed:
|
||
self.open_project_path = project_path
|
||
|
||
imgui.same_line()
|
||
if imgui.button("浏览..."):
|
||
self.path_browser_mode = "open_project"
|
||
self.path_browser_current_path = self.open_project_path if self.open_project_path else os.getcwd()
|
||
self.show_path_browser = True
|
||
self._refresh_path_browser()
|
||
|
||
imgui.separator()
|
||
|
||
# 按钮区域
|
||
if imgui.button("打开"):
|
||
if self.open_project_path:
|
||
self._open_project_path(self.open_project_path)
|
||
self.show_open_project_dialog = False
|
||
|
||
imgui.same_line()
|
||
if imgui.button("取消"):
|
||
self.show_open_project_dialog = False
|
||
|
||
def _draw_path_browser(self):
|
||
"""绘制路径选择对话框"""
|
||
if not self.show_path_browser:
|
||
return
|
||
|
||
# 设置对话框标志
|
||
flags = (imgui.WindowFlags_.no_resize |
|
||
imgui.WindowFlags_.no_collapse |
|
||
imgui.WindowFlags_.modal)
|
||
|
||
# 获取屏幕尺寸,居中显示对话框
|
||
display_size = imgui.get_io().display_size
|
||
dialog_width = 600
|
||
dialog_height = 500
|
||
imgui.set_next_window_size((dialog_width, dialog_height))
|
||
imgui.set_next_window_pos(
|
||
((display_size.x - dialog_width) / 2, (display_size.y - dialog_height) / 2)
|
||
)
|
||
|
||
with imgui_ctx.begin("选择路径", True, flags) as window:
|
||
if not window.opened:
|
||
self.show_path_browser = False
|
||
return
|
||
|
||
imgui.text("选择路径")
|
||
imgui.separator()
|
||
|
||
# 当前路径显示
|
||
imgui.text("当前路径:")
|
||
imgui.same_line()
|
||
imgui.text_colored((0.7, 0.7, 0.7, 1.0), self.path_browser_current_path)
|
||
|
||
imgui.separator()
|
||
|
||
# 路径导航按钮
|
||
if imgui.button("上级目录"):
|
||
parent_path = os.path.dirname(self.path_browser_current_path)
|
||
if parent_path != self.path_browser_current_path:
|
||
self.path_browser_current_path = parent_path
|
||
self._refresh_path_browser()
|
||
|
||
imgui.same_line()
|
||
if imgui.button("主目录"):
|
||
self.path_browser_current_path = os.path.expanduser("~")
|
||
self._refresh_path_browser()
|
||
|
||
imgui.same_line()
|
||
if imgui.button("当前目录"):
|
||
self.path_browser_current_path = os.getcwd()
|
||
self._refresh_path_browser()
|
||
|
||
imgui.separator()
|
||
|
||
# 文件和目录列表
|
||
if self.path_browser_items:
|
||
# 先显示目录
|
||
for item in self.path_browser_items:
|
||
if item['is_dir']:
|
||
# 尝试使用图标或文本标识目录
|
||
if self.icons.get('property_select_image'): # 使用现有图标作为文件夹图标
|
||
imgui.image(self.icons['property_select_image'], (16, 16))
|
||
imgui.same_line()
|
||
else:
|
||
imgui.text_colored((0.4, 0.6, 1.0, 1.0), ">")
|
||
imgui.same_line()
|
||
|
||
if imgui.selectable(item['name'], False)[0]:
|
||
self.path_browser_current_path = item['path']
|
||
self._refresh_path_browser()
|
||
if imgui.is_item_hovered() and imgui.is_mouse_double_clicked(0):
|
||
self.path_browser_current_path = item['path']
|
||
self._refresh_path_browser()
|
||
|
||
# 显示文件(仅在打开项目模式下显示.json文件)
|
||
if self.path_browser_mode == "open_project":
|
||
for item in self.path_browser_items:
|
||
if not item['is_dir'] and item['name'].endswith('.json'):
|
||
# 尝试使用图标或文本标识文件
|
||
if self.icons.get('success_icon'): # 使用成功图标作为文件图标
|
||
imgui.image(self.icons['success_icon'], (16, 16))
|
||
imgui.same_line()
|
||
else:
|
||
imgui.text_colored((1.0, 0.8, 0.4, 1.0), "-")
|
||
imgui.same_line()
|
||
|
||
if imgui.selectable(item['name'], False)[0]:
|
||
self.path_browser_selected_path = item['path']
|
||
if imgui.is_item_hovered() and imgui.is_mouse_double_clicked(0):
|
||
# 选择包含project.json的目录
|
||
self.path_browser_current_path = os.path.dirname(item['path'])
|
||
self._apply_selected_path()
|
||
|
||
imgui.separator()
|
||
|
||
# 选中路径显示
|
||
if self.path_browser_selected_path:
|
||
imgui.text("选中路径:")
|
||
imgui.same_line()
|
||
imgui.text_colored((0.7, 0.7, 0.7, 1.0), self.path_browser_selected_path)
|
||
|
||
# 按钮区域
|
||
if imgui.button("确定"):
|
||
self._apply_selected_path()
|
||
self.show_path_browser = False
|
||
|
||
imgui.same_line()
|
||
if imgui.button("取消"):
|
||
self.show_path_browser = False
|
||
|
||
def _create_new_project(self, name, path):
|
||
"""创建新项目的实际实现"""
|
||
if not hasattr(self, 'project_manager') or not self.project_manager:
|
||
print("✗ 项目管理器未初始化")
|
||
return
|
||
|
||
try:
|
||
if self._create_new_project_impl(name, path):
|
||
print(f"✓ 项目创建成功: {name}")
|
||
else:
|
||
print(f"✗ 项目创建失败: {name}")
|
||
except Exception as e:
|
||
print(f"✗ 项目创建失败: {e}")
|
||
|
||
def _open_project_path(self, path):
|
||
"""打开项目的实际实现"""
|
||
if not hasattr(self, 'project_manager') or not self.project_manager:
|
||
print("✗ 项目管理器未初始化")
|
||
return
|
||
|
||
try:
|
||
print(f"打开项目: {path}")
|
||
if self._open_project_impl(path):
|
||
print(f"✓ 项目打开成功: {path}")
|
||
else:
|
||
print(f"✗ 项目打开失败: {path}")
|
||
except Exception as e:
|
||
print(f"✗ 项目打开失败: {e}")
|
||
|
||
# ==================== 项目管理具体实现 ====================
|
||
|
||
def _save_project_impl(self):
|
||
"""保存项目的具体实现(不依赖Qt)"""
|
||
import json
|
||
import datetime
|
||
import os
|
||
|
||
project_path = self.project_manager.current_project_path
|
||
scenes_path = os.path.join(project_path, "scenes")
|
||
|
||
# 固定的场景文件名
|
||
scene_file = os.path.join(scenes_path, "scene.bam")
|
||
|
||
# 如果存在旧文件,先删除
|
||
if os.path.exists(scene_file):
|
||
try:
|
||
os.remove(scene_file)
|
||
print(f"已删除旧场景文件: {scene_file}")
|
||
except Exception as e:
|
||
print(f"删除旧场景文件失败: {str(e)}")
|
||
return False
|
||
|
||
# 保存场景
|
||
if self.scene_manager.saveScene(scene_file, project_path):
|
||
# 更新项目配置文件
|
||
config_file = os.path.join(project_path, "project.json")
|
||
if os.path.exists(config_file):
|
||
with open(config_file, "r", encoding="utf-8") as f:
|
||
project_config = json.load(f)
|
||
|
||
# 更新最后修改时间
|
||
project_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||
# 记录场景文件路径
|
||
project_config["scene_file"] = os.path.relpath(scene_file, project_path)
|
||
|
||
with open(config_file, "w", encoding="utf-8") as f:
|
||
json.dump(project_config, f, ensure_ascii=False, indent=4)
|
||
|
||
# 更新项目配置
|
||
self.project_manager.project_config = project_config
|
||
return True
|
||
return False
|
||
|
||
def _open_project_impl(self, project_path):
|
||
"""打开项目的具体实现(不依赖Qt)"""
|
||
import json
|
||
import datetime
|
||
import os
|
||
|
||
# 检查是否是有效的项目文件夹
|
||
config_file = os.path.join(project_path, "project.json")
|
||
if not os.path.exists(config_file):
|
||
print(f"⚠ 选择的不是有效的项目文件夹: {project_path}")
|
||
return False
|
||
|
||
# 读取项目配置
|
||
with open(config_file, "r", encoding="utf-8") as f:
|
||
project_config = json.load(f)
|
||
|
||
# 检查场景文件
|
||
scene_file = os.path.join(project_path, "scenes", "scene.bam")
|
||
if os.path.exists(scene_file):
|
||
# 加载场景
|
||
if self.scene_manager.loadScene(scene_file):
|
||
# 更新项目配置
|
||
project_config["scene_file"] = os.path.relpath(scene_file, project_path)
|
||
|
||
project_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||
with open(config_file, "w", encoding="utf-8") as f:
|
||
json.dump(project_config, f, ensure_ascii=False, indent=4)
|
||
|
||
# 更新项目状态
|
||
self.project_manager.current_project_path = project_path
|
||
self.project_manager.project_config = project_config
|
||
|
||
# 更新窗口标题
|
||
project_name = os.path.basename(project_path)
|
||
self._update_window_title(project_name)
|
||
|
||
return True
|
||
|
||
def _create_new_project_impl(self, name, path):
|
||
"""创建新项目的具体实现(不依赖Qt)"""
|
||
import json
|
||
import datetime
|
||
import os
|
||
|
||
full_project_path = os.path.normpath(os.path.join(path, name))
|
||
print(f"创建项目路径: {full_project_path}")
|
||
|
||
try:
|
||
# 创建项目文件夹结构
|
||
os.makedirs(full_project_path)
|
||
os.makedirs(os.path.join(full_project_path, "models")) # 模型文件夹
|
||
os.makedirs(os.path.join(full_project_path, "textures")) # 贴图文件夹
|
||
scenes_path = os.path.join(full_project_path, "scenes") # 场景文件夹
|
||
os.makedirs(scenes_path)
|
||
|
||
# 创建项目配置文件
|
||
project_config = {
|
||
"name": name,
|
||
"path": full_project_path,
|
||
"created": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||
"last_modified": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||
"version": "1.0",
|
||
"scene_file": "scenes/scene.bam"
|
||
}
|
||
|
||
# 保存项目配置
|
||
config_file = os.path.join(full_project_path, "project.json")
|
||
with open(config_file, "w", encoding="utf-8") as f:
|
||
json.dump(project_config, f, ensure_ascii=False, indent=4)
|
||
|
||
# 保存初始场景
|
||
scene_file = os.path.join(scenes_path, "scene.bam")
|
||
self.scene_manager.saveScene(scene_file, full_project_path)
|
||
|
||
# 更新项目管理器状态
|
||
self.project_manager.current_project_path = full_project_path
|
||
self.project_manager.project_config = project_config
|
||
|
||
# 更新窗口标题
|
||
self._update_window_title(name)
|
||
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"创建项目失败: {e}")
|
||
return False
|
||
|
||
def _update_window_title(self, project_name):
|
||
"""更新窗口标题"""
|
||
try:
|
||
props = WindowProperties()
|
||
props.set_title(f"EG Engine - {project_name}")
|
||
self.win.request_properties(props)
|
||
print(f"窗口标题已更新: EG Engine - {project_name}")
|
||
except Exception as e:
|
||
print(f"更新窗口标题失败: {e}")
|
||
|
||
# ==================== 路径浏览器辅助方法 ====================
|
||
|
||
def _refresh_path_browser(self):
|
||
"""刷新路径浏览器内容"""
|
||
try:
|
||
self.path_browser_items = []
|
||
|
||
if not os.path.exists(self.path_browser_current_path):
|
||
self.add_error_message(f"路径不存在: {self.path_browser_current_path}")
|
||
return
|
||
|
||
# 获取目录中的所有项目
|
||
items = []
|
||
try:
|
||
for item_name in os.listdir(self.path_browser_current_path):
|
||
item_path = os.path.join(self.path_browser_current_path, item_name)
|
||
is_dir = os.path.isdir(item_path)
|
||
|
||
items.append({
|
||
'name': item_name,
|
||
'path': item_path,
|
||
'is_dir': is_dir
|
||
})
|
||
except PermissionError:
|
||
self.add_error_message(f"无法访问路径: {self.path_browser_current_path}")
|
||
return
|
||
|
||
# 排序:目录在前,文件在后,按名称排序
|
||
items.sort(key=lambda x: (not x['is_dir'], x['name'].lower()))
|
||
self.path_browser_items = items
|
||
|
||
except Exception as e:
|
||
self.add_error_message(f"刷新路径浏览器失败: {e}")
|
||
|
||
def _apply_selected_path(self):
|
||
"""应用选择的路径"""
|
||
try:
|
||
if self.path_browser_mode == "new_project":
|
||
# 新建项目模式:直接使用当前路径
|
||
self.new_project_path = self.path_browser_current_path
|
||
self.add_info_message(f"已选择项目路径: {self.new_project_path}")
|
||
elif self.path_browser_mode == "open_project":
|
||
# 打开项目模式:使用当前路径
|
||
self.open_project_path = self.path_browser_current_path
|
||
self.add_info_message(f"已选择项目路径: {self.open_project_path}")
|
||
except Exception as e:
|
||
self.add_error_message(f"应用路径失败: {e}")
|
||
|
||
demo = MyWorld()
|
||
demo.run()
|