Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Tweoss committed Feb 1, 2024
1 parent 18347d3 commit e593bbf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
25 changes: 15 additions & 10 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@ impl ContextImpl {

let shapes = viewport
.graphics
.drain(self.memory.areas().order(), self.memory.layer_transforms());
.drain(self.memory.areas().order(), &self.memory.layer_transforms);

if viewport.input.wants_repaint() {
self.request_repaint(ended_viewport_id);
Expand Down Expand Up @@ -2078,7 +2078,13 @@ impl Context {
///
/// Can be used to implement pan and zoom (see relevant demo).
pub fn set_transform_layer(&self, layer_id: LayerId, transform: TSTransform) {
self.memory_mut(|m| m.layer_transforms_mut().insert(layer_id, transform));
self.memory_mut(|m| {
if transform == TSTransform::IDENTITY {
m.layer_transforms.remove(&layer_id)
} else {
m.layer_transforms.insert(layer_id, transform)
}
});
}

/// Top-most layer at the given position.
Expand Down Expand Up @@ -2106,13 +2112,12 @@ impl Context {
///
/// See also [`Response::contains_pointer`].
pub fn rect_contains_pointer(&self, layer_id: LayerId, rect: Rect) -> bool {
let rect = if let Some(transform) =
self.memory(|m| m.layer_transforms().get(&layer_id).cloned())
{
transform * rect
} else {
rect
};
let rect =
if let Some(transform) = self.memory(|m| m.layer_transforms.get(&layer_id).cloned()) {
transform * rect
} else {
rect
};
if !rect.is_positive() {
return false;
}
Expand Down Expand Up @@ -2155,7 +2160,7 @@ impl Context {
self.write(|ctx| {
let transform = ctx
.memory
.layer_transforms()
.layer_transforms
.get(&layer_id)
.cloned()
.unwrap_or_default();
Expand Down
14 changes: 3 additions & 11 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ pub struct Memory {
#[cfg_attr(feature = "persistence", serde(skip))]
everything_is_visible: bool,

/// Transforms per layer
pub layer_transforms: HashMap<LayerId, TSTransform>,

// -------------------------------------------------
// Per-viewport:
areas: ViewportIdMap<Areas>,
layer_transforms: HashMap<LayerId, TSTransform>,

#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interactions: ViewportIdMap<Interaction>,
Expand Down Expand Up @@ -610,16 +612,6 @@ impl Memory {
self.areas.entry(self.viewport_id).or_default()
}

/// Access layer transformations.
pub fn layer_transforms(&self) -> &HashMap<LayerId, TSTransform> {
&self.layer_transforms
}

/// Access layer transformations.
pub fn layer_transforms_mut(&mut self) -> &mut HashMap<LayerId, TSTransform> {
&mut self.layer_transforms
}

/// Top-most layer at the given position.
pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option<LayerId> {
self.areas()
Expand Down
5 changes: 3 additions & 2 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,10 @@ impl Shape {
Self::Text(text_shape) => {
text_shape.pos = transform * text_shape.pos;
let mut galley = (*text_shape.galley).clone();
let scale_transform = TSTransform::from_scaling(transform.scaling);
for row in &mut galley.rows {
row.visuals.mesh.transform(scale_transform);
for v in &mut row.visuals.mesh.vertices {
v.pos = Pos2::new(transform.scaling * v.pos.x, transform.scaling * v.pos.y);
}
}

text_shape.galley = Arc::new(galley);
Expand Down

0 comments on commit e593bbf

Please sign in to comment.