|
|
@@ -13,6 +13,10 @@ from pygame.locals import *
|
|
|
from OpenGL.GL import *
|
|
|
from OpenGL.GLU import *
|
|
|
|
|
|
+# 模拟模式开关(True: 使用模拟数据,False: 扫描真实目录)
|
|
|
+# USE_MOCK_DATA = True
|
|
|
+USE_MOCK_DATA = False
|
|
|
+
|
|
|
# 初始化 Pygame
|
|
|
pygame.init()
|
|
|
|
|
|
@@ -62,7 +66,78 @@ glLoadIdentity()
|
|
|
# 移动相机(现在在主循环中每帧设置)
|
|
|
# glTranslatef(0.0, 0.0, -5)
|
|
|
|
|
|
+def generate_mock_tree():
|
|
|
+ # 生成模拟目录树数据
|
|
|
+ nodes = [
|
|
|
+ ("root", True, -1, 0, "/mock/root"),
|
|
|
+ ("dir1", True, 0, 1, "/mock/root/dir1"),
|
|
|
+ ("dir2", True, 0, 1, "/mock/root/dir2"),
|
|
|
+ ("file1.txt", False, 0, 1, "/mock/root/file1.txt"),
|
|
|
+ ("file2.py", False, 0, 1, "/mock/root/file2.py"),
|
|
|
+ ("subdir1", True, 1, 2, "/mock/root/dir1/subdir1"),
|
|
|
+ ("file3.js", False, 1, 2, "/mock/root/dir1/file3.js"),
|
|
|
+ ("file4.md", False, 2, 2, "/mock/root/dir2/file4.md"),
|
|
|
+ ]
|
|
|
+
|
|
|
+ edges = [
|
|
|
+ (0, 1), (0, 2), (0, 3), (0, 4),
|
|
|
+ (1, 5), (1, 6),
|
|
|
+ (2, 7),
|
|
|
+ ]
|
|
|
+
|
|
|
+ # 构建子节点列表
|
|
|
+ children = [[] for _ in range(len(nodes))]
|
|
|
+ for parent_id, child_id in edges:
|
|
|
+ children[parent_id].append(child_id)
|
|
|
+
|
|
|
+ # 计算节点位置(使用原有算法)
|
|
|
+ positions = []
|
|
|
+ for i in range(len(nodes)):
|
|
|
+ positions.append([0.0, 0.0, 0.0])
|
|
|
+
|
|
|
+ # 计算最大深度
|
|
|
+ max_depth_found = 0
|
|
|
+ for node in nodes:
|
|
|
+ max_depth_found = max(max_depth_found, node[3])
|
|
|
+
|
|
|
+ # 递归计算位置函数
|
|
|
+ def calculate_positions(node_id, start_angle, end_angle, depth):
|
|
|
+ if depth > max_depth_found:
|
|
|
+ return
|
|
|
+
|
|
|
+ if node_id == 0: # 根节点
|
|
|
+ positions[node_id] = [0.0, max_depth_found * 0.4, 0.0]
|
|
|
+ else:
|
|
|
+ parent_id = nodes[node_id][2]
|
|
|
+ if parent_id >= 0:
|
|
|
+ child_index = children[parent_id].index(node_id)
|
|
|
+ num_siblings = len(children[parent_id])
|
|
|
+ angle = start_angle + (end_angle - start_angle) * (child_index / max(num_siblings, 1))
|
|
|
+ radius = 0.75 * (0.75 ** depth)
|
|
|
+ px, py, pz = positions[parent_id]
|
|
|
+ x = px + radius * math.cos(angle)
|
|
|
+ y = py - 0.65
|
|
|
+ z = pz + radius * math.sin(angle)
|
|
|
+ positions[node_id] = [x, y, z]
|
|
|
+
|
|
|
+ node_children = children[node_id]
|
|
|
+ if node_children:
|
|
|
+ angle_range = math.pi * 1.5
|
|
|
+ angle_start = -angle_range / 2
|
|
|
+ angle_step = angle_range / len(node_children)
|
|
|
+ for i, child_id in enumerate(node_children):
|
|
|
+ child_angle_start = angle_start + i * angle_step
|
|
|
+ child_angle_end = angle_start + (i + 1) * angle_step
|
|
|
+ calculate_positions(child_id, child_angle_start, child_angle_end, depth + 1)
|
|
|
+
|
|
|
+ calculate_positions(0, -math.pi, math.pi, 0)
|
|
|
+ return nodes, edges, positions, children
|
|
|
+
|
|
|
def build_directory_tree(root_path, max_depth=3, max_children_per_node=30):
|
|
|
+ # 如果启用模拟模式,返回模拟数据
|
|
|
+ if USE_MOCK_DATA:
|
|
|
+ return generate_mock_tree()
|
|
|
+
|
|
|
# 递归扫描目录树
|
|
|
nodes = [] # 每个节点: (name, is_dir, parent_id, depth)
|
|
|
edges = [] # (parent_id, child_id)
|
|
|
@@ -74,8 +149,8 @@ def build_directory_tree(root_path, max_depth=3, max_children_per_node=30):
|
|
|
|
|
|
try:
|
|
|
entries = os.listdir(current_path)
|
|
|
- except (PermissionError, FileNotFoundError):
|
|
|
- return
|
|
|
+ except Exception:
|
|
|
+ return # 忽略所有访问错误
|
|
|
|
|
|
# 过滤掉一些不需要的目录
|
|
|
filtered_entries = []
|
|
|
@@ -174,8 +249,10 @@ def build_directory_tree(root_path, max_depth=3, max_children_per_node=30):
|
|
|
|
|
|
return nodes, edges, positions, children
|
|
|
|
|
|
-# 构建目录树
|
|
|
+# 构建目录树(使用当前目录)
|
|
|
+# root_path = "."
|
|
|
root_path = "E:\\agricultural_research_platform"
|
|
|
+
|
|
|
tree_nodes, tree_edges, node_positions, tree_children = build_directory_tree(root_path)
|
|
|
|
|
|
# 初始化选中节点(根节点)
|