ball-demo.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. # 初始化 Pygame
  15. pygame.init()
  16. # 设置窗口大小
  17. width, height = 800, 600
  18. display = pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL)
  19. pygame.display.set_caption('3D Yellow Ball')
  20. # 启用深度测试
  21. glEnable(GL_DEPTH_TEST)
  22. # 设置背景色
  23. glClearColor(0.1, 0.1, 0.1, 1.0)
  24. # 设置光照
  25. glEnable(GL_LIGHTING)
  26. glEnable(GL_LIGHT0)
  27. # 设置光源位置
  28. light_position = [2.0, 2.0, 2.0, 1.0] # 点光源
  29. glLightfv(GL_LIGHT0, GL_POSITION, light_position)
  30. # 设置光源颜色
  31. light_ambient = [0.3, 0.3, 0.3, 1.0]
  32. light_diffuse = [1.0, 1.0, 1.0, 1.0]
  33. light_specular = [1.0, 1.0, 1.0, 1.0]
  34. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
  35. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
  36. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular)
  37. # 设置视角
  38. gluPerspective(45, (width/height), 0.1, 50.0)
  39. # 移动相机
  40. glTranslatef(0.0, 0.0, -5)
  41. def build_directory_tree(root_path, max_depth=3, max_children_per_node=30):
  42. # 递归扫描目录树
  43. nodes = [] # 每个节点: (name, is_dir, parent_id, depth)
  44. edges = [] # (parent_id, child_id)
  45. # 递归扫描函数
  46. def scan_directory(current_path, parent_id, depth):
  47. if depth >= max_depth:
  48. return
  49. try:
  50. entries = os.listdir(current_path)
  51. except (PermissionError, FileNotFoundError):
  52. return
  53. # 过滤掉一些不需要的目录
  54. filtered_entries = []
  55. for entry in entries:
  56. # 过滤常见的版本控制和IDE目录
  57. if entry in ['.git', '.vscode', '.idea', 'node_modules', '__pycache__']:
  58. continue
  59. filtered_entries.append(entry)
  60. # 限制每个节点的最大子项数量
  61. filtered_entries = filtered_entries[:max_children_per_node]
  62. # 当前目录的节点ID:根节点为0,其他目录为parent_id
  63. current_node_id = 0 if parent_id == -1 else parent_id
  64. for entry in filtered_entries:
  65. entry_path = os.path.join(current_path, entry)
  66. is_dir = os.path.isdir(entry_path)
  67. # 添加子节点
  68. child_id = len(nodes)
  69. nodes.append((entry, is_dir, current_node_id, depth + 1, entry_path))
  70. edges.append((current_node_id, child_id))
  71. # 如果是目录,递归扫描
  72. if is_dir:
  73. scan_directory(entry_path, child_id, depth + 1)
  74. # 添加根节点
  75. root_name = os.path.basename(root_path.rstrip('\\'))
  76. nodes.append((root_name, True, -1, 0, root_path))
  77. # 从根节点开始递归扫描
  78. scan_directory(root_path, -1, 0)
  79. # 计算节点位置
  80. positions = []
  81. for i in range(len(nodes)):
  82. positions.append([0.0, 0.0, 0.0]) # 初始位置
  83. # 构建子节点列表
  84. children = [[] for _ in range(len(nodes))]
  85. for edge in edges:
  86. parent_id, child_id = edge
  87. children[parent_id].append(child_id)
  88. # 计算最大深度
  89. max_depth_found = 0
  90. for node in nodes:
  91. max_depth_found = max(max_depth_found, node[3])
  92. # 递归计算位置
  93. def calculate_positions(node_id, start_angle, end_angle, depth):
  94. if depth > max_depth_found:
  95. return
  96. # 计算当前节点位置
  97. if node_id == 0: # 根节点
  98. positions[node_id] = [0.0, max_depth_found * 0.8, 0.0] # 顶部
  99. else:
  100. parent_id = nodes[node_id][2]
  101. if parent_id >= 0:
  102. # 子节点围绕父节点在圆上分布
  103. child_index = children[parent_id].index(node_id)
  104. num_siblings = len(children[parent_id])
  105. # 计算角度(在父节点周围的圆上)
  106. angle = start_angle + (end_angle - start_angle) * (child_index / max(num_siblings, 1))
  107. # 半径随深度减小
  108. radius = 1.5 * (0.75 ** depth) # 深度越大,半径越小
  109. # 计算位置
  110. px, py, pz = positions[parent_id]
  111. x = px + radius * math.cos(angle)
  112. y = py - 1.3 # 每层向下移动
  113. z = pz + radius * math.sin(angle)
  114. positions[node_id] = [x, y, z]
  115. # 为子节点递归计算位置
  116. node_children = children[node_id]
  117. if node_children:
  118. # 计算子节点的角度范围
  119. angle_range = math.pi * 1.5 # 270度范围
  120. angle_start = -angle_range / 2
  121. angle_step = angle_range / len(node_children)
  122. for i, child_id in enumerate(node_children):
  123. child_angle_start = angle_start + i * angle_step
  124. child_angle_end = angle_start + (i + 1) * angle_step
  125. calculate_positions(child_id, child_angle_start, child_angle_end, depth + 1)
  126. # 从根节点开始计算位置
  127. calculate_positions(0, -math.pi, math.pi, 0)
  128. return nodes, edges, positions, children
  129. # 构建目录树
  130. root_path = "E:\\agricultural_research_platform"
  131. tree_nodes, tree_edges, node_positions, tree_children = build_directory_tree(root_path)
  132. # 初始化选中节点(根节点)
  133. selected_node_index = 0
  134. # 剪贴板相关变量
  135. clipboard_content = ""
  136. clipboard_check_time = 0
  137. blinking_nodes = set()
  138. file_content_cache = {}
  139. blink_start_time = pygame.time.get_ticks()
  140. # 主循环
  141. running = True
  142. while running:
  143. # 获取当前时间(用于闪烁效果)
  144. current_time = pygame.time.get_ticks()
  145. # 检查剪贴板内容是否变化(如果剪贴板功能可用)
  146. if clipboard_available:
  147. if current_time - clipboard_check_time > 1000: # 每秒检查一次
  148. clipboard_check_time = current_time
  149. try:
  150. new_clipboard_content = pyperclip.paste()
  151. if new_clipboard_content != clipboard_content:
  152. clipboard_content = new_clipboard_content
  153. # 剪贴板内容变化,重新检查所有文件节点
  154. blinking_nodes.clear()
  155. if clipboard_content.strip(): # 剪贴板非空
  156. for i, (name, is_dir, parent_id, depth, full_path) in enumerate(tree_nodes):
  157. if not is_dir: # 文件节点
  158. # 检查文件内容是否包含剪贴板文本
  159. try:
  160. # 从缓存读取或加载文件内容
  161. if i not in file_content_cache:
  162. with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
  163. file_content_cache[i] = f.read()
  164. file_content = file_content_cache[i]
  165. if clipboard_content in file_content:
  166. blinking_nodes.add(i)
  167. except Exception as e:
  168. pass # 忽略读取错误
  169. except Exception as e:
  170. pass # 忽略剪贴板访问错误
  171. # 处理事件
  172. for event in pygame.event.get():
  173. if event.type == pygame.QUIT:
  174. running = False
  175. elif event.type == pygame.KEYDOWN:
  176. # 获取当前节点信息
  177. current_node = tree_nodes[selected_node_index]
  178. current_parent_id = current_node[2]
  179. current_depth = current_node[3]
  180. if event.key == pygame.K_UP: # 上键:移动到父节点
  181. if current_parent_id >= 0: # 有父节点
  182. selected_node_index = current_parent_id
  183. elif event.key == pygame.K_DOWN: # 下键:移动到第一个子节点
  184. if tree_children[selected_node_index]: # 有子节点
  185. selected_node_index = tree_children[selected_node_index][0]
  186. elif event.key == pygame.K_LEFT: # 左键:移动到前一个兄弟节点
  187. if current_parent_id >= 0: # 有父节点
  188. siblings = tree_children[current_parent_id]
  189. if len(siblings) > 1:
  190. current_index_in_siblings = siblings.index(selected_node_index)
  191. # 移动到前一个兄弟,如果当前是第一个则移动到最后一个
  192. new_index = (current_index_in_siblings - 1) % len(siblings)
  193. selected_node_index = siblings[new_index]
  194. elif event.key == pygame.K_RIGHT: # 右键:移动到后一个兄弟节点
  195. if current_parent_id >= 0: # 有父节点
  196. siblings = tree_children[current_parent_id]
  197. if len(siblings) > 1:
  198. current_index_in_siblings = siblings.index(selected_node_index)
  199. # 移动到后一个兄弟,如果当前是最后一个则移动到第一个
  200. new_index = (current_index_in_siblings + 1) % len(siblings)
  201. selected_node_index = siblings[new_index]
  202. # 清除屏幕
  203. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  204. # 目录树可视化
  205. ball_radius = 0.0667 # 节点球的半径(原来的1/3)
  206. # 绘制所有连线(目录树边)
  207. glDisable(GL_LIGHTING) # 禁用光照以使用纯色
  208. glColor3f(1.0, 1.0, 1.0) # 白色连线
  209. glLineWidth(2.0)
  210. glBegin(GL_LINES)
  211. for edge in tree_edges:
  212. node1_idx, node2_idx = edge
  213. x1, y1, z1 = node_positions[node1_idx]
  214. x2, y2, z2 = node_positions[node2_idx]
  215. glVertex3f(x1, y1, z1)
  216. glVertex3f(x2, y2, z2)
  217. glEnd()
  218. glEnable(GL_LIGHTING) # 重新启用光照
  219. # 绘制所有节点球(根据类型使用不同颜色)
  220. for i, (name, is_dir, parent_id, depth, full_path) in enumerate(tree_nodes):
  221. x, y, z = node_positions[i]
  222. # 根据节点类型设置材质颜色(选中节点显示为红色)
  223. if i in blinking_nodes: # 闪烁节点
  224. # 计算闪烁因子(正弦波,周期约2秒)
  225. blink_factor = (math.sin((current_time - blink_start_time) * 0.005) + 1) * 0.5
  226. # 颜色在黄色(1,1,0)和红色(1,0,0)之间变化
  227. color = [1.0, blink_factor, 0.0, 1.0]
  228. ambient = [0.3, blink_factor * 0.3, 0.0, 1.0]
  229. specular = [1.0, blink_factor * 0.5, 0.5, 1.0]
  230. elif i == selected_node_index: # 选中节点
  231. color = [1.0, 0.0, 0.0, 1.0] # 红色
  232. ambient = [0.3, 0.0, 0.0, 1.0]
  233. specular = [1.0, 0.5, 0.5, 1.0]
  234. elif i == 0: # 根节点
  235. color = [1.0, 1.0, 0.0, 1.0] # 黄色
  236. ambient = [0.3, 0.3, 0.0, 1.0]
  237. specular = [1.0, 1.0, 0.5, 1.0]
  238. elif is_dir: # 目录
  239. color = [0.0, 0.5, 1.0, 1.0] # 蓝色
  240. ambient = [0.0, 0.2, 0.3, 1.0]
  241. specular = [0.5, 0.7, 1.0, 1.0]
  242. else: # 文件
  243. color = [0.0, 0.8, 1.0, 1.0] # 亮蓝色
  244. ambient = [0.0, 0.1, 0.2, 1.0]
  245. specular = [0.6, 0.9, 1.0, 1.0]
  246. shininess = [50.0]
  247. glMaterialfv(GL_FRONT, GL_AMBIENT, ambient)
  248. glMaterialfv(GL_FRONT, GL_DIFFUSE, color)
  249. glMaterialfv(GL_FRONT, GL_SPECULAR, specular)
  250. glMaterialfv(GL_FRONT, GL_SHININESS, shininess)
  251. glPushMatrix()
  252. glTranslatef(x, y, z)
  253. quad = gluNewQuadric()
  254. gluQuadricNormals(quad, GLU_SMOOTH)
  255. # 根据深度调整球体大小:深度越大,球体越小
  256. current_radius = ball_radius * (0.85 ** depth)
  257. gluSphere(quad, current_radius, 32, 32)
  258. gluDeleteQuadric(quad)
  259. glPopMatrix()
  260. # 交换缓冲区
  261. pygame.display.flip()
  262. # 控制帧率
  263. pygame.time.wait(10)
  264. # 退出 Pygame
  265. pygame.quit()