Skip to content

Commit

Permalink
object editing
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhan45 committed Nov 21, 2019
1 parent d8308fa commit 2798cc2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ impl From<[f32; 2]> for Position {
}
}

impl From<Point> for Position {
fn from(p: Point) -> Self {
Position(p)
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Kinematics {
pub vel: Vector,
Expand Down
14 changes: 11 additions & 3 deletions src/imgui_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn make_sidepanel(
dt: &mut f32,
num_iterations: &mut i32,
signals: &mut Vec<UiSignal>,
entity: Option<Entity>,
) {
// Window
let win = imgui::Window::new(im_str!("Menu"))
Expand All @@ -69,9 +70,14 @@ pub fn make_sidepanel(
[resolution.x * 0.6, resolution.y],
);
win.build(ui, || {
ui.text(im_str!("New Object"));
ui.drag_float(im_str!("Mass"), mass).speed(0.01).build();
ui.drag_float(im_str!("Radius"), rad).speed(0.01).build();
match entity {
Some(_) => ui.text(im_str!("Edit Object")),
None => ui.text(im_str!("New Object"))
}
let mass_speed = *mass * 0.0015;
let rad_speed = *rad * 0.0015;
ui.drag_float(im_str!("Mass"), mass).speed(mass_speed).build();
ui.drag_float(im_str!("Radius"), rad).speed(rad_speed).build();
if ui.small_button(im_str!("Create Body")) {
signals.push(UiSignal::Create);
}
Expand Down Expand Up @@ -163,6 +169,7 @@ impl ImGuiWrapper {
num_iterations: &mut i32,
creating: &mut bool,
items_hovered: &mut bool,
entity: Option<Entity>,
) {
// Update mouse
self.update_mouse();
Expand Down Expand Up @@ -195,6 +202,7 @@ impl ImGuiWrapper {
dt,
num_iterations,
&mut self.sent_signals,
*entity,
);
}
_ => unimplemented!(),
Expand Down
54 changes: 40 additions & 14 deletions src/main_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,35 @@ impl EventHandler for MainState {

ggez::graphics::draw(ctx, &mesh, graphics::DrawParam::new()).expect("error drawing mesh");
let hidpi_factor = self.hidpi_factor;
self.imgui_wrapper.render(
ctx,
hidpi_factor,
&mut self.dt,
&mut self.mass,
&mut self.rad,
&mut self.num_iterations,
&mut self.creating,
&mut self.items_hovered,
);
if let Some(e) = self.selected_entity {
let mut mass = self.main_world.entity_data_mut::<Mass>(e).unwrap().0;
let mut rad = self.main_world.entity_data_mut::<Radius>(e).unwrap().0;
self.imgui_wrapper.render(
ctx,
hidpi_factor,
&mut self.dt,
&mut mass,
&mut rad,
&mut self.num_iterations,
&mut self.creating,
&mut self.items_hovered,
self.selected_entity, //TODO Remove this
);
self.main_world.entity_data_mut::<Mass>(e).unwrap().0 = mass;
self.main_world.entity_data_mut::<Radius>(e).unwrap().0 = rad;
} else {
self.imgui_wrapper.render(
ctx,
hidpi_factor,
&mut self.dt,
&mut self.mass,
&mut self.rad,
&mut self.num_iterations,
&mut self.creating,
&mut self.items_hovered,
self.selected_entity,
);
}

ggez::graphics::present(ctx)
}
Expand All @@ -171,18 +190,25 @@ impl EventHandler for MainState {
MouseButton::Right => {
self.imgui_wrapper.shown_menus.clear();
let clicked_query = <(Read<Position>, Read<Radius>)>::query();
let mut entity: Option<Entity> = None;
self.selected_entity = None;

let coords = ggez::graphics::screen_coordinates(ctx);
let mouse_pos = Point::new(
x * coords.w / self.resolution.x,
y * coords.h / self.resolution.y,
);

for (e, (pos, rad)) in clicked_query.iter_entities(&self.main_world) {
if pos.dist([x, y].into()) <= rad.0 {
entity = Some(e);
if pos.dist(mouse_pos.into()) <= rad.0 {
self.selected_entity = Some(e);
break;
}
}

//TODO Remove selected_entity
self.imgui_wrapper
.shown_menus
.push(UiChoice::SideMenu(entity));
.push(UiChoice::SideMenu(self.selected_entity));
}
MouseButton::Left => {
if self.creating {
Expand Down

0 comments on commit 2798cc2

Please sign in to comment.