Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanrhu committed Aug 29, 2020
0 parents commit 25f8b19
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
46 changes: 46 additions & 0 deletions README.md
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
122 changes: 122 additions & 0 deletions VirtualJoystick.gd
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
16 changes: 16 additions & 0 deletions VirtualJoystickPlugin.gd
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")
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions plugin.cfg
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"
Binary file added ss.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added texture-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 25f8b19

Please sign in to comment.