Skip to content

Commit

Permalink
remove corner from chamber
Browse files Browse the repository at this point in the history
  • Loading branch information
H4kor committed Dec 20, 2023
1 parent 137dd70 commit 7e2c5cb
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 3 deletions.
48 changes: 47 additions & 1 deletion src/chamber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,32 @@ impl Chamber {
min_wall
}

pub(crate) fn split(&mut self, wall_id: u32, pos: Vec2<i32>) {
/// get nearest corner of a room
/// returns a tuple of walls. The tuple will always be neighboring.
/// The common point of the walls is the nearest corner
/// The corner will always be wall_1.p2 == wall_2.p1
pub fn nearest_corner(&self, pos: Vec2<f64>) -> Option<(Wall, Wall)> {
// get the first wall
let wall_1 = self.walls.iter().min_by(|a, b| {
(a.p2 - pos.into())
.sqr_len()
.total_cmp(&(b.p2 - pos.into()).sqr_len())
});
match wall_1 {
Some(wall_1) => {
let p = self.walls.iter().position(|w| w.id == wall_1.id).unwrap();
let wall_2 = if p != self.walls.len() - 1 {
self.walls[p + 1]
} else {
self.walls[0]
};
Some((*wall_1, wall_2))
}
None => None,
}
}

pub(crate) fn split(&mut self, wall_id: WallId, pos: Vec2<i32>) {
let idx = self
.walls
.iter()
Expand All @@ -275,6 +300,27 @@ impl Chamber {
self.walls[idx] = w1;
self.walls.insert(idx + 1, w2);
}

pub(crate) fn collapse(&mut self, wall_id: WallId) -> WallId {
let idx = self
.walls
.iter()
.position(|w| w.id == wall_id)
.unwrap()
.clone();
let next_idx = if idx == self.walls.len() - 1 {
0
} else {
idx + 1
};
let removed_id = self.walls[next_idx].id;
let mut n_wall = self.walls[idx];
n_wall.p2 = self.walls[next_idx].p2;
self.walls[idx] = n_wall;
self.walls.remove(next_idx);

removed_id
}
}

impl Wall {
Expand Down
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ impl<T: Mul<T, Output = T> + Add<T, Output = T> + Into<f64> + Copy> Vec2<T> {
pub fn sqr_len(&self) -> f64 {
((self.x * self.x) + (self.y * self.y)).into()
}

pub fn len(&self) -> f64 {
f64::sqrt(self.sqr_len())
}

pub fn dot(&self, other: Vec2<T>) -> T {
(self.x * other.x) + (self.y * other.y)
}
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@ fn build_ui(app: &Application) {
state::EditMode::AppendChamber,
"document-edit",
);
let delete_corner_button = EditModeButton::new(
control.clone(),
state::EditMode::RemoveVertex,
"list-remove",
);

let add_door_button =
EditModeButton::new(control.clone(), state::EditMode::AddDoor, "insert-link");

tool_box.append(&add_chamber_button.widget);
tool_box.append(&select_chamber_button.borrow().widget);
tool_box.append(&split_edge_button.borrow().widget);
tool_box.append(&append_verts_button.borrow().widget);
tool_box.append(&split_edge_button.borrow().widget);
tool_box.append(&delete_corner_button.borrow().widget);
tool_box.append(&add_door_button.borrow().widget);
side_box.append(&tool_box);

Expand Down
21 changes: 21 additions & 0 deletions src/state/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum StateCommand {
ChangeChamberNotes(ChamberId, String),
ChangeChamberHidden(ChamberId, bool),
SplitWall(ChamberId, WallId, Vec2<i32>),
CollapseWall(ChamberId, WallId),
DeleteChamber(ChamberId),
AddDoor(Door),
SelectDoor(Option<DoorId>),
Expand Down Expand Up @@ -83,6 +84,26 @@ impl StateCommand {
.split(*wall_id, *pos);
vec![StateEvent::ChamberModified(*chamber_id)]
}
StateCommand::CollapseWall(chamber_id, wall_id) => {
let removed_wall_id = state
.dungeon
.chamber_mut(*chamber_id)
.unwrap()
.collapse(*wall_id);
// remove doors on the removed wall
let mut events: Vec<StateEvent> = state
.dungeon
.doors
.iter()
.filter(|d| d.on_wall == removed_wall_id)
.map(|d| StateEvent::DoorDeleted(d.id))
.collect();
state.dungeon.doors.retain(|d| d.on_wall != removed_wall_id);

events.push(StateEvent::ChamberModified(*chamber_id));

events
}
StateCommand::DeleteChamber(chamber_id) => {
let deleted_door_ids = state.dungeon.remove_chamber(*chamber_id);
let mut events: Vec<StateEvent> = deleted_door_ids
Expand Down
3 changes: 3 additions & 0 deletions src/state/edit_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub enum EditMode {
AppendChamber,
SplitEdge,
AddDoor,
RemoveVertex,
}

impl EditMode {
Expand All @@ -13,6 +14,7 @@ impl EditMode {
EditMode::AppendChamber => "AppendChamber".to_owned(),
EditMode::SplitEdge => "SplitEdge".to_owned(),
EditMode::AddDoor => "AddDoor".to_owned(),
EditMode::RemoveVertex => "RemoveVertex".to_owned(),
}
}

Expand All @@ -22,6 +24,7 @@ impl EditMode {
"AppendChamber" => EditMode::AppendChamber,
"SplitEdge" => EditMode::SplitEdge,
"AddDoor" => EditMode::AddDoor,
"RemoveVertex" => EditMode::RemoveVertex,
_ => todo!(),
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ fn line_to_command(l: &String) -> Option<StateCommand> {
},
))
}
"CollapseWall" => {
let v: Value = serde_json::from_str(data).unwrap();
Some(StateCommand::CollapseWall(
v["chamber_id"].as_u64().unwrap() as ChamberId,
v["wall_id"].as_u64().unwrap() as WallId,
))
}
"DeleteChamber" => {
let v: Value = serde_json::from_str(data).unwrap();
Some(StateCommand::DeleteChamber(
Expand Down Expand Up @@ -181,6 +188,7 @@ pub fn save_to_file(save_file: String, cmds: &Vec<StateCommand>) {
StateCommand::ChangeChamberHidden(_, _) => "ChangeChamberHidden".to_owned(),
StateCommand::ChangeMode(_) => "ChangeMode".to_owned(),
StateCommand::SplitWall(_, _, _) => "SplitWall".to_owned(),
StateCommand::CollapseWall(_, _) => "CollapseWall".to_owned(),
StateCommand::DeleteChamber(_) => "DeleteChamber".to_owned(),
StateCommand::AddDoor(_) => "AddDoor".to_owned(),
StateCommand::ChangeDoorName(_, _) => "ChangeDoorName".to_owned(),
Expand Down Expand Up @@ -219,6 +227,10 @@ pub fn save_to_file(save_file: String, cmds: &Vec<StateCommand>) {
"x": pos.x,
"y": pos.y
}),
StateCommand::CollapseWall(chamber_id, wall_id) => json!({
"chamber_id": chamber_id,
"wall_id": wall_id,
}),
StateCommand::DeleteChamber(chamber_id) => json!({ "chamber_id": chamber_id }),
StateCommand::AddDoor(door) => json!({
"part_of": door.part_of,
Expand Down
46 changes: 45 additions & 1 deletion src/view/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use gtk::{DrawingArea, EventControllerMotion};
use std::cell::RefCell;
use std::rc::Rc;

use super::primitives::{Line, Primitive};
use super::primitives::{Line, Point, Primitive};

pub struct Canvas {
pub widget: DrawingArea,
Expand Down Expand Up @@ -260,6 +260,39 @@ impl Canvas {
}
}
}
EditMode::RemoveVertex => {
if let Some(chamber) = control.state.active_chamber() {
match chamber.nearest_corner(control.state.cursor_world_pos()) {
Some((w1, w2)) => {
let color = Rgb {
r: 1.0,
g: 0.0,
b: 0.0,
};
Line {
from: w1.p1.into(),
to: w1.p2.into(),
color: color,
width: 3.0,
}
.draw(ctx);
Line {
from: w2.p1.into(),
to: w2.p2.into(),
color: color,
width: 3.0,
}
.draw(ctx);
Point {
at: w1.p2.into(),
color: color,
}
.draw(ctx)
}
None => {}
}
}
}
}
}

Expand Down Expand Up @@ -350,13 +383,24 @@ impl Canvas {
return vec![];
}

fn click_remove_vertex(&mut self, control: &mut StateController) -> Vec<StateCommand> {
if let Some(chamber) = control.state.active_chamber() {
match chamber.nearest_corner(control.state.cursor_world_pos()) {
Some((w1, _)) => return vec![StateCommand::CollapseWall(chamber.id, w1.id)],
None => {}
};
}
return vec![];
}

fn click(&mut self, control: Rc<RefCell<StateController>>) -> Vec<StateCommand> {
let control = &mut *control.borrow_mut();
let commands = match control.state.mode {
EditMode::Select => self.click_select(control),
EditMode::AppendChamber => self.click_append_chamber(control),
EditMode::SplitEdge => self.click_split_edge(control),
EditMode::AddDoor => self.click_add_door(control),
EditMode::RemoveVertex => self.click_remove_vertex(control),
};
self.update();
commands
Expand Down

0 comments on commit 7e2c5cb

Please sign in to comment.