Skip to content

Commit 1e77d7f

Browse files
focus_direction: also consider children
1 parent 46d51ab commit 1e77d7f

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

internal_filesystem/lib/mpos/ui/focus_direction.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import lvgl as lv
33
import mpos.util
44

5+
import math
6+
57
def get_object_center(obj):
68
"""Calculate the center (x, y) of an object based on its position and size."""
79
width = obj.get_width()
@@ -30,9 +32,19 @@ def compute_angle_to_object(from_obj, to_obj):
3032
angle_deg = math.degrees(angle_rad)
3133
return (angle_deg + 360) % 360 # Normalize to [0, 360)
3234

35+
def is_object_in_focus_group(focus_group, obj):
36+
"""Check if an object is in the focus group."""
37+
if obj is None:
38+
return False
39+
for objnr in range(focus_group.get_obj_count()):
40+
if focus_group.get_obj_by_index(objnr) is obj:
41+
return True
42+
return False
43+
3344
def find_closest_obj_in_direction(focus_group, current_focused, direction_degrees, angle_tolerance=45):
3445
"""
35-
Find the closest object (including children) in the specified direction from the current focused object.
46+
Find the closest object in the specified direction from the current focused object.
47+
Only considers objects that are in the focus_group (including children of any object).
3648
Direction is in degrees: 0° = UP, 90° = RIGHT, 180° = DOWN, 270° = LEFT (clockwise).
3749
Returns the closest object within ±angle_tolerance of direction_degrees, or None.
3850
"""
@@ -54,22 +66,23 @@ def process_object(obj, depth=0):
5466
if obj is None or obj is current_focused:
5567
return
5668

57-
# Compute angle to the object
58-
angle_deg = compute_angle_to_object(current_focused, obj)
59-
60-
# Check if object is in the desired direction (within ±angle_tolerance)
61-
angle_diff = min((angle_deg - direction_degrees) % 360, (direction_degrees - angle_deg) % 360)
62-
if angle_diff <= angle_tolerance:
63-
# Calculate Euclidean distance
64-
obj_x, obj_y = get_object_center(obj)
65-
distance = math.sqrt((obj_x - current_x)**2 + (obj_y - current_y)**2)
69+
# Check if the object is in the focus group and evaluate it
70+
if is_object_in_focus_group(focus_group, obj):
71+
# Compute angle to the object
72+
angle_deg = compute_angle_to_object(current_focused, obj)
6673

67-
# Update closest object if this one is closer
68-
if distance < min_distance:
69-
min_distance = distance
70-
closest_obj = obj
74+
# Check if object is in the desired direction (within ±angle_tolerance)
75+
angle_diff = min((angle_deg - direction_degrees) % 360, (direction_degrees - angle_deg) % 360)
76+
if angle_diff <= angle_tolerance:
77+
# Calculate Euclidean distance
78+
obj_x, obj_y = get_object_center(obj)
79+
distance = math.sqrt((obj_x - current_x)**2 + (obj_y - current_y)**2)
80+
# Update closest object if this one is closer
81+
if distance < min_distance:
82+
min_distance = distance
83+
closest_obj = obj
7184

72-
# Process children
85+
# Process children regardless of parent's focus group membership
7386
for childnr in range(obj.get_child_count()):
7487
child = obj.get_child(childnr)
7588
process_object(child, depth + 1)
@@ -87,7 +100,6 @@ def process_object(obj, depth=0):
87100

88101
return closest_obj
89102

90-
91103
# This function is missing so emulate it using focus_next():
92104
def emulate_focus_obj(focusgroup, target):
93105
for objnr in range(focusgroup.get_obj_count()):

0 commit comments

Comments
 (0)