ball-demo.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import pygame
  2. import math
  3. import time
  4. import os
  5. try:
  6. import pyperclip
  7. clipboard_available = True
  8. except ImportError:
  9. clipboard_available = False
  10. print("警告: pyperclip 模块未安装,剪贴板功能不可用。请运行 'pip install pyperclip' 安装。")
  11. from pygame.locals import *
  12. from OpenGL.GL import *
  13. from OpenGL.GLU import *
  14. # 模拟模式开关(True: 使用模拟数据,False: 扫描真实目录)
  15. # USE_MOCK_DATA = True
  16. USE_MOCK_DATA = False
  17. # 初始化 Pygame
  18. pygame.init()
  19. # 获取屏幕尺寸
  20. info = pygame.display.Info()
  21. screen_width = info.current_w
  22. screen_height = info.current_h
  23. # 设置窗口大小(高度为屏幕高度的80%,保持原始宽高比)
  24. height = int(screen_height * 0.8)
  25. width = int(height * (800 / 600)) # 保持原始宽高比 800:600
  26. display = pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL)
  27. pygame.display.set_caption('3D Yellow Ball')
  28. # 启用深度测试
  29. glEnable(GL_DEPTH_TEST)
  30. # 设置背景色
  31. glClearColor(0.1, 0.1, 0.1, 1.0)
  32. # 禁用混合,确保完全不透明
  33. glDisable(GL_BLEND)
  34. # 设置光照
  35. glEnable(GL_LIGHTING)
  36. glEnable(GL_LIGHT0)
  37. # 设置光源位置
  38. light_position = [2.0, 2.0, 2.0, 1.0] # 点光源
  39. glLightfv(GL_LIGHT0, GL_POSITION, light_position)
  40. # 设置光源颜色
  41. light_ambient = [0.3, 0.3, 0.3, 1.0]
  42. light_diffuse = [1.0, 1.0, 1.0, 1.0]
  43. light_specular = [1.0, 1.0, 1.0, 1.0]
  44. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
  45. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
  46. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular)
  47. # 设置投影矩阵(透视投影)
  48. glMatrixMode(GL_PROJECTION)
  49. glLoadIdentity()
  50. gluPerspective(45, (width/height), 0.1, 50.0)
  51. # 设置模型视图矩阵
  52. glMatrixMode(GL_MODELVIEW)
  53. glLoadIdentity()
  54. # 移动相机(现在在主循环中每帧设置)
  55. # glTranslatef(0.0, 0.0, -5)
  56. def generate_mock_tree():
  57. # 生成模拟目录树数据
  58. nodes = [
  59. ("root", True, -1, 0, "/mock/root"),
  60. ("dir1", True, 0, 1, "/mock/root/dir1"),
  61. ("dir2", True, 0, 1, "/mock/root/dir2"),
  62. ("file1.txt", False, 0, 1, "/mock/root/file1.txt"),
  63. ("file2.py", False, 0, 1, "/mock/root/file2.py"),
  64. ("subdir1", True, 1, 2, "/mock/root/dir1/subdir1"),
  65. ("file3.js", False, 1, 2, "/mock/root/dir1/file3.js"),
  66. ("file4.md", False, 2, 2, "/mock/root/dir2/file4.md"),
  67. ]
  68. edges = [
  69. (0, 1), (0, 2), (0, 3), (0, 4),
  70. (1, 5), (1, 6),
  71. (2, 7),
  72. ]
  73. # 构建子节点列表
  74. children = [[] for _ in range(len(nodes))]
  75. for parent_id, child_id in edges:
  76. children[parent_id].append(child_id)
  77. # 计算节点位置(使用原有算法)
  78. positions = []
  79. for i in range(len(nodes)):
  80. positions.append([0.0, 0.0, 0.0])
  81. # 计算最大深度
  82. max_depth_found = 0
  83. for node in nodes:
  84. max_depth_found = max(max_depth_found, node[3])
  85. # 递归计算位置函数
  86. def calculate_positions(node_id, start_angle, end_angle, depth):
  87. if depth > max_depth_found:
  88. return
  89. if node_id == 0: # 根节点
  90. positions[node_id] = [0.0, max_depth_found * 0.4, 0.0]
  91. else:
  92. parent_id = nodes[node_id][2]
  93. if parent_id >= 0:
  94. child_index = children[parent_id].index(node_id)
  95. num_siblings = len(children[parent_id])
  96. angle = start_angle + (end_angle - start_angle) * (child_index / max(num_siblings, 1))
  97. radius = 0.75 * (0.75 ** depth)
  98. px, py, pz = positions[parent_id]
  99. x = px + radius * math.cos(angle)
  100. y = py - 0.65
  101. z = pz + radius * math.sin(angle)
  102. positions[node_id] = [x, y, z]
  103. node_children = children[node_id]
  104. if node_children:
  105. angle_range = math.pi * 1.5
  106. angle_start = -angle_range / 2
  107. angle_step = angle_range / len(node_children)
  108. for i, child_id in enumerate(node_children):
  109. child_angle_start = angle_start + i * angle_step
  110. child_angle_end = angle_start + (i + 1) * angle_step
  111. calculate_positions(child_id, child_angle_start, child_angle_end, depth + 1)
  112. calculate_positions(0, -math.pi, math.pi, 0)
  113. return nodes, edges, positions, children
  114. def build_directory_tree(root_path, max_depth=3, max_children_per_node=30):
  115. # 如果启用模拟模式,返回模拟数据
  116. if USE_MOCK_DATA:
  117. return generate_mock_tree()
  118. # 递归扫描目录树
  119. nodes = [] # 每个节点: (name, is_dir, parent_id, depth)
  120. edges = [] # (parent_id, child_id)
  121. # 递归扫描函数
  122. def scan_directory(current_path, parent_id, depth):
  123. if depth >= max_depth:
  124. return
  125. try:
  126. entries = os.listdir(current_path)
  127. except Exception:
  128. return # 忽略所有访问错误
  129. # 过滤掉一些不需要的目录
  130. filtered_entries = []
  131. for entry in entries:
  132. # 过滤常见的版本控制和IDE目录
  133. if entry in ['.git', '.vscode', '.idea', 'node_modules', '__pycache__']:
  134. continue
  135. filtered_entries.append(entry)
  136. # 限制每个节点的最大子项数量
  137. filtered_entries = filtered_entries[:max_children_per_node]
  138. # 当前目录的节点ID:根节点为0,其他目录为parent_id
  139. current_node_id = 0 if parent_id == -1 else parent_id
  140. for entry in filtered_entries:
  141. entry_path = os.path.join(current_path, entry)
  142. is_dir = os.path.isdir(entry_path)
  143. # 添加子节点
  144. child_id = len(nodes)
  145. nodes.append((entry, is_dir, current_node_id, depth + 1, entry_path))
  146. edges.append((current_node_id, child_id))
  147. # 如果是目录,递归扫描
  148. if is_dir:
  149. scan_directory(entry_path, child_id, depth + 1)
  150. # 添加根节点
  151. root_name = os.path.basename(root_path.rstrip('\\'))
  152. nodes.append((root_name, True, -1, 0, root_path))
  153. # 从根节点开始递归扫描
  154. scan_directory(root_path, -1, 0)
  155. # 计算节点位置
  156. positions = []
  157. for i in range(len(nodes)):
  158. positions.append([0.0, 0.0, 0.0]) # 初始位置
  159. # 构建子节点列表
  160. children = [[] for _ in range(len(nodes))]
  161. for edge in edges:
  162. parent_id, child_id = edge
  163. children[parent_id].append(child_id)
  164. # 计算最大深度
  165. max_depth_found = 0
  166. for node in nodes:
  167. max_depth_found = max(max_depth_found, node[3])
  168. # 递归计算位置
  169. def calculate_positions(node_id, start_angle, end_angle, depth):
  170. if depth > max_depth_found:
  171. return
  172. # 计算当前节点位置
  173. if node_id == 0: # 根节点
  174. positions[node_id] = [0.0, max_depth_found * 0.4, 0.0] # 顶部,高度减半
  175. else:
  176. parent_id = nodes[node_id][2]
  177. if parent_id >= 0:
  178. # 子节点围绕父节点在圆上分布
  179. child_index = children[parent_id].index(node_id)
  180. num_siblings = len(children[parent_id])
  181. # 计算角度(在父节点周围的圆上)
  182. angle = start_angle + (end_angle - start_angle) * (child_index / max(num_siblings, 1))
  183. # 半径随深度减小
  184. radius = 0.75 * (0.75 ** depth) # 深度越大,半径越小,基础半径减半
  185. # 计算位置
  186. px, py, pz = positions[parent_id]
  187. x = px + radius * math.cos(angle)
  188. y = py - 0.65 # 每层向下移动,垂直距离减半
  189. z = pz + radius * math.sin(angle)
  190. positions[node_id] = [x, y, z]
  191. # 为子节点递归计算位置
  192. node_children = children[node_id]
  193. if node_children:
  194. # 计算子节点的角度范围
  195. angle_range = math.pi * 1.5 # 270度范围
  196. angle_start = -angle_range / 2
  197. angle_step = angle_range / len(node_children)
  198. for i, child_id in enumerate(node_children):
  199. child_angle_start = angle_start + i * angle_step
  200. child_angle_end = angle_start + (i + 1) * angle_step
  201. calculate_positions(child_id, child_angle_start, child_angle_end, depth + 1)
  202. # 从根节点开始计算位置
  203. calculate_positions(0, -math.pi, math.pi, 0)
  204. return nodes, edges, positions, children
  205. # 构建目录树(使用当前目录)
  206. # root_path = "."
  207. root_path = "E:\\agricultural_research_platform"
  208. tree_nodes, tree_edges, node_positions, tree_children = build_directory_tree(root_path)
  209. # 初始化选中节点(根节点)
  210. selected_node_index = 0
  211. # 剪贴板相关变量
  212. clipboard_content = ""
  213. clipboard_check_time = 0
  214. blinking_nodes = set()
  215. file_content_cache = {}
  216. blink_start_time = pygame.time.get_ticks()
  217. # 旋转控制变量
  218. rotation_angle = 0.0
  219. # 主循环
  220. running = True
  221. while running:
  222. # 获取当前时间(用于闪烁效果)
  223. current_time = pygame.time.get_ticks()
  224. # 更新旋转角度(每10秒旋转一圈)
  225. rotation_angle = (current_time * 0.036) % 360.0
  226. # 检查剪贴板内容是否变化(如果剪贴板功能可用)
  227. if clipboard_available:
  228. if current_time - clipboard_check_time > 1000: # 每秒检查一次
  229. clipboard_check_time = current_time
  230. try:
  231. new_clipboard_content = pyperclip.paste()
  232. if new_clipboard_content != clipboard_content:
  233. clipboard_content = new_clipboard_content
  234. # 剪贴板内容变化,重新检查所有文件节点
  235. blinking_nodes.clear()
  236. if clipboard_content.strip(): # 剪贴板非空
  237. for i, (name, is_dir, parent_id, depth, full_path) in enumerate(tree_nodes):
  238. if not is_dir: # 文件节点
  239. # 检查文件内容是否包含剪贴板文本
  240. try:
  241. # 从缓存读取或加载文件内容
  242. if i not in file_content_cache:
  243. with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
  244. file_content_cache[i] = f.read()
  245. file_content = file_content_cache[i]
  246. if clipboard_content in file_content:
  247. blinking_nodes.add(i)
  248. except Exception as e:
  249. pass # 忽略读取错误
  250. except Exception as e:
  251. pass # 忽略剪贴板访问错误
  252. # 处理事件
  253. for event in pygame.event.get():
  254. if event.type == pygame.QUIT:
  255. running = False
  256. elif event.type == pygame.KEYDOWN:
  257. # 获取当前节点信息
  258. current_node = tree_nodes[selected_node_index]
  259. current_parent_id = current_node[2]
  260. current_depth = current_node[3]
  261. if event.key == pygame.K_UP: # 上键:移动到父节点
  262. if current_parent_id >= 0: # 有父节点
  263. selected_node_index = current_parent_id
  264. elif event.key == pygame.K_DOWN: # 下键:移动到第一个子节点
  265. if tree_children[selected_node_index]: # 有子节点
  266. selected_node_index = tree_children[selected_node_index][0]
  267. elif event.key == pygame.K_LEFT: # 左键:移动到前一个兄弟节点
  268. if current_parent_id >= 0: # 有父节点
  269. siblings = tree_children[current_parent_id]
  270. if len(siblings) > 1:
  271. current_index_in_siblings = siblings.index(selected_node_index)
  272. # 移动到前一个兄弟,如果当前是第一个则移动到最后一个
  273. new_index = (current_index_in_siblings - 1) % len(siblings)
  274. selected_node_index = siblings[new_index]
  275. elif event.key == pygame.K_RIGHT: # 右键:移动到后一个兄弟节点
  276. if current_parent_id >= 0: # 有父节点
  277. siblings = tree_children[current_parent_id]
  278. if len(siblings) > 1:
  279. current_index_in_siblings = siblings.index(selected_node_index)
  280. # 移动到后一个兄弟,如果当前是最后一个则移动到第一个
  281. new_index = (current_index_in_siblings + 1) % len(siblings)
  282. selected_node_index = siblings[new_index]
  283. # 清除屏幕
  284. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  285. # 重置模型视图矩阵并设置相机位置
  286. glLoadIdentity()
  287. glTranslatef(0.0, 0.0, -5) # 相机向后移动
  288. glRotatef(rotation_angle, 0.0, 1.0, 0.0) # 绕竖直中线/Y轴旋转
  289. # 目录树可视化
  290. ball_radius = 0.0667 # 节点球的半径(原来的1/3)
  291. # 绘制所有连线(目录树边)
  292. glDisable(GL_LIGHTING) # 禁用光照以使用纯色
  293. glColor3f(1.0, 1.0, 1.0) # 白色连线
  294. glLineWidth(2.0)
  295. glBegin(GL_LINES)
  296. for edge in tree_edges:
  297. node1_idx, node2_idx = edge
  298. x1, y1, z1 = node_positions[node1_idx]
  299. x2, y2, z2 = node_positions[node2_idx]
  300. glVertex3f(x1, y1, z1)
  301. glVertex3f(x2, y2, z2)
  302. glEnd()
  303. glEnable(GL_LIGHTING) # 重新启用光照
  304. # 绘制所有节点球(根据类型使用不同颜色)
  305. for i, (name, is_dir, parent_id, depth, full_path) in enumerate(tree_nodes):
  306. x, y, z = node_positions[i]
  307. # 根据节点类型设置材质颜色(选中节点显示为红色)
  308. if i in blinking_nodes: # 闪烁节点
  309. # 计算闪烁因子(正弦波,周期约2秒)
  310. blink_factor = (math.sin((current_time - blink_start_time) * 0.005) + 1) * 0.5
  311. # 基础颜色 #c31c1f
  312. base_r, base_g, base_b = 0.7647, 0.1098, 0.1216
  313. # 亮度在0.7到1.0之间变化
  314. brightness = 0.7 + 0.3 * blink_factor
  315. color = [base_r * brightness, base_g * brightness, base_b * brightness, 1.0]
  316. ambient = [base_r * 0.5, base_g * 0.5, base_b * 0.5, 1.0]
  317. specular = [base_r * 0.8, base_g * 0.8, base_b * 0.8, 1.0]
  318. elif i == selected_node_index: # 选中节点
  319. color = [1.0, 0.0, 0.0, 1.0] # 红色
  320. ambient = [0.5, 0.0, 0.0, 1.0]
  321. specular = [0.8, 0.3, 0.3, 1.0]
  322. elif i == 0: # 根节点
  323. color = [1.0, 1.0, 0.0, 1.0] # 黄色
  324. ambient = [0.5, 0.5, 0.0, 1.0]
  325. specular = [0.8, 0.8, 0.4, 1.0]
  326. elif is_dir: # 目录
  327. color = [0.2902, 0.5961, 0.3098, 1.0] # #4a984f
  328. ambient = [0.2, 0.4, 0.2, 1.0]
  329. specular = [0.4, 0.8, 0.4, 1.0]
  330. else: # 文件
  331. color = [0.7059, 0.7176, 0.2549, 1.0] # #b4b741
  332. ambient = [0.4, 0.4, 0.15, 1.0]
  333. specular = [0.8, 0.8, 0.4, 1.0]
  334. shininess = [30.0]
  335. glMaterialfv(GL_FRONT, GL_AMBIENT, ambient)
  336. glMaterialfv(GL_FRONT, GL_DIFFUSE, color)
  337. glMaterialfv(GL_FRONT, GL_SPECULAR, specular)
  338. glMaterialfv(GL_FRONT, GL_SHININESS, shininess)
  339. glPushMatrix()
  340. glTranslatef(x, y, z)
  341. quad = gluNewQuadric()
  342. gluQuadricNormals(quad, GLU_SMOOTH)
  343. # 根据深度调整球体大小:深度越大,球体越小
  344. current_radius = ball_radius * (0.85 ** depth)
  345. gluSphere(quad, current_radius, 32, 32)
  346. gluDeleteQuadric(quad)
  347. glPopMatrix()
  348. # 交换缓冲区
  349. pygame.display.flip()
  350. # 控制帧率
  351. pygame.time.wait(10)
  352. # 退出 Pygame
  353. pygame.quit()