Advertisement
iRadEntertainment

Untitled

Jun 23rd, 2024
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2. class_name RSMouseTracker
  3.  
  4. var main: RSMain
  5.  
  6. var m_pos: Vector2
  7. var is_gui_active := false
  8. var is_on_a_control_node := false
  9. var is_on_a_window := false
  10. var is_on_title_bar := false
  11. var is_on_window_decoration := false
  12.  
  13. signal mouse_track_updated(is_passthrough)
  14.  
  15. func _ready() -> void:
  16.     set_process(false)
  17.  
  18. func start(_main : RSMain):
  19.     main = _main
  20.     set_process(true)
  21.  
  22. func _process(_d) -> void:
  23.     if not main:
  24.         printerr("RSMouseTracker: RSMain not assigned.")
  25.         return
  26.     m_pos = main.get_global_mouse_position()
  27.     is_on_title_bar = is_m_pos_in_title(m_pos)
  28.     is_on_window_decoration = is_m_pos_on_window_decoration(m_pos)
  29.     is_on_a_window = is_m_pos_in_window(m_pos)
  30.    
  31.     if is_on_title_bar or is_on_window_decoration: is_on_a_control_node = false
  32.     else: is_on_a_control_node = is_m_pos_in_control_nodes(m_pos)
  33.    
  34.     var checks = (is_on_a_control_node or is_on_title_bar or is_on_window_decoration or is_on_a_window)
  35.     if is_gui_active != checks:
  36.         is_gui_active = checks
  37.         mouse_track_updated.emit(!is_gui_active)
  38.  
  39. func is_pixel_transparent(pos: Vector2) -> bool:
  40.     if !main.get_rect().has_point(pos):
  41.         return true
  42.     return !get_window().get_texture().get_image().get_pixelv(pos).a > 0
  43.  
  44. func is_m_pos_in_control_nodes(pos: Vector2) -> bool:
  45.     Rect2i().has_point(pos)
  46.    
  47.     if !main.get_rect().has_point(pos):
  48.         return false
  49.     for ctr: Control in get_tree().get_nodes_in_group("UI"):
  50.         if !ctr.is_visible_in_tree(): continue
  51.         var pos_rotated = pos
  52.         var ctr_angle = ctr.get_global_transform().get_rotation()
  53.         if ctr_angle != 0:
  54.             pos_rotated = (pos - ctr.global_position).rotated(-ctr_angle) + ctr.global_position
  55.        
  56.         if ctr.get_global_rect().has_point(pos_rotated):
  57.             return true
  58.     return false
  59.  
  60.  
  61. func is_m_pos_in_window(pos: Vector2) -> bool:
  62.     if !main.get_rect().has_point(pos):
  63.         return false
  64.     for window: Window in get_tree().get_nodes_in_group("UIWindows"):
  65.         if not window.visible: continue
  66.         var win_rect := Rect2i(window.get_position_with_decorations(), window.get_size_with_decorations())
  67.         if win_rect.has_point(pos):
  68.             return true
  69.     return false
  70.  
  71.  
  72. func is_m_pos_in_title(pos: Vector2) -> bool:
  73.     if DisplayServer.window_get_flag(DisplayServer.WINDOW_FLAG_BORDERLESS):
  74.         return false
  75.     var title_size: Vector2i = DisplayServer.window_get_title_size("App")
  76.     @warning_ignore("narrowing_conversion")
  77.     title_size = Vector2i(main.get_rect().size.x, 30)
  78.     var title_rect := Rect2i(Vector2i(0, -title_size.y), title_size)
  79.    
  80.     return title_rect.has_point(Vector2i(pos))
  81.  
  82. func is_m_pos_on_window_decoration(pos: Vector2) -> bool:
  83.     if DisplayServer.window_get_flag(DisplayServer.WINDOW_FLAG_BORDERLESS):
  84.         return false
  85.     var window_rect := Rect2i(Vector2i(), DisplayServer.window_get_size())
  86.    
  87.     var title_size: Vector2i = DisplayServer.window_get_title_size("App")
  88.     var decorations_size := DisplayServer.window_get_size_with_decorations()
  89.     var decorations_pos := Vector2i()
  90.     @warning_ignore("integer_division")
  91.     decorations_pos.x = -(decorations_size.x - window_rect.size.x)/2
  92.     decorations_pos.y = - title_size.y - 2 #px
  93.     var window_rect_decorations := Rect2i(decorations_pos, decorations_size)
  94.    
  95.     return window_rect_decorations.has_point(pos) and not window_rect.has_point(pos)
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement