Skip to content

Commit

Permalink
Merge pull request #61 from cyypherus/lifetime-hell-no-embed
Browse files Browse the repository at this point in the history
?
  • Loading branch information
cyypherus authored Jan 31, 2025
2 parents dc2eac8 + 8159f48 commit b2c2724
Show file tree
Hide file tree
Showing 31 changed files with 1,378 additions and 1,383 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pkg/
dist/
traces/
*.DS_Store
.cargo/
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ test-api = []
macroquad-examples = ["macroquad"]
egui-examples = ["egui", "eframe", "egui_extras"]

required-features = ["macroquad-examples"]

[[example]]
name = "macroquad-example"
path = "examples/macroquad-example/src/main.rs"
Expand Down
72 changes: 37 additions & 35 deletions examples/demo-site/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl eframe::App for TemplateApp {
ctx.set_zoom_factor(zoom_factor);
}
egui::CentralPanel::default().show(ctx, |ui| {
let layout = Layout::new(my_layout_fn);
let mut layout = Layout::new(my_layout_fn());
let viewport = ctx.input(|i| i.screen_rect());
let available_area = area_from(viewport);
let mut state = State {
Expand All @@ -56,29 +56,31 @@ const DEMO_HINT: Color32 = Color32::from_rgb(35, 35, 38);
const DEMO_FG: Color32 = Color32::from_rgb(250, 250, 255);
const DEMO_FG_SECONDARY: Color32 = Color32::from_rgb(180, 180, 183);

fn my_layout_fn<'a>(state: &mut State<'_>) -> Node<State<'a>> {
stack(vec![
rect(Color32::TRANSPARENT, DEMO_BG, 0.),
row(vec![
row_divider(DEMO_GRAY).width(1.),
column(vec![
header(state),
col_divider(DEMO_GRAY).height(1.),
main_view(state),
col_divider(DEMO_GRAY).height(1.),
footer(state),
]),
fn my_layout_fn<'a>() -> Node<'a, State<'a>> {
dynamic(|state| {
stack(vec![
rect(Color32::TRANSPARENT, DEMO_BG, 0.),
row(vec![
row_divider(DEMO_GRAY).width(1.),
column(vec![
header(state),
col_divider(DEMO_GRAY).height(1.),
main_view(state),
col_divider(DEMO_GRAY).height(1.),
footer(state),
]),
])
.align(Align::Top),
if *state.sidebar {
side_bar(state)
} else {
empty()
},
])
.align(Align::Top),
if *state.sidebar {
side_bar(state)
} else {
empty()
},
])
})
}

fn footer<'a>(state: &mut State<'_>) -> Node<State<'a>> {
fn footer<'a>(state: &mut State<'_>) -> Node<'a, State<'a>> {
row_spaced(
10.,
vec![
Expand All @@ -105,7 +107,7 @@ fn footer<'a>(state: &mut State<'_>) -> Node<State<'a>> {
.height(40.)
}

fn main_view<'a>(state: &mut State<'_>) -> Node<State<'a>> {
fn main_view<'a>(state: &mut State<'_>) -> Node<'a, State<'a>> {
let profile_blurb = "Your public profile URL can be shared with anyone and allows them to immediately see your bases and activity in Backer.";
let pic_blurb = "Upload a profile picture of yourself or the character you always wanted to be. Your avatar will be displayed all over the Backer world.";
let info_blurb = "Tell the world about yourself. Information you add will be visible only in your profile, not for all users.";
Expand Down Expand Up @@ -298,7 +300,7 @@ fn main_view<'a>(state: &mut State<'_>) -> Node<State<'a>> {
.pad(20.)])
}

fn side_bar<'a>(state: &mut State<'_>) -> Node<State<'a>> {
fn side_bar<'a>(state: &mut State<'_>) -> Node<'a, State<'a>> {
stack(vec![
rect(Color32::TRANSPARENT, DEMO_BG, 0.),
column_spaced(
Expand Down Expand Up @@ -331,7 +333,7 @@ fn side_bar<'a>(state: &mut State<'_>) -> Node<State<'a>> {
.width(200.)
}

fn header<'a>(state: &mut State<'_>) -> Node<State<'a>> {
fn header<'a>(state: &mut State<'_>) -> Node<'a, State<'a>> {
row_spaced(
10.,
vec![
Expand Down Expand Up @@ -366,7 +368,7 @@ fn header<'a>(state: &mut State<'_>) -> Node<State<'a>> {
.height(80.)
}

fn menu_button<'a>(_ui: &mut State<'_>) -> Node<State<'a>> {
fn menu_button<'a>(_ui: &mut State<'_>) -> Node<'a, State<'a>> {
draw(move |area, ui: &mut State<'_>| {
if ui
.ui
Expand All @@ -385,7 +387,7 @@ fn menu_button<'a>(_ui: &mut State<'_>) -> Node<State<'a>> {
.height(30.)
}

fn icon<'a>(image: impl Into<ImageSource<'static>> + 'static) -> Node<State<'a>> {
fn icon<'a>(image: impl Into<ImageSource<'static>> + 'static) -> Node<'a, State<'a>> {
let image = Image::new(image).tint(Color32::WHITE);
draw(move |area, ui: &mut State<'_>| {
ui.ui.put(rect_from(area), image.clone());
Expand All @@ -396,15 +398,15 @@ fn label<'a, S: AsRef<str> + 'static + Clone + Copy>(
state: &mut State<'_>,
text: S,
size: f32,
) -> Node<State<'a>> {
) -> Node<'a, State<'a>> {
label_common(state, text, size, false, Color32::WHITE)
}

fn fit_label<'a, S: AsRef<str> + 'static + Clone + Copy>(
state: &mut State<'_>,
text: S,
size: f32,
) -> Node<State<'a>> {
) -> Node<'a, State<'a>> {
label_common(state, text, size, true, Color32::WHITE)
}

Expand All @@ -413,7 +415,7 @@ fn fit_label_color<'a, S: AsRef<str> + 'static + Clone + Copy>(
text: S,
size: f32,
color: Color32,
) -> Node<State<'a>> {
) -> Node<'a, State<'a>> {
label_common(state, text, size, true, color)
}

Expand All @@ -422,7 +424,7 @@ fn label_color<'a, S: AsRef<str> + 'static + Clone + Copy>(
text: S,
size: f32,
color: Color32,
) -> Node<State<'a>> {
) -> Node<'a, State<'a>> {
label_common(state, text, size, false, color)
}

Expand All @@ -432,7 +434,7 @@ fn label_common<'a, S: AsRef<str> + 'static + Clone + Copy>(
size: f32,
fit_width: bool,
color: Color32,
) -> Node<State<'a>> {
) -> Node<'a, State<'a>> {
fn layout_job(
font_size: f32,
width: f32,
Expand Down Expand Up @@ -502,7 +504,7 @@ fn label_common<'a, S: AsRef<str> + 'static + Clone + Copy>(
}
}

fn col_divider<'a>(color: Color32) -> Node<State<'a>> {
fn col_divider<'a>(color: Color32) -> Node<'a, State<'a>> {
draw(move |area, ui: &mut State<'_>| {
ui.ui.painter().line_segment(
[
Expand All @@ -514,7 +516,7 @@ fn col_divider<'a>(color: Color32) -> Node<State<'a>> {
})
}

fn row_divider<'a>(color: Color32) -> Node<State<'a>> {
fn row_divider<'a>(color: Color32) -> Node<'a, State<'a>> {
draw(move |area, ui: &mut State<'_>| {
ui.ui.painter().line_segment(
[
Expand All @@ -526,7 +528,7 @@ fn row_divider<'a>(color: Color32) -> Node<State<'a>> {
})
}

fn rect<'a>(stroke: Color32, fill: Color32, rounding: f32) -> Node<State<'a>> {
fn rect<'a>(stroke: Color32, fill: Color32, rounding: f32) -> Node<'a, State<'a>> {
draw(move |area, ui: &mut State<'_>| {
ui.ui
.painter()
Expand All @@ -535,7 +537,7 @@ fn rect<'a>(stroke: Color32, fill: Color32, rounding: f32) -> Node<State<'a>> {
})
}

fn rect_stroke<'a>(color: Color32) -> Node<State<'a>> {
fn rect_stroke<'a>(color: Color32) -> Node<'a, State<'a>> {
draw(move |area, ui: &mut State<'_>| {
ui.ui
.painter()
Expand Down
1 change: 1 addition & 0 deletions examples/egui-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn my_layout_fn(ui: &mut Ui) -> Node<Ui> {
draw_c(ui),
],
)
.pad(10.)
}

fn draw_a(ui: &mut Ui) -> Node<Ui> {
Expand Down
28 changes: 10 additions & 18 deletions examples/macroquad-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use backer::models::*;
use backer::nodes::*;
use backer::traits::Scopable;
use backer::Layout;
use backer::Node;
use macroquad::prelude::*;
Expand Down Expand Up @@ -41,24 +40,17 @@ async fn main() {

const BTN_SIZE: f32 = 50.;
fn layout_for_highlight(ctx: &mut State) -> Node<State> {
struct HighlightScoper;
impl Scopable<State, HighlightedCase> for HighlightScoper {
fn scope<Result>(
scoping: &mut State,
f: impl FnOnce(&mut HighlightedCase) -> Result,
) -> Result {
f(&mut scoping.highlight)
}
}
let highlight = ctx.highlight;
row_spaced(
10.,
vec![
if highlight == HighlightedCase::RelAbsSequence || highlight == HighlightedCase::None {
scope::<_, _, HighlightScoper>(|highlight| rel_abs_seq(*highlight))
} else {
empty()
},
draw(|area, state: &mut State| {
Layout::new(|state: &mut HighlightedCase| rel_abs_seq(*state))
.draw(area, &mut state.highlight);
})
.visible(
highlight == HighlightedCase::RelAbsSequence || highlight == HighlightedCase::None,
),
if highlight == HighlightedCase::AlignmentOffset || highlight == HighlightedCase::None {
column_spaced(
10.,
Expand Down Expand Up @@ -134,7 +126,7 @@ fn rel_abs_seq(_highlight: HighlightedCase) -> Node<HighlightedCase> {

fn text<U>(string: &'static str, font_size: f32, color: Color) -> Node<U> {
let dimensions = measure_text(string, None, font_size as u16, 1.0);
draw(move |area: Area, _| {
draw(move |area: Area, _: &mut U| {
draw_text(
string,
area.x + ((area.width - dimensions.width) * 0.5),
Expand All @@ -148,7 +140,7 @@ fn text<U>(string: &'static str, font_size: f32, color: Color) -> Node<U> {
}

fn rect<U>(color: Color) -> Node<U> {
draw(move |area: Area, _| {
draw(move |area: Area, _: &mut U| {
draw_rectangle(area.x, area.y, area.width, area.height, color);
})
}
Expand All @@ -157,7 +149,7 @@ fn button<U, Action>(label: &'static str, action: Action) -> Node<U>
where
Action: Fn(&mut U) + 'static,
{
draw(move |area: Area, ctx| {
draw(move |area: Area, ctx: &mut U| {
if widgets::Button::new(label)
.size(vec2(area.width, area.height))
.position(vec2(area.x, area.y))
Expand Down
Loading

0 comments on commit b2c2724

Please sign in to comment.