taqin/src/DepthButton.gd

120 lines
3.5 KiB
GDScript

tool
class_name DepthButton
extends Control
signal pressed()
signal button_down()
signal button_up()
signal toggled()
export var text := ""
export(Styles.CornerType) var corner_type = Styles.CornerType.SINGLE
export var corner_radius := 10
export var depth := 10
export var toggle_mode := false
export var group: ButtonGroup
var pressed setget set_pressed, is_pressed
var _pressed_depth := 4
var _toggled := false
var _toggled_depth := _pressed_depth + 3
onready var background := $Background
onready var button: Button = $Button
func _ready():
assert(_toggled_depth > _pressed_depth)
button.text = text
button.margin_bottom = -depth
button.toggle_mode = toggle_mode
button.group = group
button.rect_size.y = rect_size.y - depth
#
# Styles
#
var background_style := Styles.get_stylebox_flat(background.get_stylebox("panel", "panel"), "button_background", corner_type)
background_style.set_bg_color(Color(0.73, 0.35, 0.13))
background.set('custom_styles/panel', background_style)
match corner_type:
Styles.CornerType.SINGLE:
background_style.corner_radius_bottom_left = corner_radius
background_style.corner_radius_bottom_right = corner_radius
Styles.CornerType.LEFT:
background_style.corner_radius_bottom_left = corner_radius
background_style.corner_radius_bottom_right = 0
Styles.CornerType.MIDDLE:
background_style.corner_radius_bottom_left = 0
background_style.corner_radius_bottom_right = 0
Styles.CornerType.RIGHT:
background_style.corner_radius_bottom_left = 0
background_style.corner_radius_bottom_right = corner_radius
for style_name in ["normal", "hover", "focus", "pressed"]:
var original_style = button.get_stylebox(style_name)
# print_debug("style for ", style_name, ": ", original_style)
var stylebox := Styles.get_stylebox_flat(original_style, style_name, corner_type)
button.set("custom_styles/%s" % style_name, stylebox)
match corner_type:
Styles.CornerType.SINGLE:
stylebox.corner_radius_top_left = corner_radius
stylebox.corner_radius_bottom_left = corner_radius
stylebox.corner_radius_top_right = corner_radius
stylebox.corner_radius_bottom_right = corner_radius
Styles.CornerType.LEFT:
stylebox.corner_radius_top_left = corner_radius
stylebox.corner_radius_bottom_left = corner_radius
stylebox.corner_radius_top_right = 0
stylebox.corner_radius_bottom_right = 0
Styles.CornerType.MIDDLE:
stylebox.corner_radius_top_left = 0
stylebox.corner_radius_bottom_left = 0
stylebox.corner_radius_top_right = 0
stylebox.corner_radius_bottom_right = 0
Styles.CornerType.RIGHT:
stylebox.corner_radius_top_left = 0
stylebox.corner_radius_bottom_left = 0
stylebox.corner_radius_top_right = corner_radius
stylebox.corner_radius_bottom_right = corner_radius
func is_pressed() -> bool:
return button.is_pressed()
func set_pressed(value: bool) -> void:
button.set_pressed(value)
#
# Signals
#
func _on_Button_button_down():
button.rect_position.y = depth - _pressed_depth
$AudioDown.play()
emit_signal("button_down")
func _on_Button_button_up():
if _toggled:
button.rect_position.y = depth - _toggled_depth
else:
button.rect_position.y = 0
$AudioUp.play()
emit_signal("button_up")
func _on_Button_toggled(button_pressed):
if _toggled == button_pressed:
return
if button_pressed:
button.rect_position.y = depth - _toggled_depth
else:
button.rect_position.y = 0
_toggled = button_pressed
emit_signal("toggled", _toggled)
func _on_Button_pressed():
emit_signal("pressed")