GridPositioner2D
Summary
Section titled “Summary”Controls the on-grid cursor used by building/interaction systems.
Responsibilities: - Recenter according to GridTargetingSettings policy when input/mode is enabled - Follow mouse/keyboard input to update on-grid position - STRICTLY for tile center targeting - does NOT handle rotation or manipulation
Targeting separation: Target acquisition/collision detection is handled by dedicated components (e.g., TargetingShapeCast2D). GridPositioner2D no longer depends on or manages any shapecast component.
Manipulation separation: Object rotation, flipping, and manipulation are handled by ManipulationParent. GridPositioner2D focuses solely on positioning the targeting cursor at tile centers.
Dependency Injection: resolve_gb_dependencies(container) is the standard method called by GBInjectorSystem to inject dependencies. This is the primary integration pattern for runtime use. set_dependencies(...) is an internal helper for testing and advanced use cases.
Properties
Section titled “Properties”input_processing_enabled: bool = falsePublic flag & API for enabling/disabling input processing (used by tests)
Methods
Section titled “Methods”func _init( ) -> voidFail fast design. Defaults should be explicitly set by scenes or tests.
func _ready( ) -> voidfunc _physics_process( delta: float ) -> voidfunc _process( delta: float ) -> voidfunc set_input_processing_enabled( p_enabled: bool ) -> voidPublic API used by tests to enable input handling on this node.
func is_input_processing_enabled( ) -> boolfunc is_input_ready( ) -> boolPublic helper: returns true when dependencies are injected and a target map is assigned
func are_dependencies_ready( ) -> boolHelper: returns true when all critical dependencies are injected (logger, settings, mode state) Use this to guard input methods that need to log or access settings
func resolve_gb_dependencies( p_config: GBCompositionContainer ) -> voidStandard dependency injection method called by GBInjectorSystem. This is the primary integration pattern - implement this method to participate in DI. Internally delegates to set_dependencies() for actual wiring.
func _input( event: InputEvent )Forward to explicit dependency injection helper to avoid service locator usage.
Handles all input for the positioner including mouse movement, keyboard movement, and visibility INPUT: All mouse movement is handled here in response to events; not in physics.
func _handle_mouse_motion_event( motion: InputEventMouseMotion, current_mode: GBEnums.Mode ) -> voidExtracted helper: handle an InputEventMouseMotion event (snapped move + visibility)
func _convert_screen_to_world( screen_pos: Vector2 ) -> Vector2Deprecated: Use GBPositioning2DUtils.get_tile_from_global_position() and move_to_tile_center() instead. This function has been removed to maintain DRY principles and single source of truth.
Single source of truth: centralized screen-to-world coordinate conversion Delegates to GBPositioning2DUtils for DRY compliance and maintainability
func _move_to_cached_mouse_world( ) -> Vector2iDeprecated: Use GBPositioning2DUtils.get_tile_from_global_position() and move_to_tile_center() instead. This function has been removed to maintain DRY principles and single source of truth.
Move the positioner back to its cached mouse world location when available.
func _cache_current_world_position( ) -> voidCache the current global position as the latest mouse world reference.
func _get_active_viewport( ) -> ViewportHelper: retrieve active Viewport for the current target map (may be null)
func _get_active_camera( p_vp: Viewport ) -> Camera2DHelper: retrieve active Camera2D from a viewport (may be null)
func _is_disabled_in_off_mode( ) -> boolHelper: check if positioner should be disabled when in OFF mode Returns true if positioner should be blocked from input/positioning in OFF mode
func _is_mouse_cursor_on_screen( ) -> boolHelper: check if mouse cursor is within viewport bounds Returns true if mouse cursor is within the visible viewport area
func _is_mouse_follow_allowed( ) -> boolHelper: decide whether mouse-follow behavior should run this frame
func _mouse_input_gate( ) -> boolReturns a gate result for handling mouse input with an explicit reason when blocked
func _apply_mouse_world( world: Vector2, method: int, screen: Vector2 ) -> voidCentralizes caching + movement application and diagnostics
func _log_mouse_motion( p_method: int, p_screen: Vector2, p_world: Vector2 ) -> voidHelper: structured mouse motion log
func _log_mouse_gate( p_allowed: bool ) -> voidHelper: structured mouse gate log
func _apply_recenter_on_enable( ) -> voidRecenter behavior when movement/input becomes enabled again User-specified logic: move to mouse cursor if mouse enabled and cursor on screen, otherwise move to center tile position Respects the active when off setting - no positioning changes when disabled in OFF mode
func _apply_positioning_and_visibility_sequence( ) -> voidApply positioning and then visibility in proper sequence after dependency injection This ensures Camera2D is available for positioning and visibility is updated based on final position
func _recenter_decision_to_string( p_decision: int ) -> Stringfunc _handle_mouse_movement( mouse_global_override: Variant ) -> voidHandle mouse movement to update positioner position. If a
mouse_global_overrideis provided (e.g., from InputEventMouseMotion), it will be used instead of querying the viewport/cursor state. This enables compatibility with tests (SceneRunner) and hidden cursor scenarios.func _move_positioner_by_tile( p_tile_delta: Vector2i ) -> voidMoves the positioner by a specified number of tiles in a given direction.
p_direction: Vector2 - Direction vector (e.g., Vector2(1,0) for right, Vector2(0,1) for down)func move_to_viewport_center_tile( ) -> Vector2iMove positioner to the center tile of the viewport/camera view. Returns the tile coordinate where the positioner was positioned, or Vector2i.ZERO if failed.
func move_to_cursor_center_tile( ) -> Vector2iMove positioner to the center tile at the cursor location when available. Falls back to viewport mouse, then TileMap global, then view center. Returns the tile coordinate where the positioner was positioned.
func _apply_recenter( ) -> voidApply manual recenter logic based on settings.
func get_last_manual_recenter_mode( ) -> intRetrieve the last manual recenter mode applied.
func _exit_tree( ) -> voidfunc remove_self_as_positioner( )Removes this object from being set to the _targeting_state.positioner property
func update_visibility( ) -> voidRemove self from being the state positioner
Uses whether the mouse movement was consumed by UI to determine if the positioner and child objects should be visible or invisible. Only called when hide_on_handled is true
func should_be_visible( ) -> boolReturns whether the positioner should be visible given current mode/settings. Mouse handled gating remains event-driven and is not applied here.
func set_dependencies( p_states: GBStates, p_config: GBConfig, p_logger: GBLogger, p_actions: GBActions, enable_input: bool ) -> voidInternal dependency injection helper method. For runtime use, prefer implementing resolve_gb_dependencies() which is called by GBInjectorSystem. This method is primarily for testing and advanced use cases where you need explicit control.
p_states: GBStates providing .targeting and .modep_config: GBConfig for settings (expects settings.targeting)p_logger: Optional GBLogger for diagnosticsp_actions: Optional GBActions for keyboard bindingsenable_input: When true, enable input processing immediately (default true)func get_runtime_issues( ) -> Array[String]Checks if the properties of the GridPositioner2D are set properly during gameplay. Returns validation issues if dependencies are missing, empty array if valid.
return: Array[String] - List of validation issues (empty if valid)func validate_dependencies( ) -> boolRuntime validation logs all issues that should be resolved at runtime
Call after all dependencies are expected to be resolved.
func _on_mode_changed( p_mode: GBEnums.Mode )func _should_be_visible_for_mode( p_mode: GBEnums.Mode ) -> boolHelper to compute visibility given an explicit mode value
func get_visual_node( ) -> NodeVisual helpers are provided by GBSearchUtils universally
func is_visual_visible( ) -> boolfunc _set_visible_state( p_visible: bool ) -> voidHelper to keep this node and its visual in sync for visibility
func log_current_visual_state( )Helper: explicit render state log for manual triggering if needed
func _schedule_end_of_frame_state_log( ) -> voidSchedule end-of-frame state logging (deferred to next frame to catch final state)
func _log_end_of_frame_state_async( ) -> voidAsync log of end-of-frame state (called deferred)
func _log_screen_and_mouse_state( )Diagnostic: log screen/camera bounds, positioner screen relation, mouse world, and tile info
func _visibility_context( visual: CanvasItem ) -> StringBuild a visibility context string: first hidden ancestor, alpha, z-index, and position
func is_positioner_active( ) -> bool(Stack summary moved to GBDiagnostics.format_stack_summary)
Whether the positioner is active and should update each frame
func should_hide_under_handled_ui( ) -> boolWhether the positioner should hide when the mouse events are handled by UI
func are_dependencies_resolved( ) -> boolCheck if all critical dependencies have been resolved
func to_diagnostic_string( ) -> StringDiagnostics: concise state summary for tests and debug logging Safe to call even before dependency injection - provides fallback values
func _set_visibility_reason( p_reason: String ) -> voidfunc _visibility_decision_trace( ) -> Stringfunc _get_debug_log_mode( ) -> GBDebugSettings.GridPositionerLogModefunc _is_logging_mode( p_mode: GBDebugSettings.GridPositionerLogMode ) -> boolfunc _is_visibility_logging_enabled( ) -> boolfunc _is_mouse_input_logging_enabled( ) -> boolfunc _is_positioning_logging_enabled( ) -> boolfunc _is_state_flow_logging_enabled( ) -> boolfunc _log_debug( p_mode: GBDebugSettings.GridPositionerLogMode, p_message: String, p_throttled: bool ) -> voidfunc _log_visibility( p_message: String, p_throttled: bool ) -> voidfunc _log_mouse( p_message: String, p_throttled: bool ) -> voidfunc _log_positioning( p_message: String, p_throttled: bool ) -> voidfunc _log_state_flow( p_message: String, p_throttled: bool ) -> voidfunc _test_convert_screen_to_world( screen_pos: Vector2 ) -> Vector2Unit test helper: verify screen-to-world coordinate conversion Returns the converted world position for testing purposes
func _test_get_tile_center_from_screen( screen_pos: Vector2 ) -> DictionaryUnit test helper: verify tile center calculation Returns the tile coordinate and world position for testing
func _apply_visibility_result( p_result: MouseEventVisibilityResult ) -> void
Source
Section titled “Source”addons/grid_building/systems/grid_targeting/grid_positioner/grid_positioner_2d.gd
This API reference is automatically generated from the plugin source code. For implementation examples and usage guides, see the guides section.