ball-demo.py 13 KB

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