-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Guide-1 #3825
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
Open
jsjgdh
wants to merge
7
commits into
GraphiteEditor:master
Choose a base branch
from
jsjgdh:Guide-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Guide-1 #3825
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c12a495
Guide-1: Core guide infrastructure + overlays + frontend ruler drag
jsjgdh e5a8e7c
Refactor: extract duplicate viewport-to-document conversion, simplify…
jsjgdh 117c69d
Rename
jsjgdh cdbf4df
Conflict Fix
jsjgdh 6b37426
Merge branch 'master' into Guide-1
jsjgdh 2150da7
Merge branch 'master' into Guide-1
jsjgdh 31cfc2d
Eslint
jsjgdh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| use crate::messages::portfolio::document::overlays::utility_types::OverlayContext; | ||
| use crate::messages::portfolio::document::utility_types::guide::{GuideDirection, GuideId}; | ||
| use crate::messages::prelude::*; | ||
|
|
||
| #[impl_message(Message, DocumentMessage, Guide)] | ||
| #[derive(PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)] | ||
| pub enum GuideMessage { | ||
| CreateGuide { id: GuideId, direction: GuideDirection, mouse_x: f64, mouse_y: f64 }, | ||
| MoveGuide { id: GuideId, mouse_x: f64, mouse_y: f64 }, | ||
| DeleteGuide { id: GuideId }, | ||
| GuideOverlays { context: OverlayContext }, | ||
| ToggleGuidesVisibility, | ||
| SetHoveredGuide { id: Option<GuideId> }, | ||
| } |
108 changes: 108 additions & 0 deletions
108
editor/src/messages/portfolio/document/guide_message_handler.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| use super::utility_types::guide::{Guide, GuideDirection, GuideId}; | ||
| use crate::messages::portfolio::document::guide_message::{GuideMessage, GuideMessageDiscriminant}; | ||
| use crate::messages::portfolio::document::overlays::guide_overlays::guide_overlay; | ||
| use crate::messages::portfolio::document::utility_types::misc::PTZ; | ||
| use crate::messages::prelude::*; | ||
| use glam::DVec2; | ||
|
|
||
| #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, ExtractField)] | ||
| #[serde(default)] | ||
| pub struct GuideMessageHandler { | ||
| #[serde(default)] | ||
| pub guides: Vec<Guide>, | ||
| #[serde(default = "default_guides_visible")] | ||
| pub guides_visible: bool, | ||
| #[serde(skip)] | ||
| pub hovered_guide_id: Option<GuideId>, | ||
| } | ||
|
|
||
| fn default_guides_visible() -> bool { | ||
| true | ||
|
jsjgdh marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| impl Default for GuideMessageHandler { | ||
| fn default() -> Self { | ||
| Self { | ||
| guides: Vec::new(), | ||
| guides_visible: true, | ||
| hovered_guide_id: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(ExtractField)] | ||
| pub struct GuideMessageContext<'a> { | ||
| pub navigation_handler: &'a NavigationMessageHandler, | ||
| pub document_ptz: &'a PTZ, | ||
| pub viewport: &'a ViewportMessageHandler, | ||
| } | ||
|
|
||
| #[message_handler_data] | ||
| impl MessageHandler<GuideMessage, GuideMessageContext<'_>> for GuideMessageHandler { | ||
| fn actions(&self) -> ActionList { | ||
| actions!(GuideMessageDiscriminant; ToggleGuidesVisibility) | ||
| } | ||
|
|
||
| fn process_message(&mut self, message: GuideMessage, responses: &mut VecDeque<Message>, context: GuideMessageContext) { | ||
| let GuideMessageContext { | ||
| navigation_handler, | ||
| document_ptz, | ||
| viewport, | ||
| } = context; | ||
|
|
||
| let viewport_to_document_point = |mouse_x: f64, mouse_y: f64| -> DVec2 { | ||
| let document_to_viewport = navigation_handler.calculate_offset_transform(viewport.center_in_viewport_space().into(), document_ptz); | ||
| document_to_viewport.inverse().transform_point2(DVec2::new(mouse_x, mouse_y)) | ||
| }; | ||
|
|
||
| match message { | ||
| GuideMessage::CreateGuide { id, direction, mouse_x, mouse_y } => { | ||
|
jsjgdh marked this conversation as resolved.
Outdated
|
||
| let document_point = viewport_to_document_point(mouse_x, mouse_y); | ||
|
|
||
| let document_position = match direction { | ||
| GuideDirection::Horizontal => document_point.y, | ||
| GuideDirection::Vertical => document_point.x, | ||
| }; | ||
|
|
||
| let guide = Guide::with_id(id, direction, document_position); | ||
| self.guides.push(guide); | ||
| responses.add(OverlaysMessage::Draw); | ||
| responses.add(PortfolioMessage::UpdateDocumentWidgets); | ||
| } | ||
| GuideMessage::MoveGuide { id, mouse_x, mouse_y } => { | ||
| let document_point = viewport_to_document_point(mouse_x, mouse_y); | ||
|
|
||
| if let Some(guide) = self.guides.iter_mut().find(|guide| guide.id == id) { | ||
| guide.position = match guide.direction { | ||
| GuideDirection::Horizontal => document_point.y, | ||
| GuideDirection::Vertical => document_point.x, | ||
| }; | ||
| } | ||
| responses.add(OverlaysMessage::Draw); | ||
| } | ||
| GuideMessage::DeleteGuide { id } => { | ||
| self.guides.retain(|g| g.id != id); | ||
| responses.add(OverlaysMessage::Draw); | ||
| responses.add(PortfolioMessage::UpdateDocumentWidgets); | ||
| } | ||
| GuideMessage::GuideOverlays { context: mut overlay_context } => { | ||
| if self.guides_visible { | ||
| let document_to_viewport = navigation_handler.calculate_offset_transform(overlay_context.viewport.center_in_viewport_space().into(), document_ptz); | ||
| guide_overlay(self, &mut overlay_context, document_to_viewport); | ||
| } | ||
| } | ||
| GuideMessage::ToggleGuidesVisibility => { | ||
| self.guides_visible = !self.guides_visible; | ||
| responses.add(OverlaysMessage::Draw); | ||
| responses.add(PortfolioMessage::UpdateDocumentWidgets); | ||
| responses.add(MenuBarMessage::SendLayout); | ||
| } | ||
| GuideMessage::SetHoveredGuide { id } => { | ||
| if self.hovered_guide_id != id { | ||
| self.hovered_guide_id = id; | ||
| responses.add(OverlaysMessage::Draw); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
editor/src/messages/portfolio/document/overlays/guide_overlays.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| use crate::consts::{COLOR_OVERLAY_BLUE, COLOR_OVERLAY_BLUE_50}; | ||
| use crate::messages::portfolio::document::guide_message_handler::GuideMessageHandler; | ||
| use crate::messages::portfolio::document::overlays::utility_types::OverlayContext; | ||
| use crate::messages::portfolio::document::utility_types::guide::GuideDirection; | ||
| use glam::{DAffine2, DVec2}; | ||
|
|
||
| fn extend_line_to_viewport(point: DVec2, direction: DVec2, viewport_size: DVec2) -> Option<(DVec2, DVec2)> { | ||
| let dir = direction.try_normalize()?; | ||
|
|
||
| // Calculates t values for intersections with viewport edges | ||
| let mut t_values = Vec::new(); | ||
|
|
||
| let edges = graphene_std::renderer::Quad::from_box([DVec2::ZERO, viewport_size]).all_edges(); | ||
| for [start, end] in edges { | ||
| let t_along_viewport = (point - start).perp_dot(dir) / (end - start).perp_dot(dir); | ||
| let t_along_direction = (point - start).perp_dot(end - start) / (end - start).perp_dot(dir); | ||
| if 0. <= t_along_viewport && t_along_viewport <= 1. && t_along_direction.is_finite() { | ||
| t_values.push(t_along_direction); | ||
| } | ||
| } | ||
|
|
||
| if t_values.len() < 2 { | ||
| return None; | ||
| } | ||
|
|
||
| let t_min = t_values.iter().cloned().fold(f64::INFINITY, f64::min); | ||
| let t_max = t_values.iter().cloned().fold(f64::NEG_INFINITY, f64::max); | ||
|
|
||
| let start = point + dir * t_min; | ||
| let end = point + dir * t_max; | ||
|
|
||
| Some((start, end)) | ||
| } | ||
|
|
||
| pub fn guide_overlay(guide_handler: &GuideMessageHandler, overlay_context: &mut OverlayContext, document_to_viewport: DAffine2) { | ||
| let viewport_size: DVec2 = overlay_context.viewport.size().into(); | ||
|
|
||
| for guide in &guide_handler.guides { | ||
| let (doc_point, doc_direction) = match guide.direction { | ||
| GuideDirection::Horizontal => (DVec2::new(0.0, guide.position), DVec2::X), | ||
| GuideDirection::Vertical => (DVec2::new(guide.position, 0.0), DVec2::Y), | ||
| }; | ||
|
|
||
| let viewport_point = document_to_viewport.transform_point2(doc_point); | ||
| let viewport_direction = document_to_viewport.transform_vector2(doc_direction); | ||
|
|
||
| let color = if guide_handler.hovered_guide_id == Some(guide.id) { | ||
| COLOR_OVERLAY_BLUE_50 | ||
| } else { | ||
| COLOR_OVERLAY_BLUE | ||
| }; | ||
|
|
||
| if let Some((start, end)) = extend_line_to_viewport(viewport_point, viewport_direction, viewport_size) { | ||
| overlay_context.line(start, end, Some(color), None); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.