-
-
Notifications
You must be signed in to change notification settings - Fork 577
Allow the Fill tool to flood fill raster images #2519
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
Draft
EllenGYY
wants to merge
12
commits into
GraphiteEditor:master
Choose a base branch
from
EllenGYY:raster-fill-tool
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.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc297a5
Add raster fill node to graph
EllenGYY 1d8ce81
it works v1.0
EllenGYY 1ca145d
fix color bug + add threshold adjustment based on LAB
EllenGYY cffbb9c
Merge branch 'GraphiteEditor:master' into raster-fill-tool
EllenGYY a909015
Merge branch 'master' into raster-fill-tool
Keavon 59dacf2
Edit test cases for new raster fill
EllenGYY 3a69b79
Delete ignore raster test case
EllenGYY a1d1e51
Update f32 assert to avoid ==
EllenGYY 614d83b
reverse unwanted changes
EllenGYY bb629fd
Merge branch 'GraphiteEditor:master' into raster-fill-tool
EllenGYY b93e19e
Merge branch 'master' into raster-fill-tool
Keavon 7915469
Code review
Keavon 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,10 @@ | ||||||
use super::tool_prelude::*; | ||||||
use crate::messages::portfolio::document::graph_operation::transform_utils::get_current_transform; | ||||||
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier; | ||||||
use crate::messages::tool::common_functionality::graph_modification_utils::NodeGraphLayer; | ||||||
use graphene_core::vector::style::Fill; | ||||||
use graph_craft::document::value::TaggedValue; | ||||||
use graphene_std::vector::style::Fill; | ||||||
|
||||||
#[derive(Default)] | ||||||
pub struct FillTool { | ||||||
fsm_state: FillToolFsmState, | ||||||
|
@@ -38,7 +42,8 @@ impl LayoutHolder for FillTool { | |||||
|
||||||
impl<'a> MessageHandler<ToolMessage, &mut ToolActionHandlerData<'a>> for FillTool { | ||||||
fn process_message(&mut self, message: ToolMessage, responses: &mut VecDeque<Message>, tool_data: &mut ToolActionHandlerData<'a>) { | ||||||
self.fsm_state.process_event(message, &mut (), tool_data, &(), responses, true); | ||||||
let raster_fill_tool_data = &mut FillToolData::default(); | ||||||
self.fsm_state.process_event(message, raster_fill_tool_data, tool_data, &(), responses, true); | ||||||
} | ||||||
fn actions(&self) -> ActionList { | ||||||
match self.fsm_state { | ||||||
|
@@ -71,37 +76,111 @@ enum FillToolFsmState { | |||||
Filling, | ||||||
} | ||||||
|
||||||
#[derive(Clone, Debug, Default)] | ||||||
struct FillToolData { | ||||||
fills: Vec<Fill>, | ||||||
start_pos: Vec<DVec2>, | ||||||
tolerance: f64, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
impl FillToolData { | ||||||
fn load_existing_fills(&mut self, document: &mut DocumentMessageHandler, layer_identifier: LayerNodeIdentifier) -> Option<LayerNodeIdentifier> { | ||||||
let node_graph_layer = NodeGraphLayer::new(layer_identifier, &document.network_interface); | ||||||
let existing_fills = node_graph_layer.find_node_inputs("Flood Fill"); | ||||||
|
||||||
if let Some(existing_fills) = existing_fills { | ||||||
let fills = if let Some(TaggedValue::VecFill(fills)) = existing_fills[1].as_value().cloned() { | ||||||
fills | ||||||
} else { | ||||||
Vec::new() | ||||||
}; | ||||||
let start_pos = if let Some(TaggedValue::VecDVec2(start_pos)) = existing_fills[2].as_value().cloned() { | ||||||
start_pos | ||||||
} else { | ||||||
Vec::new() | ||||||
}; | ||||||
let tolerance = if let Some(TaggedValue::F64(tolerance)) = existing_fills[3].as_value().cloned() { | ||||||
tolerance | ||||||
} else { | ||||||
1. | ||||||
}; | ||||||
|
||||||
*self = Self { fills, start_pos, tolerance }; | ||||||
} | ||||||
|
||||||
// TODO: Why do we overwrite the tolerance that we just set a couple lines above? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
self.tolerance = 1.; | ||||||
|
||||||
None | ||||||
} | ||||||
} | ||||||
|
||||||
impl Fsm for FillToolFsmState { | ||||||
type ToolData = (); | ||||||
type ToolData = FillToolData; | ||||||
type ToolOptions = (); | ||||||
|
||||||
fn transition(self, event: ToolMessage, _tool_data: &mut Self::ToolData, handler_data: &mut ToolActionHandlerData, _tool_options: &Self::ToolOptions, responses: &mut VecDeque<Message>) -> Self { | ||||||
fn transition(self, event: ToolMessage, tool_data: &mut Self::ToolData, handler_data: &mut ToolActionHandlerData, _tool_options: &Self::ToolOptions, responses: &mut VecDeque<Message>) -> Self { | ||||||
let ToolActionHandlerData { | ||||||
document, global_tool_data, input, .. | ||||||
} = handler_data; | ||||||
|
||||||
let ToolMessage::Fill(event) = event else { return self }; | ||||||
match (self, event) { | ||||||
(FillToolFsmState::Ready, color_event) => { | ||||||
let Some(layer_identifier) = document.click(input) else { | ||||||
return self; | ||||||
}; | ||||||
// If the layer is a raster layer, don't fill it, wait till the flood fill tool is implemented | ||||||
if NodeGraphLayer::is_raster_layer(layer_identifier, &mut document.network_interface) { | ||||||
return self; | ||||||
} | ||||||
let Some(layer_identifier) = document.click(input) else { return self }; | ||||||
let fill = match color_event { | ||||||
FillToolMessage::FillPrimaryColor => Fill::Solid(global_tool_data.primary_color.to_gamma_srgb()), | ||||||
FillToolMessage::FillSecondaryColor => Fill::Solid(global_tool_data.secondary_color.to_gamma_srgb()), | ||||||
_ => return self, | ||||||
}; | ||||||
|
||||||
responses.add(DocumentMessage::AddTransaction); | ||||||
responses.add(GraphOperationMessage::FillSet { layer: layer_identifier, fill }); | ||||||
|
||||||
// If the layer is a raster layer, we perform a flood fill | ||||||
if NodeGraphLayer::is_raster_layer(layer_identifier, &mut document.network_interface) { | ||||||
// Try to load existing fills for this layer | ||||||
tool_data.load_existing_fills(document, layer_identifier); | ||||||
|
||||||
// Get position in layer space | ||||||
let layer_pos = document | ||||||
.network_interface | ||||||
.document_metadata() | ||||||
.downstream_transform_to_viewport(layer_identifier) | ||||||
.inverse() | ||||||
.transform_point2(input.mouse.position); | ||||||
|
||||||
let node_graph_layer = NodeGraphLayer::new(layer_identifier, &document.network_interface); | ||||||
if let Some(transform_inputs) = node_graph_layer.find_node_inputs("Transform") { | ||||||
let image_transform = get_current_transform(transform_inputs); | ||||||
let image_local_pos = image_transform.inverse().transform_point2(layer_pos); | ||||||
|
||||||
// Store the fill in our tool data with its position | ||||||
tool_data.fills.push(fill.clone()); | ||||||
tool_data.start_pos.push(image_local_pos); | ||||||
} | ||||||
|
||||||
// Send the fill operation message | ||||||
responses.add(GraphOperationMessage::FillRaster { | ||||||
layer: layer_identifier, | ||||||
fills: tool_data.fills.clone(), | ||||||
start_pos: tool_data.start_pos.clone(), | ||||||
tolerance: tool_data.tolerance, | ||||||
}); | ||||||
} | ||||||
// Otherwise the layer is assumed to be a vector layer, so we apply a vector fill | ||||||
else { | ||||||
responses.add(GraphOperationMessage::FillSet { layer: layer_identifier, fill }); | ||||||
} | ||||||
|
||||||
FillToolFsmState::Filling | ||||||
} | ||||||
(FillToolFsmState::Filling, FillToolMessage::PointerUp) => FillToolFsmState::Ready, | ||||||
(FillToolFsmState::Filling, FillToolMessage::PointerUp) => { | ||||||
// Clear the `fills` and `start_pos` data when we're done | ||||||
tool_data.fills.clear(); | ||||||
tool_data.start_pos.clear(); | ||||||
|
||||||
FillToolFsmState::Ready | ||||||
} | ||||||
(FillToolFsmState::Filling, FillToolMessage::Abort) => { | ||||||
responses.add(DocumentMessage::AbortTransaction); | ||||||
|
||||||
|
@@ -136,7 +215,6 @@ mod test_fill { | |||||
|
||||||
async fn get_fills(editor: &mut EditorTestUtils) -> Vec<Fill> { | ||||||
let instrumented = editor.eval_graph().await; | ||||||
|
||||||
instrumented.grab_all_input::<fill::FillInput<Fill>>(&editor.runtime).collect() | ||||||
} | ||||||
|
||||||
|
@@ -149,15 +227,6 @@ mod test_fill { | |||||
assert!(get_fills(&mut editor,).await.is_empty()); | ||||||
} | ||||||
|
||||||
#[tokio::test] | ||||||
async fn ignore_raster() { | ||||||
let mut editor = EditorTestUtils::create(); | ||||||
editor.new_document().await; | ||||||
editor.create_raster_image(Image::new(100, 100, Color::WHITE), Some((0., 0.))).await; | ||||||
editor.click_tool(ToolType::Fill, MouseKeys::LEFT, DVec2::new(2., 2.), ModifierKeys::empty()).await; | ||||||
assert!(get_fills(&mut editor,).await.is_empty()); | ||||||
} | ||||||
|
||||||
#[tokio::test] | ||||||
async fn primary() { | ||||||
let mut editor = EditorTestUtils::create(); | ||||||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to set the tolerance in the tool's control bar. It should range from 0 (only the identically colored pixels) to 1 (all pixels regardless of color). We should also have a checkbox for contiguous.