|
| 1 | +importmath |
| 2 | +importlvglaslv |
| 3 | +importmpos.util |
| 4 | + |
| 5 | +defget_object_center(obj): |
| 6 | +"""Calculate the center (x, y) of an object.""" |
| 7 | +width=obj.get_width() |
| 8 | +height=obj.get_height() |
| 9 | +x=obj.get_x() |
| 10 | +y=obj.get_y() |
| 11 | +center_x=x+width/2 |
| 12 | +center_y=y+height/2 |
| 13 | +returncenter_x,center_y |
| 14 | + |
| 15 | +defcompute_angle_to_object(from_obj,to_obj): |
| 16 | +"""Compute the clockwise angle (degrees) from from_obj's center to to_obj's center (0° = UP).""" |
| 17 | +# Get centers |
| 18 | +from_x,from_y=get_object_center(from_obj) |
| 19 | +to_x,to_y=get_object_center(to_obj) |
| 20 | + |
| 21 | +# Compute vector |
| 22 | +dx=to_x-from_x |
| 23 | +dy=to_y-from_y |
| 24 | + |
| 25 | +# Calculate angle (0° = UP, 90° = RIGHT, clockwise) |
| 26 | +angle_rad=math.atan2(-dx,dy)# -dx, dy for 0° = UP |
| 27 | +angle_deg=math.degrees(angle_rad) |
| 28 | +return (angle_deg+360)%360# Normalize to [0, 360) |
| 29 | + |
| 30 | +deffind_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 | +forobjnrinrange(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 | + |
| 44 | +ifnotcurrent_focused: |
| 45 | +print("No current focused object.") |
| 46 | +returnNone |
| 47 | + |
| 48 | +print(f"Current focused object:{current_focused}") |
| 49 | +print(f"Default focus group has{focus_group.get_obj_count()} items") |
| 50 | + |
| 51 | +closest_obj=None |
| 52 | +min_distance=float('inf') |
| 53 | + |
| 54 | +# Iterate through objects in the focus group |
| 55 | +forobjnrinrange(focus_group.get_obj_count()): |
| 56 | +obj=focus_group.get_obj_by_index(objnr) |
| 57 | +ifobjiscurrent_focused: |
| 58 | +print(f"Skipping{obj} because it's the currently focused object.") |
| 59 | +continue |
| 60 | + |
| 61 | +# Compute angle to the object |
| 62 | +angle_deg=compute_angle_to_object(current_focused,obj) |
| 63 | +print(f"angle_deg is{angle_deg}") |
| 64 | + |
| 65 | +# Check if object is in the desired direction (within ±angle_tolerance) |
| 66 | +angle_diff=min((angle_deg-direction_degrees)%360, (direction_degrees-angle_deg)%360) |
| 67 | +ifangle_diff<=angle_tolerance: |
| 68 | +# Calculate Euclidean distance |
| 69 | +current_x,current_y=get_object_center(current_focused) |
| 70 | +obj_x,obj_y=get_object_center(obj) |
| 71 | +distance=math.sqrt((obj_x-current_x)**2+ (obj_y-current_y)**2) |
| 72 | + |
| 73 | +# Update closest object if this one is closer |
| 74 | +ifdistance<min_distance: |
| 75 | +min_distance=distance |
| 76 | +closest_obj=obj |
| 77 | + |
| 78 | +# Result |
| 79 | +ifclosest_obj: |
| 80 | +print(f"Closest object in direction{direction_degrees}°:{closest_obj}") |
| 81 | +else: |
| 82 | +print(f"No object found in direction{direction_degrees}°") |
| 83 | + |
| 84 | +returnclosest_obj |