Skip to content

Commit f7a228b

Browse files
committed
Remove ContentsLayout and use trait objects.
- This fixes a newer compiler lifetime issue (rust-lang/rust#42868) and requires concrete types in trait methods for object safety (`ContentsStorage` and slices for item lists). - Since item list slots can't be remapped, we need to include an offset with the context for section contents. - Section contents now requires the item list be sorted by slot and `section_items` no longer clones. - Move inline contents to a new module.
1 parent e38757c commit f7a228b

File tree

8 files changed

+333
-343
lines changed

8 files changed

+333
-343
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ edition = "2021"
1111
name = "ex1"
1212

1313
[dependencies]
14-
ambassador = { version = "0.3.4", default-features = false }
1514
bitvec = "1.0.1"
1615
egui = "0.19.0"
1716
flagset = "0.4.3"

examples/ex1.rs

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() {
2323
}
2424
};
2525

26-
let mut contents = HashMap::new();
26+
let mut contents = ContentsStorage::new();
2727
let mut images = HashMap::new();
2828

2929
let boomerang = Item::new(
@@ -64,80 +64,69 @@ fn main() {
6464

6565
let potion2 = potion.clone().with_id(next_id()).with_name("Potion 2");
6666

67+
// Setup containers. It's important to note here that there are only three containers,
68+
// the paper doll, the ground, and the pouch. Sectioned contents is one container split
69+
// into many sections.
6770
let paper_doll_id = next_id();
6871
let paper_doll = SectionContents::new(
6972
SectionLayout::Grid(1),
7073
vec![
7174
HeaderContents::new(
7275
"Bag of any! 4x4:",
73-
GridContents::new((4, 4))
74-
// accepts any item
75-
.with_flags(FlagSet::full()),
76+
GridContents::new((4, 4)).with_flags(FlagSet::full()), // accepts any item
7677
)
77-
.into(),
78+
.boxed(),
7879
HeaderContents::new(
7980
"Only potions! 2x2:",
80-
GridContents::new((2, 2))
81-
// accepts only potions
82-
.with_flags(ItemFlags::Potion),
81+
GridContents::new((2, 2)).with_flags(ItemFlags::Potion), // accepts only potions
8382
)
84-
.into(),
83+
.boxed(),
8584
HeaderContents::new(
8685
"Weapon here:",
87-
ExpandingContents::new((2, 2))
88-
// accepts only weapons
89-
.with_flags(ItemFlags::Weapon),
86+
ExpandingContents::new((2, 2)).with_flags(ItemFlags::Weapon), // accepts only weapons
9087
)
91-
.into(),
88+
.boxed(),
9289
HeaderContents::new(
9390
"Section contents 3x1x2:",
9491
SectionContents::new(
9592
SectionLayout::Grid(3),
96-
vec![
97-
GridContents::new((1, 2))
98-
.with_flags(FlagSet::full()) // accepts any item
99-
.into(),
100-
GridContents::new((1, 2))
101-
.with_flags(FlagSet::full()) // accepts any item
102-
.into(),
103-
GridContents::new((1, 2))
104-
.with_flags(FlagSet::full()) // accepts any item
105-
.into(),
106-
],
93+
std::iter::repeat(
94+
GridContents::new((1, 2)).with_flags(FlagSet::full()), // accepts any item
95+
)
96+
.take(3)
97+
.map(Contents::boxed)
98+
.collect(),
10799
),
108100
)
109-
.into(),
101+
.boxed(),
110102
HeaderContents::new(
111103
"Holds a container:",
112104
InlineContents::new(
113-
ExpandingContents::new((2, 2))
114-
// we only accept containers
115-
.with_flags(ItemFlags::Container),
105+
ExpandingContents::new((2, 2)).with_flags(ItemFlags::Container), // we only accept containers
116106
),
117107
)
118-
.into(),
108+
.boxed(),
119109
],
120110
);
121-
contents.insert(paper_doll_id, (paper_doll.into(), vec![]));
111+
112+
contents.insert(paper_doll_id, (paper_doll.boxed(), vec![]));
122113

123114
let pouch_contents = SectionContents::new(
124115
SectionLayout::Grid(4),
125-
std::iter::repeat(
126-
GridContents::new((1, 1))
127-
.with_flags(ItemFlags::Potion)
128-
.into(),
129-
)
130-
.take(4)
131-
.collect(),
116+
std::iter::repeat(GridContents::new((1, 1)).with_flags(ItemFlags::Potion))
117+
.take(4)
118+
.map(Contents::boxed)
119+
.collect(),
132120
);
133-
contents.insert(pouch.id, (pouch_contents.into(), vec![]));
121+
contents.insert(pouch.id, (Box::new(pouch_contents), vec![]));
134122

135123
let ground_id = next_id();
136124
let ground = GridContents::new((10, 10)).with_flags(FlagSet::full());
137125
contents.insert(
138126
ground_id,
139127
(
140-
ground.into(),
128+
Box::new(ground),
129+
// MUST BE SORTED BY SLOT
141130
vec![
142131
(0, boomerang),
143132
(2, pouch),
@@ -151,16 +140,14 @@ fn main() {
151140
Box::new(Runic {
152141
images,
153142
drag_item: None,
154-
contents: ContentsStorage(contents),
143+
contents,
155144
paper_doll_id,
156145
ground_id,
157146
})
158147
}),
159148
)
160149
}
161150

162-
struct ContentsStorage(HashMap<usize, (ContentsLayout, Vec<(usize, Item)>)>);
163-
164151
//#[derive(Default)]
165152
struct Runic {
166153
#[allow(dead_code)]
@@ -202,10 +189,6 @@ impl eframe::App for Runic {
202189
egui::CentralPanel::default().show(ctx, |ui| {
203190
let drag_item = &mut self.drag_item;
204191
let q = &self.contents;
205-
let q = |id: usize| {
206-
q.0.get(&id)
207-
.map(|(c, i)| (c, i.iter().map(|(slot, item)| (*slot, item))))
208-
};
209192

210193
let move_data = ContainerSpace::show(drag_item, ui, |drag_item, ui| {
211194
let data = MoveData::default();
@@ -254,7 +237,6 @@ impl eframe::App for Runic {
254237
// refs would make this transactable.
255238
match self
256239
.contents
257-
.0
258240
.get_mut(&drag.container.0)
259241
.and_then(|(_, items)| {
260242
let idx =
@@ -269,8 +251,14 @@ impl eframe::App for Runic {
269251

270252
// Insert item. The contents must exist
271253
// already to insert an item?
272-
match self.contents.0.get_mut(&container) {
273-
Some((_, items)) => items.push((slot, item)),
254+
match self.contents.get_mut(&container) {
255+
Some((_, items)) => {
256+
// Items must be ordered by slot in order for section contents to work.
257+
let i = items
258+
.binary_search_by_key(&slot, |&(slot, _)| slot)
259+
.expect_err("item slot free");
260+
items.insert(i, (slot, item));
261+
}
274262
None => tracing::error!(
275263
"could not find container {} to add to",
276264
container

0 commit comments

Comments
 (0)