Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an editor preference for touched/enclosed/directional based selection #2156

Merged
merged 22 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions editor/src/messages/portfolio/document/document_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,33 @@ impl DocumentMessageHandler {
self.intersect_quad(viewport_quad, ipp).filter(|layer| !self.network_interface.is_artboard(&layer.to_node(), &[]))
}

pub fn is_layer_fully_inside(&self, layer: &LayerNodeIdentifier, quad: graphene_core::renderer::Quad) -> bool {
// Get the bounding box of the layer in document space
if let Some(bounding_box) = self.metadata().bounding_box_viewport(*layer) {
// Check if the bounding box is fully within the selection quad
let [top_left, bottom_right] = bounding_box;

let quad_bbox = quad.bounding_box();

let quad_left = quad_bbox[0].x;
let quad_right = quad_bbox[1].x;
let quad_top = quad_bbox[0].y.max(quad_bbox[1].y); // Correct top
let quad_bottom = quad_bbox[0].y.min(quad_bbox[1].y); // Correct bottom

// Extract layer's bounding box coordinates
let layer_left = top_left.x;
let layer_right = bottom_right.x;
let layer_top = bottom_right.y;
let layer_bottom = top_left.y;

let is_fully_contained = layer_left >= quad_left && layer_right <= quad_right && layer_top <= quad_top && layer_bottom >= quad_bottom;

is_fully_contained
} else {
false
}
}

/// Find all of the layers that were clicked on from a viewport space location
pub fn click_xray(&self, ipp: &InputPreprocessorMessageHandler) -> impl Iterator<Item = LayerNodeIdentifier> + '_ {
let document_to_viewport = self.navigation_handler.calculate_offset_transform(ipp.viewport_bounds.center(), &self.document_ptz);
Expand Down
30 changes: 28 additions & 2 deletions editor/src/messages/tool/tool_messages/select_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use super::tool_prelude::*;
use crate::consts::{ROTATE_SNAP_ANGLE, SELECTION_TOLERANCE};
use crate::messages::input_mapper::utility_types::input_mouse::ViewportPosition;
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
use crate::messages::portfolio::document::node_graph::utility_types::Direction;
use crate::messages::portfolio::document::overlays::utility_types::OverlayContext;
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
use crate::messages::portfolio::document::utility_types::document_metadata::{self, LayerNodeIdentifier};
use crate::messages::portfolio::document::utility_types::misc::{AlignAggregate, AlignAxis, FlipAxis};
use crate::messages::portfolio::document::utility_types::network_interface::{FlowType, NodeNetworkInterface, NodeTemplate};
use crate::messages::portfolio::document::utility_types::transformation::Selected;
Expand All @@ -19,6 +20,7 @@ use graph_craft::document::NodeId;
use graphene_core::renderer::Quad;
use graphene_std::renderer::Rect;
use graphene_std::vector::misc::BooleanOperation;
// use web_sys::console;

use std::fmt;

Expand All @@ -39,6 +41,12 @@ pub enum SelectOptionsUpdate {
NestedSelectionBehavior(NestedSelectionBehavior),
}

enum SelectionDirection {
LeftWards,
Keavon marked this conversation as resolved.
Show resolved Hide resolved
Rightwards,
None,
}

#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, Hash, serde::Serialize, serde::Deserialize, specta::Type)]
pub enum NestedSelectionBehavior {
#[default]
Expand Down Expand Up @@ -301,6 +309,17 @@ impl SelectToolData {
Quad::from_box(bbox)
}

fn calculate_direction(&self) -> SelectionDirection {
let bbox: [DVec2; 2] = self.selection_box();
if bbox[1].x > bbox[0].x {
SelectionDirection::Rightwards
} else if bbox[1].x < bbox[0].x {
SelectionDirection::LeftWards
} else {
SelectionDirection::None
}
}

fn selection_box(&self) -> [DVec2; 2] {
if self.drag_current == self.drag_start {
let tolerance = DVec2::splat(SELECTION_TOLERANCE);
Expand Down Expand Up @@ -913,6 +932,7 @@ impl Fsm for SelectToolFsmState {

if !tool_data.has_dragged && input.keyboard.key(remove_from_selection) && tool_data.layer_selected_on_start.is_none() {
let quad = tool_data.selection_quad();

let intersection = document.intersect_quad_no_artboards(quad, input);

if let Some(path) = intersection.last() {
Expand Down Expand Up @@ -1006,7 +1026,13 @@ impl Fsm for SelectToolFsmState {
}
(SelectToolFsmState::DrawingBox { .. }, SelectToolMessage::DragStop { .. } | SelectToolMessage::Enter) => {
let quad = tool_data.selection_quad();
let new_selected: HashSet<_> = document.intersect_quad_no_artboards(quad, input).collect();
let direction = tool_data.calculate_direction();
// let new_selected: HashSet<_> = document.intersect_quad_no_artboards(quad, input).collect();
let new_selected: HashSet<_> = match direction {
SelectionDirection::Rightwards => document.intersect_quad_no_artboards(quad, input).filter(|layer| document.is_layer_fully_inside(layer, quad)).collect(),
SelectionDirection::LeftWards => document.intersect_quad_no_artboards(quad, input).collect(),
SelectionDirection::None => HashSet::new(),
};
let current_selected: HashSet<_> = document.network_interface.selected_nodes(&[]).unwrap().selected_layers(document.metadata()).collect();
if new_selected != current_selected {
tool_data.layers_dragging = new_selected.into_iter().collect();
Expand Down
Loading