diff --git a/src/inventory/ui.rs b/src/inventory/ui.rs index c20894e..a17c9b2 100644 --- a/src/inventory/ui.rs +++ b/src/inventory/ui.rs @@ -2,7 +2,7 @@ use std::f32::consts::FRAC_PI_2; use bevy::prelude::*; -use crate::{inventory::{ActiveInventory, Inventory, item::Item}, ui::{UiRoot, WindowSize}}; +use crate::{inventory::{ActiveInventory, Inventory, item::Item}, ui::{UiRoot, UiRotateEvent, WindowSize}}; const UI_SLOT_ASSET_PATH: &'static str = "sprites/ui/inventory_slot.png"; const TEMP_ITEM_PATH: &'static str = "sprites/items/choco_bar.png"; @@ -19,6 +19,9 @@ pub struct UiInventorySlot(UVec2); #[require(Node, ImageNode)] pub struct UiItem(Entity); +#[derive(Component)] +pub struct HoveredItem(Entity); + fn on_slot_over(e: On>, mut query: Query<&mut ImageNode, With>) { if let Ok(mut image) = query.get_mut(e.event_target()) { image.color = Color::WHITE.darker(0.3); @@ -31,6 +34,18 @@ fn on_slot_out(e: On>, mut query: Query<&mut ImageNode, With>, mut commands: Commands, query: Query>) { + if let Ok(_) = query.get(e.event_target()) { + commands.entity(e.event_target()).insert(HoveredItem(e.event_target())); + } +} + +fn on_item_out(e: On>, mut commands: Commands, query: Query>) { + if let Ok(_) = query.get(e.event_target()) { + commands.entity(e.event_target()).remove::(); + } +} + fn on_item_drag(e: On>, mut query: Query<&mut UiTransform, With>) { if let Ok(mut transform) = query.get_mut(e.event_target()) { transform.translation = Val2::px(e.distance.x, e.distance.y); @@ -80,6 +95,30 @@ fn on_item_drag_drop( } } +fn on_ui_rotate( + event: On, + item_query: Query<&Item>, + mut mut_item_query: Query<(&ChildOf, &mut Item)>, + inventory_query: Query<(&Inventory, Option<&Children>)>, + ui_item_query: Query<(&mut UiItem, &mut Node), With>, +) { + for (mut ui_item, mut node) in ui_item_query { + let Ok((item_parent, mut mut_item)) = mut_item_query.get_mut(ui_item.0) else { + continue; + }; + let Ok((inventory, children)) = inventory_query.get(item_parent.0) else { + continue; + }; + + let children = match children { + Some(children) => &children[..], + None => &[], + }; + + // TODO + } +} + fn ui_inventory_bundle( inventory: &Inventory, inventory_entity: Entity, @@ -223,6 +262,8 @@ pub fn setup_ui_inventory( .find(|(i, _)| i.position.unwrap_or_default() == UVec2::new(x, y)) { slot_commands.with_children(|commands| { commands.spawn(ui_item_bundle(item, *entity, temp_item_image.clone())) + .observe(on_item_over) + .observe(on_item_out) .observe(on_item_drag) .observe(on_item_drag_end); }); diff --git a/src/lib.rs b/src/lib.rs index 1911306..a3ad871 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,11 @@ pub enum InputAction { Interact, } +#[derive(Actionlike, PartialEq, Eq, Hash, Debug, Clone, Reflect, Serialize, Deserialize)] +pub enum UiAction { + Rotate, +} + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)] pub enum GameState { #[default] @@ -29,14 +34,21 @@ pub enum GameState { impl InputAction { pub fn default_input_map() -> InputMap { - let input_map = InputMap::default() + InputMap::default() .with_axis(Self::Move, VirtualAxis::ad()) .with_axis(Self::Move, GamepadAxis::LeftStickX) .with(Self::ToggleInventory, KeyCode::KeyI) .with(Self::ToggleInventory, GamepadButton::Select) .with(Self::Interact, KeyCode::KeyE) - .with(Self::Interact, GamepadButton::East); - input_map + .with(Self::Interact, GamepadButton::East) + } +} + +impl UiAction { + pub fn default_input_map() -> InputMap { + InputMap::default() + .with(Self::Rotate, KeyCode::KeyR) + .with(Self::Rotate, GamepadButton::West) } } diff --git a/src/ui.rs b/src/ui.rs index 26add71..15c2fd8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,7 @@ use bevy::{prelude::*, window::WindowResized}; +use leafwing_input_manager::prelude::*; + +use crate::UiAction; #[derive(Component, Reflect)] #[require(Node)] @@ -25,6 +28,7 @@ impl UiRoot { ..default() }, Pickable::IGNORE, + UiAction::default_input_map(), Name::new("UiRoot"), ) } @@ -39,3 +43,19 @@ pub fn update_window_size( window_size.y = event.height; } } + +#[derive(Event)] +pub struct UiRotateEvent; + +pub fn handle_input( + mut commands: Commands, + input: Query<&ActionState, With>, +) { + let Ok(action_state) = input.single() else { + error!("More than one UiRoot node"); + return; + }; + if action_state.just_released(&UiAction::Rotate) { + commands.trigger(UiRotateEvent); + } +}