-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.rs
115 lines (104 loc) · 4.38 KB
/
inventory.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use bevy::prelude::*;
use bevy_ui_builder::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(UiBuilderPlugin)
.register_bind_data_source::<Inventory>(true)
.register_bind_data_source::<Item>(true)
.register_bind_component::<Inventory, Item>()
.register_bind_event::<MyEvent, Inventory>()
.add_startup_system(setup)
.run();
}
#[derive(Component, Clone)]
pub struct Inventory {
pub items: Vec<Item>,
}
#[derive(Component, Clone)]
pub struct Item(String, u32);
#[derive(Clone)]
pub struct MyEvent;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let mut b = UiBuilder::new(&mut commands, ());
b.set_default_text_style(TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 24.,
color: Color::BLACK,
});
let inventory = Inventory {
items: vec![Item("a".into(), 1), Item("b".into(), 3)],
};
let mut inventory_entity = Entity::from_raw(u32::MAX);
b.node()
.with_name("inventory")
.pull_last(&mut inventory_entity)
.with_component(inventory)
.with_style_modifier((StyleSize::FULL, StyleCenterChildren, FlexDirection::Column))
.with_children(|b| {
b.text("").with_bind_source(
inventory_entity,
|_, inventory: &Inventory, mut text: Mut<Text>| {
text.sections[0].value = format!(
"total num: {}",
inventory.items.iter().fold(0, |s, a| s + a.1)
);
},
);
let mut items_entity = Entity::from_raw(u32::MAX);
let text_style = b.default_text_style.clone();
b.node()
.with_name("items")
.pull_last(&mut items_entity)
.with_style_modifier((StyleCenterChildren, FlexDirection::Column))
.with_on_source_change(
inventory_entity,
move |commands: &mut Commands, inventory: &Inventory| {
commands.entity(items_entity).despawn_descendants();
let mut b = UiBuilder::new(commands, ());
b.set_default_text_style(text_style.clone());
b.set_parent(items_entity);
for (idx, item) in inventory.items.iter().enumerate() {
b.node()
.with_name(format!("item{}", idx))
.with_component(item.clone())
.with_bind_source(
inventory_entity,
move |_, inventory: &Inventory, mut item: Mut<Item>| {
*item = inventory.items[idx].clone();
},
)
.with_children(|b| {
let parent = b.parent();
b.text("").with_bind_source(
parent,
|_, item: &Item, mut text: Mut<Text>| {
text.sections[0].value =
format!("name:{} amount:{}", item.0, item.1);
},
);
});
}
},
);
// controller
b.button()
.with_name("btn")
.with_style_modifier((StyleCenterChildren, StyleMargin::all_px(15.0)))
.with_send_event_click(MyEvent)
.with_event_bind_to_target(
inventory_entity,
|_commands, _ev: &MyEvent, mut inventory: Mut<Inventory>| {
info!("button clicked");
for item in inventory.as_mut().items.iter_mut() {
item.1 += 1;
}
inventory.items.push(Item("c".into(), 1));
},
)
.with_children(|b| {
b.text("all amount++\nadd item c");
});
});
}