ball-demo.py 13 KB

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