diff options
-rw-r--r-- | components/HackButton.tscn | 12 | ||||
-rw-r--r-- | components/button.gd | 51 | ||||
-rw-r--r-- | components/button.tscn | 14 |
3 files changed, 67 insertions, 10 deletions
diff --git a/components/HackButton.tscn b/components/HackButton.tscn index 2241ac6..dd665f3 100644 --- a/components/HackButton.tscn +++ b/components/HackButton.tscn @@ -3,7 +3,7 @@ [ext_resource path="res://textures/rooot.png" type="Texture" id=1] [ext_resource path="res://components/HackButton.gd" type="Script" id=2] -[sub_resource type="Shader" id=2] +[sub_resource type="Shader" id=1] code = "shader_type canvas_item;
uniform vec4 glow_color : hint_color = vec4(1,1,1,1);
@@ -12,11 +12,11 @@ void fragment() { }
" -[sub_resource type="ShaderMaterial" id=3] -shader = SubResource( 2 ) +[sub_resource type="ShaderMaterial" id=2] +shader = SubResource( 1 ) shader_param/glow_color = Color( 0.254902, 0, 0, 1 ) -[sub_resource type="RectangleShape2D" id=1] +[sub_resource type="RectangleShape2D" id=3] extents = Vector2( 100, 100 ) [node name="HackButton" type="Area2D"] @@ -25,12 +25,12 @@ monitorable = false script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] -material = SubResource( 3 ) +material = SubResource( 2 ) scale = Vector2( 0.5, 0.5 ) texture = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource( 1 ) +shape = SubResource( 3 ) [connection signal="input_event" from="." to="." method="_on_HackButton_input_event"] [connection signal="mouse_entered" from="." to="." method="_on_HackButton_mouse_entered"] diff --git a/components/button.gd b/components/button.gd index 0303480..b6753fe 100644 --- a/components/button.gd +++ b/components/button.gd @@ -1,11 +1,31 @@ extends Node2D class_name ButtonTest export var text = "Hello, Button" +# Force a fixed width if != -1 +export var fixed_width = -1 +signal on_click() +var is_hovered = false + + + +############# +# INTERNALS # +############# + onready var stretcher : Node2D = $Middle onready var left : Node2D = $Left onready var right : Node2D = $Right onready var font_source : RichTextLabel = $FontSource +onready var hitbox : CollisionShape2D = $Hitbox const BASE_WIDTH = 6 +const BASE_HEIGHT = 16 + +const left_nohover = preload("res://textures/components/button/left.png") +const left_hover = preload("res://textures/components/button/left_hover.png") +const middle_nohover = preload("res://textures/components/button/middle.png") +const middle_hover = preload("res://textures/components/button/middle_hover.png") +const right_nohover = preload("res://textures/components/button/right.png") +const right_hover = preload("res://textures/components/button/right_hover.png") func _ready(): var label = Label.new() @@ -14,7 +34,34 @@ func _ready(): add_child(label) label.rect_position = -label.rect_size / 2 var width = label.rect_size.x + if fixed_width > 0: + width = fixed_width stretcher.scale.x = width / BASE_WIDTH - left.position.x = -label.rect_size.x / 2 - BASE_WIDTH - right.position.x = label.rect_size.x / 2 + left.position.x = -width / 2 - BASE_WIDTH + right.position.x = width / 2 + BASE_WIDTH + hitbox.shape.extents.x = width / 2 + BASE_WIDTH * 3 + + + +func _on_ButtonTest_input_event(viewport, event, shape_idx): + if not (event is InputEventMouseButton): + return + if not event.pressed: + return + emit_signal("on_click") + +func _set_all_textures(l, m, r): + left.texture = l + stretcher.texture = m + right.texture = r + +func _on_ButtonTest_mouse_entered(): + if not is_hovered: + _set_all_textures(left_hover, middle_hover, right_hover) + is_hovered = true + +func _on_ButtonTest_mouse_exited(): + if is_hovered: + _set_all_textures(left_nohover, middle_nohover, right_nohover) + is_hovered = false diff --git a/components/button.tscn b/components/button.tscn index 9519475..0071746 100644 --- a/components/button.tscn +++ b/components/button.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=2] +[gd_scene load_steps=9 format=2] [ext_resource path="res://textures/components/button/middle.png" type="Texture" id=1] [ext_resource path="res://textures/components/button/right.png" type="Texture" id=2] @@ -13,7 +13,10 @@ font_data = ExtResource( 3 ) [sub_resource type="Theme" id=2] default_font = SubResource( 1 ) -[node name="ButtonTest" type="Node2D"] +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 10, 32 ) + +[node name="ButtonTest" type="Area2D"] script = ExtResource( 5 ) [node name="Left" type="Sprite" parent="."] @@ -42,3 +45,10 @@ scroll_active = false __meta__ = { "_edit_use_anchors_": false } + +[node name="Hitbox" type="CollisionShape2D" parent="."] +shape = SubResource( 3 ) + +[connection signal="input_event" from="." to="." method="_on_ButtonTest_input_event"] +[connection signal="mouse_entered" from="." to="." method="_on_ButtonTest_mouse_entered"] +[connection signal="mouse_exited" from="." to="." method="_on_ButtonTest_mouse_exited"] |