Skip to content

Commit

Permalink
Refactor duplicate to use_duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Cactice committed Jul 15, 2024
1 parent e583afe commit 1624ba9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 55 deletions.
55 changes: 19 additions & 36 deletions examples/list/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use experiment::responsive::constraint::YConstraint;
use experiment::responsive::layout_machine::ConstraintMap;
use experiment::serde_json;
use experiment::uses::use_duplicate;
use experiment::{responsive::layout_machine::LayoutMachine, uses::use_svg};
use guppies::bytemuck::cast_slice;
use guppies::{GpuRedraw, Guppy};
Expand All @@ -21,8 +22,24 @@ pub fn main() {
);
let container_name = "ComponentBox #transform #layout".to_owned();

let mut list_1 = duplicate(&mut layout_machine, container_name.clone(), 0);
let mut list_2 = duplicate(&mut layout_machine, container_name, 1);
let component_name = "ListItem #transform #layout #component".to_string();
let xml = &include_str!("../V2.svg");
let mut list_1 = use_duplicate(
xml.to_string(),
&mut layout_machine,
component_name.clone(),
container_name.clone(),
0,
70.0,
);
let mut list_2 = use_duplicate(
xml.to_string(),
&mut layout_machine,
component_name,
container_name.clone(),
1,
70.0,
);
list_1.update_text("word #dynamicText #transform #layout", "abb");
list_2.update_text("word #dynamicText #transform #layout", "abbbbbbbabfdkj");

Expand All @@ -44,40 +61,6 @@ pub fn main() {
guppy.start();
}

fn duplicate(
layout_machine: &mut LayoutMachine,
container_name: String,
index: u32,
) -> salvage::svg_set::SvgSet {
let container_name_with_suffix = container_name.clone() + " " + &index.to_string();
let transform_id = (layout_machine.layouts.len() + 1).try_into().unwrap();
let list = use_svg(
include_str!("../V2.svg").to_string(),
|node, mut pass_down| {
layout_machine.add_node(&node, &mut pass_down, Some(&index.to_string()));
},
Some((
"ListItem #transform #layout #component".to_string(),
container_name_with_suffix.clone(),
)),
Some(transform_id),
);

let mut layout = layout_machine
.id_to_layout
.get(&container_name)
.cloned()
.expect(&container_name);
layout.constraint.y = match layout.constraint.y {
YConstraint::Top(y) => YConstraint::Top(y + 70.0 * index as f32),
y => y,
};
layout_machine
.id_to_layout
.insert(container_name_with_suffix, layout);
list
}

#[mobile_entry_point]
pub fn mobile_main() {
main()
Expand Down
4 changes: 2 additions & 2 deletions experiment/src/responsive/clickable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ impl ClickableBbox {
ClickableBbox::Layout(id) => layout_machine.get_bbox_for(id.to_string()).unwrap(),
ClickableBbox::Bbox(bbox) => *bbox,
};
let click = Mat4::from_scale([1., -1., 1.].into())
let click = bbox.inverse()
* Mat4::from_scale([1., -1., 1.].into())
* Mat4::from_translation([-1.0, -1., 0.].into())
* layout_machine.display_mat4.inverse()
* click;
let click = bbox.inverse() * click;
if click.x.abs() < 1. && click.y.abs() < 1. {
return true;
}
Expand Down
39 changes: 38 additions & 1 deletion experiment/src/uses.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use salvage::{svg_set::SvgSet, usvg::Node};

use crate::svg_init::{get_default_init_callback, PassDown};
use crate::{
responsive::{constraint::YConstraint, layout_machine::LayoutMachine},
svg_init::{get_default_init_callback, PassDown},
};

pub fn use_svg<C: FnMut(&Node, &mut PassDown)>(
xml: String,
Expand All @@ -24,3 +27,37 @@ pub fn use_svg<C: FnMut(&Node, &mut PassDown)>(
(geometry, passdown)
})
}

pub fn use_duplicate(
xml: String,
layout_machine: &mut LayoutMachine,
component_name: String,
container_name: String,
index: u32,
offset: f32,
) -> salvage::svg_set::SvgSet {
let container_name_with_suffix = container_name.clone() + " " + &index.to_string();
let transform_id = (layout_machine.layouts.len() + 1).try_into().unwrap();
let list = use_svg(
xml,
|node, mut pass_down| {
layout_machine.add_node(&node, &mut pass_down, Some(&index.to_string()));
},
Some((component_name, container_name_with_suffix.clone())),
Some(transform_id),
);

let mut layout = layout_machine
.id_to_layout
.get(&container_name)
.cloned()
.expect(&container_name);
layout.constraint.y = match layout.constraint.y {
YConstraint::Top(y) => YConstraint::Top(y + offset * index as f32),
y => y,
};
layout_machine
.id_to_layout
.insert(container_name_with_suffix, layout);
list
}
32 changes: 16 additions & 16 deletions guppies/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ impl<'a> RedrawMachine<'a> {
surface_format,
config,
} = self;
let msaa_texture = device
.create_texture(&wgpu::TextureDescriptor {
label: Some("Multisampled frame descriptor"),
size: wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: SAMPLE_COUNT,
dimension: wgpu::TextureDimension::D2,
format: config.format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: Default::default(),
})
.create_view(&wgpu::TextureViewDescriptor::default());
let msaa_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Multisampled frame descriptor"),
size: wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: SAMPLE_COUNT,
dimension: wgpu::TextureDimension::D2,
format: config.format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: Default::default(),
});
let x = msaa_texture.as_image_copy();
let msaa_texture = msaa_texture.create_view(&wgpu::TextureViewDescriptor::default());
redraws
.iter()
.zip(gpu_redraws.iter_mut())
Expand Down

0 comments on commit 1624ba9

Please sign in to comment.