Skip to content

Commit

Permalink
Get bounding box working in place without action keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitish-bot committed Jan 24, 2025
1 parent a75f2b9 commit a137b9c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
10 changes: 10 additions & 0 deletions editor/src/messages/tool/common_functionality/pivot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ impl Pivot {
}
}

pub fn update_box_pivot(&mut self, normalized_pivot: DVec2, layer_transform: DAffine2, bounds_transform: DAffine2, overlay_context: &mut OverlayContext) {
self.normalized_pivot = normalized_pivot;
self.transform_from_normalized = layer_transform * bounds_transform;

self.pivot = Some(self.transform_from_normalized.transform_point2(normalized_pivot));
if let Some(pivot) = self.pivot {
overlay_context.pivot(pivot);
}
}

pub fn update_pivot(&mut self, document: &DocumentMessageHandler, overlay_context: &mut OverlayContext) {
self.recalculate_pivot(document);
if let Some(pivot) = self.pivot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub fn text_bounding_box(layer: LayerNodeIdentifier, document: &DocumentMessageH
let buzz_face = font_cache.get(font).map(|data| load_face(data));
let far = graphene_core::text::bounding_box(text, buzz_face, typesetting);
let quad = Quad::from_box([DVec2::ZERO, far]);
let transformed_quad = document.metadata().transform_to_viewport(layer) * quad;

transformed_quad
quad
}
4 changes: 2 additions & 2 deletions editor/src/messages/tool/tool_messages/select_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ impl Fsm for SelectToolFsmState {
overlay_context.outline(document.metadata().layer_outline(layer), document.metadata().transform_to_viewport(layer));

if is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text") {
let quad = text_bounding_box(layer, document, font_cache);
overlay_context.dashed_quad(quad, None, Some(7.), Some(5.), None);
let transformed_quad = document.metadata().transform_to_viewport(layer) * text_bounding_box(layer, document, font_cache);
overlay_context.dashed_quad(transformed_quad, None, Some(7.), Some(5.), None);
}
}

Expand Down
21 changes: 14 additions & 7 deletions editor/src/messages/tool/tool_messages/text_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,10 @@ impl TextToolData {
.all_layers()
.filter(|&layer| is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text"))
.find(|&layer| {
let quad = text_bounding_box(layer, document, font_cache);
let transformed_quad = document.metadata().transform_to_viewport(layer) * text_bounding_box(layer, document, font_cache);
let mouse = DVec2::new(input.mouse.position.x, input.mouse.position.y);

quad.contains(mouse)
transformed_quad.contains(mouse)
})
}

Expand Down Expand Up @@ -495,8 +495,8 @@ impl Fsm for TextToolFsmState {
.selected_visible_and_unlocked_layers(&document.network_interface)
{
if is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text") {
let quad = text_bounding_box(layer, document, font_cache);
overlay_context.quad(quad, None);
let transformed_quad = document.metadata().transform_to_viewport(layer) * text_bounding_box(layer, document, font_cache);
overlay_context.quad(transformed_quad, None);
}
}
}
Expand All @@ -519,14 +519,17 @@ impl Fsm for TextToolFsmState {
if let Some(bounds) = bounds {
let bounding_box_manager = tool_data.bounding_box_manager.get_or_insert(BoundingBoxManager::default());
bounding_box_manager.bounds = [bounds.0[0], bounds.0[2]];
bounding_box_manager.transform = transform * transform.inverse();
bounding_box_manager.transform = transform;
bounding_box_manager.render_overlays(&mut overlay_context);
} else {
tool_data.bounding_box_manager.take();
}

// Update pivot
tool_data.pivot.update_pivot(document, &mut overlay_context);
let (min, max) = bounds.map(|quad| (quad.0[0], quad.0[2])).unwrap();
let bounds_transform = DAffine2::from_translation(min) * DAffine2::from_scale(max - min);
let layer_transform = document.metadata().transform_to_viewport(layer.unwrap());
tool_data.pivot.update_box_pivot(DVec2::new(0.5, 0.5), layer_transform, bounds_transform, &mut overlay_context);

tool_data.resize.snap_manager.draw_overlays(SnapData::new(document, input), &mut overlay_context);

Expand Down Expand Up @@ -615,7 +618,10 @@ impl Fsm for TextToolFsmState {
return state;
}
(TextToolFsmState::Ready, TextToolMessage::PointerMove { .. }) => {
let mut cursor = tool_data.bounding_box_manager.as_ref().map_or(MouseCursorIcon::Default, |bounds| bounds.get_cursor(input, false));
let mut cursor = tool_data.bounding_box_manager.as_ref().map_or(MouseCursorIcon::Text, |bounds| bounds.get_cursor(input, false));
if cursor == MouseCursorIcon::Default {
cursor = MouseCursorIcon::Text;
}

// Dragging the pivot overrules the other operations
if tool_data.pivot.is_over(input.mouse.position) {
Expand Down Expand Up @@ -843,6 +849,7 @@ impl Fsm for TextToolFsmState {
self
}
(TextToolFsmState::Editing, TextToolMessage::Abort) => {
tool_data.bounding_box_manager.take();
if tool_data.new_text.is_empty() {
return tool_data.delete_empty_layer(font_cache, responses);
}
Expand Down

0 comments on commit a137b9c

Please sign in to comment.