Skip to content

Commit de53d68

Browse files
focus_direction: use absolute coordinates
1 parent 697b8fc commit de53d68

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

internal_filesystem/lib/mpos/ui/focus_direction.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
import lvgl as lv
33
import mpos.util
44

5+
import math
6+
import mpos.util
57

68
def get_object_center(obj):
7-
"""Calculate the center (x, y) of an object based on its position and size."""
8-
width = obj.get_width()
9-
height = obj.get_height()
10-
x = obj.get_x()
11-
y = obj.get_y()
9+
"""Calculate the center (x, y) of an object based on its absolute screen coordinates."""
10+
obj_area = lv.area_t()
11+
obj.get_coords(obj_area)
12+
width = obj_area.x2 - obj_area.x1
13+
height = obj_area.y2 - obj_area.y1
14+
x = obj_area.x1
15+
y = obj_area.y1
1216
center_x = x + width / 2
1317
center_y = y + height / 2
1418
return center_x, center_y
@@ -27,7 +31,7 @@ def compute_angle_to_object(from_obj, to_obj):
2731
dy = to_y - from_y
2832

2933
# Calculate angle (0° = UP, 90° = RIGHT, 180° = DOWN, 270° = LEFT)
30-
angle_rad = math.atan2(dx, -dy) # Fixed for correct convention
34+
angle_rad = math.atan2(dx, -dy)
3135
angle_deg = math.degrees(angle_rad)
3236
return (angle_deg + 360) % 360 # Normalize to [0, 360)
3337

@@ -45,12 +49,14 @@ def get_closest_edge_point_and_distance(from_x, from_y, obj, direction_degrees,
4549
Calculate the distance to the closest edge point on obj, and check if its angle is within the direction cone.
4650
Returns (distance, closest_x, closest_y, angle_deg) or None if not in direction or inside.
4751
"""
48-
x = obj.get_x()
49-
y = obj.get_y()
50-
width = obj.get_width()
51-
height = obj.get_height()
52-
right = x + width
53-
bottom = y + height
52+
obj_area = lv.area_t()
53+
obj.get_coords(obj_area)
54+
x = obj_area.x1
55+
y = obj_area.y1
56+
right = obj_area.x2
57+
bottom = obj_area.y2
58+
width = right - x
59+
height = bottom - y
5460

5561
# Clamp to the rect bounds to find closest point
5662
closest_x = max(x, min(from_x, right))
@@ -70,15 +76,15 @@ def get_closest_edge_point_and_distance(from_x, from_y, obj, direction_degrees,
7076
distance = math.sqrt(dx**2 + dy**2)
7177

7278
# Compute angle to the closest point (using same convention)
73-
angle_rad = math.atan2(dx, -dy) # Fixed
79+
angle_rad = math.atan2(dx, -dy)
7480
angle_deg = math.degrees(angle_rad)
7581
angle_deg = (angle_deg + 360) % 360
7682

7783
# Check if in direction cone (±45°)
7884
angle_diff = min((angle_deg - direction_degrees) % 360, (direction_degrees - angle_deg) % 360)
7985
if angle_diff > 45:
8086
if debug:
81-
print(f" {obj} at ({x}, {y}) size ({width}x{height}): closest point ({closest_x:.1f}, {closest_y:.1f}), angle {angle_deg:.1f}°, diff {angle_diff:.1f}° > 45, skipped")
87+
print(f" {obj} at ({x}, {y}) size ({width}x{height}): closest point ({closest_x:.1f}, {closest_y:.1f}), angle {angle_deg:.1f}°, diff {angle_diff:.1f}° > 45°, skipped")
8288
return None
8389

8490
if debug:
@@ -145,6 +151,18 @@ def process_object(obj, depth=0):
145151

146152
return closest_obj
147153

154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
148166
# This function is missing so emulate it using focus_next():
149167
def emulate_focus_obj(focusgroup, target):
150168
for objnr in range(focusgroup.get_obj_count()):

internal_filesystem/lib/mpos/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ def print_lvgl_widget(obj, depth=0):
1616
if obj:
1717
label = ""
1818
hidden = ""
19+
obj_area = lv.area_t()
20+
obj.get_coords(obj_area)
1921
if obj.has_flag(lv.obj.FLAG.HIDDEN):
2022
hidden = "hidden "
2123
if isinstance(obj,lv.label):
2224
label = f" has label '{obj.get_text()}'"
2325
padding = " " * depth
24-
print(f"{padding}{hidden}{obj} with size {obj.get_width()}x{obj.get_height()}{label}")
26+
print(f"{padding}{hidden}{obj} with abs position {obj_area.x1}x{obj_area.y1} and size {obj_area.get_width()}x{obj_area.get_height()}{label}")
2527
for childnr in range(obj.get_child_count()):
2628
print_lvgl_widget(obj.get_child(childnr), depth+1)
2729
else:

0 commit comments

Comments
 (0)