107 lines
4.3 KiB
GDScript
107 lines
4.3 KiB
GDScript
extends PanelContainer
|
|
const GEAR := preload("res://scripts/planets/captain_equipment.gd")
|
|
var kind := "breathing"
|
|
var uid := -1
|
|
var equipped_slot := true
|
|
var return_slot := false
|
|
var _picture: TextureRect
|
|
var _label: Label
|
|
var _valid_drop := false
|
|
|
|
func _ready() -> void:
|
|
mouse_filter = Control.MOUSE_FILTER_STOP
|
|
custom_minimum_size = Vector2(58, 58)
|
|
_picture = TextureRect.new()
|
|
_picture.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
|
_picture.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
_picture.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR_WITH_MIPMAPS
|
|
_picture.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
add_child(_picture)
|
|
_label = Label.new()
|
|
_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
|
_label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
|
|
_label.add_theme_font_size_override("font_size", 9)
|
|
_label.add_theme_color_override("font_shadow_color", Color.BLACK)
|
|
_label.add_theme_constant_override("shadow_outline_size", 3)
|
|
add_child(_label)
|
|
GameState.captain.changed.connect(refresh)
|
|
refresh()
|
|
|
|
func gear_item() -> Dictionary:
|
|
if return_slot:
|
|
return {}
|
|
return GEAR.equipped(GameState.captain, kind) if equipped_slot else GEAR.item(GameState.captain, uid)
|
|
|
|
func refresh() -> void:
|
|
if not is_node_ready():
|
|
return
|
|
var gear := gear_item()
|
|
_picture.texture = GEAR.icon(kind) if not return_slot else null
|
|
_picture.modulate.a = 1.0 if not gear.is_empty() else 0.22
|
|
_label.text = "POZ. %d" % int(gear.level) if not gear.is_empty() else ("ODŁÓŻ" if return_slot else "—")
|
|
tooltip_text = "Odłóż wyposażenie kapitana" if return_slot else "%s\n%s\nPrzeciągnij przedmiot do pasującego slotu. Dwuklik zakłada lub zdejmuje." % [GEAR.NAMES.get(kind, kind), "Poziom %d" % int(gear.level) if not gear.is_empty() else "Pusty slot"]
|
|
if not return_slot and not gear.is_empty():
|
|
tooltip_text += "\nRedukcja zagrożenia: %d pp (2 pp/poziom, maks. 100 pp).\nPoziom 50 całkowicie neutralizuje odpowiadające zagrożenie." % mini(100,2*int(gear.level))
|
|
_style()
|
|
|
|
func _style() -> void:
|
|
var frame := StyleBoxFlat.new()
|
|
frame.bg_color = Color(0.035, 0.065, 0.09, 0.8)
|
|
frame.border_color = Color("83f7be") if _valid_drop else GEAR.COLORS.get(kind, Color("638da0")).darkened(0.4)
|
|
frame.set_border_width_all(2 if _valid_drop else 1)
|
|
frame.set_corner_radius_all(6)
|
|
frame.set_content_margin_all(4)
|
|
add_theme_stylebox_override("panel", frame)
|
|
|
|
func _process(_delta: float) -> void:
|
|
var valid := get_viewport().gui_is_dragging() and _can_drop_data(Vector2.ZERO, get_viewport().gui_get_drag_data())
|
|
if valid != _valid_drop:
|
|
_valid_drop = valid
|
|
_style()
|
|
|
|
func _gui_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT and event.double_click:
|
|
var gear := gear_item()
|
|
if not gear.is_empty():
|
|
if equipped_slot:
|
|
GEAR.unequip(GameState.captain, kind)
|
|
else:
|
|
GEAR.equip(GameState.captain, str(gear.kind), int(gear.uid))
|
|
accept_event()
|
|
|
|
func _get_drag_data(_at: Vector2) -> Variant:
|
|
var gear := gear_item()
|
|
if gear.is_empty():
|
|
return null
|
|
var overlay := get_tree().get_first_node_in_group("item_drag_overlay")
|
|
if overlay == null:
|
|
overlay = CanvasLayer.new()
|
|
overlay.set_script(preload("res://scripts/ui/item_drag_overlay.gd"))
|
|
var host := get_tree().current_scene
|
|
if host == null:
|
|
host = get_viewport()
|
|
host.add_child(overlay)
|
|
overlay.begin_icon(GEAR.icon(str(gear.kind)), GEAR.COLORS[str(gear.kind)], GEAR.NAMES[str(gear.kind)])
|
|
var native := Control.new()
|
|
native.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
set_drag_preview(native)
|
|
return {"captain_gear": int(gear.uid)}
|
|
|
|
func _can_drop_data(_at: Vector2, data: Variant) -> bool:
|
|
if not data is Dictionary or not data.has("captain_gear"):
|
|
return false
|
|
var gear := GEAR.item(GameState.captain, int(data.captain_gear))
|
|
if gear.is_empty():
|
|
return false
|
|
if return_slot:
|
|
return int(GameState.captain.gear_slots.get(gear.kind, -1)) == int(gear.uid)
|
|
return equipped_slot and gear.kind == kind and int(GameState.captain.gear_slots.get(kind, -1)) != int(gear.uid)
|
|
|
|
func _drop_data(_at: Vector2, data: Variant) -> void:
|
|
var gear := GEAR.item(GameState.captain, int(data.captain_gear))
|
|
if return_slot:
|
|
GEAR.unequip(GameState.captain, str(gear.kind))
|
|
else:
|
|
GEAR.equip(GameState.captain, kind, int(gear.uid))
|