-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconky_lua_maker_main.py
435 lines (329 loc) · 15.1 KB
/
conky_lua_maker_main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Prupose :
=======
This is a python interactive interface to create a conky. Purpose is to easely
draw shapes (ring, bar, text) and then generate a Lua configuration file for
https://github.com/fisadev/conky-draw. See more in README about goal and usage.
Program principle :
=================
Main idea of this program consist on:
- a module containing all possible drawing definitions
- on a module containing all interface elements (see Interface preview),
At main_interface init, it create all interface elements, and create an object
from ::luadrawings:: class that will create new drawings and store them in a dict
At each loop it checks if there is any interaction with interface elements,
if yes, it execute a main_interface method. A majority of those methods change
a property of a drawing, those changes will also call (update values) other
interface elements.
At the loop end, it will loop over drawings dict to update the conky preview.
Interface preview :
=================
______________________________________________________________________
| | |
| Menu Panel | Select |
|________________________________________________| Panel |
| | ________________________________________ | |
| C | | | |___________________|
| h | | | | |
| o | | | | |
| i | | | | |
| c | | | | |
| e | | | | Option |
| | | Preview Panel | | Panel |
| B | | | | |
| u | | | | |
| t | | | | |
| t | | | | |
| o | | | | |
| n | | | | |
| s | |_______________________________________ | | |
| |____________________________________________| |
| | | |
| | Slider + Mouse display | |
|___|____________________________________________|___________________|
"""
import time
import pygame
import pygame_gui
from interfaceclasses import (
PreviewPanel,
ChoiceButtonPanel,
OptionPanel,
SelectPanel,
MenuButtons,
INTERFACE_SIZE,
MP_RECT,
)
from luadrawings import LuaDrawings
class main_interface:
""" j'en sais rien du tout moi """
def __init__(self):
"""
Initiate pygame
Initiate interface elements
Create an LuaDrawings object that create and store drawings
"""
pygame.init()
pygame.display.set_caption("ConkyLuaMakerGui")
self.window_surface = pygame.display.set_mode(INTERFACE_SIZE)
self.manager = pygame_gui.UIManager(INTERFACE_SIZE, "themes/label_theme.json")
# Inferface init
self.background = pygame.Surface(INTERFACE_SIZE)
self.background.fill(pygame.Color("#222222"))
self.menupanel = MenuButtons(self.manager)
self.previewpanel = PreviewPanel(self.window_surface, self.manager)
self.choicepanel = ChoiceButtonPanel(self.window_surface, self.manager)
self.optionpanel = OptionPanel(self.manager, self.window_surface)
self.selectpanel = SelectPanel(self.manager, self.window_surface)
# Store drawings that will create lua conf file
self.drawings = LuaDrawings(self.previewpanel.background)
self.clock = pygame.time.Clock()
self.timer = Timer()
self.mouse = Mouse(self.manager, self.window_surface, self.previewpanel.rect)
def loop(self):
"""
main interface loop :
check for interactions with interface and run methods
"""
update_graph = 2 # update during 2 loops(prevent unkown bug)
is_running = True
while is_running:
time_delta = self.clock.tick(15) / 1000.0
self.keys = pygame.key.get_pressed()
self.timer.print_loop_time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
elif event.type == pygame.USEREVENT:
update_graph = 2
utype = event.user_type
if utype == pygame_gui.UI_BUTTON_PRESSED:
self.process_push_buttons_event(event)
elif utype == pygame_gui.UI_DROP_DOWN_MENU_CHANGED:
self.drop_down_menu_select(event.text)
elif utype == pygame_gui.UI_TEXT_ENTRY_FINISHED:
self.process_entry_box_event(event)
elif utype == pygame_gui.UI_TEXT_BOX_LINK_CLICKED:
self.help_box_show_new_page(event)
elif self.mouse.on_previewpanel:
if self.mouse.left_click(event):
update_graph = 1
if self.drawings.buf.waiting_inputs:
self.add_input_to_buffer()
elif self.drawings.objects: # if an object exist
self.toggle_object_motion()
elif self.drawings.buf.waiting_inputs:
update_graph = 1
self.dynamic_draw_preview()
elif self.drawings.an_object_is_moving:
update_graph = 1
self.move_object()
elif self.drawings.an_object_is_resizing:
update_graph = 1
self.resize_object()
self.manager.process_events(event)
self.manager.update(time_delta)
if update_graph > 0:
update_graph -= 1
self.update_interface()
self.manager.draw_ui(self.window_surface)
pygame.display.update()
############## Inner event test in loop ############################
def process_push_buttons_event(self, event):
if event.ui_element in self.choicepanel.buttons:
self.create_new_draw(event.ui_element)
elif event.ui_element.text == "Load config":
self.load_conf_file()
elif event.ui_element.text == "Generate luaconf":
self.generate_conf_file()
elif event.ui_element.text == "?":
self.show_help()
elif event.ui_element.text == "Del":
self.delete_selected_object()
elif self.previewpanel.grid_size_as_changed():
self.change_grid_size()
def process_entry_box_event(self, event):
if event.ui_element in self.optionpanel.entrys:
self.change_selected_object_property(event.ui_element)
elif event.ui_element == self.selectpanel.new_name_entry_box:
self.rename_selected_object()
################ Methods : actions on event ########################
def create_new_draw(self, ui_element):
"""
Init new draw creation from user choice,
inteface will now look for previewpanel interactions
"""
ID = self.choicepanel.get_name(ui_element)
self.drawings.create(ID)
def load_conf_file(self):
"""
load "conky_draw_config.lua" and convert into drawing properties dict,
and create a new drawing from it.
"""
dct_list = self.menupanel.load()
for ID, dct in dct_list.items():
self.drawings.create_from_dct(dct, ID)
self.selectpanel.update_list(list(self.drawings.objects.keys()))
def generate_conf_file(self):
"""
Convert all drawings properties dict into lua configuration file
format and save it to "conky_draw_config.lua"
"""
self.menupanel.gen_luaconf(self.drawings.objects)
def show_help(self):
"""
display a tiny help popup
"""
self.menupanel.display_help()
def delete_selected_object(self):
"""
remove a drawing for drawings dict by it's key
"""
# delete object by it's key
self.selectpanel.delete(self.drawings.selected_item_ID)
self.drawings.delete_selected_item()
if self.drawings.selected_item_ID != 0:
self.optionpanel.update_lua_dct(self.drawings.selected_draw.get_lua_dct())
else:
self.optionpanel.clear()
def change_grid_size(self):
"""
set drawing own grid size from slider value
set grid size for mouse too
"""
if self.drawings.objects:
self.drawings.selected_draw.grid_step = self.previewpanel.grid_size
self.mouse.pp_grid_size = self.previewpanel.grid_size
def drop_down_menu_select(self, ID):
"""*
select a new object and display it's properties
"""
self.drawings.selected_item_ID = ID
self.optionpanel.update_lua_dct(self.drawings.selected_draw.get_lua_dct())
self.previewpanel.grid_size = self.drawings.selected_draw.grid_step
self.mouse.pp_grid_size = self.drawings.selected_draw.grid_step
def change_selected_object_property(self, ui_element):
"""
update 1 property of a drawing from entry box interaction
"""
name, new_value = self.optionpanel.get_new_entry(ui_element)
self.drawings.selected_draw.set_dct_item_from_lua({name: new_value})
def rename_selected_object(self):
old_ID = self.drawings.selected_item_ID
new_ID = self.selectpanel.rename(old_ID)
self.drawings.rename_draw(new_ID)
def help_box_show_new_page(self, event):
self.menupanel.display_link(event.link_target)
def add_input_to_buffer(self):
# if self.drawings.buf.waiting_inputs :
self.drawings.buf.add_input(
self.mouse.pp_grid_pos
) # store mouse position in a buffer
if not self.drawings.buf.waiting_inputs: # and buffer is non empty :
self.drawings.draw_from_buffer() # draw object
self.optionpanel.update_lua_dct(self.drawings.selected_draw.get_lua_dct())
self.selectpanel.add_item_to_list(self.drawings.selected_item_ID)
self.drawings.selected_draw.grid_step = self.previewpanel.grid_size
def toggle_object_motion(self):
if self.drawings.an_object_is_moving: # stop moving at click
self.drawings.an_object_is_moving = False
elif self.drawings.an_object_is_resizing:
self.drawings.an_object_is_resizing = False
else: # click on object => start moving
for ID, drawing in self.drawings.objects.items():
test_pos = (
int(self.mouse.pp_rel_pos[0] - drawing.pos[0]),
int(self.mouse.pp_rel_pos[1] - drawing.pos[1]),
)
if drawing.surface.get_rect().collidepoint(test_pos):
if drawing.mask.get_at(test_pos):
self.drawings.selected_item_ID = ID
self.selectpanel.set_select_item(ID)
self.optionpanel.update_lua_dct(drawing.get_lua_dct())
self.previewpanel.grid_size = drawing.grid_step
self.mouse.pp_grid_size = drawing.grid_step
if self.keys[pygame.K_LCTRL]:
self.mouse.store_click_pos(drawing.pos)
self.drawings.an_object_is_moving = True
break
elif self.keys[pygame.K_LSHIFT]:
self.mouse.store_click_pos()
self.drawings.an_object_is_resizing = True
break
def dynamic_draw_preview(self):
self.drawings.preview_from_buffer(self.mouse.pp_grid_pos)
def move_object(self):
self.drawings.selected_draw.pos = self.mouse.displacment_since_last_click()
self.optionpanel.update_position(self.drawings.selected_draw.get_lua_dct())
def resize_object(self):
self.drawings.selected_draw.resize(self.mouse.pp_grid_pos)
self.optionpanel.update_size(self.drawings.selected_draw.get_lua_dct())
def update_interface(self):
self.window_surface.blit(self.background, (0, 0))
self.previewpanel.blit()
self.previewpanel.clear()
for drawing in self.drawings.objects.values():
drawing.blit()
if self.drawings.buf.input_remaning == 1:
try:
self.drawings.buf.drawing.blit()
except:
print("thickness is bigger than size :\n => enlarge your draw")
class Mouse:
def __init__(self, manager, background, previewpanel_rect):
self.pp_rect = previewpanel_rect
self.pp_pos = previewpanel_rect[0:2]
self.pp_grid_size = 20
self.prev_obj_pos = (0, 0)
self.background = background
rect = MP_RECT
self.manager = manager
self.mouse_label = pygame_gui.elements.UILabel(
relative_rect=rect, text="mouse_pos", manager=self.manager
)
@property
def abs_pos(self):
pos = pygame.mouse.get_pos()
self.mouse_label.set_text("mouse position = {}".format(pos))
return pos # absolute mouse pos
@property
def pp_rel_pos(self):
return (self.abs_pos[0] - self.pp_pos[0], self.abs_pos[1] - self.pp_pos[1])
@property
def pp_grid_pos(self):
s = self.pp_grid_size
return (
(self.abs_pos[0] - self.pp_pos[0]) // s * s,
(self.abs_pos[1] - self.pp_pos[1]) // s * s,
)
@property
def on_previewpanel(self):
return self.pp_rect.collidepoint(self.abs_pos)
def left_click(self, event):
return event.type == pygame.MOUSEBUTTONDOWN and event.button == 1
def store_click_pos(self, obj_pos=(0, 0)):
self.prev_obj_pos = obj_pos
self.pp_prev_pos = self.pp_rel_pos
def displacment_since_last_click(self):
return (
self.prev_obj_pos[0] + self.pp_rel_pos[0] - self.pp_prev_pos[0],
self.prev_obj_pos[1] + self.pp_rel_pos[1] - self.pp_prev_pos[1],
)
class Timer:
def __init__(self):
self.tic = time.time()
self.nb_loop = 0
self.loop_threshold = 200
def print_loop_time(self):
self.nb_loop += 1
if self.nb_loop >= self.loop_threshold:
toc = time.time() - self.tic
loop_time_average = toc * 1000 / self.nb_loop
self.nb_loop = 0
self.tic = time.time()
print("mean loop time = {:4.2f}ms".format(loop_time_average))
if __name__ == "__main__":
interface = main_interface()
interface.loop()