22import lvgl as lv
33import mpos .util
44
5+ import math
6+
57def get_object_center (obj ):
68"""Calculate the center (x, y) of an object based on its position and size."""
79width = obj .get_width ()
@@ -30,9 +32,19 @@ def compute_angle_to_object(from_obj, to_obj):
3032angle_deg = math .degrees (angle_rad )
3133return (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+
3344def 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):
5466if obj is None or obj is current_focused :
5567return
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
7386for childnr in range (obj .get_child_count ()):
7487child = obj .get_child (childnr )
7588process_object (child ,depth + 1 )
@@ -87,7 +100,6 @@ def process_object(obj, depth=0):
87100
88101return closest_obj
89102
90-
91103# This function is missing so emulate it using focus_next():
92104def emulate_focus_obj (focusgroup ,target ):
93105for objnr in range (focusgroup .get_obj_count ()):