Advertisement
asseater64

Area2D mouse selection

Jun 28th, 2021
1,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Area2D
  2.  
  3. var is_selected = false setget on_is_selected
  4. signal area_selected
  5. signal area_deselected
  6.  
  7.  
  8. func _ready():
  9. # warning-ignore:return_value_discarded
  10.     connect("area_deselected", self, "on_area_deselected")
  11. # warning-ignore:return_value_discarded
  12.     connect("area_selected", self, "on_area_selected")
  13.     print(name, ": ", z_index)
  14.  
  15.  
  16. func _on_Area2D_mouse_entered():
  17.     emit_signal("area_selected", self)
  18.  
  19.  
  20. func _on_Area2D_mouse_exited():
  21.     emit_signal("area_deselected", self)
  22.  
  23.  
  24. func on_area_selected(area: Object):
  25.     if area == self:
  26.         var smb_selected = false
  27.         for a in get_overlapping_areas():
  28.             if a.is_selected == true:
  29.                 smb_selected = true
  30.         if not smb_selected:
  31.             self.is_selected = true
  32.  
  33.  
  34. func on_area_deselected(area: Object):
  35.     if area == self:
  36.         if is_selected == true:
  37.             self.is_selected = false
  38.            
  39.             for a in get_overlapping_areas():
  40.                 var space = get_world_2d().direct_space_state
  41.                 var mousePos = get_global_mouse_position()
  42.                 if not space.intersect_point(mousePos, 4, [], 2147483647, false, true).empty():
  43.                     print(space.intersect_point(mousePos, 4, [], 2147483647, false, true))
  44.                     for i in space.intersect_point(mousePos, 4, [], 2147483647, false, true):
  45.                         if i["collider"] == a:
  46.                             a.on_area_selected(a)
  47.  
  48.  
  49. func on_is_selected(value):
  50.     is_selected = value
  51.     if value == true:
  52.         z_index += 1
  53.     else:
  54.         z_index -= 1
  55.     print(name, ": ", z_index)
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement