Skip to content

Instantly share code, notes, and snippets.

@Minimuino
Last active June 21, 2023 00:27
Show Gist options
  • Save Minimuino/22493541fc44443dd78268cbf160db35 to your computer and use it in GitHub Desktop.
Save Minimuino/22493541fc44443dd78268cbf160db35 to your computer and use it in GitHub Desktop.
Improved Godot SpinBox node
extends Button
export var value: int = 0
export var step: int = 1
export var min_value: int = 0
export var max_value: int = 999
export var prefix: String
export var suffix: String
func _ready():
_update_value(value)
func _input(event):
if self.has_focus():
if event.is_action_released("ui_left"):
_update_value(self.value - self.step)
elif event.is_action_released("ui_right"):
_update_value(self.value + self.step)
func _update_value(new_value: int) -> void:
value = int(clamp(new_value, min_value, max_value))
$HBoxContainer/Label.text = prefix + String(value) + suffix
func _on_LeftButton_pressed():
_update_value(self.value - self.step)
func _on_RightButton_pressed():
_update_value(self.value + self.step)
[gd_scene load_steps=4 format=2]
[ext_resource path="res://CustomSpinBox.gd" type="Script" id=1]
[ext_resource path="res://triangle.png" type="Texture" id=2]
[ext_resource path="res://triangle-flip-h.png" type="Texture" id=3]
[node name="CustomSpinBox" type="Button"]
anchor_right = 1.0
anchor_bottom = 1.0
text = "Label"
align = 0
script = ExtResource( 1 )
[node name="HBoxContainer" type="HBoxContainer" parent="."]
anchor_left = 0.78
anchor_right = 1.0
anchor_bottom = 1.0
[node name="LeftButton" type="Button" parent="HBoxContainer"]
margin_right = 29.0
margin_bottom = 1080.0
focus_mode = 0
enabled_focus_mode = 0
icon = ExtResource( 3 )
flat = true
icon_align = 1
[node name="Label" type="Label" parent="HBoxContainer"]
margin_left = 33.0
margin_top = 533.0
margin_right = 389.0
margin_bottom = 547.0
size_flags_horizontal = 3
align = 1
[node name="RightButton" type="Button" parent="HBoxContainer"]
margin_left = 393.0
margin_right = 422.0
margin_bottom = 1080.0
focus_mode = 0
enabled_focus_mode = 0
icon = ExtResource( 2 )
flat = true
icon_align = 1
[connection signal="pressed" from="HBoxContainer/LeftButton" to="." method="_on_LeftButton_pressed"]
[connection signal="pressed" from="HBoxContainer/RightButton" to="." method="_on_RightButton_pressed"]
@Minimuino
Copy link
Author

This is a custom SpinBox node for Godot Engine. The builtin SpinBox does not work with a gamepad, so I made this one using Buttons and Labels that should have full gamepad support. Also works with keyboard and mouse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment