Created
January 27, 2021 08:00
-
-
Save harumaxy/0217637498d57afc471bab2a1055ca5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 現状の Rust UI Node の z-order は、UI Component の Hierarchy だけで決まる | |
// 最終的には z_index とかを Style で指定して順序を変えられるようになるっぽいので、待とう | |
// (Sprite は Translation.z の値で重なりを制御できるけど) | |
use bevy::{ | |
input::{ | |
mouse::{MouseButtonInput, MouseMotion}, | |
ElementState, | |
}, | |
prelude::*, | |
render::draw::RenderCommand, | |
}; | |
use bevy_mod_picking::PickableMesh; | |
pub struct MyUIPlugin; | |
pub struct UIMaterials { | |
dark: Handle<ColorMaterial>, | |
gray: Handle<ColorMaterial>, | |
clear: Handle<ColorMaterial>, | |
white: Handle<ColorMaterial>, | |
} | |
#[derive(Default)] | |
pub struct DraggingPreview { | |
entity: Option<Entity>, | |
} | |
impl FromResources for UIMaterials { | |
fn from_resources(resources: &Resources) -> Self { | |
let mut materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap(); | |
let dark = materials.add(Color::rgb(0.2, 0.2, 0.2).into()); | |
let gray = materials.add(Color::DARK_GRAY.into()); | |
let clear = materials.add(Color::NONE.into()); | |
let white = materials.add(Color::WHITE.into()); | |
Self { | |
dark, | |
gray, | |
clear, | |
white, | |
} | |
} | |
} | |
struct MyButton; | |
fn drag_system( | |
mut query: Query<&mut Style, With<Node>>, | |
dragging_preview: Res<DraggingPreview>, | |
motions: Res<Events<MouseMotion>>, | |
mut reader: Local<EventReader<MouseMotion>>, | |
) { | |
let entity = match dragging_preview.entity { | |
Some(e) => e, | |
None => return, | |
}; | |
let mut style = match query.get_mut(entity) { | |
Ok(s) => s, | |
Err(_) => return, | |
}; | |
reader.iter(&motions).for_each(|motion| { | |
let Vec2 { x, y } = motion.delta; | |
style.position.left += x; | |
style.position.top += y; | |
// println!("{:?}", t.translation); | |
println!("{:?}", motion); | |
}) | |
} | |
fn pick_system( | |
commands: &mut Commands, | |
// mut query: Query<&mut Transform, With<Node>>, | |
mut dragging_preview: ResMut<DraggingPreview>, | |
colors: ResMut<UIMaterials>, | |
mouse_events: Res<Events<MouseButtonInput>>, | |
mut reader: Local<EventReader<MouseButtonInput>>, | |
) { | |
reader.latest(&mouse_events).iter().for_each(|&ev| { | |
match ev.button { | |
MouseButton::Left => {} | |
_ => return, | |
}; | |
match ev.state { | |
ElementState::Pressed => { | |
commands.clear_current_entity(); | |
dragging_preview.entity = commands | |
.spawn(NodeBundle { | |
style: Style { | |
size: Size::new(Val::Px(100.), Val::Px(100.)), | |
position_type: PositionType::Absolute, | |
position: Rect { | |
top: Val::Px(100.), | |
left: Val::Px(100.), | |
bottom: Val::Auto, | |
right: Val::Auto, | |
}, | |
..Default::default() | |
}, | |
material: colors.white.clone_weak(), | |
global_transform: GlobalTransform { | |
translation: Vec3::new(0., 0., 100.), | |
..Default::default() | |
}, | |
..Default::default() | |
}) | |
.current_entity(); | |
println!("spwan"); | |
} | |
ElementState::Released => { | |
if let Some(entity) = dragging_preview.entity { | |
commands.despawn(entity); | |
dragging_preview.entity = None; | |
println!("despwan"); | |
} | |
} | |
} | |
}); | |
} | |
impl Plugin for MyUIPlugin { | |
fn build(&self, app: &mut AppBuilder) { | |
app.init_resource::<UIMaterials>() | |
.add_resource(DraggingPreview::default()) | |
.init_resource::<WindowDescriptor>() | |
.add_system(drag_system.system()) | |
.add_system(pick_system.system()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment