2023-05-13 07:42:08 +00:00
|
|
|
mod global_state;
|
|
|
|
mod polygon;
|
|
|
|
|
|
|
|
///! This example illustrates how to resize windows, and how to respond to a window being resized.
|
|
|
|
use bevy::{
|
|
|
|
input::{
|
|
|
|
mouse::{MouseButtonInput, MouseWheel},
|
|
|
|
ButtonState,
|
|
|
|
},
|
|
|
|
prelude::*,
|
|
|
|
sprite::MaterialMesh2dBundle,
|
|
|
|
window::WindowResized,
|
|
|
|
};
|
|
|
|
|
|
|
|
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
|
|
|
use global_state::GlobalState;
|
|
|
|
use polygon::ObjectInfos;
|
|
|
|
|
2023-05-13 04:45:46 +00:00
|
|
|
fn main() {
|
2023-05-13 07:42:08 +00:00
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_plugin(EguiPlugin)
|
|
|
|
.insert_resource(GlobalState::default())
|
|
|
|
.insert_resource(ObjectInfos::default())
|
|
|
|
.add_startup_systems((setup_camera, setup))
|
|
|
|
.add_systems((
|
|
|
|
print_mouse_events_system,
|
|
|
|
update_ui_side_panel,
|
|
|
|
on_resize_system,
|
|
|
|
))
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
) {
|
|
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
|
|
|
|
transform: Transform::default()
|
|
|
|
.with_scale(Vec3::splat(4.))
|
|
|
|
.with_translation(Vec3::new(100.0, 10.0, 0.0)),
|
|
|
|
material: materials.add(ColorMaterial::from(Color::YELLOW)),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawns the camera that draws UI
|
|
|
|
fn setup_camera(mut cmd: Commands) {
|
|
|
|
cmd.spawn(Camera2dBundle::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_ui_side_panel(mut contexts: EguiContexts, mut global_state: ResMut<GlobalState>) {
|
|
|
|
egui::Window::new("Hello").show(contexts.ctx_mut(), |ui| {
|
|
|
|
if global_state.polygon_started {
|
|
|
|
// TODO
|
|
|
|
} else {
|
|
|
|
if ui.button("New Polygon").clicked() {
|
|
|
|
global_state.polygon_started = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This system prints out all mouse events as they come in
|
|
|
|
fn print_mouse_events_system(
|
|
|
|
mut mouse_button_input_events: EventReader<MouseButtonInput>,
|
|
|
|
mut cursor_moved_events: EventReader<CursorMoved>,
|
|
|
|
mut mouse_wheel_events: EventReader<MouseWheel>,
|
|
|
|
mut global_state: ResMut<GlobalState>,
|
|
|
|
mut object_infos: ResMut<ObjectInfos>,
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
) {
|
|
|
|
for event in mouse_button_input_events.iter() {
|
|
|
|
match event.button {
|
|
|
|
MouseButton::Left => match event.state {
|
|
|
|
ButtonState::Pressed => {
|
|
|
|
println!(
|
|
|
|
"Left mouse button pressed at {:?}",
|
|
|
|
global_state.cursor_position
|
|
|
|
);
|
|
|
|
|
|
|
|
let polygon = match object_infos.active_polygon() {
|
|
|
|
Some(polygon) => polygon,
|
|
|
|
None => object_infos.add_polygon(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if polygon.add_point(global_state.cursor_position) {
|
|
|
|
global_state.polygon_started = false;
|
|
|
|
|
|
|
|
// TODO remove single points -> draw full polygon
|
|
|
|
} else {
|
|
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
|
|
|
|
transform: Transform::default()
|
|
|
|
.with_scale(Vec3::splat(4.0))
|
|
|
|
.with_translation(
|
|
|
|
(global_state.cursor_position
|
|
|
|
- (global_state.window_extent * 0.5))
|
|
|
|
.extend(0.0),
|
|
|
|
),
|
|
|
|
material: materials.add(ColorMaterial::from(Color::BLACK)),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ButtonState::Released => (),
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// store latest mouse cursor location
|
|
|
|
for event in cursor_moved_events.iter() {
|
|
|
|
global_state.cursor_position = event.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
for _event in mouse_wheel_events.iter() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_resize_system(
|
|
|
|
mut resize_reader: EventReader<WindowResized>,
|
|
|
|
mut global_state: ResMut<GlobalState>,
|
|
|
|
) {
|
|
|
|
for e in resize_reader.iter() {
|
|
|
|
// When resolution is being changed
|
|
|
|
global_state.window_extent = Vec2::new(e.width, e.height);
|
|
|
|
}
|
2023-05-13 04:45:46 +00:00
|
|
|
}
|