Skip to content

Commit

Permalink
edit object style
Browse files Browse the repository at this point in the history
  • Loading branch information
H4kor committed Jan 6, 2024
1 parent 329409e commit 83eb4e7
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 10 deletions.
155 changes: 147 additions & 8 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
chamber::ChamberId,
common::{Rgb, Vec2},
config::{GRID_SIZE, WALL_WIDTH},
view::primitives::{Line, Polygon, Primitive},
view::primitives::{Circle, Line, Polygon, Primitive},
};

pub type ObjectId = u32;
Expand All @@ -14,12 +14,31 @@ pub enum ObjectStyle {
Round,
}

impl ObjectStyle {
pub fn to_str(&self) -> String {
match self {
ObjectStyle::Blocker => "Blocker".to_owned(),
ObjectStyle::Stairs => "Stairs".to_owned(),
ObjectStyle::Round => "Round".to_owned(),
}
}

pub fn from_str(s: &str) -> Self {
match s {
"Blocker" => ObjectStyle::Blocker,
"Stairs" => ObjectStyle::Stairs,
"Round" => ObjectStyle::Round,
_ => todo!(),
}
}
}

pub struct ObjectDrawOptions {
pub color: Option<Rgb>,
}

impl ObjectDrawOptions {
pub(crate) fn empty() -> ObjectDrawOptions {
pub fn empty() -> ObjectDrawOptions {
ObjectDrawOptions { color: None }
}
}
Expand Down Expand Up @@ -55,6 +74,23 @@ impl Object {
b: 1.0,
});

match self.style {
ObjectStyle::Blocker => self.draw_blocker(color),
ObjectStyle::Stairs => self.draw_stairs(color),
ObjectStyle::Round => self.draw_round(color),
}
}

pub fn contains(&self, pos: Vec2<f64>) -> bool {
let s: f64 = GRID_SIZE as f64;
let obj_pos: Vec2<f64> = self.pos.into();
pos.x >= obj_pos.x
&& pos.y >= obj_pos.y
&& pos.x <= (obj_pos.x + s)
&& pos.y <= (obj_pos.y + s)
}

fn draw_blocker(&self, color: Rgb) -> Vec<Box<dyn Primitive>> {
vec![
// draw box
Box::new(Polygon {
Expand Down Expand Up @@ -112,13 +148,116 @@ impl Object {
]
}

pub(crate) fn contains(&self, pos: Vec2<f64>) -> bool {
fn draw_stairs(&self, color: Rgb) -> Vec<Box<dyn Primitive>> {
let s: f64 = GRID_SIZE as f64;
let obj_pos: Vec2<f64> = self.pos.into();
pos.x >= obj_pos.x
&& pos.y >= obj_pos.y
&& pos.x <= (obj_pos.x + s)
&& pos.y <= (obj_pos.y + s)
let c: f64 = GRID_SIZE as f64 / 2.0;

vec![
// draw box
Box::new(Polygon {
dashed: self.hidden,
fill_color: color,
fill_opacity: 0.0,
stroke_color: color,
stroke_width: WALL_WIDTH,
points: vec![
Vec2 {
x: self.pos.x as f64,
y: self.pos.y as f64,
},
Vec2 {
x: (self.pos.x + GRID_SIZE) as f64,
y: self.pos.y as f64,
},
Vec2 {
x: (self.pos.x + GRID_SIZE) as f64,
y: (self.pos.y + GRID_SIZE) as f64,
},
Vec2 {
x: self.pos.x as f64,
y: (self.pos.y + GRID_SIZE) as f64,
},
],
}),
Box::new(Line {
color: color,
dashed: self.hidden,
from: Vec2 {
x: self.pos.x as f64 + c - 0.8 * c,
y: self.pos.y as f64 + 0.2 * s,
},
to: Vec2 {
x: self.pos.x as f64 + c + 0.8 * c,
y: self.pos.y as f64 + 0.2 * s,
},
width: WALL_WIDTH,
}),
Box::new(Line {
color: color,
dashed: self.hidden,
from: Vec2 {
x: self.pos.x as f64 + c - 0.6 * c,
y: self.pos.y as f64 + 0.4 * s,
},
to: Vec2 {
x: self.pos.x as f64 + c + 0.6 * c,
y: self.pos.y as f64 + 0.4 * s,
},
width: WALL_WIDTH,
}),
Box::new(Line {
color: color,
dashed: self.hidden,
from: Vec2 {
x: self.pos.x as f64 + c - 0.4 * c,
y: self.pos.y as f64 + 0.6 * s,
},
to: Vec2 {
x: self.pos.x as f64 + c + 0.4 * c,
y: self.pos.y as f64 + 0.6 * s,
},
width: WALL_WIDTH,
}),
Box::new(Line {
color: color,
dashed: self.hidden,
from: Vec2 {
x: self.pos.x as f64 + c - 0.2 * c,
y: self.pos.y as f64 + 0.8 * s,
},
to: Vec2 {
x: self.pos.x as f64 + c + 0.2 * c,
y: self.pos.y as f64 + 0.8 * s,
},
width: WALL_WIDTH,
}),
]
}

fn draw_round(&self, color: Rgb) -> Vec<Box<dyn Primitive>> {
let pos: Vec2<f64> = self.pos.into();
vec![
Box::new(Circle {
at: pos
+ Vec2::<f64> {
x: GRID_SIZE as f64 / 2.0,
y: GRID_SIZE as f64 / 2.0,
},
radius: GRID_SIZE as f64 / 2.0,
width: WALL_WIDTH,
color: color,
}),
Box::new(Circle {
at: pos
+ Vec2::<f64> {
x: GRID_SIZE as f64 / 2.0,
y: GRID_SIZE as f64 / 2.0,
},
radius: GRID_SIZE as f64 / 4.0,
width: WALL_WIDTH,
color: color,
}),
]
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/state/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
chamber::{Chamber, ChamberId, WallId},
common::Vec2,
door::{Door, DoorId},
object::{Object, ObjectId},
object::{Object, ObjectId, ObjectStyle},
};

use super::{events::StateEvent, EditMode, State};
Expand Down Expand Up @@ -34,6 +34,7 @@ pub enum StateCommand {
ChangeObjectName(ObjectId, String),
ChangeObjectNotes(ObjectId, String),
ChangeObjectHidden(ObjectId, bool),
ChangeObjectStyle(ObjectId, ObjectStyle),
}

impl StateCommand {
Expand Down Expand Up @@ -233,6 +234,10 @@ impl StateCommand {
state.dungeon.object_mut(*object_id).unwrap().hidden = *hidden;
vec![StateEvent::ObjectModified(*object_id)]
}
StateCommand::ChangeObjectStyle(object_id, style) => {
state.dungeon.object_mut(*object_id).unwrap().style = *style;
vec![StateEvent::ObjectModified(*object_id)]
}
}
}
}
14 changes: 13 additions & 1 deletion src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::chamber::{ChamberId, WallId};
use crate::common::Vec2;
use crate::door::{Door, DoorId};
use crate::object::ObjectId;
use crate::object::{ObjectId, ObjectStyle};
use crate::state::{EditMode, StateCommand};
use serde_json::json;
use serde_json::Value;
Expand Down Expand Up @@ -199,6 +199,13 @@ fn line_to_command(l: &String) -> Option<StateCommand> {
v["hidden"].as_bool().unwrap(),
))
}
"ChangeObjectStyle" => {
let v: Value = serde_json::from_str(data).unwrap();
Some(StateCommand::ChangeObjectStyle(
v["object_id"].as_u64().unwrap() as ObjectId,
ObjectStyle::from_str(v["style"].as_str().unwrap()),
))
}

_ => None,
},
Expand Down Expand Up @@ -265,6 +272,7 @@ pub fn save_to_file(save_file: String, cmds: &Vec<StateCommand>) {
StateCommand::ChangeObjectName(_, _) => "ChangeObjectName".to_owned(),
StateCommand::ChangeObjectNotes(_, _) => "ChangeObjectNotes".to_owned(),
StateCommand::ChangeObjectHidden(_, _) => "ChangeObjectHidden".to_owned(),
StateCommand::ChangeObjectStyle(_, _) => "ChangeObjectStyle".to_owned(),
};
let data = match cmd {
StateCommand::AddChamber => serde_json::Value::Null,
Expand Down Expand Up @@ -349,6 +357,10 @@ pub fn save_to_file(save_file: String, cmds: &Vec<StateCommand>) {
"object_id": object_id,
"hidden": hidden,
}),
StateCommand::ChangeObjectStyle(object_id, style) => json!({
"object_id": object_id,
"style": style.to_str(),
}),
};
data_str += format!("{} >> {}\n", name, data).as_str();
}
Expand Down
55 changes: 55 additions & 0 deletions src/view/object_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gtk::{gio, CheckButton, ListItem, PolicyType, ScrolledWindow, SignalListItem
use gtk::{prelude::*, Label, TextView};
use gtk::{Box, Entry};

use crate::object::ObjectStyle;
use crate::state::events::StateEvent;
use crate::state::{StateCommand, StateController, StateEventSubscriber};

Expand All @@ -17,6 +18,9 @@ pub struct ObjectEdit {
name_input: Entry,
notes_input: TextView,
hidden_input: CheckButton,
blocker_style: CheckButton,
stair_style: CheckButton,
round_style: CheckButton,
}

impl ObjectEdit {
Expand All @@ -31,6 +35,16 @@ impl ObjectEdit {
.css_classes(vec!["form-input"])
.label("Hidden")
.build();
let blocker_style = CheckButton::builder().label("Blocker").build();
let stair_style = CheckButton::builder()
.label("Stairs")
.group(&blocker_style)
.build();
let round_style = CheckButton::builder()
.css_classes(vec!["form-input"])
.label("Round")
.group(&blocker_style)
.build();

let chamber_vec: Vec<ChamberObject> =
vec![ChamberObject::new(None, "-- No Chamber --".to_owned())];
Expand Down Expand Up @@ -105,6 +119,33 @@ impl ObjectEdit {
}),
);

blocker_style.connect_toggled(
clone!(@strong control => move |w| if let Ok(mut control) = control.try_borrow_mut() {
match control.state.active_object_id {
None => (),
Some(object_id) => control.apply(StateCommand::ChangeObjectStyle(object_id, ObjectStyle::Blocker)),
}
}),
);

stair_style.connect_toggled(
clone!(@strong control => move |w| if let Ok(mut control) = control.try_borrow_mut() {
match control.state.active_object_id {
None => (),
Some(object_id) => control.apply(StateCommand::ChangeObjectStyle(object_id, ObjectStyle::Stairs)),
}
}),
);

round_style.connect_toggled(
clone!(@strong control => move |w| if let Ok(mut control) = control.try_borrow_mut() {
match control.state.active_object_id {
None => (),
Some(object_id) => control.apply(StateCommand::ChangeObjectStyle(object_id, ObjectStyle::Round)),
}
}),
);

let b = Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
Expand All @@ -117,6 +158,12 @@ impl ObjectEdit {
b.append(&Label::new(Some("Name")));
b.append(&name_i);
b.append(&hidden_i);

b.append(&Label::new(Some("Style")));
b.append(&blocker_style);
b.append(&stair_style);
b.append(&round_style);

b.append(&Label::new(Some("Notes")));
b.append(
&ScrolledWindow::builder()
Expand All @@ -136,6 +183,9 @@ impl ObjectEdit {
name_input: name_i,
notes_input: notes_i,
hidden_input: hidden_i,
blocker_style: blocker_style,
stair_style: stair_style,
round_style: round_style,
}));

control.borrow_mut().subscribe_any(re.clone());
Expand All @@ -159,6 +209,11 @@ impl ObjectEdit {
self.name_input.set_text(&object.name);
self.notes_input.buffer().set_text(&object.notes);
self.hidden_input.set_active(object.hidden);
match object.style {
ObjectStyle::Blocker => self.blocker_style.set_active(true),
ObjectStyle::Stairs => self.stair_style.set_active(true),
ObjectStyle::Round => self.round_style.set_active(true),
};
self.widget.set_visible(true);
} else {
self.widget.set_visible(false);
Expand Down
Loading

0 comments on commit 83eb4e7

Please sign in to comment.