11# 基于pygame的图形化CAD编辑器
22# made by Gaoxuan
33
4- import pygame , sys
4+ import pygame , sys , os
55from pygame .locals import *
66
7+ os .chdir (os .path .dirname (__file__ ))
78
89mainwindow = pygame .display .set_mode ((1600 , 900 ), pygame .RESIZABLE , 32 )
910pygame .display .set_caption ('PyCAD' )
10- icon = pygame .image .load ('./resources/icon.png' )
11+ icon = pygame .image .load ('./resources/pic/ icon.png' )
1112pygame .display .set_icon (icon )
1213
1314WHITE = (255 , 255 , 255 )
2021pygame .init ()
2122fpsClock = pygame .time .Clock ()
2223
24+ bar_h = 36
2325bar_font_file = pygame .font .match_font ('SimHei' )
2426bar_font = pygame .font .Font (bar_font_file , 20 )
25-
2627menu_bar_tabs = ['文件' , '编辑' ]
2728tab_interval = 10
2829
2930
3031def display_menu_bar_tabs ():
31- tabx = tab_interval
32+ tab_x = tab_interval
3233 for tab_text in menu_bar_tabs :
3334 tab_text_surface = bar_font .render (tab_text , True , BLACK , GREEN )
3435 tab_rect = tab_text_surface .get_rect ()
35- tab_rect .topleft = (tabx , 8 )
36- tabx = tabx + tab_rect .width + tab_interval
36+ tab_rect .topleft = (tab_x , 8 )
37+ tab_x = tab_x + tab_rect .width + tab_interval
3738 mainwindow .blit (tab_text_surface , tab_rect )
3839
3940
4041def display_menu_bar ():
41- menu_bar_rect = (0 , 0 , mainwindow_w , 36 )
42+ menu_bar_rect = (0 , 0 , mainwindow_w , bar_h )
4243 pygame .draw .rect (mainwindow , BLUE , menu_bar_rect )
4344 display_menu_bar_tabs ()
4445
4546
47+ bar_interval = 3
48+ tool_bar_tabs = []
49+ tool_tab_img = [pygame .image .load ('./resources/tools/%s' % tab_img_name ) for tab_img_name in tool_bar_tabs ]
50+
51+ def display_tool_bar_tabs ():
52+ tab_x = tab_interval
53+ for tab_img in tool_tab_img :
54+ mainwindow .blit (tab_img , (tab_x , bar_h + bar_interval + 2 ))
55+ tab_x = tab_x + tab_img .get_width ()+ tab_interval
56+
57+ def display_tool_bar ():
58+ tool_bar_rect = (0 , bar_h + bar_interval , mainwindow_w , bar_h )
59+ pygame .draw .rect (mainwindow , BLUE , tool_bar_rect )
60+ display_tool_bar_tabs ()
61+
62+
4663def display_xy_status ():
4764 xy_status = 'x:%d y:%d' % (mouse_x , mouse_y )
4865 tab_text_surface = bar_font .render (xy_status , True , BLACK , YELLOW )
@@ -52,23 +69,31 @@ def display_xy_status():
5269
5370
5471def display_status_bar ():
55- status_bar_rect = (0 , mainwindow_h - 36 , mainwindow_w , 36 )
72+ status_bar_rect = (0 , mainwindow_h - bar_h , mainwindow_w , bar_h )
5673 pygame .draw .rect (mainwindow , RED , status_bar_rect )
5774 display_xy_status ()
5875
5976
77+
6078def display_main_window ():
6179 mainwindow .fill (WHITE )
6280 display_menu_bar ()
6381 display_status_bar ()
82+ display_tool_bar ()
83+
84+ def init ():
85+ cursor = pygame .cursors .Cursor (pygame .SYSTEM_CURSOR_CROSSHAIR )
86+ pygame .mouse .set_cursor (cursor )
6487
6588
89+ init ()
6690while True :
6791 mainwindow_w , mainwindow_h = pygame .display .get_surface ().get_size ()
6892 mouse_x , mouse_y = pygame .mouse .get_pos ()
6993 display_main_window ()
7094 for event in pygame .event .get ():
7195 if event .type == QUIT :
96+ print ("exit with 0" )
7297 pygame .quit ()
7398 sys .exit ()
7499 pygame .display .update ()
0 commit comments