-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGUI.py
266 lines (240 loc) · 10.5 KB
/
GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from geomdl import BSpline
from geomdl import knotvector
from pygame import gfxdraw
import pygame,sys
import GUI_text_box
import csv
import os
pygame.init()
cwd = os.getcwd()
# Configurtion set up
class Config():
def __init__(self):
self.displaySize = (500,500)
self.dark = (0,0,0)
self.grey = (127,127,127)
self.white = (255,255,255)
self.red = (255,0,0)
self.green = (0,255,0)
self.blue = (0,0,255)
self.show_points=True
# NURBS Curve set up
class Spline():
def __init__(self, degree=2):
self.degree = degree
self.crv = BSpline.Curve()
self.crv.degree = degree
def draw(self, points):
self.crv.ctrlpts = points
self.crv.knotvector = knotvector.generate(self.crv.degree, self.crv.ctrlpts_size)
return self.crv.evalpts
def set_degree(self,degree):
self.crv.degree = degree
def get_curve_points(self):
return self.crv.evalpts
# Canvas setting
class Canvas():
def __init__(self,screen):
self.screen = screen
self.curve = Spline()
self.ctrl_points=[]
self.count=0
self.selected=None
self.move_point=False
self.add_mode=0
# rect = (x_position, y_position, length, width)
self.addButton = (220,470,70,20)
self.deleteButton = (320,470,70,20)
self.NewButton = (420,470,70,20)
self.connectButton = (20, 20, 70, 20)
self.windowButton = (20, 470, 70, 20)
def add_points(self,pts):
self.ctrl_points.append(pts)
def region(self,button,x,y):
bx,by,brx,bry=button
if bx<x<bx+brx and by<y<by+bry:
return True
return False
def button_render(self):
if self.add_mode:
pygame.draw.rect(self.screen,cfg.green,self.addButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Add', True, cfg.red, cfg.green)
textRect = text.get_rect()
textRect.center = (255, 480)
screen.blit(text, textRect)
pygame.draw.rect(self.screen,cfg.red,self.windowButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Window', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (55, 480)
screen.blit(text, textRect)
pygame.draw.rect(self.screen,cfg.red,self.NewButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Clear', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (455, 480)
screen.blit(text, textRect)
else:
pygame.draw.rect(self.screen,cfg.red,self.addButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Add', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (255, 480)
screen.blit(text, textRect)
pygame.draw.rect(self.screen,cfg.red,self.windowButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Window', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (55, 480)
screen.blit(text, textRect)
pygame.draw.rect(self.screen,cfg.red,self.NewButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Clear', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (455, 480)
screen.blit(text, textRect)
if self.selected!=None:
pygame.draw.rect(self.screen,cfg.red,self.deleteButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Delete', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (355, 480)
screen.blit(text, textRect)
pygame.draw.rect(self.screen,cfg.red,self.connectButton)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('Connect', True, cfg.green, cfg.red)
textRect = text.get_rect()
textRect.center = (55, 30)
screen.blit(text, textRect)
def render(self):
if cfg.show_points:
if self.count>=2:
pygame.draw.lines(self.screen, cfg.grey, 0,self.ctrl_points)
for i,point in enumerate(self.ctrl_points):
if i==0:
pygame.draw.circle(self.screen,cfg.blue,point,5)
elif i==self.selected:
pygame.draw.circle(self.screen,cfg.green,point,5)
else:
pygame.draw.circle(self.screen,cfg.grey,point,5)
if self.count>=self.curve.degree+1:
pygame.draw.lines(self.screen,cfg.white,0,self.curve.draw(self.ctrl_points),width=1)
pts = self.curve.get_curve_points()
for x in pts:
pygame.gfxdraw.pixel(screen, int(x[0]), int(x[1]), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]), int(x[1]+1), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]+1), int(x[1]+1), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]-1), int(x[1]+1), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]+1), int(x[1]), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]-1), int(x[1]), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]), int(x[1]-1), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]+1), int(x[1]-1), cfg.red)
pygame.gfxdraw.pixel(screen, int(x[0]-1), int(x[1]-1), cfg.red)
# open the file in the write mode
with open(cwd + '/curve_points_file.csv', 'w', encoding='UTF8') as f:
# create the csv writer
writer = csv.writer(f)
writer.writerow(['x_pos', 'y_pos'])
writer.writerows(pts)
self.button_render()
def select(self,x,y,r=10):
can_add=True
if self.selected != None:
if self.region(self.connectButton,x,y):
self.add_points(self.ctrl_points[0])
self.count += 1
self.move_point=True
can_add=False
if self.region(self.addButton,x,y):
self.add_mode=not self.add_mode
can_add=False
elif self.region(self.NewButton,x,y):
self.screen = screen
self.curve = Spline()
self.ctrl_points=[]
self.count=0
self.selected=None
self.move_point=False
self.add_mode=0
elif self.region(self.windowButton,x,y):
check, xpos, ypos = GUI_text_box.windowInput()
if check == 1:
self.add_points((xpos,ypos))
self.count += 1
self.move_point=True
can_add=False
if check == 2:
with open(cwd + '/ctrl_points_file.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
if row != [] and row != ['x_pos', 'y_pos']:
self.add_points((int(row[0]),int(row[1])))
self.count += 1
self.move_point=True
can_add=False
if check == 4:
pts = self.ctrl_points
# open the file in the write mode
with open(cwd + '/ctrl_points_file.csv', 'w', encoding='UTF8') as f:
# create the csv writer
writer = csv.writer(f)
writer.writerow(['x_pos', 'y_pos'])
writer.writerows(pts)
elif self.region(self.deleteButton,x,y):
self.ctrl_points.pop(self.selected)
self.count-=1
self.selected=None
can_add=False
elif self.count and cfg.show_points:
for i,points in enumerate(self.ctrl_points):
px,py = points
if px-r<x<px+r and py-r<y<py+r:
self.selected=i
self.move_point=True
can_add=False
break
if (self.add_mode and can_add) and cfg.show_points and self.region(self.connectButton,x,y) == False and self.region(self.windowButton,x,y) == False:
if self.selected != None:
xpoint,_=self.ctrl_points[self.selected]
if xpoint>x:
self.ctrl_points.insert(self.selected,(x,y))
else:
self.ctrl_points.insert(self.selected+1,(x,y))
else:
self.ctrl_points.append((x,y))
self.count+=1
def move(self,xy):
if self.move_point and cfg.show_points:
self.ctrl_points.pop(self.selected)
self.ctrl_points.insert(self.selected,xy)
return True
cfg = Config()
screen = pygame.display.set_mode(cfg.displaySize)
canvas = Canvas(screen)
def update():
screen.fill(cfg.dark)
#pygame.draw.rect(canvas.screen,cfg.red, canvas.addButton)
#font = pygame.font.Font('freesansbold.ttf', 12)
#text = font.render('Add', True, (0,255,0), cfg.red)
#textRect = text.get_rect()
#textRect.center = (55, 280)
#screen.blit(text, textRect)
#pygame.draw.rect(canvas.screen,cfg.red, canvas.deleteButton)
#pygame.draw.rect(canvas.screen,cfg.red, canvas.selectButton)
canvas.render()
pygame.display.update()
update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
canvas.select(*event.pos)
update()
elif event.type == pygame.MOUSEBUTTONUP:
canvas.move_point=False
elif event.type == pygame.MOUSEMOTION:
if canvas.move(event.pos):
update()