-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
341 lines (281 loc) · 11.9 KB
/
editor.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from interface import Interface, TextBox, Cadre, Font, C, Button, InputText, Form
from settings import Settings
import pygame
DIM_TOOLS = (400, 80)
DIM_DRAG = (200,60)
DIM_BDONE = (120, 60)
DIM_CORNER = (15,15)
POS_TY = 200
POS_WIN = (600, 200)
SEPARATOR = '/'
class DragSurf(Form):
def __init__(self, pos, color, objtype):
super().__init__(DIM_DRAG, pos, color)
self.objtype = objtype
def run(self):
pos = pygame.mouse.get_pos()
self.display(pos=pos)
POS_X = Interface.dim.x - 600
class GuiObj(Button):
changing_dim = False
chan_dim_corner = None
name = ''
color = 'WHITE'
is_draging = False
drag_delay = 3
delay = 0
delta_pos = None # dif between mouse pos and topleft when draging
def __init__(self, pos, color, objtype):
# don't scale pos as it is the mouse pos
super().__init__(DIM_DRAG, pos,color, scale_pos=False)
self.input_text = InputText(DIM_DRAG, pos, C.WHITE, font=Font.f(25), centered=True, scale_pos=False)
self.objtype = objtype
self.selected = True
self.SELECT_COLOR = C.BLUE
self.set_corners_points()
self.color_choice = 'default' # store color choice way: default, text or range
# color range
self.ptr_pos = None
self.bptr_y = None
if self.objtype == 'TextBox' or self.objtype == 'Button':
self.can_input = True
else:
self.can_input = False
def get_real_pos(self):
return (self.pos[0]-POS_WIN[0], self.pos[1]-POS_WIN[1])
def set_corners_points(self):
corner = Button(DIM_CORNER, (0,0), C.BLUE)
corner.MARGE_WIDTH = 3
self.CTOPLEFT = corner
self.CTOPLEFT.set_pos(self.TOPLEFT, center=True)
corner = Button(DIM_CORNER, (0,0), C.BLUE)
corner.MARGE_WIDTH = 3
self.CTOPRIGHT = corner
self.CTOPRIGHT.set_pos(self.TOPRIGHT, center=True)
corner = Button(DIM_CORNER, (0,0), C.BLUE)
corner.MARGE_WIDTH = 3
self.CBOTTOMRIGHT = corner
self.CBOTTOMRIGHT.set_pos(self.BOTTOMRIGHT, center=True)
corner = Button(DIM_CORNER, (0,0), C.BLUE)
corner.MARGE_WIDTH = 3
self.CBOTTOMLEFT = corner
self.CBOTTOMLEFT.set_pos(self.BOTTOMLEFT, center=True)
self.corners_points = [self.CTOPLEFT, self.CTOPRIGHT, self.CBOTTOMLEFT, self.CBOTTOMRIGHT]
def set_new_dim_mouse(self):
mouse_pos = pygame.mouse.get_pos()
if self.chan_dim_corner == 0:
pos = mouse_pos
dim = [self.TOPRIGHT[0] -mouse_pos[0], self.BOTTOMLEFT[1] - mouse_pos[1]]
elif self.chan_dim_corner == 1:
pos = [self.pos[0], mouse_pos[1]]
dim = [mouse_pos[0] - self.TOPLEFT[0], self.BOTTOMLEFT[1] - mouse_pos[1]]
elif self.chan_dim_corner == 2:
pos = [mouse_pos[0], self.pos[1]]
dim = [self.BOTTOMRIGHT[0] - mouse_pos[0], mouse_pos[1] - self.TOPLEFT[1]]
elif self.chan_dim_corner == 3:
pos = self.pos
dim = [mouse_pos[0] - self.TOPLEFT[0], mouse_pos[1] - self.TOPLEFT[1]]
self.set_new_dim(dim, pos)
def set_new_dim(self, dim, pos):
self.set_dim_pos(dim, pos)
self.input_text.set_dim_pos(dim, pos)
self.set_corners_points()
# change text in setting
Settings.set_pos(pos)
Settings.set_dim(dim)
def set_select_color(self, color):
self.SELECT_COLOR = color
for cp in self.corners_points:
cp.set_color(color, marge=True)
def display(self):
super().display()
self.input_text.display()
if self.selected:
pygame.draw.line(Interface.screen, self.SELECT_COLOR, self.BOTTOMLEFT, self.BOTTOMRIGHT, 3)
pygame.draw.line(Interface.screen, self.SELECT_COLOR, self.TOPLEFT, self.TOPRIGHT, 3)
pygame.draw.line(Interface.screen, self.SELECT_COLOR, self.TOPLEFT, self.BOTTOMLEFT, 3)
pygame.draw.line(Interface.screen, self.SELECT_COLOR, self.TOPRIGHT, self.BOTTOMRIGHT, 3)
for cp in self.corners_points:
cp.display()
def react_events(self, events, pressed):
self.as_change_dim = False
mouse_pos = pygame.mouse.get_pos()
if self.selected:
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
self.is_draging = False
self.delay = 0 # reset delay
if self.changing_dim:
try:
self.set_new_dim_mouse()
except:
print('Error in setting new dim')
self.changing_dim = False
self.set_select_color(C.BLUE)
self.as_change_dim = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if Editor.TOP_RIGHT[0] > mouse_pos[0]:
self.is_draging = True
dx = mouse_pos[0] - self.TOPLEFT[0]
dy = mouse_pos[1] - self.TOPLEFT[1]
self.delta_pos = [dx, dy]
# when draging: update pos
if self.is_draging:
self.delay += 1
if self.delay >= self.drag_delay:
new_x = mouse_pos[0] - self.delta_pos[0]
new_y = mouse_pos[1] - self.delta_pos[1]
self.set_new_dim(self.dim, (new_x, new_y))
if not self.as_change_dim: # bool value to avoid reselect instantly
for i, cp in enumerate(self.corners_points):
if cp.pushed(events):
self.chan_dim_corner = i
self.changing_dim = True
self.set_select_color(C.GREEN)
if self.selected and self.can_input:
self.input_text.run(events, pressed)
class Editor:
# tools
button_text_box = Button(DIM_TOOLS, (0, POS_TY), C.XLIGHT_GREY,'TextBox')
button_button = Button(DIM_TOOLS, (0, POS_TY+80), C.XLIGHT_GREY,'Button')
button_input_text = Button(DIM_TOOLS, (0, POS_TY+160), C.XLIGHT_GREY,'InputText')
button_cadre = Button(DIM_TOOLS, (0, POS_TY+240), C.XLIGHT_GREY,'Cadre')
button_done = Button(DIM_BDONE, (2800, 1400), C.LIGHT_BLUE,'Done',font=Font.f(30))
drag_surf = None
objs = []
state = 'running'
# set attr here for on_resize method
window = None
TOP_LEFT = [0,0]
TOP_RIGHT = [0,0]
BOTTOM_LEFT = [0,0]
BOTTOM_RIGHT = [0,0]
current_selected = None
@classmethod
def set_window(cls, window):
cls.window = window
# don't rescale window.dim to have the original dimension stored (for on_resize)
dim = window.dim
cls.cadre = Cadre(dim, POS_WIN, C.WHITE)
cls.TOP_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1]])
cls.TOP_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]])
cls.BOTTOM_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]])
cls.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )
@staticmethod
def on_resize(factor):
if Editor.window:
dim = Editor.window.dim
Editor.TOP_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1]])
Editor.TOP_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]])
Editor.BOTTOM_LEFT = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]])
Editor.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )
@classmethod
def create_obj(cls, pos, objtype):
gui_obj = GuiObj(pos, C.WHITE, objtype)
cls.objs.append(gui_obj)
cls.current_selected = gui_obj
Settings.set_obj(gui_obj)
@classmethod
def check_deselect(cls):
if cls.current_selected:
if cls.cadre.on_it() and not cls.current_selected.as_change_dim:
if not cls.current_selected.changing_dim and not cls.current_selected.on_it():
return True
return False
@classmethod
def check_done(cls):
for gobj in cls.objs:
if not gobj.name:
return
return True
@classmethod
def str_gobj(cls, gobj):
'''
Name, objtype, dim, pos, text, color, font
'''
string = gobj.name + '\n'
string += gobj.objtype + '\n'
string += f'{gobj.dim[0]} {gobj.dim[1]}\n'
pos = gobj.get_real_pos()
string += f'{pos[0]} {pos[1]}\n'
string += gobj.input_text.content + '\n'
string += str(gobj.color) + '\n' # can be either str or list
string += str(gobj.input_text.font['size'])
return string
@classmethod
def create_savefile(cls):
# create file
with open(cls.window.name+'.pygui','w') as file:
sep = '\n'+SEPARATOR+'\n'
# first write name, dim of window
global_info = f'{cls.window.name} {cls.window.dim[0]} {cls.window.dim[1]}'
file.write(global_info)
file.write(sep)
for gobj in cls.objs:
file.write(cls.str_gobj(gobj))
file.write(sep)
@classmethod
def display(cls):
cls.cadre.display()
cls.button_text_box.display()
cls.button_button.display()
cls.button_input_text.display()
cls.button_cadre.display()
cls.button_done.display()
if cls.drag_surf:
cls.drag_surf.run()
for gui_obj in cls.objs:
gui_obj.display()
@classmethod
def react_events(cls, events, pressed):
pos = pygame.mouse.get_pos()
Settings.run(events, pressed)
# first check for changing dim
for gobj in cls.objs:
gobj.react_events(events, pressed)
# check if click anywhere -> deselect gui obj
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
if cls.check_deselect():
cls.current_selected.selected = False
cls.current_selected = None
Settings.deselect()
# then check for new selected
for gobj in cls.objs:
if gobj.pushed(events):
if not cls.current_selected:
cls.current_selected = gobj
gobj.selected = True
Settings.set_obj(gobj)
# check for gui obj to remove
if pressed[pygame.K_DELETE]:
if cls.current_selected:
cls.objs.remove(cls.current_selected)
cls.current_selected = None
Settings.deselect()
# check for deselect by enter
if pressed[pygame.K_RETURN]:
if cls.current_selected:
cls.current_selected.selected = False
cls.current_selected = None
Settings.deselect()
# last check for new gui obj
if cls.drag_surf:
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
# set new obj on window
cls.create_obj(pos,cls.drag_surf.objtype)
# reset drag_surf
cls.drag_surf = None
if cls.button_text_box.pushed(events):
cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'TextBox')
elif cls.button_button.pushed(events):
cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Button')
elif cls.button_input_text.pushed(events):
cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'InputText')
elif cls.button_cadre.pushed(events):
cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Cadre')
# end edition
if cls.button_done.pushed(events):
cls.create_savefile()
cls.state = 'done'