ball-demo.py 12 KB

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