|
| 1 | +use crate::consts::ROTATE_INCREMENT; |
1 | 2 | use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier; |
| 3 | +use crate::messages::portfolio::document::utility_types::transformation::Selected; |
2 | 4 | use crate::messages::prelude::*; |
3 | 5 | use crate::messages::tool::common_functionality::graph_modification_utils::get_text; |
| 6 | +use crate::messages::tool::common_functionality::transformation_cage::SelectedEdges; |
4 | 7 | use crate::messages::tool::tool_messages::path_tool::PathOverlayMode; |
5 | | -use glam::DVec2; |
| 8 | +use crate::messages::tool::utility_types::ToolType; |
| 9 | +use glam::{DAffine2, DVec2}; |
6 | 10 | use graphene_core::renderer::Quad; |
7 | 11 | use graphene_core::text::{FontCache, load_face}; |
8 | 12 | use graphene_std::vector::{ManipulatorPointId, PointId, SegmentId, VectorData}; |
9 | 13 |
|
| 14 | +use super::snapping::{SnapCandidatePoint, SnapData, SnapManager}; |
| 15 | +use super::transformation_cage::{BoundingBoxManager, SizeSnapData}; |
| 16 | + |
10 | 17 | /// Determines if a path should be extended. Goal in viewport space. Returns the path and if it is extending from the start, if applicable. |
11 | 18 | pub fn should_extend( |
12 | 19 | document: &DocumentMessageHandler, |
@@ -137,3 +144,219 @@ pub fn is_visible_point( |
137 | 144 | } |
138 | 145 | } |
139 | 146 | } |
| 147 | + |
| 148 | +pub fn resize_bounds( |
| 149 | + document: &DocumentMessageHandler, |
| 150 | + responses: &mut VecDeque<Message>, |
| 151 | + bounds: &mut BoundingBoxManager, |
| 152 | + dragging_layers: &mut Vec<LayerNodeIdentifier>, |
| 153 | + snap_manager: &mut SnapManager, |
| 154 | + snap_candidates: &mut Vec<SnapCandidatePoint>, |
| 155 | + input: &InputPreprocessorMessageHandler, |
| 156 | + center: bool, |
| 157 | + constrain: bool, |
| 158 | + tool: ToolType, |
| 159 | +) { |
| 160 | + if let Some(movement) = &mut bounds.selected_edges { |
| 161 | + let center = center.then_some(bounds.center_of_transformation); |
| 162 | + let snap = Some(SizeSnapData { |
| 163 | + manager: snap_manager, |
| 164 | + points: snap_candidates, |
| 165 | + snap_data: SnapData::ignore(document, input, &dragging_layers), |
| 166 | + }); |
| 167 | + let (position, size) = movement.new_size(input.mouse.position, bounds.original_bound_transform, center, constrain, snap); |
| 168 | + let (delta, mut pivot) = movement.bounds_to_scale_transform(position, size); |
| 169 | + |
| 170 | + let pivot_transform = DAffine2::from_translation(pivot); |
| 171 | + let transformation = pivot_transform * delta * pivot_transform.inverse(); |
| 172 | + |
| 173 | + dragging_layers.retain(|layer| { |
| 174 | + if *layer != LayerNodeIdentifier::ROOT_PARENT { |
| 175 | + document.network_interface.document_network().nodes.contains_key(&layer.to_node()) |
| 176 | + } else { |
| 177 | + log::error!("ROOT_PARENT should not be part of layers_dragging"); |
| 178 | + false |
| 179 | + } |
| 180 | + }); |
| 181 | + let selected = &dragging_layers; |
| 182 | + let mut selected = Selected::new(&mut bounds.original_transforms, &mut pivot, selected, responses, &document.network_interface, None, &tool, None); |
| 183 | + |
| 184 | + selected.apply_transformation(bounds.original_bound_transform * transformation * bounds.original_bound_transform.inverse(), None); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +pub fn rotate_bounds( |
| 189 | + document: &DocumentMessageHandler, |
| 190 | + responses: &mut VecDeque<Message>, |
| 191 | + bounds: &mut BoundingBoxManager, |
| 192 | + dragging_layers: &mut Vec<LayerNodeIdentifier>, |
| 193 | + drag_start: DVec2, |
| 194 | + mouse_position: DVec2, |
| 195 | + snap_angle: bool, |
| 196 | + tool: ToolType, |
| 197 | +) { |
| 198 | + let angle = { |
| 199 | + let start_offset = drag_start - bounds.center_of_transformation; |
| 200 | + let end_offset = mouse_position - bounds.center_of_transformation; |
| 201 | + start_offset.angle_to(end_offset) |
| 202 | + }; |
| 203 | + |
| 204 | + let snapped_angle = if snap_angle { |
| 205 | + let snap_resolution = ROTATE_INCREMENT.to_radians(); |
| 206 | + (angle / snap_resolution).round() * snap_resolution |
| 207 | + } else { |
| 208 | + angle |
| 209 | + }; |
| 210 | + |
| 211 | + let delta = DAffine2::from_angle(snapped_angle); |
| 212 | + |
| 213 | + dragging_layers.retain(|layer| { |
| 214 | + if *layer != LayerNodeIdentifier::ROOT_PARENT { |
| 215 | + document.network_interface.document_network().nodes.contains_key(&layer.to_node()) |
| 216 | + } else { |
| 217 | + log::error!("ROOT_PARENT should not be part of replacement_selected_layers"); |
| 218 | + false |
| 219 | + } |
| 220 | + }); |
| 221 | + let mut selected = Selected::new( |
| 222 | + &mut bounds.original_transforms, |
| 223 | + &mut bounds.center_of_transformation, |
| 224 | + &dragging_layers, |
| 225 | + responses, |
| 226 | + &document.network_interface, |
| 227 | + None, |
| 228 | + &tool, |
| 229 | + None, |
| 230 | + ); |
| 231 | + |
| 232 | + selected.update_transforms(delta, None, None); |
| 233 | +} |
| 234 | + |
| 235 | +pub fn skew_bounds( |
| 236 | + document: &DocumentMessageHandler, |
| 237 | + responses: &mut VecDeque<Message>, |
| 238 | + bounds: &mut BoundingBoxManager, |
| 239 | + free_movement: bool, |
| 240 | + layers: &mut Vec<LayerNodeIdentifier>, |
| 241 | + mouse_position: DVec2, |
| 242 | + tool: ToolType, |
| 243 | +) { |
| 244 | + if let Some(movement) = &mut bounds.selected_edges { |
| 245 | + let transformation = movement.skew_transform(mouse_position, bounds.original_bound_transform, free_movement); |
| 246 | + |
| 247 | + layers.retain(|layer| { |
| 248 | + if *layer != LayerNodeIdentifier::ROOT_PARENT { |
| 249 | + document.network_interface.document_network().nodes.contains_key(&layer.to_node()) |
| 250 | + } else { |
| 251 | + log::error!("ROOT_PARENT should not be part of layers_dragging"); |
| 252 | + false |
| 253 | + } |
| 254 | + }); |
| 255 | + let selected = &layers; |
| 256 | + let mut pivot = DVec2::ZERO; |
| 257 | + let mut selected = Selected::new(&mut bounds.original_transforms, &mut pivot, selected, responses, &document.network_interface, None, &tool, None); |
| 258 | + |
| 259 | + selected.apply_transformation(bounds.original_bound_transform * transformation * bounds.original_bound_transform.inverse(), None); |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +pub fn transforming_tranform_cage( |
| 264 | + document: &DocumentMessageHandler, |
| 265 | + mut bounding_box_manager: &mut Option<BoundingBoxManager>, |
| 266 | + input: &InputPreprocessorMessageHandler, |
| 267 | + responses: &mut VecDeque<Message>, |
| 268 | + layers_dragging: &mut Vec<LayerNodeIdentifier>, |
| 269 | +) -> (bool, bool, bool) { |
| 270 | + let dragging_bounds = bounding_box_manager.as_mut().and_then(|bounding_box| { |
| 271 | + let edges = bounding_box.check_selected_edges(input.mouse.position); |
| 272 | + |
| 273 | + bounding_box.selected_edges = edges.map(|(top, bottom, left, right)| { |
| 274 | + let selected_edges = SelectedEdges::new(top, bottom, left, right, bounding_box.bounds); |
| 275 | + bounding_box.opposite_pivot = selected_edges.calculate_pivot(); |
| 276 | + selected_edges |
| 277 | + }); |
| 278 | + |
| 279 | + edges |
| 280 | + }); |
| 281 | + |
| 282 | + let rotating_bounds = bounding_box_manager.as_ref().map(|bounding_box| bounding_box.check_rotate(input.mouse.position)).unwrap_or_default(); |
| 283 | + |
| 284 | + let selected: Vec<_> = document.network_interface.selected_nodes().selected_visible_and_unlocked_layers(&document.network_interface).collect(); |
| 285 | + |
| 286 | + let is_flat_layer = bounding_box_manager.as_ref().map(|bounding_box_manager| bounding_box_manager.transform_tampered).unwrap_or(true); |
| 287 | + |
| 288 | + if dragging_bounds.is_some() && !is_flat_layer { |
| 289 | + responses.add(DocumentMessage::StartTransaction); |
| 290 | + |
| 291 | + *layers_dragging = selected; |
| 292 | + |
| 293 | + if let Some(bounds) = &mut bounding_box_manager { |
| 294 | + bounds.original_bound_transform = bounds.transform; |
| 295 | + |
| 296 | + layers_dragging.retain(|layer| { |
| 297 | + if *layer != LayerNodeIdentifier::ROOT_PARENT { |
| 298 | + document.network_interface.document_network().nodes.contains_key(&layer.to_node()) |
| 299 | + } else { |
| 300 | + log::error!("ROOT_PARENT should not be part of layers_dragging"); |
| 301 | + false |
| 302 | + } |
| 303 | + }); |
| 304 | + |
| 305 | + let mut selected = Selected::new( |
| 306 | + &mut bounds.original_transforms, |
| 307 | + &mut bounds.center_of_transformation, |
| 308 | + &layers_dragging, |
| 309 | + responses, |
| 310 | + &document.network_interface, |
| 311 | + None, |
| 312 | + &ToolType::Select, |
| 313 | + None, |
| 314 | + ); |
| 315 | + bounds.center_of_transformation = selected.mean_average_of_pivots(); |
| 316 | + |
| 317 | + // Check if we're hovering over a skew triangle |
| 318 | + let edges = bounds.check_selected_edges(input.mouse.position); |
| 319 | + if let Some(edges) = edges { |
| 320 | + let closest_edge = bounds.get_closest_edge(edges, input.mouse.position); |
| 321 | + if bounds.check_skew_handle(input.mouse.position, closest_edge) { |
| 322 | + return (false, false, true); |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + return (true, false, false); |
| 327 | + } |
| 328 | + |
| 329 | + if rotating_bounds { |
| 330 | + responses.add(DocumentMessage::StartTransaction); |
| 331 | + |
| 332 | + if let Some(bounds) = &mut bounding_box_manager { |
| 333 | + layers_dragging.retain(|layer| { |
| 334 | + if *layer != LayerNodeIdentifier::ROOT_PARENT { |
| 335 | + document.network_interface.document_network().nodes.contains_key(&layer.to_node()) |
| 336 | + } else { |
| 337 | + log::error!("ROOT_PARENT should not be part of layers_dragging"); |
| 338 | + false |
| 339 | + } |
| 340 | + }); |
| 341 | + |
| 342 | + let mut selected = Selected::new( |
| 343 | + &mut bounds.original_transforms, |
| 344 | + &mut bounds.center_of_transformation, |
| 345 | + &selected, |
| 346 | + responses, |
| 347 | + &document.network_interface, |
| 348 | + None, |
| 349 | + &ToolType::Select, |
| 350 | + None, |
| 351 | + ); |
| 352 | + |
| 353 | + bounds.center_of_transformation = selected.mean_average_of_pivots(); |
| 354 | + } |
| 355 | + |
| 356 | + *layers_dragging = selected; |
| 357 | + |
| 358 | + return (false, true, false); |
| 359 | + } |
| 360 | + |
| 361 | + return (false, false, false); |
| 362 | +} |
0 commit comments