generated from 2ndbeam/bevy-template
ui: Unfinished item rotating
This commit is contained in:
parent
55cbd39172
commit
55c4e3a1d1
3 changed files with 77 additions and 4 deletions
|
|
@ -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<Pointer<Over>>, mut query: Query<&mut ImageNode, With<UiInventorySlot>>) {
|
||||
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<Pointer<Out>>, mut query: Query<&mut ImageNode, With<UiInve
|
|||
}
|
||||
}
|
||||
|
||||
fn on_item_over(e: On<Pointer<Over>>, mut commands: Commands, query: Query<Entity, With<UiItem>>) {
|
||||
if let Ok(_) = query.get(e.event_target()) {
|
||||
commands.entity(e.event_target()).insert(HoveredItem(e.event_target()));
|
||||
}
|
||||
}
|
||||
|
||||
fn on_item_out(e: On<Pointer<Out>>, mut commands: Commands, query: Query<Entity, With<UiItem>>) {
|
||||
if let Ok(_) = query.get(e.event_target()) {
|
||||
commands.entity(e.event_target()).remove::<HoveredItem>();
|
||||
}
|
||||
}
|
||||
|
||||
fn on_item_drag(e: On<Pointer<Drag>>, mut query: Query<&mut UiTransform, With<UiItem>>) {
|
||||
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<UiRotateEvent>,
|
||||
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<HoveredItem>>,
|
||||
) {
|
||||
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);
|
||||
});
|
||||
|
|
|
|||
18
src/lib.rs
18
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<Self> {
|
||||
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<Self> {
|
||||
InputMap::default()
|
||||
.with(Self::Rotate, KeyCode::KeyR)
|
||||
.with(Self::Rotate, GamepadButton::West)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
20
src/ui.rs
20
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<UiAction>, With<UiRoot>>,
|
||||
) {
|
||||
let Ok(action_state) = input.single() else {
|
||||
error!("More than one UiRoot node");
|
||||
return;
|
||||
};
|
||||
if action_state.just_released(&UiAction::Rotate) {
|
||||
commands.trigger(UiRotateEvent);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue