straywild/ui/components/shop_sale_tray_slot.gd

93 lines
2.7 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name ShopSaleTraySlot
extends Button
signal remove_requested(key: String)
signal drop_requested(payload: Dictionary)
var entry_key: String = ""
var _icon: TextureRect
var _quantity: Label
var _quality_tier: int = -1
func _ready() -> void:
custom_minimum_size = GeneralInventoryGrid.DEFAULT_SLOT_SIZE
focus_mode = Control.FOCUS_ALL
texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
pressed.connect(func() -> void: remove_requested.emit(entry_key))
_icon = TextureRect.new()
_icon.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_icon.offset_left = 6.0
_icon.offset_top = 4.0
_icon.offset_right = -6.0
_icon.offset_bottom = -4.0
_icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
_icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
_icon.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
_icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_icon)
_quantity = Label.new()
_quantity.set_anchors_preset(Control.PRESET_BOTTOM_RIGHT)
_quantity.position = Vector2(-34.0, -20.0)
_quantity.size = Vector2(30.0, 16.0)
_quantity.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
_quantity.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_quantity)
_apply_style()
func _apply_style() -> void:
var normal_color := GeneralInventorySlot.quality_background_color(
_quality_tier,
GeneralInventorySlot.QualityEmphasis.NORMAL,
)
var hover_color := GeneralInventorySlot.quality_background_color(
_quality_tier,
GeneralInventorySlot.QualityEmphasis.SELECTED,
)
# Preserve the tray's established opacity while sharing the inventory's
# quality colors. Ordinary items and the empty drop target stay unchanged.
normal_color.a = 0.96
hover_color.a = 0.96
var normal := UtilityPageStyle.rounded_style(
normal_color,
26,
)
var hover := UtilityPageStyle.rounded_style(
hover_color,
26,
)
for state: StringName in [&"normal", &"pressed", &"disabled"]:
add_theme_stylebox_override(state, normal)
for state: StringName in [&"hover", &"focus"]:
add_theme_stylebox_override(state, hover)
func configure(
key: String,
icon: Texture2D,
label: String,
quantity: int = 1,
quality: int = -1,
) -> void:
entry_key = key
_quality_tier = quality
_icon.texture = icon
_quantity.text = "×%d" % quantity if quantity > 1 else ""
tooltip_text = "%s · select to remove" % label
accessibility_name = tooltip_text
_apply_style()
func _can_drop_data(_at_position: Vector2, data: Variant) -> bool:
return (
typeof(data) == TYPE_DICTIONARY
and str((data as Dictionary).get("kind", "")) in [
"bag_item", "cooler_fish"
]
)
func _drop_data(_at_position: Vector2, data: Variant) -> void:
if _can_drop_data(Vector2.ZERO, data):
drop_requested.emit((data as Dictionary).duplicate(true))