forked from talonhub/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgui.py
453 lines (376 loc) · 13.7 KB
/
imgui.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from talon import Module, skia, ui, settings
from talon.skia.image import Image
from talon.skia.imagefilter import ImageFilter as ImageFilter
from talon.canvas import Canvas, MouseEvent
from talon.screen import Screen
from talon.ui import Rect
from typing import Callable, Optional
from dataclasses import dataclass
background_color = "ffffff"
border_color = "000000"
text_color = "444444"
button_bg_color = "aaaaaa"
button_text_color = "000000"
border_radius = 8
button_radius = 4
mod = Module()
setting_max_rows = mod.setting(
"gui_max_rows",
type=int,
default=5,
)
setting_max_col = mod.setting(
"gui_max_cols",
type=int,
default=50,
)
class State:
def __init__(self, canvas: skia.Canvas, dpi: float, numbered: bool):
self.max_rows = setting_max_rows.get()
self.max_cols = setting_max_col.get()
self.canvas = canvas
self.font_size = round(dpi * settings.get("imgui.scale") / 10)
self.padding = self.rem(0.5)
self.image_height = self.max_rows * self.font_size
self.image_width = 5 * self.image_height
self.text_offset = self.rem(2.5) if numbered else self.padding
self.x = canvas.x + self.padding
self.x_text = canvas.x + self.text_offset
self.y = canvas.y + self.padding
self.width = 0
self.height = self.padding
def add_width(self, width: float, offset: bool):
text_offset = self.text_offset if offset else self.padding
self.width = max(self.width, text_offset + width)
def add_height(self, height: float):
self.y += height
self.height += height
def get_width(self):
if self.width:
return round(self.width + self.padding)
else:
return 0
def get_height(self):
return round(self.height + self.padding)
def rem(self, number: int or float):
return round(self.font_size * number)
class Text:
def __init__(self, text: str, header: bool):
self.numbered = not header
self.text = text
self.header = header
self.rect = None
self._is_clicked = False
def is_clicked(self):
is_clicked = self._is_clicked
self._is_clicked = False
return is_clicked
def click(self):
self._is_clicked = True
def draw(self, state: State):
state.canvas.paint.style = state.canvas.paint.Style.FILL
state.canvas.paint.font.embolden = self.header
state.canvas.paint.textsize = state.font_size
state.canvas.paint.color = text_color
x = state.x if self.header else state.x_text
start_x = state.x
start_y = state.y
width = 0
height = 0
lines = self.text.split("\n")
if len(lines) > state.max_rows:
lines = lines[: state.max_rows]
lines[-1] = "..."
for line in lines:
line = line.replace("\t", " ")
if len(line) > state.max_cols + 4:
line = line[: state.max_cols] + " ..."
rect = state.canvas.paint.measure_text(line)[1]
state.canvas.draw_text(line, x, state.y + state.font_size)
state.add_width(rect.x + rect.width, offset=not self.header)
state.add_height(state.font_size)
width = max(width, rect.x + rect.width)
height += state.font_size
self.rect = Rect(
start_x,
start_y,
width + x - start_x,
height + state.padding / 2,
)
# state.canvas.draw_rect(self.rect) TODO remove
state.add_height(state.padding)
@classmethod
def draw_number(cls, state: State, y_start: float, number: int):
state.canvas.paint.style = state.canvas.paint.Style.FILL
state.canvas.paint.font.embolden = False
state.canvas.paint.textsize = state.font_size
state.canvas.paint.color = button_text_color
text = str(number).rjust(2)
rect = state.canvas.paint.measure_text(text)[1]
x = state.x + rect.x
y = (state.y + y_start + rect.y + state.font_size) / 2
# state.canvas.paint.style = state.canvas.paint.Style.FILL
# state.canvas.paint.color = button_bg_color
# state.canvas.draw_rect(Rect(x, y_start, rect.x + rect.width, state.y - y_start))
# state.canvas.paint.color = button_text_color
state.canvas.draw_text(text, x, y)
class Button:
def __init__(self, text: str):
self.numbered = False
self.text = text
self.rect = None
self._is_clicked = False
def is_clicked(self):
is_clicked = self._is_clicked
self._is_clicked = False
return is_clicked
def click(self):
self._is_clicked = True
def draw(self, state: State):
state.canvas.paint.textsize = state.font_size
text_rect = state.canvas.paint.measure_text(self.text)[1]
padding = state.rem(0.25)
width = text_rect.width + 2 * padding
height = state.font_size + 2 * padding
self.rect = Rect(
state.x + text_rect.x - padding,
state.y + (height + text_rect.y - text_rect.height) / 2,
width,
height,
)
rrect = skia.RoundRect.from_rect(self.rect, x=button_radius, y=button_radius)
state.canvas.paint.style = state.canvas.paint.Style.FILL
state.canvas.paint.color = button_bg_color
state.canvas.draw_rrect(rrect)
state.canvas.paint.style = state.canvas.paint.Style.STROKE
state.canvas.paint.color = border_color
state.canvas.draw_rrect(rrect)
state.canvas.paint.style = state.canvas.paint.Style.FILL
state.canvas.paint.font.embolden = False
state.canvas.paint.textsize = state.font_size
state.canvas.paint.color = button_text_color
state.canvas.draw_text(self.text, state.x, state.y + state.font_size)
state.add_width(width, offset=False)
state.add_height(height + state.padding)
class Line:
def __init__(self, bold: bool):
self.numbered = False
self.bold = bold
self.rect = None
def draw(self, state: State):
y = state.y + state.padding - 1
state.canvas.paint.style = state.canvas.paint.Style.FILL
state.canvas.paint.color = text_color if self.bold else button_bg_color
state.canvas.draw_line(
state.x, y, state.x + state.canvas.width - state.font_size, y
)
state.add_height(state.font_size)
class Spacer:
def __init__(self):
self.numbered = False
self.rect = None
def draw(self, state: State):
state.add_height(state.font_size)
class Image:
def __init__(self, image: Image):
self.numbered = True
self._image = image
self.rect = None
def _resize(self, width: int, height: int) -> Image:
aspect_ratio = self._image.width / self._image.height
if self._image.width < self._image.height:
height = round(width / aspect_ratio)
else:
width = round(height * aspect_ratio)
return self._image.reshape(width, height)
def draw(self, state: State):
image = self._resize(state.image_width, state.image_height)
state.canvas.draw_image(image, state.x_text, state.y)
state.add_width(image.width, offset=True)
state.add_height(image.height + state.padding)
class GUI:
def __init__(
self,
callback: Callable,
screen: Screen or None,
x: float or None,
y: float or None,
numbered: bool,
):
self._callback = callback
self._screen = screen
self._x = x
self._y = y
self._numbered = numbered
self._x_moved = None
self._y_moved = None
self._showing = False
self._screen_current = None
self._buttons: dict[str, Button] = {}
self._texts: dict[str, Text] = {}
@property
def showing(self):
return self._showing
def show(self):
self._screen_current = self._get_screen()
# Initializes at minimum size so to calculate and set correct size later
self._canvas = Canvas(0, 0, 1, 1)
self._showing = True
self._canvas.draggle = True
self._canvas.blocks_mouse = True
self._last_mouse_pos = None
self._canvas.register("draw", self._draw)
self._canvas.register("mouse", self._mouse)
def freeze(self):
self._canvas.freeze()
def hide(self):
if self._showing:
self._canvas.unregister("draw", self._draw)
self._canvas.unregister("mouse", self._mouse)
self._canvas.close()
self._buttons = {}
self._texts = {}
self._showing = False
def text(self, text: str) -> bool:
return self._text(text, header=False)
def header(self, text: str) -> bool:
return self._text(text, header=True)
def _text(self, text: str, header: bool) -> bool:
if text in self._texts:
element = self._texts[text]
else:
element = Text(text, header)
self._texts[text] = element
self._elements.append(element)
return element.is_clicked()
def button(self, text: str) -> bool:
if text in self._buttons:
element = self._buttons[text]
else:
element = Button(text)
self._buttons[text] = element
self._elements.append(element)
return element.is_clicked()
def line(self, bold: Optional[bool] = False):
self._elements.append(Line(bold))
def spacer(self):
self._elements.append(Spacer())
def image(self, image):
self._elements.append(Image(image))
def _draw(self, canvas):
self._elements = []
self._callback(self)
self._draw_background(canvas)
state = State(canvas, self._screen_current.dpi, self._numbered)
number = 1
if self._elements:
for el in self._elements:
y_start = state.y
el.draw(state)
if self._numbered and el.numbered:
Text.draw_number(state, y_start, number)
number += 1
else:
state.width = 1
state.height = 1
# Resize to fit content
if canvas.width != state.get_width() or canvas.height != state.get_height():
self._resize(state.get_width(), state.get_height())
def _resize(self, width: int or float, height: int or float):
screen = self._screen_current
if self._x_moved:
x = self._x_moved
elif self._x is not None:
x = screen.x + screen.width * self._x
else:
x = screen.x + max(0, (screen.width - width) / 2)
if self._y_moved:
y = self._y_moved
elif self._y is not None:
y = screen.y + screen.height * self._y
else:
y = screen.y + max(0, (screen.height - height) / 2)
if self._showing:
self._canvas.rect = Rect(x, y, width, height)
def _draw_background(self, canvas):
rrect = skia.RoundRect.from_rect(canvas.rect, x=border_radius, y=border_radius)
canvas.paint.style = canvas.paint.Style.FILL
canvas.paint.color = background_color
canvas.draw_rrect(rrect)
canvas.paint.style = canvas.paint.Style.STROKE
canvas.paint.color = border_color
canvas.draw_rrect(rrect)
def _mouse(self, e: MouseEvent):
if e.event == "mousedown" and e.button == 0:
if not self._get_element(e.gpos):
self._last_mouse_pos = e.gpos
elif e.event == "mousemove" and self._last_mouse_pos:
dx = e.gpos.x - self._last_mouse_pos.x
dy = e.gpos.y - self._last_mouse_pos.y
self._last_mouse_pos = e.gpos
self._x_moved = self._canvas.rect.x + dx
self._y_moved = self._canvas.rect.y + dy
self._canvas.move(self._x_moved, self._y_moved)
self._dont_center = True
elif e.event == "mouseup" and e.button == 0:
self._last_mouse_pos = None
element = self._get_element(e.gpos)
if element:
element.click()
def _get_element(self, pos):
for el in self._elements:
if el.rect and el.rect.contains(pos.x, pos.y):
return el
return None
def _get_screen(self) -> Screen:
if self._screen is not None:
return self._screen
try:
return ui.active_window().screen
except:
return ui.main_screen()
@dataclass
class ImGUI:
GUI: GUI
@classmethod
def open(
cls,
screen: Optional[Screen] = None,
x: Optional[float] = None,
y: Optional[float] = None,
numbered: Optional[bool] = False,
):
def open_inner(draw):
return GUI(
draw,
numbered=numbered,
screen=screen,
x=x,
y=y,
)
return open_inner
imgui = ImGUI(GUI)
@imgui.open(numbered=True, x=0.7, y=0.3)
def gui(gui: imgui.GUI):
gui.header("Some header")
gui.line(bold=True)
gui.text("text before spacer")
gui.spacer()
gui.text("text after spacer, jg")
for i in range(10):
gui.line()
gui.text(f"stuff stuff {i}")
gui.line()
gui.text(
"""def draw(self, state: State):
y = state.y + state.padding - 1
state.canvas.paint.style = state.canvas.paint.Style.FILL
state.canvas.paint.color = text_color if self.bold else button_bg_color
state.canvas.draw_line(
state.x, y, state.x + state.canvas.width - state.font_size, y
)
state.add_height(state.font_size)"""
)
if gui.button("some text"):
print("Hide")
# gui.show()