@@ -23,7 +23,7 @@ fn main() {
23
23
}
24
24
} ;
25
25
26
- let mut contents = HashMap :: new ( ) ;
26
+ let mut contents = ContentsStorage :: new ( ) ;
27
27
let mut images = HashMap :: new ( ) ;
28
28
29
29
let boomerang = Item :: new (
@@ -64,80 +64,69 @@ fn main() {
64
64
65
65
let potion2 = potion. clone ( ) . with_id ( next_id ( ) ) . with_name ( "Potion 2" ) ;
66
66
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.
67
70
let paper_doll_id = next_id ( ) ;
68
71
let paper_doll = SectionContents :: new (
69
72
SectionLayout :: Grid ( 1 ) ,
70
73
vec ! [
71
74
HeaderContents :: new(
72
75
"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
76
77
)
77
- . into ( ) ,
78
+ . boxed ( ) ,
78
79
HeaderContents :: new(
79
80
"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
83
82
)
84
- . into ( ) ,
83
+ . boxed ( ) ,
85
84
HeaderContents :: new(
86
85
"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
90
87
)
91
- . into ( ) ,
88
+ . boxed ( ) ,
92
89
HeaderContents :: new(
93
90
"Section contents 3x1x2:" ,
94
91
SectionContents :: new(
95
92
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( ) ,
107
99
) ,
108
100
)
109
- . into ( ) ,
101
+ . boxed ( ) ,
110
102
HeaderContents :: new(
111
103
"Holds a container:" ,
112
104
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
116
106
) ,
117
107
)
118
- . into ( ) ,
108
+ . boxed ( ) ,
119
109
] ,
120
110
) ;
121
- contents. insert ( paper_doll_id, ( paper_doll. into ( ) , vec ! [ ] ) ) ;
111
+
112
+ contents. insert ( paper_doll_id, ( paper_doll. boxed ( ) , vec ! [ ] ) ) ;
122
113
123
114
let pouch_contents = SectionContents :: new (
124
115
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 ( ) ,
132
120
) ;
133
- contents. insert ( pouch. id , ( pouch_contents . into ( ) , vec ! [ ] ) ) ;
121
+ contents. insert ( pouch. id , ( Box :: new ( pouch_contents ) , vec ! [ ] ) ) ;
134
122
135
123
let ground_id = next_id ( ) ;
136
124
let ground = GridContents :: new ( ( 10 , 10 ) ) . with_flags ( FlagSet :: full ( ) ) ;
137
125
contents. insert (
138
126
ground_id,
139
127
(
140
- ground. into ( ) ,
128
+ Box :: new ( ground) ,
129
+ // MUST BE SORTED BY SLOT
141
130
vec ! [
142
131
( 0 , boomerang) ,
143
132
( 2 , pouch) ,
@@ -151,16 +140,14 @@ fn main() {
151
140
Box :: new ( Runic {
152
141
images,
153
142
drag_item : None ,
154
- contents : ContentsStorage ( contents ) ,
143
+ contents,
155
144
paper_doll_id,
156
145
ground_id,
157
146
} )
158
147
} ) ,
159
148
)
160
149
}
161
150
162
- struct ContentsStorage ( HashMap < usize , ( ContentsLayout , Vec < ( usize , Item ) > ) > ) ;
163
-
164
151
//#[derive(Default)]
165
152
struct Runic {
166
153
#[ allow( dead_code) ]
@@ -202,10 +189,6 @@ impl eframe::App for Runic {
202
189
egui:: CentralPanel :: default ( ) . show ( ctx, |ui| {
203
190
let drag_item = & mut self . drag_item ;
204
191
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
- } ;
209
192
210
193
let move_data = ContainerSpace :: show ( drag_item, ui, |drag_item, ui| {
211
194
let data = MoveData :: default ( ) ;
@@ -254,7 +237,6 @@ impl eframe::App for Runic {
254
237
// refs would make this transactable.
255
238
match self
256
239
. contents
257
- . 0
258
240
. get_mut ( & drag. container . 0 )
259
241
. and_then ( |( _, items) | {
260
242
let idx =
@@ -269,8 +251,14 @@ impl eframe::App for Runic {
269
251
270
252
// Insert item. The contents must exist
271
253
// 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
+ }
274
262
None => tracing:: error!(
275
263
"could not find container {} to add to" ,
276
264
container
0 commit comments