-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuAlign.py
697 lines (539 loc) · 19.5 KB
/
FuAlign.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
from __future__ import annotations
from dataclasses import dataclass
import tkinter as tk
from typing import Callable
# For testing inside VSCode.
try:
from fa_backend import Tool, Comp, Fusion
try:
fusion
except NameError:
print("Initializing fake Fusion.")
fusion = Fusion()
comp = Comp()
except ModuleNotFoundError:
pass
# =========================================================================== #
# ======================== BACKEND =================================== #
# =========================================================================== #
# Constant for DataWindow Error.
NEG_MILL = -1_000_000
EMPTY_DATA_WINDOW = {key + 1: NEG_MILL for key in range(4)}
HISTORY: list[dict[Tool, dict[int, float]]] = []
@dataclass
class Align:
merge: Tool
def __post_init__(self):
self.fg_tool = self.get_tool()
def get_tool(self):
return self.merge.Foreground.GetConnectedOutput().GetTool()
@property
def tool_pixel_width(self):
return self.fg_tool.GetAttrs("TOOLI_ImageWidth")
@property
def tool_pixel_height(self):
return self.fg_tool.GetAttrs("TOOLI_ImageHeight")
@property
def tool_img_pixel_width(self) -> int:
if not self.tool_data_window:
return self.tool_pixel_width
x0, y0, x1, y1 = self.tool_data_window.values()
return x1 - x0
@property
def tool_img_pixel_height(self) -> int:
if not self.tool_data_window:
return self.tool_pixel_height
x0, y0, x1, y1 = self.tool_data_window.values()
return y1 - y0
@property
def tool_rel_width(self):
return self.tool_img_pixel_width * self.merge_size / self.merge_pixel_width
@property
def tool_rel_height(self):
return self.tool_img_pixel_height * self.merge_size / self.merge_pixel_height
@property
def merge_pixel_width(self) -> int:
return self.merge.GetAttrs("TOOLI_ImageWidth")
@property
def merge_pixel_height(self) -> int:
return self.merge.GetAttrs("TOOLI_ImageHeight")
@property
def merge_size(self) -> float:
return self.merge.GetInput("Size")
@property
def tool_center_coords(self) -> tuple[float, float]:
"""Returns a normalized value of the tool's 'Center' position relative to itself.
If its image information is only a fraction of its width, this method will return
where in the screen this information is centered. Else, we get the default 0.5, 0.5 values.
"""
if self.tool_data_window:
x0, y0, x1, y1 = self.tool_data_window.values()
else:
print(
f"It looks like {self.fg_tool.GetAttrs('TOOLS_Name')} is off screen. Alignment might not work properly."
)
return 0.5, 0.5
x = self.tool_img_pixel_width / 2 + x0
y = self.tool_img_pixel_height / 2 + y0
# Normalized...
x = x / self.tool_pixel_width
y = y / self.tool_pixel_height
return x, y
@property
def tool_offset_in_self(self) -> tuple[float, float]:
"""Returns the amount of offset from center of a tool's image rectangle."""
x_offset, y_offset = [pos - 0.5 for pos in self.tool_center_coords]
return x_offset, y_offset
@property
def tool_offset_in_merge(self) -> tuple[float, float]:
"""Returns the amount of offset from center of a tool when it is first placed on the Merge."""
x_offset, y_offset = [
offset * rel_res
for offset, rel_res in zip(
self.tool_offset_in_self, self.tool_rel_resolution
)
]
return x_offset, y_offset
@property
def tool_rel_resolution(self) -> tuple[float, float]:
rel_x = self.tool_pixel_width / self.merge_pixel_width
rel_y = self.tool_pixel_height / self.merge_pixel_height
return rel_x, rel_y
@property
def tool_data_window(self) -> dict[int, int] | None:
dw = self.fg_tool.Output.GetValue().DataWindow
if dw == EMPTY_DATA_WINDOW:
return None
if not dw:
return None
return dw
@property
def merge_current_x(self):
return self.merge.GetInput("Center")[1]
@property
def merge_current_y(self):
return self.merge.GetInput("Center")[2]
@property
def edges_and_centers(self) -> dict[str, float]:
"""Returns normalized edge positions (top, left, bottom, right) for tool in merge"""
edges_and_centers = {}
x_offset, y_offset = self.tool_offset_in_merge
x = x_offset + self.merge_current_x
y = y_offset + self.merge_current_y
edges_and_centers["top"] = y + self.tool_rel_height / 2
edges_and_centers["left"] = x - self.tool_rel_width / 2
edges_and_centers["bottom"] = y - self.tool_rel_height / 2
edges_and_centers["right"] = x + self.tool_rel_width / 2
edges_and_centers["horizontal"] = x
edges_and_centers["vertical"] = y
return edges_and_centers
def get_merges(comp: Comp) -> list[Align] | None:
merges = comp.GetToolList(True, "Merge").values()
if not merges:
return None
merges_and_tools = [Align(merge) for merge in merges]
return merges_and_tools
# Align edges funcs
def align_left_edges(object: Align, edge: float) -> None:
x = edge + object.tool_rel_width / 2 - object.tool_offset_in_merge[0]
y = object.merge_current_y
object.merge.SetInput("Center", [x, y])
def align_right_edges(object: Align, edge: float) -> None:
x = edge - object.tool_rel_width / 2 - object.tool_offset_in_merge[0]
y = object.merge_current_y
object.merge.SetInput("Center", [x, y])
def align_top_edges(object: Align, edge: float) -> None:
x = object.merge_current_x
y = edge - object.tool_rel_height / 2 - object.tool_offset_in_merge[1]
object.merge.SetInput("Center", [x, y])
def align_bottom_edges(object: Align, edge: float) -> None:
x = object.merge_current_x
y = edge + object.tool_rel_height / 2 - object.tool_offset_in_merge[1]
object.merge.SetInput("Center", [x, y])
# Align centers funcs
def align_horizontal_centers(object: Align, center: float) -> None:
x = center - object.tool_offset_in_merge[0]
y = object.merge_current_y
object.merge.SetInput("Center", [x, y])
def align_vertical_centers(object: Align, center: float) -> None:
x = object.merge_current_x
y = center - object.tool_offset_in_merge[1]
object.merge.SetInput("Center", [x, y])
# Distribute funcs
def distribute_horizontally(align_objects: list[Align]) -> None:
global comp
def by_edge(object: Align):
return object.edges_and_centers["left"]
align_objects.sort(key=by_edge)
left_edge = min([obj.edges_and_centers["left"] for obj in align_objects])
right_edge = max([obj.edges_and_centers["right"] for obj in align_objects])
canvas_width = right_edge - left_edge
total_tool_width = sum([obj.tool_rel_width for obj in align_objects])
gutter = (canvas_width - total_tool_width) / (len(align_objects) - 1)
comp.Lock()
for idx, object in enumerate(align_objects):
if idx == 0 or idx == len(align_objects) - 1:
edge = object.edges_and_centers["right"] + gutter
continue
else:
align_left_edges(object, edge)
edge = object.edges_and_centers["right"] + gutter
comp.Unlock()
def distribute_vertically(align_objects: list[Align]) -> None:
global comp
def by_edge(object: Align):
return object.edges_and_centers["bottom"]
align_objects.sort(key=by_edge)
bottom_edge = min([obj.edges_and_centers["bottom"] for obj in align_objects])
top_edge = max([obj.edges_and_centers["top"] for obj in align_objects])
canvas_height = top_edge - bottom_edge
total_tool_height = sum([obj.tool_rel_height for obj in align_objects])
gutter = (canvas_height - total_tool_height) / (len(align_objects) - 1)
comp.Lock()
for idx, object in enumerate(align_objects):
if idx == 0 or idx == len(align_objects) - 1:
edge = object.edges_and_centers["top"] + gutter
continue
else:
align_bottom_edges(object, edge)
edge = object.edges_and_centers["top"] + gutter
comp.Unlock()
@dataclass
class Operation:
id: str
full_name: str
group: str
keyboard_shortcut: str
icon_dims: list[tuple]
align_func: Callable
edge_func: Callable = None
parsed_shortcuts: list[str] = None
def execute(self) -> None:
global comp
align_objects = get_merges(comp)
if not align_objects:
print("No merges selected.")
return
# for distribute functions
if not self.edge_func:
self.align_func(align_objects)
return
edges_or_centers = [
object.edges_and_centers[self.id] for object in align_objects
]
edge_or_center = self.edge_func(edges_or_centers)
comp.Lock()
comp.StartUndo(f"FuAlign: {self.full_name}")
for obj in align_objects:
self.align_func(obj, edge_or_center)
comp.EndUndo(True)
comp.Unlock()
return
# CREATING OPERATIONS ==================================================
GROUPS = ["align edges", "align centers", "distribute"]
OPERATIONS: dict[str, Operation] = {}
OPERATIONS["top"] = Operation(
id="top",
full_name="Align top edges",
group=GROUPS[0],
keyboard_shortcut="T",
icon_dims=[
(1, 0.1, 0, 0.05), # line
(0.3, 0.7, 0.15, 0.25), # rect1
(0.3, 0.5, 0.55, 0.25), # rect 2
],
align_func=align_top_edges,
edge_func=max,
)
OPERATIONS["bottom"] = Operation(
id="bottom",
full_name="Align bottom edges",
group=GROUPS[0],
keyboard_shortcut="B",
icon_dims=[
(1, 0.1, 0, 0.85), # line
(0.3, 0.7, 0.15, 0.05), # rect1
(0.3, 0.5, 0.55, 0.25), # rect2
],
align_func=align_bottom_edges,
edge_func=min,
)
OPERATIONS["left"] = Operation(
id="left",
full_name="Align left edges",
group=GROUPS[0],
keyboard_shortcut="L",
icon_dims=[
(0.1, 1, 0.05, 0), # line
(0.7, 0.3, 0.25, 0.15), # rect1
(0.5, 0.3, 0.25, 0.55), # rect 2
],
align_func=align_left_edges,
edge_func=min,
)
OPERATIONS["right"] = Operation(
id="right",
full_name="Align right edges",
group=GROUPS[0],
keyboard_shortcut="R",
icon_dims=[
(0.1, 1, 0.85, 0), # line
(0.7, 0.3, 0.05, 0.15), # rect1
(0.5, 0.3, 0.25, 0.55), # rect2
],
align_func=align_right_edges,
edge_func=max,
)
OPERATIONS["horizontal"] = Operation(
id="horizontal",
full_name="Align horizontal centers",
group=GROUPS[1],
keyboard_shortcut="H",
icon_dims=[
(0.1, 1, 0.45, 0), # line
(0.5, 0.3, 0.25, 0.15), # rect1
(0.7, 0.3, 0.15, 0.55), # rect2
],
align_func=align_horizontal_centers,
edge_func=lambda x: (max(x) - min(x)) / 2 + min(x),
)
OPERATIONS["vertical"] = Operation(
id="vertical",
full_name="Align vertical centers",
group=GROUPS[1],
keyboard_shortcut="V",
icon_dims=[
(1, 0.1, 0, 0.45), # line
(0.3, 0.5, 0.15, 0.25), # rect1
(0.3, 0.7, 0.55, 0.15), # rect2
],
align_func=align_vertical_centers,
edge_func=lambda x: (max(x) - min(x)) / 2 + min(x),
)
OPERATIONS["horizontally"] = Operation(
id="horizontally",
full_name="Distribute horizontally",
group=GROUPS[2],
keyboard_shortcut="⇧H",
icon_dims=[
(0.1, 1, 0.1, 0), # line1
(0.3, 0.6, 0.35, 0.2), # rect1
(0.1, 1, 0.8, 0), # line2
],
align_func=distribute_horizontally,
)
OPERATIONS["vertically"] = Operation(
id="vertically",
full_name="Distribute vertically",
group=GROUPS[2],
keyboard_shortcut="⇧V",
icon_dims=[
(1, 0.1, 0, 0.1), # line1
(0.6, 0.3, 0.2, 0.35), # rect1
(1, 0.1, 0, 0.8), # line2
],
align_func=distribute_vertically,
)
# ########################################################################### #
# =========================================================================== #
# ======================== FRONTEND =================================== #
# =========================================================================== #
class Color:
HOVER = "#c1c1c1"
NORMAL = "#868686"
ACTIVE = "#f1f1f1"
CANVAS_BG = "#252525"
TEXT = "#A3A3A3"
class Font:
DEFAULT = "TkTextFont"
TOOLTIP_BOLD = "TkTooltipFont 12 bold"
TOOLTIP = "TkTooltipFont 12"
@dataclass
class UIRow:
parent: tk.Widget
frame: tk.Frame
name: str
def __post_init__(self):
self.title_var = tk.StringVar()
self.title = tk.Label(
self.frame,
textvariable=self.title_var,
width=20,
height=1 if self.name in ["header", "footer"] else 2,
anchor=tk.SW,
padx=5,
pady=0 if self.name in ["header", "footer"] else 5,
)
self.title.grid(column=1, columnspan=4, row=1, sticky=tk.W)
def describe(self, name: str):
if not name:
self.title_var.set(self.name.capitalize())
return
self.title_var.set(name)
@dataclass
class UIElement:
parent: UIRow
name: str
row: int
col: int
operation: Operation
key: str
canvas: tk.Canvas = None
icon_dims: list[tuple] = None
def describe(self, event):
self.parent.describe(self.full_name)
def undescribe(self, event):
self.parent.describe("")
@property
def full_name(self):
return f"{self.operation.full_name} [{self.key}]"
# Creates rectangle from normalized dimensions instead of absolute coordinates.
def draw_rect(
self,
width: float,
height: float,
x: float,
y: float,
**configs,
) -> int:
multiplier = self.canvas.winfo_width()
x0 = x * multiplier
x1 = (x + width) * multiplier
y0 = y * multiplier
y1 = (y + height) * multiplier
return self.canvas.create_rectangle(x0, y0, x1, y1, **configs)
# Creates all rectangles necessary to draw each icon
def draw_icon(self, **config) -> None:
for dim in self.icon_dims:
self.draw_rect(*dim, **config)
set_hover_style(self.canvas)
# Button style event handlers ======================================
def on_hover(event: tk.Event):
canvas: tk.Canvas = event.widget
canvas.itemconfig("icon", fill=Color.HOVER)
def on_leave(event: tk.Event):
canvas: tk.Canvas = event.widget
canvas.itemconfig("icon", fill=Color.NORMAL)
def on_click(event: tk.Event):
canvas: tk.Canvas = event.widget
canvas.itemconfig("icon", fill=Color.ACTIVE)
def set_hover_style(canvas: tk.Canvas):
canvas.bind("<Enter>", on_hover)
canvas.bind("<Leave>", on_leave)
canvas.bind("<Button-1>", on_click)
canvas.bind("<ButtonRelease-1>", on_hover)
# PARSE KEYBOARD SHORTCUTS
def parse_key(key: str) -> list[str]:
if "⇧" in key:
modifier = "Shift"
key = key[-1]
upper_and_lower = False
else:
modifier = "Key"
upper_and_lower = True
if not upper_and_lower:
return [f"<{modifier}-{key}>"]
return [f"<{modifier}-{key}>", f"<{modifier}-{key.lower()}>"]
for operation in OPERATIONS.values():
shortcut = operation.keyboard_shortcut
operation.parsed_shortcuts = parse_key(shortcut)
class App:
def build(self):
root = tk.Tk()
self.root = root
# Appearance.
root.configure(background=Color.CANVAS_BG)
root.title("FuAlign")
root.resizable(False, False)
root.attributes("-topmost", True)
root.option_add("*background", Color.CANVAS_BG)
root.option_add("*foreground", Color.TEXT)
# Setting up layout.
root.rowconfigure(index=1, weight=0) # HEADER ROW
for idx in range(len(GROUPS)):
root.rowconfigure(index=idx + 2, weight=1)
root.rowconfigure(index=idx + 2, weight=0) # FOOTER ROW
# Creating UIRows.
header = tk.Frame(pady=10, height=10)
footer = tk.Label(pady=10, height=1)
self.ui_rows: dict[str, UIRow] = {}
for group_name in GROUPS:
ui_row = UIRow(self.root, tk.Frame(padx=10), group_name)
self.ui_rows[group_name] = ui_row
ui_row.title_var.set(group_name.capitalize())
# Gridding rows
header.grid(row=1)
for idx, row in enumerate(self.ui_rows.values()):
row.frame.grid(row=idx + 2)
footer.grid(row=idx + 3)
# Configuring row grids.
for row in self.ui_rows.values():
row.frame.rowconfigure(index=1, weight=0)
row.frame.rowconfigure(index=2, weight=1)
row.frame.columnconfigure(index=1, weight=0)
row.frame.columnconfigure(index=2, weight=0)
row.frame.columnconfigure(index=3, weight=0)
row.frame.columnconfigure(index=4, weight=1)
# Creating UIElements.
self.ui_elements: dict[str, UIElement] = {}
for group in GROUPS:
operations = [op for op in OPERATIONS.values() if op.group == group]
for idx, operation in enumerate(operations):
ui_element = UIElement(
parent=self.ui_rows[operation.group],
name=operation.id,
row=2,
col=idx + 1,
operation=operation,
key=operation.keyboard_shortcut,
)
ui_element.icon_dims = operation.icon_dims
self.ui_elements[ui_element.name] = ui_element
# Creating Canvases for the icons
for el in self.ui_elements.values():
el.canvas = tk.Canvas(
el.parent.frame,
width=20,
height=20,
background=Color.CANVAS_BG,
highlightthickness=0,
relief="ridge",
bd=0,
)
el.canvas.grid(row=el.row, column=el.col, sticky=tk.W, padx=10, pady=5)
# Updates canvas before drawing icons.
for el in self.ui_elements.values():
el.canvas.update()
# Draws icons.
for el in self.ui_elements.values():
el.draw_icon(fill=Color.NORMAL, outline=Color.CANVAS_BG, tag="icon")
# Binds canvases.
for el in self.ui_elements.values():
el.canvas.bind("<Enter>", el.describe, add="+")
el.canvas.bind("<Leave>", el.undescribe, add="+")
el.canvas.bind(
"<ButtonRelease-1>",
lambda e, name=el.name: OPERATIONS[name].execute(),
add="+",
)
# Binds root for keyboard shortcuts
for op in OPERATIONS.values():
name, shortcuts = op.id, op.parsed_shortcuts
for shortcut in shortcuts:
root.bind(shortcut, lambda e, name=name: OPERATIONS[name].execute())
# Window size and position
window_width = root.winfo_width()
window_height = root.winfo_height()
# get the screen dimension
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# find the center point
x = int((screen_width / 2 - window_width / 2))
y = int((screen_height / 2 - window_height / 2))
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
def run(self):
self.build()
self.root.mainloop()
if __name__ == "__main__":
app = App()
app.run()