22import lvgl as lv
33import mpos .util
44
5+
6+ # This function is missing so emulate it using focus_next():
7+ def emulate_focus_obj (focusgroup ,target ):
8+ for objnr in range (focusgroup .get_obj_count ()):
9+ currently_focused = focusgroup .get_focused ()
10+ #print ("emulate_focus_obj: currently focused:") ; mpos.util.print_lvgl_widget(currently_focused)
11+ if currently_focused is target :
12+ print ("emulate_focus_obj: found target, stopping" )
13+ return
14+ else :
15+ focusgroup .focus_next ()
16+ print ("WARNING: emulate_focus_obj failed to find target" )
17+
518def get_object_center (obj ):
619"""Calculate the center (x, y) of an object."""
720width = obj .get_width ()
@@ -27,42 +40,25 @@ def compute_angle_to_object(from_obj, to_obj):
2740angle_deg = math .degrees (angle_rad )
2841return (angle_deg + 360 )% 360 # Normalize to [0, 360)
2942
30- def find_closest_obj_in_direction (direction_degrees ,angle_tolerance = 45 ):
31- print (f"default focus group has{ lv .group_get_default ().get_obj_count ()} items" )
32- focusgroup = lv .group_get_default ()
33- for objnr in range (focusgroup .get_obj_count ()):
34- obj = focusgroup .get_obj_by_index (objnr )
35- print ("checking obj for equality..." )
36- mpos .util .print_lvgl_widget (obj )
37- print (f"current focus object:{ lv .group_get_default ().get_focused ()} " )
38-
39- """Find the closest object in the specified direction from the current focused object."""
40- # Get focus group and current focused object
41- focus_group = lv .group_get_default ()
42- current_focused = focus_group .get_focused ()
43-
43+ def find_closest_obj_in_direction (focus_group ,current_focused ,direction_degrees ,angle_tolerance = 45 ):
4444if not current_focused :
4545print ("No current focused object." )
4646return None
47-
48- print (f"Current focused object:{ current_focused } " )
49- print (f"Default focus group has{ focus_group .get_obj_count ()} items" )
50-
47+
5148closest_obj = None
5249min_distance = float ('inf' )
5350
5451# Iterate through objects in the focus group
5552for objnr in range (focus_group .get_obj_count ()):
5653obj = focus_group .get_obj_by_index (objnr )
5754if obj is current_focused :
58- print (f"Skipping{ obj } because it's the currently focused object." )
55+ # print(f"Skipping {obj} because it's the currently focused object.")
5956continue
6057
6158# Compute angle to the object
6259angle_deg = compute_angle_to_object (current_focused ,obj )
63- print (f"angle_deg is{ angle_deg } for" )
64- mpos .util .print_lvgl_widget (obj )
65-
60+ #print(f"angle_deg is {angle_deg} for") ; mpos.util.print_lvgl_widget(obj)
61+
6662# Check if object is in the desired direction (within ±angle_tolerance)
6763angle_diff = min ((angle_deg - direction_degrees )% 360 , (direction_degrees - angle_deg )% 360 )
6864if angle_diff <= angle_tolerance :
@@ -75,11 +71,30 @@ def find_closest_obj_in_direction(direction_degrees, angle_tolerance=45):
7571if distance < min_distance :
7672min_distance = distance
7773closest_obj = obj
78-
74+
7975# Result
8076if closest_obj :
8177print (f"Closest object in direction{ direction_degrees } °:{ closest_obj } " )
8278else :
8379print (f"No object found in direction{ direction_degrees } °" )
8480
8581return closest_obj
82+
83+ def move_focus_direction (angle ):
84+ focus_group = lv .group_get_default ()
85+ if not focus_group :
86+ print ("move_focus_direction: no default focus_group found, returning..." )
87+ return
88+ current_focused = focus_group .get_focused ()
89+ if not current_focused :
90+ print ("move_focus_direction: nothing is focused, choosing the next thing" )
91+ focus_group .focus_next ()
92+ current_focused = focus_group .get_focused ()
93+ if isinstance (current_focused ,lv .keyboard ):
94+ print ("focus is on a keyboard, which has its own move_focus_direction: NOT moving" )
95+ return
96+ o = find_closest_obj_in_direction (focus_group ,current_focused ,angle )
97+ if o :
98+ print ("move_focus_direction: moving focus to:" )
99+ mpos .util .print_lvgl_widget (o )
100+ emulate_focus_obj (focus_group ,o )