forked from xiangwang25/whale-town-front-v2
46 lines
2.0 KiB
GDScript
46 lines
2.0 KiB
GDScript
extends Button
|
|
|
|
# ============================================================================
|
|
# SettingsToggle.gd - 设置面板开关控件
|
|
# ============================================================================
|
|
# 轻量自绘开关,避免默认 CheckButton 的厚重视觉。
|
|
# ============================================================================
|
|
|
|
const ON_COLOR: Color = Color(0.337, 0.690, 0.934, 1.0)
|
|
const OFF_COLOR: Color = Color(0.805, 0.843, 0.882, 1.0)
|
|
const KNOB_COLOR: Color = Color(1.0, 1.0, 1.0, 1.0)
|
|
const SHADOW_COLOR: Color = Color(0.114, 0.286, 0.420, 0.15)
|
|
|
|
func _ready() -> void:
|
|
custom_minimum_size = Vector2(74, 40)
|
|
focus_mode = Control.FOCUS_NONE
|
|
mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
|
|
toggle_mode = true
|
|
text = ""
|
|
add_theme_stylebox_override("normal", StyleBoxEmpty.new())
|
|
add_theme_stylebox_override("hover", StyleBoxEmpty.new())
|
|
add_theme_stylebox_override("pressed", StyleBoxEmpty.new())
|
|
add_theme_stylebox_override("focus", StyleBoxEmpty.new())
|
|
toggled.connect(func(_pressed: bool) -> void: queue_redraw())
|
|
|
|
func _draw() -> void:
|
|
var trackRect: Rect2 = Rect2(Vector2(6, 7), Vector2(size.x - 12, size.y - 14))
|
|
var radius: float = trackRect.size.y * 0.5
|
|
var color: Color = ON_COLOR if button_pressed else OFF_COLOR
|
|
draw_style_box(_create_round_box(color, int(radius)), trackRect)
|
|
|
|
var knobRadius: float = max(10.0, trackRect.size.y * 0.38)
|
|
var x: float = trackRect.position.x + trackRect.size.x - radius if button_pressed else trackRect.position.x + radius
|
|
var center: Vector2 = Vector2(x, trackRect.position.y + radius)
|
|
draw_circle(center + Vector2(0, 1.2), knobRadius + 0.4, SHADOW_COLOR)
|
|
draw_circle(center, knobRadius, KNOB_COLOR)
|
|
|
|
func _create_round_box(color: Color, radius: int) -> StyleBoxFlat:
|
|
var style := StyleBoxFlat.new()
|
|
style.bg_color = color
|
|
style.corner_radius_top_left = radius
|
|
style.corner_radius_top_right = radius
|
|
style.corner_radius_bottom_left = radius
|
|
style.corner_radius_bottom_right = radius
|
|
return style
|