├── VirtualJoystick.png ├── LICENSE ├── README.md └── VirtualJoystick.gd /VirtualJoystick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jon-Davis/Godot-Virtual-Joystick/HEAD/VirtualJoystick.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jonathon Davis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Godot-Virtual-Joystick 2 | A simple, highly customizable, virtual joystick for Android and IOS on the Godot Game Engine that supports multitouch 3 | ![alt text](VirtualJoystick.png "Example Virtual Joystick") 4 | 5 | # Install 6 | Create a Control Node and add this script onto it 7 | 8 | # Use 9 | get_value - returns the value of the virtual joystick, a vector2 ranging from -1,-1 to 1,1 10 | 11 | is_active - returns whether or not the virtual joystick is receiving input. 12 | 13 | ## MISC 14 | Persistent - If persistent is set to true then the virtual joystick will always be visable, If it is set to false then the virtual joystick will appear whenever a spot inside of the control node is clicked and will originate from the spot clicked. 15 | 16 | Start Pos - If persistent then start pos is the initial position of the virtual joystick 17 | 18 | Mouse Trigger - Defaults to left mouse button, determines what mouse button is used to control the virtual joystick, useful for debuging 19 | 20 | ## BACKGROUND 21 | Background Color - What color the background circle will be 22 | 23 | Background Stroke Width - The width of the background circles stroke 24 | 25 | Background Stroke Color - The color of the background circles stroke 26 | 27 | Background Radius - The radius of the background circle 28 | 29 | Background Resolution - The number of points used to approximate the background circle 30 | 31 | ## Foreground 32 | Foreground Color - What color the foreground circle will be 33 | 34 | Foreground Stroke Width - The width of the foreground circles stroke 35 | 36 | Foreground Stroke Color - The color of the foreground circles stroke 37 | 38 | Foreground Radius - The radius of the foreground circle 39 | 40 | Foreground Resolution - The number of points used to approximate the foreground circle 41 | -------------------------------------------------------------------------------- /VirtualJoystick.gd: -------------------------------------------------------------------------------- 1 | extends Control 2 | 3 | # boolean to check for mouse events 4 | export var persistent = false 5 | export var mouse_trigger = BUTTON_LEFT 6 | 7 | #fields for the background circle 8 | export var background_color = Color(0.5,0.5,0.5,0.5) 9 | export var background_stroke_width = 1 10 | export var background_stroke_color = Color(1,1,1) 11 | export var background_radius = 50 12 | export var background_resolution = 64 13 | 14 | #fields for the forground circle 15 | export var foreground_color = Color(1,1,1) 16 | export var foreground_stroke_width = 1 17 | export var foreground_stroke_color = Color(0,0,0) 18 | export var foreground_radius = 10 19 | export var foreground_resolution = 64 20 | 21 | # fake index for mouse 22 | const MOUSE_INDEX = -2 23 | # stores the current index that represents the finger controlling the analog controller 24 | var index = -1 25 | # stores the inital down position 26 | export var start_pos = Vector2(0,0) 27 | # stores the current down position 28 | var curr_pos = start_pos 29 | # stores the current vector from -1,-1 to 1,1 30 | var value = Vector2(0,0) 31 | 32 | func _ready(): 33 | curr_pos = start_pos 34 | 35 | func get_value(): 36 | return value 37 | 38 | func is_active(): 39 | return index != -1 40 | 41 | # Handles touch for the virtual analog controller and supports multi-touch 42 | func _input(event): 43 | if event is InputEventScreenTouch: 44 | handle_press(event,event.get_index()) 45 | elif event is InputEventMouseButton and event.get_button_index() == mouse_trigger: 46 | handle_press(event,MOUSE_INDEX) 47 | # if the current touch controller moves 48 | elif (event is InputEventScreenDrag and event.get_index() == index) or (event is InputEventMouseMotion and index == MOUSE_INDEX): 49 | var dist = (event.get_position() - start_pos).clamped(background_radius); 50 | curr_pos = start_pos + dist 51 | value = (dist / background_radius).reflect(Vector2(1,0)) 52 | self.update() 53 | 54 | func handle_press(event, _index): 55 | # if the current touch is empty and a new valid touch is recv 56 | if index == -1 and event.is_pressed() and self.get_rect().has_point(event.get_position()): 57 | index = _index 58 | if not persistent: 59 | start_pos = event.get_position() 60 | curr_pos = start_pos 61 | self.update() 62 | # if the current touch controlling the virtual analog controller is released 63 | elif (index == _index) and not event.is_pressed(): 64 | index = -1 65 | curr_pos = start_pos 66 | value = Vector2(0,0) 67 | self.update() 68 | 69 | func _draw(): 70 | if index != -1 or persistent: 71 | var radius = 50 72 | _draw_circle(start_pos, background_radius, background_color, background_resolution, background_stroke_width, background_stroke_color) 73 | _draw_circle(curr_pos, foreground_radius, foreground_color, foreground_resolution, foreground_stroke_width, foreground_stroke_color) 74 | 75 | func _draw_circle(center, radius, color, nb_points, stroke_width, stroke_color): 76 | var points_arc = PoolVector2Array() 77 | points_arc.push_back(center) 78 | var colors = PoolColorArray([color]) 79 | for i in range(nb_points+1): 80 | var angle_point = deg2rad(i * 360 / nb_points - 90) 81 | points_arc.push_back(center + Vector2(cos(angle_point), sin(angle_point)) * radius) 82 | draw_polygon(points_arc, colors) 83 | if (stroke_width > 0): 84 | points_arc.remove(0) 85 | draw_polyline(points_arc, stroke_color) 86 | --------------------------------------------------------------------------------