wgui: widget_node_map to use more performant SecondaryMap
This commit is contained in:
@@ -124,7 +124,7 @@ impl Animation {
|
|||||||
return res; // failed
|
return res; // failed
|
||||||
};
|
};
|
||||||
|
|
||||||
let widget_node = widget_node_map.get(self.target_widget);
|
let widget_node = *widget_node_map.get(self.target_widget).unwrap();
|
||||||
let layout = tree.layout(widget_node).unwrap(); // should always succeed
|
let layout = tree.layout(widget_node).unwrap(); // should always succeed
|
||||||
|
|
||||||
let mut widget = widget.lock().unwrap();
|
let mut widget = widget.lock().unwrap();
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
use std::{
|
use std::sync::{Arc, Mutex};
|
||||||
collections::HashMap,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
animation::{self, Animations},
|
animation::{self, Animations},
|
||||||
@@ -12,25 +9,17 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use glam::{Vec2, vec2};
|
use glam::{Vec2, vec2};
|
||||||
use slotmap::HopSlotMap;
|
use slotmap::{HopSlotMap, SecondaryMap, new_key_type};
|
||||||
use taffy::{TaffyTree, TraversePartialTree};
|
use taffy::{TaffyTree, TraversePartialTree};
|
||||||
|
|
||||||
pub type WidgetID = slotmap::DefaultKey;
|
new_key_type! {
|
||||||
pub type BoxWidget = Arc<Mutex<WidgetState>>;
|
pub struct WidgetID;
|
||||||
pub type WidgetMap = HopSlotMap<slotmap::DefaultKey, BoxWidget>;
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct WidgetNodeMap(pub HashMap<WidgetID, taffy::NodeId>);
|
|
||||||
|
|
||||||
impl WidgetNodeMap {
|
|
||||||
pub fn get(&self, widget_id: WidgetID) -> taffy::NodeId {
|
|
||||||
let Some(node) = self.0.get(&widget_id).cloned() else {
|
|
||||||
// this shouldn't happen!
|
|
||||||
panic!("node_map is corrupted");
|
|
||||||
};
|
|
||||||
node
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type BoxWidget = Arc<Mutex<WidgetState>>;
|
||||||
|
pub type WidgetMap = HopSlotMap<WidgetID, BoxWidget>;
|
||||||
|
pub type WidgetNodeMap = SecondaryMap<WidgetID, taffy::NodeId>;
|
||||||
|
|
||||||
struct PushEventState<'a> {
|
struct PushEventState<'a> {
|
||||||
pub animations: &'a mut Vec<animation::Animation>,
|
pub animations: &'a mut Vec<animation::Animation>,
|
||||||
pub transform_stack: &'a mut TransformStack,
|
pub transform_stack: &'a mut TransformStack,
|
||||||
@@ -74,7 +63,7 @@ fn add_child_internal(
|
|||||||
tree.add_child(parent_node, child_node)?;
|
tree.add_child(parent_node, child_node)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
widget_node_map.0.insert(child_id, child_node);
|
widget_node_map.insert(child_id, child_node);
|
||||||
|
|
||||||
Ok((child_id, child_node))
|
Ok((child_id, child_node))
|
||||||
}
|
}
|
||||||
@@ -86,7 +75,7 @@ impl Layout {
|
|||||||
widget: WidgetState,
|
widget: WidgetState,
|
||||||
style: taffy::Style,
|
style: taffy::Style,
|
||||||
) -> anyhow::Result<(WidgetID, taffy::NodeId)> {
|
) -> anyhow::Result<(WidgetID, taffy::NodeId)> {
|
||||||
let parent_node = self.widget_node_map.get(parent_widget_id);
|
let parent_node = *self.widget_node_map.get(parent_widget_id).unwrap();
|
||||||
|
|
||||||
self.needs_redraw = true;
|
self.needs_redraw = true;
|
||||||
|
|
||||||
@@ -241,7 +230,7 @@ impl Layout {
|
|||||||
pub fn new(assets: Box<dyn AssetProvider>) -> anyhow::Result<Self> {
|
pub fn new(assets: Box<dyn AssetProvider>) -> anyhow::Result<Self> {
|
||||||
let mut tree = TaffyTree::new();
|
let mut tree = TaffyTree::new();
|
||||||
let mut widget_node_map = WidgetNodeMap::default();
|
let mut widget_node_map = WidgetNodeMap::default();
|
||||||
let mut widget_map = HopSlotMap::new();
|
let mut widget_map = HopSlotMap::with_key();
|
||||||
|
|
||||||
let (root_widget, root_node) = add_child_internal(
|
let (root_widget, root_node) = add_child_internal(
|
||||||
&mut tree,
|
&mut tree,
|
||||||
|
|||||||
Reference in New Issue
Block a user