2023-05-13 07:42:08 +00:00
|
|
|
mod global_state;
|
|
|
|
mod polygon;
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
input::{
|
|
|
|
mouse::{MouseButtonInput, MouseWheel},
|
|
|
|
ButtonState,
|
|
|
|
},
|
|
|
|
prelude::*,
|
|
|
|
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())
|
2023-05-13 14:08:26 +00:00
|
|
|
.add_startup_system(setup_camera)
|
|
|
|
.add_systems((mouse_event_system, update_ui_side_panel, on_resize_system))
|
2023-05-13 07:42:08 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2023-05-13 14:08:26 +00:00
|
|
|
// Spawns the camera
|
2023-05-13 07:42:08 +00:00
|
|
|
fn setup_camera(mut cmd: Commands) {
|
|
|
|
cmd.spawn(Camera2dBundle::default());
|
|
|
|
}
|
|
|
|
|
2023-05-13 14:08:26 +00:00
|
|
|
// Create UI
|
2023-05-13 07:42:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-13 14:08:26 +00:00
|
|
|
/// Handles all mouse events
|
|
|
|
fn mouse_event_system(
|
2023-05-13 07:42:08 +00:00
|
|
|
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 => {
|
2023-05-13 14:08:26 +00:00
|
|
|
// only add new points if "New Polygon" button got pressed
|
|
|
|
if global_state.polygon_started {
|
|
|
|
let polygon = match object_infos.active_polygon() {
|
|
|
|
Some(polygon) => polygon,
|
|
|
|
None => object_infos.add_polygon(),
|
|
|
|
};
|
2023-05-13 07:42:08 +00:00
|
|
|
|
2023-05-13 14:08:26 +00:00
|
|
|
if polygon.add_point(
|
|
|
|
&global_state,
|
|
|
|
&mut commands,
|
|
|
|
&mut meshes,
|
|
|
|
&mut materials,
|
|
|
|
) {
|
|
|
|
global_state.polygon_started = false;
|
|
|
|
}
|
2023-05-13 07:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 14:08:26 +00:00
|
|
|
// Handles resize events
|
2023-05-13 07:42:08 +00:00
|
|
|
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
|
|
|
}
|