virtual d-pad (=

This commit is contained in:
Harmony Honey Monroe 2024-09-18 01:08:32 -04:00
parent 994d338126
commit ba42634f27
6 changed files with 128 additions and 120 deletions

View file

@ -7,6 +7,8 @@ onready var keys := [$Control/HBoxRight/C, $Control/HBoxRight/X]
onready var buttons := [$Control/HBoxRight/C/Control/Button, $Control/HBoxRight/X/Control/Button]
onready var joystick := $Control/Joystick
onready var btns := $Control/Joystick/Buttons.get_children()
func _ready():
connect("visibility_changed", self, "vis")
@ -17,8 +19,6 @@ func _ready():
func vis():
if is_instance_valid(UI.keys_node):
UI.keys_node.visible = !visible
if is_instance_valid(joystick):
joystick.is_input = visible
func show_keys(arg_arrows := true, arg_c := true, arg_x := true, arg_pause := false, arg_passby := false):
left.visible = arg_arrows
@ -28,9 +28,13 @@ func show_keys(arg_arrows := true, arg_c := true, arg_x := true, arg_pause := fa
func set_game(arg := false):
var i = "" if arg else "ui_"
joystick.set_actions(i + "up", i + "down", i + "left", i + "right")
set_actions(i + "up", i + "down", i + "left", i + "right")
buttons[0].action = "action" if arg else "ui_no"
buttons[1].action = "jump" if arg else "ui_yes"
for f in buttons:
f.passby_press = arg
func set_actions(_up, _down, _left, _right):
for i in 4:
btns[i].action = [_right, _down, _left, _up][i]
btns[i].passby_press = !("ui_" in _up)

View file

@ -1,11 +1,10 @@
[gd_scene load_steps=16 format=2]
[gd_scene load_steps=15 format=2]
[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/round_button.gd" type="Script" id=3]
[ext_resource path="res://src/menu/joystick.gd" type="Script" id=4]
[ext_resource path="res://src/menu/button_modulate.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]
[ext_resource path="res://src/circle.gd" type="Script" id=8]
[sub_resource type="CircleShape2D" id=3]
@ -77,6 +76,9 @@ visibility_mode = 1
[node name="Circle" type="Sprite" parent="Control/HBoxRight/C/Control"]
modulate = Color( 1, 1, 1, 0.156863 )
texture = ExtResource( 2 )
script = ExtResource( 4 )
button_path = NodePath("../Button")
idle_color = Color( 1, 1, 1, 0.156863 )
[node name="Key" type="Label" parent="Control/HBoxRight/C/Control"]
modulate = Color( 1, 0, 0.301961, 1 )
@ -113,6 +115,9 @@ visibility_mode = 1
[node name="Circle" type="Sprite" parent="Control/HBoxRight/X/Control"]
modulate = Color( 1, 1, 1, 0.156863 )
texture = ExtResource( 2 )
script = ExtResource( 4 )
button_path = NodePath("../Button")
idle_color = Color( 1, 1, 1, 0.156863 )
[node name="Key" type="Label" parent="Control/HBoxRight/X/Control"]
modulate = Color( 0, 0.894118, 0.211765, 1 )
@ -168,15 +173,14 @@ margin_right = 5.0
margin_bottom = 6.0
[node name="Joystick" type="Control" parent="Control"]
modulate = Color( 1, 1, 1, 0.75 )
margin_left = 26.0
margin_top = 98.0
margin_right = 26.0
margin_bottom = 98.0
rect_pivot_offset = Vector2( -84, -71 )
script = ExtResource( 4 )
[node name="Base" type="Control" parent="Control/Joystick"]
visible = false
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
@ -185,43 +189,6 @@ script = ExtResource( 8 )
radius = 20.0
points = 16
[node name="Guide" type="Control" parent="Control/Joystick/Base"]
modulate = Color( 1, 1, 1, 0.509804 )
[node name="ColorRect" type="ColorRect" parent="Control/Joystick/Base/Guide"]
margin_left = -1.0
margin_top = -15.0
margin_right = 1.0
margin_bottom = 15.0
[node name="ColorRect2" type="ColorRect" parent="Control/Joystick/Base/Guide"]
margin_left = -15.0
margin_top = -1.0
margin_right = 15.0
margin_bottom = 1.0
[node name="Tip" type="Control" parent="Control/Joystick"]
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
[node name="Image" type="TextureRect" parent="Control/Joystick/Tip"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -12.0
margin_top = -12.0
margin_right = 12.0
margin_bottom = 12.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
texture = ExtResource( 7 )
stretch_mode = 4
[node name="Buttons" type="Control" parent="Control/Joystick"]
margin_right = 40.0
margin_bottom = 40.0
@ -231,6 +198,17 @@ shape = SubResource( 7 )
shape_centered = false
action = "ui_right"
script = ExtResource( 3 )
poly_path = NodePath("Polygon2D")
inner_radius = 18.0
inner_offset = 2.0
[node name="Polygon2D" type="Polygon2D" parent="Control/Joystick/Buttons/0"]
modulate = Color( 1, 1, 1, 0.3 )
position = Vector2( 2, 0 )
polygon = PoolVector2Array( 2.12132, 2.12132, 3, 0, 2.12132, -2.12132, 12.7279, -12.7279, 16.6298, -6.8883, 18, 0, 16.6298, 6.8883, 12.7279, 12.7279 )
script = ExtResource( 4 )
button_path = NodePath("..")
idle_color = Color( 1, 1, 1, 0.5 )
[node name="1" type="TouchScreenButton" parent="Control/Joystick/Buttons"]
shape = SubResource( 8 )
@ -238,6 +216,17 @@ shape_centered = false
action = "ui_down"
script = ExtResource( 3 )
angle = 90.0
poly_path = NodePath("Polygon2D")
inner_radius = 18.0
inner_offset = 2.0
[node name="Polygon2D" type="Polygon2D" parent="Control/Joystick/Buttons/1"]
modulate = Color( 1, 1, 1, 0.3 )
position = Vector2( -8.74228e-08, 2 )
polygon = PoolVector2Array( -2.12132, 2.12132, -1.31134e-07, 3, 2.12132, 2.12132, 12.7279, 12.7279, 6.8883, 16.6298, -7.86805e-07, 18, -6.8883, 16.6298, -12.7279, 12.7279 )
script = ExtResource( 4 )
button_path = NodePath("..")
idle_color = Color( 1, 1, 1, 0.5 )
[node name="2" type="TouchScreenButton" parent="Control/Joystick/Buttons"]
shape = SubResource( 9 )
@ -245,6 +234,17 @@ shape_centered = false
action = "ui_left"
script = ExtResource( 3 )
angle = 180.0
poly_path = NodePath("Polygon2D")
inner_radius = 18.0
inner_offset = 2.0
[node name="Polygon2D" type="Polygon2D" parent="Control/Joystick/Buttons/2"]
modulate = Color( 1, 1, 1, 0.3 )
position = Vector2( -2, -1.74846e-07 )
polygon = PoolVector2Array( -2.12132, -2.12132, -3, -2.62268e-07, -2.12132, 2.12132, -12.7279, 12.7279, -16.6298, 6.8883, -18, -1.57361e-06, -16.6298, -6.8883, -12.7279, -12.7279 )
script = ExtResource( 4 )
button_path = NodePath("..")
idle_color = Color( 1, 1, 1, 0.5 )
[node name="3" type="TouchScreenButton" parent="Control/Joystick/Buttons"]
shape = SubResource( 10 )
@ -252,3 +252,14 @@ shape_centered = false
action = "ui_up"
script = ExtResource( 3 )
angle = 270.0
poly_path = NodePath("Polygon2D")
inner_radius = 18.0
inner_offset = 2.0
[node name="Polygon2D" type="Polygon2D" parent="Control/Joystick/Buttons/3"]
modulate = Color( 1, 1, 1, 0.3 )
position = Vector2( 2.38498e-08, -2 )
polygon = PoolVector2Array( 2.12132, -2.12132, 3.57746e-08, -3, -2.12132, -2.12132, -12.7279, -12.7279, -6.8883, -16.6298, 2.14648e-07, -18, 6.8883, -16.6298, 12.7279, -12.7279 )
script = ExtResource( 4 )
button_path = NodePath("..")
idle_color = Color( 1, 1, 1, 0.5 )

View file

@ -0,0 +1,22 @@
extends Node
export var button_path : NodePath = ""
onready var button : TouchScreenButton = get_node_or_null(button_path)
export var color_path : NodePath = "."
onready var color_node : CanvasItem = get_node_or_null(color_path)
export var press_color := Color(1,0,1, 1.0)
export var idle_color := Color(1,1,1, 0.66)
func _ready():
if is_instance_valid(button):
button.connect("pressed", self, "interact")
button.connect("released", self, "interact")
if is_instance_valid(color_node):
color_node.modulate = idle_color
func interact():
if is_instance_valid(button) and is_instance_valid(color_node):
color_node.modulate = press_color if button.is_pressed() else idle_color

View file

@ -1,48 +0,0 @@
tool
extends Control
export var radius := 20.0
export var max_range := 60.0
onready var base := $Base
onready var tip := $Tip
onready var buttons := $Buttons.get_children()
var vec := Vector2.ZERO
var vl := 0.0
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
var is_touch = event is InputEventScreenTouch
var is_drag = event is InputEventScreenDrag
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:
if !event.pressed: is_joy = false
index = event.index if is_joy else -1
vec = vec.limit_length(radius)
tip.modulate = Color.red if is_joy else Color.white
tip.rect_position = vec if is_joy else Vector2.ZERO
func _physics_process(delta):
if Engine.editor_hint: return
func set_actions(_up, _down, _left, _right):
is_joy = false
for i in 4:
buttons[i].action = [_right, _down, _left, _up][i]
buttons[i].passby_press = !("ui_" in _up)

View file

@ -74,15 +74,14 @@ align = 1
[node name="MenuItems" type="VBoxContainer" parent="Center/Control"]
margin_left = 58.0
margin_top = 18.0
margin_top = 21.0
margin_right = 174.0
margin_bottom = 128.0
margin_bottom = 113.0
alignment = 1
[node name="Keyboard" type="HBoxContainer" parent="Center/Control/MenuItems"]
margin_top = 9.0
margin_right = 116.0
margin_bottom = 17.0
margin_bottom = 8.0
rect_min_size = Vector2( 0, 8 )
script = ExtResource( 3 )
@ -106,9 +105,9 @@ margin_bottom = 8.0
rect_min_size = Vector2( 8, 0 )
[node name="Gamepad" type="HBoxContainer" parent="Center/Control/MenuItems"]
margin_top = 21.0
margin_top = 12.0
margin_right = 116.0
margin_bottom = 29.0
margin_bottom = 20.0
rect_min_size = Vector2( 0, 8 )
script = ExtResource( 3 )
is_gamepad = true
@ -133,14 +132,14 @@ margin_bottom = 8.0
rect_min_size = Vector2( 8, 0 )
[node name="Sounds" parent="Center/Control/MenuItems" instance=ExtResource( 2 )]
margin_top = 33.0
margin_top = 24.0
margin_right = 116.0
margin_bottom = 41.0
margin_bottom = 32.0
[node name="Music" parent="Center/Control/MenuItems" instance=ExtResource( 2 )]
margin_top = 45.0
margin_top = 36.0
margin_right = 116.0
margin_bottom = 53.0
margin_bottom = 44.0
bus = 2
[node name="Label" parent="Center/Control/MenuItems/Music" index="0"]
@ -168,9 +167,9 @@ stream = ExtResource( 5 )
bus = "Music"
[node name="Fullscreen" type="HBoxContainer" parent="Center/Control/MenuItems"]
margin_top = 57.0
margin_top = 48.0
margin_right = 116.0
margin_bottom = 65.0
margin_bottom = 56.0
script = ExtResource( 20 )
[node name="Label" type="Label" parent="Center/Control/MenuItems/Fullscreen"]
@ -218,9 +217,9 @@ margin_bottom = 8.0
rect_min_size = Vector2( 8, 0 )
[node name="WindowSize" type="HBoxContainer" parent="Center/Control/MenuItems"]
margin_top = 69.0
margin_top = 60.0
margin_right = 116.0
margin_bottom = 77.0
margin_bottom = 68.0
rect_min_size = Vector2( 0, 8 )
script = ExtResource( 21 )
@ -254,9 +253,9 @@ text = "228 x 128"
align = 1
[node name="Borderless" type="HBoxContainer" parent="Center/Control/MenuItems"]
margin_top = 81.0
margin_top = 72.0
margin_right = 116.0
margin_bottom = 89.0
margin_bottom = 80.0
script = ExtResource( 8 )
[node name="Label" type="Label" parent="Center/Control/MenuItems/Borderless"]
@ -301,9 +300,9 @@ margin_bottom = 8.0
rect_min_size = Vector2( 8, 0 )
[node name="TouchControls" type="HBoxContainer" parent="Center/Control/MenuItems"]
margin_top = 93.0
margin_top = 84.0
margin_right = 116.0
margin_bottom = 101.0
margin_bottom = 92.0
script = ExtResource( 22 )
[node name="Label" type="Label" parent="Center/Control/MenuItems/TouchControls"]

View file

@ -6,6 +6,11 @@ export var points := 5 setget set_points
export var angle := 0.0 setget set_angle
export var deadzone := 3.0 setget set_deadzone
export var poly_path : NodePath = ""
onready var poly : Polygon2D = get_node_or_null(poly_path)
export var inner_radius := 50.0 setget set_inner_radius
export var inner_offset := 5.0 setget set_inner_offset
func set_radius(arg := radius):
radius = arg
act()
@ -22,17 +27,32 @@ func set_deadzone(arg := deadzone):
deadzone = arg
act()
func set_inner_radius(arg := inner_radius):
inner_radius = arg
inner_act()
func set_inner_offset(arg := inner_offset):
inner_offset = arg
inner_act()
func act():
var r = Vector2(radius, 0)
shape = ConvexPolygonShape2D.new()
shape.points = make_shape()
func inner_act():
if is_instance_valid(poly):
poly.polygon = make_shape(inner_radius)
poly.position = Vector2(inner_offset, 0).rotated(deg2rad(angle))
func make_shape(_radius := radius, _points := points, _angle := angle, _deadzone := deadzone):
var r = Vector2(_radius, 0)
var vec = PoolVector2Array()
for i in [1, 0, -1]:
vec.append(Vector2(deadzone, 0).rotated(deg2rad(angle + (i * 45))))
vec.append(Vector2(_deadzone, 0).rotated(deg2rad(_angle + (i * 45))))
for i in points:
var f = i / float(points - 1)
vec.append(r.rotated(deg2rad(angle + lerp(-45, 45, f))))
shape = ConvexPolygonShape2D.new()
shape.points = vec
for i in _points:
var f = i / float(_points - 1)
vec.append(r.rotated(deg2rad(_angle + lerp(-45, 45, f))))
return vec