Skip to content

Commit 1472091

Browse files
committed
0.0.4
tiny but huge update!
1 parent b00c637 commit 1472091

File tree

10 files changed

+107
-8
lines changed

10 files changed

+107
-8
lines changed

code_editor.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pygame
2+
from pygame_texteditor import TextEditor
3+
4+
# minimal pygame setup
5+
pygame.init()
6+
screen = pygame.display.set_mode((500, 600))
7+
pygame.display.set_caption("Pygame")
8+
pygame.display.get_surface().fill((200, 200, 200)) # background coloring
9+
10+
# Instantiation & customization of the text editor
11+
TX = TextEditor(
12+
offset_x=50, offset_y=50, editor_width=500, editor_height=400, screen=pygame.display.get_surface()
13+
)
14+
TX.set_line_numbers(True)
15+
TX.set_syntax_highlighting(True)
16+
TX.set_font_size(36)
17+
18+
# TextEditor in the pygame-loop
19+
while True:
20+
# INPUT - Mouse + Keyboard
21+
pygame_events = pygame.event.get()
22+
pressed_keys = pygame.key.get_pressed()
23+
mouse_x, mouse_y = pygame.mouse.get_pos()
24+
mouse_pressed = pygame.mouse.get_pressed()
25+
26+
# displays editor functionality once per loop
27+
TX.display_editor(pygame_events, pressed_keys, mouse_x, mouse_y, mouse_pressed)
28+
pygame.display.flip() # updates pygame window

main.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# 基于pygame的图形化CAD编辑器
22
# made by Gaoxuan
33

4-
import pygame, sys
4+
import pygame, sys, os
55
from pygame.locals import *
66

7+
os.chdir(os.path.dirname(__file__))
78

89
mainwindow = pygame.display.set_mode((1600, 900), pygame.RESIZABLE, 32)
910
pygame.display.set_caption('PyCAD')
10-
icon = pygame.image.load('./resources/icon.png')
11+
icon = pygame.image.load('./resources/pic/icon.png')
1112
pygame.display.set_icon(icon)
1213

1314
WHITE = (255, 255, 255)
@@ -20,29 +21,45 @@
2021
pygame.init()
2122
fpsClock = pygame.time.Clock()
2223

24+
bar_h = 36
2325
bar_font_file = pygame.font.match_font('SimHei')
2426
bar_font = pygame.font.Font(bar_font_file, 20)
25-
2627
menu_bar_tabs = ['文件', '编辑']
2728
tab_interval = 10
2829

2930

3031
def 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

4041
def 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+
4663
def 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

5471
def 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+
6078
def 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()
6690
while 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()

resources/icon-2.png

13.2 KB
Loading

resources/icon.png

-10.2 KB
Loading

resources/pic/icon.png

250 KB
Loading

resources/tools/icon.png

119 KB
Loading

resources/tools/test1.png

3.06 KB
Loading

resources/tools/test2.png

3.06 KB
Loading

resources/tools/test3.png

3.06 KB
Loading

test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# pg setup
2+
import pygame as pg
3+
4+
pg.init()
5+
screen = pg.display.set_mode([600, 400])
6+
pg.display.set_caption("Example code for the cursors module")
7+
8+
# create a system cursor
9+
system = pg.cursors.Cursor(pg.SYSTEM_CURSOR_SIZENWSE)
10+
11+
# create bitmap cursors
12+
# pg.cursors.broken_x
13+
bitmap_1 = pg.cursors.Cursor(*pg.cursors.tri_left)
14+
# cursor = pg.cursors.compile(pg.cursors.textmarker_strings)
15+
# pg.mouse.set_cursor((8, 16), (0, 0), *cursor)
16+
17+
# bitmap_2 = pg.cursors.compile(pg.cursors.sizer_x_strings)
18+
19+
# create a color cursor
20+
surf = pg.Surface((40, 40)) # you could also load an image
21+
surf.fill((120, 50, 50)) # and use that as your surface
22+
color = pg.cursors.Cursor((20, 20), surf)
23+
24+
cursors = [system, bitmap_1, bitmap_2, color]
25+
cursor_index = 0
26+
27+
pg.mouse.set_cursor(system)
28+
29+
clock = pg.time.Clock()
30+
going = True
31+
while going:
32+
clock.tick(60)
33+
screen.fill((0, 75, 30))
34+
pg.display.flip()
35+
36+
for event in pg.event.get():
37+
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
38+
going = False
39+
40+
# if the mouse is clicked it will switch to a new cursor
41+
if event.type == pg.MOUSEBUTTONDOWN:
42+
cursor_index += 1
43+
cursor_index %= len(cursors)
44+
pg.mouse.set_cursor(cursors[cursor_index])
45+
46+
pg.quit()

0 commit comments

Comments
 (0)