Skip to content

Commit

Permalink
Fix drag-and-drop preview following emilk#3906
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored and hacknus committed Oct 30, 2024
1 parent 5e8ee4b commit e627e4c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
32 changes: 32 additions & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2278,9 +2278,13 @@ impl Context {
impl Context {
/// Transform the graphics of the given layer.
///
/// This will also affect input.
///
/// This is a sticky setting, remembered from one frame to the next.
///
/// Can be used to implement pan and zoom (see relevant demo).
///
/// For a temporary transform, use [`Self::transform_layer_shapes`] instead.
pub fn set_transform_layer(&self, layer_id: LayerId, transform: TSTransform) {
self.memory_mut(|m| {
if transform == TSTransform::IDENTITY {
Expand All @@ -2291,6 +2295,34 @@ impl Context {
});
}

/// Move all the graphics at the given layer.
///
/// Is used to implement drag-and-drop preview.
///
/// This only applied to the existing graphics at the layer, not to new graphics added later.
///
/// For a persistent transform, use [`Self::set_transform_layer`] instead.
#[deprecated = "Use `transform_layer_shapes` instead"]
pub fn translate_layer(&self, layer_id: LayerId, delta: Vec2) {
if delta != Vec2::ZERO {
let transform = emath::TSTransform::from_translation(delta);
self.transform_layer_shapes(layer_id, transform);
}
}

/// Transform all the graphics at the given layer.
///
/// Is used to implement drag-and-drop preview.
///
/// This only applied to the existing graphics at the layer, not to new graphics added later.
///
/// For a persistent transform, use [`Self::set_transform_layer`] instead.
pub fn transform_layer_shapes(&self, layer_id: LayerId, transform: TSTransform) {
if transform != TSTransform::IDENTITY {
self.graphics_mut(|g| g.entry(layer_id).transform(transform));
}
}

/// Top-most layer at the given position.
pub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId> {
self.memory(|mem| {
Expand Down
13 changes: 11 additions & 2 deletions crates/egui/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,17 @@ impl GraphicLayers {
}

// Also draw areas that are missing in `area_order`:
for shapes in order_map.values_mut() {
all_shapes.append(&mut shapes.0);
for (id, list) in order_map {
let layer_id = LayerId::new(order, *id);

if let Some(transform) = transforms.get(&layer_id) {
for clipped_shape in &mut list.0 {
clipped_shape.clip_rect = *transform * clipped_shape.clip_rect;
clipped_shape.shape.transform(*transform);
}
}

all_shapes.append(&mut list.0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,7 @@ impl Ui {
if let Some(pointer_pos) = self.ctx().pointer_interact_pos() {
let delta = pointer_pos - response.rect.center();
self.ctx()
.set_transform_layer(layer_id, emath::TSTransform::from_translation(delta));
.transform_layer_shapes(layer_id, emath::TSTransform::from_translation(delta));
}

InnerResponse::new(inner, response)
Expand Down

0 comments on commit e627e4c

Please sign in to comment.