forked from PPakalns/bevy_immediate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhot_patching.rs
More file actions
89 lines (73 loc) · 2.82 KB
/
hot_patching.rs
File metadata and controls
89 lines (73 loc) · 2.82 KB
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
use bevy::ecs::component::Component;
use bevy::ui::{Node, px};
use bevy::utils::default;
use bevy_immediate::{
Imm,
attach::{BevyImmediateAttachPlugin, ImmediateAttach},
lch, lid,
ui::{CapsUi, text::ImmUiText},
};
use crate::styles::{self, title_text_style};
pub struct HotPatchingExamplePlugin;
impl bevy::app::Plugin for HotPatchingExamplePlugin {
fn build(&self, app: &mut bevy::app::App) {
// Add bevy immediate plugin with UI support which will construct UI
// rooted at entity with `HelloWorldRoot` component
app.add_plugins(BevyImmediateAttachPlugin::<CapsUi, HotPatchingRoot>::new());
}
}
#[derive(Component)]
pub struct HotPatchingRoot;
impl ImmediateAttach<CapsUi> for HotPatchingRoot {
type Params = (); // Access data from World using SystemParam
fn construct(ui: &mut Imm<CapsUi>, _: &mut ()) {
// There are two possible workflows:
// 1.
// If `hotpatching` feature is enabled for `bevy_immediate` and `bevy` crates
//
// it will trigger recreation of UI.
//
// To execute demo with hotpatching: See bevy_immediate README.md "Hotpatching" section.
// 2.
// If `hotpatching` feature **is not** enabled for this crate.
//
// Hot patching will try to reuse already existing nodes
//
// This may not reload changes that are marked as `on_spawn`
//
// To force recreation of node:
//
// * Change id value passed to current `.ch_id(__)` or to any ancestor element.
// * Comment out all code and then uncomment it.
// * Open, close current UI,
// * You can use `lid!()` macro which will use line and column number for id.
// Then node recreation will be based on source code location.
// This library will try to develop a better way in future:
// * like inserting compilation time as id :)
lch!(ui)
.on_spawn_insert(title_text_style)
.on_spawn_text("Hot patching ");
ui.ch()
.on_spawn_text("Install dx tool and launch demo example with hotpatching");
ui.ch()
.on_spawn_text("(See bevy_immediate README.md \"Hotpatching\" section)");
lch!(ui).on_spawn_insert(|| Node {
height: px(50.),
..default()
});
ui.ch().on_spawn_text("COMMENT ME in code!");
// lch!(ui).on_spawn_text("UNCOMMENT ME!");
ui.ch().on_spawn_insert(|| Node {
height: px(50.),
..default()
});
ui.ch().on_spawn_text("Change loop counter in code:");
for idx in 0..3 {
ui.ch_id(lid!(idx))
.on_spawn_insert(styles::button_bundle)
.add(|ui| {
ui.ch().on_spawn_text("Button");
});
}
}
}