-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 25f8b19
Showing
8 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Oğuzhan Eroğlu <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# virtual-joystick | ||
Virtual joystick plugin for Godot Engine. | ||
|
||
![Godot Virtual Joystick](ss.png) | ||
|
||
## Installation | ||
### GIT | ||
You can install the plugin with GIT. | ||
|
||
```bash | ||
cd /path/to/project | ||
mkdir -p addons | ||
cd addons | ||
git clone https://github.com/rohanrhu/virtual-joystick | ||
``` | ||
|
||
## Usage | ||
### Add a `Virtualjoystick` node. | ||
### Add a `CollisionShape2D` and set a `CircleShape2D` to that. | ||
### Set container and button textures | ||
![Texture Settings](texture-settings.png) | ||
|
||
## Events | ||
### `controlling` | ||
Fires during user control. | ||
|
||
### `trimming` | ||
Fires while button is trimming to container's border. | ||
|
||
### `released` | ||
Fires when user released the joystick. | ||
|
||
## Properties | ||
### `container_texture: Texture` | ||
### `button_texture: Texture` | ||
### `is_mouseover: bool` | ||
### `is_dragging: bool` | ||
### `is_trimming: bool` | ||
### `angle: float` | ||
### `velocity: Vector2` | ||
|
||
## Notices | ||
* While you are testing it on mouse, you may want to enable `Project Settings > General > Input Devices > Pointing > Emulate Touch From Mouse` setting. | ||
|
||
## License | ||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Virtual Joystick plugin for Godot Engine | ||
# | ||
# https://github.com/rohanrhu/virtual-joystick | ||
# | ||
# Licensed under MIT | ||
# Copyright (C) 2020, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <[email protected]> | ||
|
||
extends Area2D | ||
|
||
export(Texture) var container_texture | ||
export(Texture) var button_texture | ||
|
||
onready var shape = $CollisionShape2D | ||
onready var container | ||
onready var button | ||
|
||
var is_mouseover = false | ||
var is_dragging = false | ||
var is_trimming = false | ||
var angle = 0.0 | ||
var velocity = Vector2.ZERO | ||
var current_event = false | ||
|
||
signal controlling | ||
signal trimming | ||
signal released | ||
|
||
func _ready(): | ||
connect("mouse_entered", self, "_on_mouse_entered") | ||
connect("mouse_exited", self, "_on_mouse_exited") | ||
|
||
container = Sprite.new() | ||
button = Sprite.new() | ||
|
||
add_child(container) | ||
add_child(button) | ||
|
||
container.texture = container_texture | ||
button.texture = button_texture | ||
|
||
container.global_position = global_position | ||
button.global_position = container.global_position | ||
|
||
func _process(delta): | ||
pass | ||
|
||
func _input(event): | ||
if not (event is InputEventScreenDrag) and not (event is InputEventScreenTouch): | ||
return | ||
|
||
if current_event is InputEvent and current_event.index == event.index: | ||
current_event = event | ||
else: | ||
return | ||
|
||
if not is_mouseover: | ||
if event is InputEventScreenTouch: | ||
if not event.pressed: | ||
is_dragging = false | ||
is_trimming = false | ||
button.global_position = container.global_position | ||
velocity = Vector2.ZERO | ||
|
||
emit_signal("released") | ||
|
||
_process_button() | ||
|
||
func _input_event(viewport, event, shape_idx): | ||
if not (event is InputEventScreenDrag) and not (event is InputEventScreenTouch): | ||
return | ||
|
||
current_event = event | ||
|
||
if event is InputEventScreenTouch: | ||
is_dragging = event.pressed | ||
if not is_dragging: | ||
is_trimming = false | ||
button.global_position = container.global_position | ||
velocity = Vector2.ZERO | ||
angle = 0.0 | ||
|
||
emit_signal("released") | ||
|
||
_process_button() | ||
|
||
func _process_button(): | ||
if is_dragging: | ||
button.global_position = current_event.position | ||
|
||
var da = abs(button.global_position.x - container.global_position.x) | ||
var db = abs(button.global_position.y - container.global_position.y) | ||
var dh = sqrt(pow(da, 2) + pow(db, 2)); | ||
|
||
var a = button.global_position.x - container.global_position.x | ||
var b = button.global_position.y - container.global_position.y | ||
var h = sqrt(pow(a, 2) + pow(b, 2)); | ||
angle = atan2(a, b) | ||
|
||
is_trimming = dh > shape.shape.radius | ||
|
||
if is_trimming: | ||
var x = container.global_position.x + sin(angle) * shape.shape.radius | ||
var y = container.global_position.y + cos(angle) * shape.shape.radius | ||
|
||
button.global_position.x = x | ||
button.global_position.y = y | ||
|
||
var v = Vector2(sin(angle), cos(angle)) | ||
var is_changed = velocity != v | ||
velocity = v | ||
|
||
if is_changed: | ||
emit_signal("controlling") | ||
|
||
if is_trimming: | ||
emit_signal("trimming") | ||
|
||
func _on_mouse_entered(): | ||
is_mouseover = true | ||
|
||
func _on_mouse_exited(): | ||
is_mouseover = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Virtual Joystick plugin for Godot Engine | ||
# | ||
# https://github.com/rohanrhu/virtual-joystick | ||
# | ||
# Licensed under MIT | ||
# Copyright (C) 2020, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <[email protected]> | ||
|
||
tool | ||
|
||
extends EditorPlugin | ||
|
||
func _enter_tree(): | ||
add_custom_type("VirtualJoystick", "Area2D", preload("VirtualJoystick.gd"), preload("icon.png")) | ||
|
||
func _exit_tree(): | ||
remove_custom_type("Joystick") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[plugin] | ||
|
||
name="VirtualJoystick" | ||
description="Virtual joystick for Godot." | ||
author="Oğuzhan Eroğlu" | ||
version="1.0" | ||
script="VirtualJoystickPlugin.gd" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.