|
class_name QuickSlots
|
|
extends Node
|
|
|
|
|
|
@export var highlight_theme: Resource
|
|
@export var slots_container: GridContainer
|
|
@export var quickslots_slot: PackedScene
|
|
|
|
var _previous_selected_item: int = 0
|
|
var _selected_item: int = 0 :
|
|
set(new_item_index):
|
|
if new_item_index < 0:
|
|
new_item_index = InventoryManager.quick_slot_count - 1
|
|
elif new_item_index >= InventoryManager.quick_slot_count:
|
|
new_item_index = 0
|
|
_previous_selected_item = _selected_item
|
|
_selected_item = new_item_index
|
|
|
|
|
|
func _init() -> void:
|
|
InventoryManager.next_quick_slot.connect(select_next_item)
|
|
InventoryManager.previous_quick_slot.connect(select_previous_item)
|
|
InventoryManager.select_quick_slot.connect(select_quick_slot)
|
|
|
|
func _ready() -> void:
|
|
remove_slots()
|
|
generate_quick_slots()
|
|
|
|
# This doesn't quite work on ready as, for some reason, it will select
|
|
# Slot0 (one of the slots BEFORE remove_slots()) instead of the first slot
|
|
# create in generate_quick_slots()
|
|
select_quick_slot(0)
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("quickslot1"):
|
|
select_quick_slot(0)
|
|
elif event.is_action_pressed("quickslot2"):
|
|
select_quick_slot(1)
|
|
elif event.is_action_pressed("quickslot3"):
|
|
select_quick_slot(2)
|
|
elif event.is_action_pressed("quickslot4"):
|
|
select_quick_slot(3)
|
|
elif event.is_action_pressed("quickslot5"):
|
|
select_quick_slot(4)
|
|
elif event.is_action_pressed("quickslot6"):
|
|
select_quick_slot(5)
|
|
elif event.is_action_pressed("quickslot7"):
|
|
select_quick_slot(6)
|
|
elif event.is_action_pressed("quickslot8"):
|
|
select_quick_slot(7)
|
|
elif event.is_action_pressed("quickslot9"):
|
|
select_quick_slot(8)
|
|
elif event.is_action_pressed("quickslot10"):
|
|
select_quick_slot(9)
|
|
elif event.is_action_pressed("quickslot_next"):
|
|
select_next_item()
|
|
elif event.is_action_pressed("quickslot_previous"):
|
|
select_previous_item()
|
|
|
|
|
|
func clear_slots() -> void:
|
|
for slot: QuickSlotsSlot in slots_container.get_children():
|
|
slot.clear()
|
|
|
|
func generate_quick_slots() -> void:
|
|
for slot_index: int in range(InventoryManager.quick_slot_count):
|
|
var slot: QuickSlotsSlot = quickslots_slot.instantiate()
|
|
slots_container.add_child(slot)
|
|
slot.init(slot_index)
|
|
|
|
func remove_slots() -> void:
|
|
for slot: QuickSlotsSlot in slots_container.get_children():
|
|
slot.queue_free()
|
|
|
|
func select_next_item() -> void:
|
|
select_quick_slot(_selected_item + 1)
|
|
|
|
func select_previous_item() -> void:
|
|
select_quick_slot(_selected_item - 1)
|
|
|
|
func select_quick_slot(slot_index: int) -> void:
|
|
_selected_item = slot_index
|
|
InventoryManager.quick_slot_selected.emit(_selected_item)
|
|
update_highlighted_slot()
|
|
|
|
func update_highlighted_slot() -> void:
|
|
if _previous_selected_item != _selected_item:
|
|
var previous_slot: Panel = slots_container.get_child(_previous_selected_item)
|
|
previous_slot.set("theme_override_styles/panel", null)
|
|
|
|
var current_slot: Panel = slots_container.get_child(_selected_item)
|
|
current_slot.set("theme_override_styles/panel", highlight_theme)
|