Skip to content

Fix hardcoded input indices#3829

Open
Tyooughtul wants to merge 2 commits intoGraphiteEditor:masterfrom
Tyooughtul:fix_hardcoded_input_indices
Open

Fix hardcoded input indices#3829
Tyooughtul wants to merge 2 commits intoGraphiteEditor:masterfrom
Tyooughtul:fix_hardcoded_input_indices

Conversation

@Tyooughtul
Copy link

fix: replace hardcoded input indices with ::INDEX constants
replaces hardcoded numeric indices with predefined ::INDEX constants across various node input operations, improving code maintainability and reducing the risk of index mismatches.

changes include:
for native nodes, replaced hardcoded indices like 1, 2, 3 with module-level constants such as TranslationInput::INDEX, BlendModeInput::INDEX, etc.
in contexts like array access within functions, introduced local const declarations.

during testing, some failing cases were debugged with comments, but these were reverted to keep the code clean in this pr.

Fixes #1490

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Tyooughtul, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors how node input indices are handled across the codebase. By replacing magic numbers with named constants, the changes significantly improve the clarity and robustness of the graph operations. This ensures that future modifications to node structures are less prone to errors caused by incorrect input indexing, making the system more resilient and easier to debug.

Highlights

  • Replaced Hardcoded Indices with Constants: Updated various node input operations to use descriptive ::INDEX constants (e.g., TranslationInput::INDEX, BlendModeInput::INDEX) instead of hardcoded numeric indices, enhancing code readability and maintainability.
  • Introduced Local Constants for Array Access: Defined local const declarations (e.g., TRANSLATION_INDEX, ROTATION_INDEX) for array access within functions like get_current_transform and get_current_normalized_pivot, further improving clarity.
  • Improved Code Maintainability: The changes reduce the risk of index mismatches and make the code easier to understand and modify by providing meaningful names for node inputs.
Changelog
  • editor/src/messages/portfolio/document/graph_operation/transform_utils.rs
    • Imported NodeInputDecleration and transform node types.
    • Replaced hardcoded numeric indices with TranslationInput::INDEX, RotationInput::INDEX, ScaleInput::INDEX, and SkewInput::INDEX when setting transform node inputs.
    • Introduced local const declarations for input indices within get_current_transform and get_current_normalized_pivot functions.
  • editor/src/messages/portfolio/document/graph_operation/utility_types.rs
    • Replaced hardcoded numeric indices with BlendModeInput::INDEX, OpacityInput::INDEX, FillInput::INDEX, and ClipInput::INDEX for blending node inputs.
  • editor/src/messages/tool/common_functionality/gizmos/shape_gizmos/circle_arc_radius_handle.rs
    • Imported NodeInputDecleration.
    • Replaced hardcoded numeric index with RadiusInput::INDEX for circle radius input.
  • editor/src/messages/tool/common_functionality/gizmos/shape_gizmos/number_of_points_dial.rs
    • Imported NodeInputDecleration.
    • Replaced hardcoded numeric index with SidesInput::<u32>::INDEX for star shape sides input.
  • editor/src/messages/tool/common_functionality/gizmos/shape_gizmos/sweep_angle_gizmo.rs
    • Imported NodeInputDecleration and arc generator nodes.
    • Replaced hardcoded numeric indices with StartAngleInput::INDEX and SweepAngleInput::INDEX for arc angle inputs.
  • editor/src/messages/tool/common_functionality/shapes/arc_shape.rs
    • Imported NodeInputDecleration and arc generator nodes.
    • Replaced hardcoded numeric index with RadiusInput::INDEX for arc radius input.
  • editor/src/messages/tool/common_functionality/shapes/arrow_shape.rs
    • Imported NodeInputDecleration and arrow generator nodes.
    • Replaced hardcoded numeric indices with StartInput::INDEX and EndInput::INDEX for arrow start and end points.
  • editor/src/messages/tool/common_functionality/shapes/circle_shape.rs
    • Imported NodeInputDecleration.
    • Replaced hardcoded numeric index with RadiusInput::INDEX for circle radius input.
  • editor/src/messages/tool/common_functionality/shapes/ellipse_shape.rs
    • Imported NodeInputDecleration and ellipse generator nodes.
    • Replaced hardcoded numeric indices with RadiusXInput::INDEX and RadiusYInput::INDEX for ellipse radii.
  • editor/src/messages/tool/common_functionality/shapes/line_shape.rs
    • Imported NodeInputDecleration and line generator nodes.
    • Replaced hardcoded numeric indices with StartInput::INDEX and EndInput::INDEX for line start and end points.
  • editor/src/messages/tool/common_functionality/shapes/polygon_shape.rs
    • Imported NodeInputDecleration and regular_polygon generator nodes.
    • Replaced hardcoded numeric indices with RadiusInput::INDEX and SidesInput::<u32>::INDEX for polygon properties.
  • editor/src/messages/tool/common_functionality/shapes/rectangle_shape.rs
    • Imported NodeInputDecleration and rectangle generator nodes.
    • Replaced hardcoded numeric indices with WidthInput::INDEX and HeightInput::INDEX for rectangle dimensions.
  • editor/src/messages/tool/common_functionality/shapes/shape_utility.rs
    • Replaced hardcoded numeric index with RadiusInput::INDEX for polygon radius.
    • Replaced hardcoded numeric indices with Radius1Input::INDEX and Radius2Input::INDEX for star radii.
  • node-graph/libraries/no-std-types/src/color/discrete_srgb.rs
    • Added commented-out debug lines within a test function.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request does a great job of replacing hardcoded numeric indices with named constants, which significantly improves code readability and maintainability. I've identified a couple of areas for further improvement: one function can be simplified by using imported constants directly, and there's a recurring typo in an imported name that should ideally be fixed upstream. Additionally, it looks like some commented-out debug code was unintentionally left in a test file.

Comment on lines 79 to 103
const TRANSLATION_INDEX: usize = 1;
const ROTATION_INDEX: usize = 2;
const SCALE_INDEX: usize = 3;
const SKEW_INDEX: usize = 4;

let translation = if let Some(&TaggedValue::DVec2(translation)) = inputs[TRANSLATION_INDEX].as_value() {
translation
} else {
DVec2::ZERO
};
let rotation = if let Some(&TaggedValue::F64(rotation)) = inputs[2].as_value() { rotation } else { 0. };
let scale = if let Some(&TaggedValue::DVec2(scale)) = inputs[3].as_value() { scale } else { DVec2::ONE };
let shear = if let Some(&TaggedValue::DVec2(shear)) = inputs[4].as_value() { shear } else { DVec2::ZERO };
let rotation = if let Some(&TaggedValue::F64(rotation)) = inputs[ROTATION_INDEX].as_value() {
rotation
} else {
0.
};
let scale = if let Some(&TaggedValue::DVec2(scale)) = inputs[SCALE_INDEX].as_value() {
scale
} else {
DVec2::ONE
};
let shear = if let Some(&TaggedValue::DVec2(shear)) = inputs[SKEW_INDEX].as_value() {
shear
} else {
DVec2::ZERO
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since graphene_std::transform_nodes::transform::* is already imported, you can use the INDEX constants from TranslationInput, RotationInput, ScaleInput, and SkewInput directly instead of defining local constants. This improves maintainability by using the single source of truth from the node definitions.

Suggested change
const TRANSLATION_INDEX: usize = 1;
const ROTATION_INDEX: usize = 2;
const SCALE_INDEX: usize = 3;
const SKEW_INDEX: usize = 4;
let translation = if let Some(&TaggedValue::DVec2(translation)) = inputs[TRANSLATION_INDEX].as_value() {
translation
} else {
DVec2::ZERO
};
let rotation = if let Some(&TaggedValue::F64(rotation)) = inputs[2].as_value() { rotation } else { 0. };
let scale = if let Some(&TaggedValue::DVec2(scale)) = inputs[3].as_value() { scale } else { DVec2::ONE };
let shear = if let Some(&TaggedValue::DVec2(shear)) = inputs[4].as_value() { shear } else { DVec2::ZERO };
let rotation = if let Some(&TaggedValue::F64(rotation)) = inputs[ROTATION_INDEX].as_value() {
rotation
} else {
0.
};
let scale = if let Some(&TaggedValue::DVec2(scale)) = inputs[SCALE_INDEX].as_value() {
scale
} else {
DVec2::ONE
};
let shear = if let Some(&TaggedValue::DVec2(shear)) = inputs[SKEW_INDEX].as_value() {
shear
} else {
DVec2::ZERO
};
let translation = if let Some(&TaggedValue::DVec2(translation)) = inputs[TranslationInput::INDEX].as_value() {
translation
} else {
DVec2::ZERO
};
let rotation = if let Some(&TaggedValue::F64(rotation)) = inputs[RotationInput::INDEX].as_value() {
rotation
} else {
0.
};
let scale = if let Some(&TaggedValue::DVec2(scale)) = inputs[ScaleInput::INDEX].as_value() {
scale
} else {
DVec2::ONE
};
let shear = if let Some(&TaggedValue::DVec2(shear)) = inputs[SkewInput::INDEX].as_value() {
shear
} else {
DVec2::ZERO
};

use glam::DAffine2;
use graph_craft::document::NodeInput;
use graph_craft::document::value::TaggedValue;
use graphene_std::NodeInputDecleration;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There seems to be a typo in NodeInputDecleration. It should likely be NodeInputDeclaration. This might be an issue in the upstream graphene_std crate that should be corrected.

Suggested change
use graphene_std::NodeInputDecleration;
use graphene_std::NodeInputDeclaration;

use glam::DAffine2;
use graph_craft::document::NodeInput;
use graph_craft::document::value::TaggedValue;
use graphene_std::NodeInputDecleration;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There seems to be a typo in NodeInputDecleration. It should likely be NodeInputDeclaration. This might be an issue in the upstream graphene_std crate that should be corrected.

Suggested change
use graphene_std::NodeInputDecleration;
use graphene_std::NodeInputDeclaration;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a module introduced in March 2025

@Keavon
Copy link
Member

Keavon commented Feb 25, 2026

(you don't have to follow the AI review comments, they are advisory only)

@Tyooughtul Tyooughtul force-pushed the fix_hardcoded_input_indices branch from afeccc9 to 8acf3db Compare February 26, 2026 06:01
@Keavon
Copy link
Member

Keavon commented Feb 26, 2026

This is a surprisingly small number of changes. Does this comprehensively capture every instance, or is this just progress towards that goal?

@Tyooughtul
Copy link
Author

This is a surprisingly small number of changes. Does this comprehensively capture every instance, or is this just progress towards that goal?

Thanks for the review! 😊 The left hardcoded 0 and 1 indices are primary input for main data flow into any node and index 1 is the secondary input used by layer nodes for chain stacking. As for Artboard's location (2) and dimensions (3), I found that LocationInput::INDEX and DimensionsInput::INDEX simply don't exist anywhere in the codebase.The node macro apparently didn't generate these constants for the artboard node, so those two may not be converted without first adding that infrastructure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Find and replace all magic number node parameter indexes with ::INDEX const

2 participants