Advertisement
Guest User

Wheel Menu Script

a guest
Jun 15th, 2019
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. extends Sprite
  2.  
  3. var time_start = 0 # The time since the application was started
  4. var elapsed_time = 0 # The time since time start was set
  5.  
  6. var outer_r = 0.45 # The outer radius of the wheel
  7. var inner_r = 0.2 # The inner radius of the wheel
  8.  
  9. # The colours of each segment starting from the bottom right and going clockwise
  10. var colours = [Color(1.0, 1.0, 1.0), Color(1.0, 0.0, 0.0), Color(0.0, 1.0, 0.0), Color(0.0, 0.0, 1.0)]
  11.  
  12. var no_of_colours = len(colours) # Number of colours in the colours array
  13. var selected_colour = -1 # The index of the selected colour within the colours array
  14. var old_selected_colour = 0 # Used to track if the selected colour was changed so the animation can be restarted
  15.  
  16. var size = Vector2()
  17. var mouse_pos = Vector2()
  18. var rel_mouse_pos = Vector2() # Mouse position relative to the position of the wheel
  19. var angle = 0 # The angle, in radians, starting from the right, going clockwise
  20. var norm_angle = 0 # The angle, but normalized (from 0 to 1)
  21.  
  22. var mouse_entered = false # If the mouse has entered the wheel
  23.  
  24. var mat # The shader material
  25.  
  26. signal colour_change # The signal corresponding to if the selection has changed
  27. signal mouse_exited # The signal corresponding to if the mouse has exited the wheel area. Used to make sure that the colour doesn't change
  28.  
  29. func _ready():
  30. # These signals are connected to another script that spawns this wheel
  31. if get_parent():
  32. connect("colour_change", get_parent().get_parent(), "_on_SelectionWheel_colour_change")
  33. connect("mouse_exited", get_parent().get_parent(), "_on_SelectionWheel_mouse_exited")
  34. size = get_texture().get_size()
  35. mat = self.get_material()
  36. mat.set_shader_param("no_of_colours", no_of_colours)
  37. mat.set_shader_param("initial_outer_r", outer_r)
  38. mat.set_shader_param("inner_r", inner_r)
  39.  
  40. # Sets the colours of the colour variables in the shader according to the colours array within this script
  41. for i in range(no_of_colours):
  42. mat.set_shader_param("colour"+str(i), Vector3(colours[i].r, colours[i].g, colours[i].b))
  43.  
  44. func _process(delta):
  45. mouse_pos = get_global_mouse_position()
  46.  
  47. rel_mouse_pos = mouse_pos - position
  48. angle = atan2(rel_mouse_pos.y, rel_mouse_pos.x) # Calculates the angle in radians
  49. norm_angle = angle/(2*PI)
  50.  
  51. # Without this if statement, the angle goes from 0 to 0.5 on the bottom half and -0.5 to 0 on the top half, clockwise.
  52. # The if statement is used to make sure it goes from 0 to 1 clockwise all the way around the wheel
  53. if norm_angle < 0:
  54. norm_angle += 1
  55.  
  56. # Selects the appropriate colour according to where the mouse is
  57. for i in range(no_of_colours):
  58. if ((norm_angle > float(i)/float(no_of_colours)) and (norm_angle < float(i+1)/float(no_of_colours))):
  59. selected_colour = i
  60.  
  61. # The radii are multiplied by the size of the sprite and the scale to make it work for any texture and scale.
  62. # This assumes that the texture used is a SQUARE.
  63. if (rel_mouse_pos.length() < outer_r*size.x*scale.x) and (rel_mouse_pos.length() > inner_r*size.x*scale.x):
  64.  
  65. # This condition is met either when the mouse enters the wheel's bounding area or a new colour is hovered over.
  66. # This is to make sure that the animation starts from the initial position of the segment
  67. if (not mouse_entered) or (selected_colour != old_selected_colour):
  68. time_start = OS.get_ticks_msec()
  69. old_selected_colour = selected_colour
  70. mouse_entered = true
  71.  
  72. elapsed_time = OS.get_ticks_msec() - time_start
  73. mat.set_shader_param("selected_colour", selected_colour)
  74. mat.set_shader_param("elapsed_time", elapsed_time/1000.0) # 1000.0 was chosen as it made the animation an appropriate speed
  75.  
  76. emit_signal("colour_change", colours[selected_colour]) # Emits a signal with the selected colour
  77. else:
  78. emit_signal("mouse_exited")
  79. mouse_entered = false
  80. mat.set_shader_param("selected_colour", -1) # Sets the selected colour to be nothing as the mouse isn't hovering over the wheel anymore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement