Skip to content

Commit

Permalink
0.0.4
Browse files Browse the repository at this point in the history
tiny but huge update!
  • Loading branch information
speedyGaoxuan committed Apr 26, 2024
1 parent b00c637 commit 1472091
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 8 deletions.
28 changes: 28 additions & 0 deletions code_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pygame
from pygame_texteditor import TextEditor

# minimal pygame setup
pygame.init()
screen = pygame.display.set_mode((500, 600))
pygame.display.set_caption("Pygame")
pygame.display.get_surface().fill((200, 200, 200)) # background coloring

# Instantiation & customization of the text editor
TX = TextEditor(
offset_x=50, offset_y=50, editor_width=500, editor_height=400, screen=pygame.display.get_surface()
)
TX.set_line_numbers(True)
TX.set_syntax_highlighting(True)
TX.set_font_size(36)

# TextEditor in the pygame-loop
while True:
# INPUT - Mouse + Keyboard
pygame_events = pygame.event.get()
pressed_keys = pygame.key.get_pressed()
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()

# displays editor functionality once per loop
TX.display_editor(pygame_events, pressed_keys, mouse_x, mouse_y, mouse_pressed)
pygame.display.flip() # updates pygame window
41 changes: 33 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# 基于pygame的图形化CAD编辑器
# made by Gaoxuan

import pygame, sys
import pygame, sys, os
from pygame.locals import *

os.chdir(os.path.dirname(__file__))

mainwindow = pygame.display.set_mode((1600, 900), pygame.RESIZABLE, 32)
pygame.display.set_caption('PyCAD')
icon = pygame.image.load('./resources/icon.png')
icon = pygame.image.load('./resources/pic/icon.png')
pygame.display.set_icon(icon)

WHITE = (255, 255, 255)
Expand All @@ -20,29 +21,45 @@
pygame.init()
fpsClock = pygame.time.Clock()

bar_h = 36
bar_font_file = pygame.font.match_font('SimHei')
bar_font = pygame.font.Font(bar_font_file, 20)

menu_bar_tabs = ['文件', '编辑']
tab_interval = 10


def display_menu_bar_tabs():
tabx = tab_interval
tab_x = tab_interval
for tab_text in menu_bar_tabs:
tab_text_surface = bar_font.render(tab_text, True, BLACK, GREEN)
tab_rect = tab_text_surface.get_rect()
tab_rect.topleft = (tabx, 8)
tabx = tabx + tab_rect.width + tab_interval
tab_rect.topleft = (tab_x, 8)
tab_x = tab_x + tab_rect.width + tab_interval
mainwindow.blit(tab_text_surface, tab_rect)


def display_menu_bar():
menu_bar_rect = (0, 0, mainwindow_w, 36)
menu_bar_rect = (0, 0, mainwindow_w, bar_h)
pygame.draw.rect(mainwindow, BLUE, menu_bar_rect)
display_menu_bar_tabs()


bar_interval = 3
tool_bar_tabs = []
tool_tab_img = [pygame.image.load('./resources/tools/%s' % tab_img_name) for tab_img_name in tool_bar_tabs]

def display_tool_bar_tabs():
tab_x = tab_interval
for tab_img in tool_tab_img:
mainwindow.blit(tab_img, (tab_x, bar_h+bar_interval+2))
tab_x = tab_x+tab_img.get_width()+tab_interval

def display_tool_bar():
tool_bar_rect = (0, bar_h+bar_interval, mainwindow_w, bar_h)
pygame.draw.rect(mainwindow, BLUE, tool_bar_rect)
display_tool_bar_tabs()


def display_xy_status():
xy_status = 'x:%d y:%d' % (mouse_x, mouse_y)
tab_text_surface = bar_font.render(xy_status, True, BLACK, YELLOW)
Expand All @@ -52,23 +69,31 @@ def display_xy_status():


def display_status_bar():
status_bar_rect = (0, mainwindow_h - 36, mainwindow_w, 36)
status_bar_rect = (0, mainwindow_h - bar_h, mainwindow_w, bar_h)
pygame.draw.rect(mainwindow, RED, status_bar_rect)
display_xy_status()



def display_main_window():
mainwindow.fill(WHITE)
display_menu_bar()
display_status_bar()
display_tool_bar()

def init():
cursor = pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_CROSSHAIR)
pygame.mouse.set_cursor(cursor)


init()
while True:
mainwindow_w, mainwindow_h = pygame.display.get_surface().get_size()
mouse_x, mouse_y = pygame.mouse.get_pos()
display_main_window()
for event in pygame.event.get():
if event.type == QUIT:
print("exit with 0")
pygame.quit()
sys.exit()
pygame.display.update()
Expand Down
Binary file added resources/icon-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/pic/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/tools/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/tools/test1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/tools/test2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/tools/test3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# pg setup
import pygame as pg

pg.init()
screen = pg.display.set_mode([600, 400])
pg.display.set_caption("Example code for the cursors module")

# create a system cursor
system = pg.cursors.Cursor(pg.SYSTEM_CURSOR_SIZENWSE)

# create bitmap cursors
# pg.cursors.broken_x
bitmap_1 = pg.cursors.Cursor(*pg.cursors.tri_left)
# cursor = pg.cursors.compile(pg.cursors.textmarker_strings)
# pg.mouse.set_cursor((8, 16), (0, 0), *cursor)

# bitmap_2 = pg.cursors.compile(pg.cursors.sizer_x_strings)

# create a color cursor
surf = pg.Surface((40, 40)) # you could also load an image
surf.fill((120, 50, 50)) # and use that as your surface
color = pg.cursors.Cursor((20, 20), surf)

cursors = [system, bitmap_1, bitmap_2, color]
cursor_index = 0

pg.mouse.set_cursor(system)

clock = pg.time.Clock()
going = True
while going:
clock.tick(60)
screen.fill((0, 75, 30))
pg.display.flip()

for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
going = False

# if the mouse is clicked it will switch to a new cursor
if event.type == pg.MOUSEBUTTONDOWN:
cursor_index += 1
cursor_index %= len(cursors)
pg.mouse.set_cursor(cursors[cursor_index])

pg.quit()

0 comments on commit 1472091

Please sign in to comment.