mirror of
https://github.com/HarmonyHoney/tiny_crate.git
synced 2026-01-23 02:34:53 +00:00
using multitouch visually for joystick.gd! still using touchscreenbuttons (=
lil clean up
This commit is contained in:
parent
c2a973ba35
commit
9c4be69ef9
9 changed files with 8 additions and 252 deletions
|
|
@ -78,11 +78,6 @@ _global_script_classes=[ {
|
|||
"class": "SwitchBlock",
|
||||
"language": "GDScript",
|
||||
"path": "res://src/actor/SwitchBlock.gd"
|
||||
}, {
|
||||
"base": "Control",
|
||||
"class": "VirtualJoystick",
|
||||
"language": "GDScript",
|
||||
"path": "res://src/menu/joystick/virtual_joystick.gd"
|
||||
} ]
|
||||
_global_script_class_icons={
|
||||
"Actor": "",
|
||||
|
|
@ -98,8 +93,7 @@ _global_script_class_icons={
|
|||
"Player": "",
|
||||
"SaveDict": "",
|
||||
"Switch": "",
|
||||
"SwitchBlock": "",
|
||||
"VirtualJoystick": ""
|
||||
"SwitchBlock": ""
|
||||
}
|
||||
|
||||
[application]
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
[ext_resource path="res://src/autoload/touch_screen.gd" type="Script" id=1]
|
||||
[ext_resource path="res://media/image/circle.png" type="Texture" id=2]
|
||||
[ext_resource path="res://src/menu/joystick/round_button.gd" type="Script" id=3]
|
||||
[ext_resource path="res://src/menu/round_button.gd" type="Script" id=3]
|
||||
[ext_resource path="res://src/menu/joystick.gd" type="Script" id=4]
|
||||
[ext_resource path="res://media/font/NicoPaint-Regular.ttf" type="DynamicFontData" id=5]
|
||||
[ext_resource path="res://media/image/diamond16.png" type="Texture" id=7]
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ var joy = Vector2.ZERO
|
|||
var is_joy := false
|
||||
var last_val = -1
|
||||
var is_input = true
|
||||
var index := -1
|
||||
|
||||
func _input(event):
|
||||
if Engine.editor_hint or !is_input: return
|
||||
|
|
@ -21,14 +22,15 @@ func _input(event):
|
|||
var is_touch = event is InputEventScreenTouch
|
||||
var is_drag = event is InputEventScreenDrag
|
||||
|
||||
if (is_drag or is_touch):
|
||||
if (is_drag or is_touch) and (event.index == index or index == -1):
|
||||
vec = event.position - rect_global_position
|
||||
vl = vec.length()
|
||||
joy = vec.normalized()
|
||||
|
||||
is_joy = vl < max_range
|
||||
if is_touch and !event.pressed:
|
||||
is_joy = false
|
||||
if is_touch:
|
||||
if !event.pressed: is_joy = false
|
||||
index = event.index if is_joy else -1
|
||||
|
||||
vec = vec.limit_length(radius)
|
||||
|
||||
|
|
@ -43,3 +45,4 @@ func set_actions(_up, _down, _left, _right):
|
|||
for i in 4:
|
||||
buttons[i].action = [_right, _down, _left, _up][i]
|
||||
buttons[i].passby_press = !("ui_" in _up)
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 4.7 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 2.2 KiB |
|
|
@ -1,189 +0,0 @@
|
|||
class_name VirtualJoystick
|
||||
|
||||
extends Control
|
||||
|
||||
# https://github.com/MarcoFazioRandom/Virtual-Joystick-Godot
|
||||
|
||||
#### EXPORTED VARIABLE ####
|
||||
|
||||
# The color of the button when the joystick is in use.
|
||||
export(Color) var pressed_color := Color.gray
|
||||
|
||||
# If the input is inside this range, the output is zero.
|
||||
export(float, 0, 200, 1) var deadzone_size : float = 10
|
||||
|
||||
# The max distance the tip can reach.
|
||||
export(float, 0, 500, 1) var clampzone_size : float = 75
|
||||
|
||||
# FIXED: The joystick doesn't move.
|
||||
# DYNAMIC: Every time the joystick area is pressed, the joystick position is set on the touched position.
|
||||
enum JoystickMode {FIXED, DYNAMIC}
|
||||
|
||||
export(JoystickMode) var joystick_mode := JoystickMode.FIXED
|
||||
|
||||
# VISIBILITY_ALWAYS = Always visible.
|
||||
# VISIBILITY_TOUCHSCREEN_ONLY = Visible on touch screens only.
|
||||
enum VisibilityMode {ALWAYS , TOUCHSCREEN_ONLY }
|
||||
|
||||
export(VisibilityMode) var visibility_mode := VisibilityMode.ALWAYS
|
||||
|
||||
# Use Input Actions
|
||||
export var use_input_actions := true
|
||||
|
||||
# Project -> Project Settings -> Input Map
|
||||
export var action_left := "ui_left"
|
||||
export var action_right := "ui_right"
|
||||
export var action_up := "ui_up"
|
||||
export var action_down := "ui_down"
|
||||
|
||||
#### PUBLIC VARIABLES ####
|
||||
|
||||
# If the joystick is receiving inputs.
|
||||
var _pressed := false setget , is_pressed
|
||||
|
||||
func is_pressed() -> bool:
|
||||
return _pressed
|
||||
|
||||
# The joystick output.
|
||||
var _output := Vector2.ZERO setget , get_output
|
||||
|
||||
func get_output() -> Vector2:
|
||||
return _output
|
||||
|
||||
#### PRIVATE VARIABLES ####
|
||||
|
||||
var _touch_index : int = -1
|
||||
|
||||
onready var _base := $Base
|
||||
onready var _tip := $Base/Tip
|
||||
|
||||
onready var _base_radius = _base.rect_size * _base.get_global_transform_with_canvas().get_scale() / 2
|
||||
|
||||
onready var _base_default_position : Vector2 = _base.rect_position
|
||||
onready var _tip_default_position : Vector2 = _tip.rect_position
|
||||
|
||||
onready var _default_color : Color = _tip.modulate
|
||||
|
||||
#### FUNCTIONS ####
|
||||
|
||||
func _ready() -> void:
|
||||
if not OS.has_touchscreen_ui_hint() and visibility_mode == VisibilityMode.TOUCHSCREEN_ONLY:
|
||||
hide()
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventScreenTouch:
|
||||
if event.pressed:
|
||||
if _is_point_inside_joystick_area(event.position) and _touch_index == -1:
|
||||
if joystick_mode == JoystickMode.DYNAMIC or (joystick_mode == JoystickMode.FIXED and _is_point_inside_base(event.position)):
|
||||
if joystick_mode == JoystickMode.DYNAMIC:
|
||||
_move_base(event.position)
|
||||
_touch_index = event.index
|
||||
_tip.modulate = pressed_color
|
||||
_update_joystick(event.position)
|
||||
#get_tree().set_input_as_handled()
|
||||
elif event.index == _touch_index:
|
||||
_reset()
|
||||
#get_tree().set_input_as_handled()
|
||||
elif event is InputEventScreenDrag:
|
||||
if event.index == _touch_index:
|
||||
_update_joystick(event.position)
|
||||
#get_tree().set_input_as_handled()
|
||||
|
||||
func _move_base(new_position: Vector2) -> void:
|
||||
_base.rect_global_position = new_position - _base.rect_pivot_offset * get_global_transform_with_canvas().get_scale()
|
||||
|
||||
func _move_tip(new_position: Vector2) -> void:
|
||||
_tip.rect_global_position = new_position - _tip.rect_pivot_offset * _base.get_global_transform_with_canvas().get_scale()
|
||||
|
||||
func _is_point_inside_joystick_area(point: Vector2) -> bool:
|
||||
var x: bool = point.x >= rect_global_position.x and point.x <= rect_global_position.x + (rect_size.x * get_global_transform_with_canvas().get_scale().x)
|
||||
var y: bool = point.y >= rect_global_position.y and point.y <= rect_global_position.y + (rect_size.y * get_global_transform_with_canvas().get_scale().y)
|
||||
return x and y
|
||||
|
||||
func _is_point_inside_base(point: Vector2) -> bool:
|
||||
var center : Vector2 = _base.rect_global_position + _base_radius
|
||||
var vector : Vector2 = point - center
|
||||
if vector.length_squared() <= _base_radius.x * _base_radius.x:
|
||||
return true
|
||||
else:
|
||||
return false
|
||||
|
||||
func _update_joystick(touch_position: Vector2) -> void:
|
||||
var center : Vector2 = _base.rect_global_position + _base_radius
|
||||
var vector : Vector2 = touch_position - center
|
||||
vector = vector.limit_length(clampzone_size)
|
||||
|
||||
_move_tip(center + vector)
|
||||
|
||||
if vector.length_squared() > deadzone_size * deadzone_size:
|
||||
_pressed = true
|
||||
_output = (vector - (vector.normalized() * deadzone_size)) / (clampzone_size - deadzone_size)
|
||||
else:
|
||||
_pressed = false
|
||||
_output = Vector2.ZERO
|
||||
|
||||
if use_input_actions:
|
||||
_update_input_actions()
|
||||
|
||||
func _update_input_actions():
|
||||
if _output.x < 0:
|
||||
var a = InputEventAction.new()
|
||||
a.action = action_left
|
||||
a.pressed = true
|
||||
a.strength = -_output.x
|
||||
Input.parse_input_event(a)
|
||||
elif Input.is_action_pressed(action_left):
|
||||
Input.action_release(action_left)
|
||||
|
||||
if _output.x > 0:
|
||||
var a = InputEventAction.new()
|
||||
a.action = action_right
|
||||
a.pressed = true
|
||||
a.strength = _output.x
|
||||
Input.parse_input_event(a)
|
||||
elif Input.is_action_pressed(action_right):
|
||||
Input.action_release(action_right)
|
||||
|
||||
if _output.y < 0:
|
||||
var a = InputEventAction.new()
|
||||
a.action = action_up
|
||||
a.pressed = true
|
||||
a.strength = -_output.y
|
||||
Input.parse_input_event(a)
|
||||
elif Input.is_action_pressed(action_up):
|
||||
Input.action_release(action_up)
|
||||
|
||||
if _output.y > 0:
|
||||
var a = InputEventAction.new()
|
||||
a.action = action_down
|
||||
a.pressed = true
|
||||
a.strength = _output.y
|
||||
Input.parse_input_event(a)
|
||||
elif Input.is_action_pressed(action_down):
|
||||
Input.action_release(action_down)
|
||||
|
||||
func _reset():
|
||||
_pressed = false
|
||||
_output = Vector2.ZERO
|
||||
_touch_index = -1
|
||||
_tip.modulate = _default_color
|
||||
_base.rect_position = _base_default_position
|
||||
_tip.rect_position = _tip_default_position
|
||||
if use_input_actions:
|
||||
if Input.is_action_pressed(action_left) or Input.is_action_just_pressed(action_left):
|
||||
Input.action_release(action_left)
|
||||
if Input.is_action_pressed(action_right) or Input.is_action_just_pressed(action_right):
|
||||
Input.action_release(action_right)
|
||||
if Input.is_action_pressed(action_down) or Input.is_action_just_pressed(action_down):
|
||||
Input.action_release(action_down)
|
||||
if Input.is_action_pressed(action_up) or Input.is_action_just_pressed(action_up):
|
||||
Input.action_release(action_up)
|
||||
|
||||
func set_actions(_left, _right, _up, _down):
|
||||
for i in [action_left, action_right, action_up, action_down]:
|
||||
Input.action_release(i)
|
||||
|
||||
action_left = _left
|
||||
action_right = _right
|
||||
action_up = _up
|
||||
action_down = _down
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://src/menu/joystick/virtual_joystick.gd" type="Script" id=1]
|
||||
[ext_resource path="res://src/menu/joystick/textures/joystick_tip_arrows.png" type="Texture" id=2]
|
||||
[ext_resource path="res://src/menu/joystick/textures/joystick_base_outline.png" type="Texture" id=3]
|
||||
|
||||
[node name="Virtual joystick" type="Control"]
|
||||
modulate = Color( 1, 1, 1, 0.509804 )
|
||||
anchor_top = 1.0
|
||||
anchor_bottom = 1.0
|
||||
margin_left = 50.0
|
||||
margin_top = -250.0
|
||||
margin_right = 250.0
|
||||
margin_bottom = -50.0
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="Base" type="TextureRect" parent="."]
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
margin_left = -100.0
|
||||
margin_top = -100.0
|
||||
margin_right = 100.0
|
||||
margin_bottom = 100.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
rect_pivot_offset = Vector2( 100, 100 )
|
||||
size_flags_horizontal = 4
|
||||
size_flags_vertical = 4
|
||||
texture = ExtResource( 3 )
|
||||
stretch_mode = 6
|
||||
|
||||
[node name="Tip" type="TextureRect" parent="Base"]
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
margin_left = -50.0
|
||||
margin_top = -50.0
|
||||
margin_right = 50.0
|
||||
margin_bottom = 50.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
rect_pivot_offset = Vector2( 50, 50 )
|
||||
size_flags_horizontal = 4
|
||||
size_flags_vertical = 4
|
||||
texture = ExtResource( 2 )
|
||||
stretch_mode = 6
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue