Skip to content

Commit

Permalink
fix: Use combo bounding boxes for offset calculations
Browse files Browse the repository at this point in the history
Fixes #70
  • Loading branch information
caksoylar committed Mar 11, 2024
1 parent 6defcaf commit 6f5524a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions keymap_drawer/draw/combo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def _draw_line_dendron(self, p_1: Point, p_2: Point, shorten: float) -> None:
line = f"l{round(diff.x)},{round(diff.y)}"
self.out.write(f'<path d="{start} {line}" class="combo"/>\n')

def print_combo(self, combo: ComboSpec, combo_ind: int) -> Point:
def print_combo(self, combo: ComboSpec, combo_ind: int) -> tuple[Point, Point]: # pylint: disable=too-many-locals
"""
Print SVG code for a rectangle with text representing a combo specification, which contains the key positions
that trigger it and what it does when triggered. The position of the rectangle depends on the alignment
specified, along with whether dendrons are drawn going to each key position from the combo.
Returns the midpoint of the combo box, for bounding box calculations.
Returns top-left/bottom-right coordinates of bounding box that contains the combo.
"""
p_keys = [self.layout.keys[p] for p in combo.key_positions]
width, height = combo.width or self.cfg.combo_w, combo.height or self.cfg.combo_h
Expand Down Expand Up @@ -156,7 +156,11 @@ def print_combo(self, combo: ComboSpec, combo_ind: int) -> Point:
self.out.write("</g>\n")

self.out.write("</g>\n")
return p

# calculate bounding box coordinates by creating a temporary PhysicalKey
combo_key = PhysicalKey(p, width, height, combo.rotation)
dims = 0.5 * Point(combo_key.bounding_width, combo_key.bounding_height)
return p - dims, p + dims

def print_combos_for_layer(self, combos: Sequence[ComboSpec]) -> tuple[float | None, float | None]:
"""
Expand All @@ -166,7 +170,7 @@ def print_combos_for_layer(self, combos: Sequence[ComboSpec]) -> tuple[float | N
combo_pts = []
for combo_ind, combo_spec in enumerate(combos):
combo_pts.append(self.print_combo(combo_spec, combo_ind))
return min((p.y for p in combo_pts), default=None), max((p.y for p in combo_pts), default=None)
return min((p.y for p, _ in combo_pts), default=None), max((p.y for _, p in combo_pts), default=None)

def create_combo_diagrams(
self, scale_factor: int, ghost_keys: Sequence[int] | None = None
Expand Down

0 comments on commit 6f5524a

Please sign in to comment.