Skip to content

Instantly share code, notes, and snippets.

@Gatada
Last active October 4, 2022 12:19
Show Gist options
  • Save Gatada/5fadf64938c84c2ae758fe481cb11717 to your computer and use it in GitHub Desktop.
Save Gatada/5fadf64938c84c2ae758fe481cb11717 to your computer and use it in GitHub Desktop.
A simple Label @tool with a highlight state
@tool
extends Control
## Highlight Label Tool
##
## A simple node with a highlight state. Highlighting the node will change the background color.
## The highlight state will reset after `state_duration` seconds (to ensure the highlight is seen).
##
## The scene consists of:
##
## State (Control)
## ∟ Backdrop (ColorRect)
## ∟ Caption (Label)
@export var _is_highlighted: bool = false
@export var _caption: String = "Caption"
@export var _caption_color: Color = Color(1, 1, 1, 1)
@export var _backdrop_color: Color = Color(0, 0, 0, 0.30)
@export var _backdrop_color_highlighted: Color = Color(0, 0.57, 0.84)
var state_duration: float = 0
var state_retention: float = 0.07
func _ready() -> void:
$Caption.add_theme_override("font_sizes", 22)
_update_caption(_caption)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Engine.is_editor_hint():
_update_caption(_caption)
if _is_highlighted:
# $Caption.add_theme_color_override("font_color", _caption_color_highlighted)
_update_backdrop_color(_backdrop_color_highlighted)
if !Engine.is_editor_hint():
reset_highlight_if_needed(delta)
else:
# $Caption.remove_theme_color_override("font_color")
_update_backdrop_color(_backdrop_color)
var highlighted: bool:
set(new_value):
_is_highlighted = new_value
state_duration = 0
get:
return _is_highlighted
# Helpers
# --------------------------------------------------------------------------------------------------
func _update_caption(new_string: String) -> void:
_caption = new_string
$Caption.text = _caption
$Caption.add_theme_color_override("font_color", _caption_color) # Not working?
func _update_backdrop_color(new_color: Color) -> void:
$Backdrop.color = new_color
func reset_highlight_if_needed(delta: float) -> void:
state_duration += delta
if state_duration > state_retention:
_is_highlighted = false
state_duration = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment