summaryrefslogtreecommitdiff
path: root/components/button.gd
blob: e3d12b8c4b102b52e56b444a6744ccbbb5c2828c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
var disabled = false


#############
# INTERNALS #
#############

onready var stretcher : Node2D = $Middle
onready var left : Node2D = $Left
onready var right : Node2D = $Right
onready var hitbox : CollisionShape2D = $Hitbox
const BASE_WIDTH = 6
const BASE_HEIGHT = 16

const theme : Theme = preload("res://fonts/Flipps_Theme.tres")
const left_nohover = preload("res://textures/components/button/left.png")
const left_hover = preload("res://textures/components/button/left_hover.png")
const left_press = preload("res://textures/components/button/left_press.png")
const middle_nohover = preload("res://textures/components/button/middle.png")
const middle_hover = preload("res://textures/components/button/middle_hover.png")
const middle_press = preload("res://textures/components/button/middle_press.png")
const right_nohover = preload("res://textures/components/button/right.png")
const right_hover = preload("res://textures/components/button/right_hover.png")
const right_press = preload("res://textures/components/button/right_press.png")

var label

func _ready():
	label = Label.new()
	label.text = text
	label.theme = theme
	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 = -width / 2 - BASE_WIDTH
	right.position.x = width / 2 + BASE_WIDTH
	hitbox.shape = RectangleShape2D.new()
	hitbox.shape.extents.y = 32
	hitbox.shape.extents.x = right.position.x + right.scale.x * BASE_WIDTH / 2



func _on_ButtonTest_input_event(viewport, event, shape_idx):
	if not (event is InputEventMouseButton):
		return
	if not event.pressed:
		_set_pressed(false)
		return
	_set_pressed(true)
	if not (event.button_index == BUTTON_LEFT):
		return
	if disabled:
		return
	BackgroundMusic.click_sound()
	emit_signal("on_click")
	
func _set_all_textures(l, m, r):
	left.texture = l
	stretcher.texture = m
	right.texture = r
	
func _set_pressed(press: bool):
	if press:
		label.rect_position = -label.rect_size / 2 + Vector2(0, 4)
		_set_all_textures(left_press, middle_press, right_press)
	else:
		label.rect_position = -label.rect_size / 2
		_set_all_textures(left_hover, middle_hover, right_hover)

func _on_ButtonTest_mouse_entered():
	if not is_hovered:
		_set_all_textures(left_hover, middle_hover, right_hover)
		BackgroundMusic.hover_sound()
	is_hovered = true


func _on_ButtonTest_mouse_exited():
	if is_hovered:
		_set_all_textures(left_nohover, middle_nohover, right_nohover)
	is_hovered = false