Camera2D

package
v0.0.0-...-5e8c587 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Camera node for 2D scenes. It forces the screen (current layer) to scroll following this node. This makes it easier (and faster) to program scrollable scenes than manually changing the position of CanvasItem-based nodes.

Cameras register themselves in the nearest Viewport node (when ascending the tree). Only one camera can be active per viewport. If no viewport is available ascending the tree, the camera will register in the global viewport.

This node is intended to be a simple helper to get things going quickly, but more functionality may be desired to change how the camera works. To make your own custom camera node, inherit it from Node2D and change the transform of the canvas by setting Viewport.CanvasTransform in Viewport (you can obtain the current Viewport by using Node.GetViewport).

Note that the Camera2D node's Node2D.GlobalPosition doesn't represent the actual position of the screen, which may differ due to applied smoothing or limits. You can use GetScreenCenterPosition to get the real position. Same for the node's Node2D.GlobalRotation which may be different due to applied rotation smoothing. You can use GetScreenRotation to get the current rotation of the screen.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type AnchorMode

type AnchorMode int //gd:Camera2D.AnchorMode
const (
	// The camera's position is fixed so that the top-left corner is always at the origin.
	AnchorModeFixedTopLeft AnchorMode = 0
	// The camera's position takes into account vertical/horizontal offsets and the screen size.
	AnchorModeDragCenter AnchorMode = 1
)

type Any

type Any interface {
	gd.IsClass
	AsCamera2D() Instance
}

type Camera2DProcessCallback

type Camera2DProcessCallback int //gd:Camera2D.Camera2DProcessCallback
const (
	// The camera updates during physics frames (see [Node.NotificationInternalPhysicsProcess]).
	Camera2dProcessPhysics Camera2DProcessCallback = 0
	// The camera updates during process frames (see [Node.NotificationInternalProcess]).
	Camera2dProcessIdle Camera2DProcessCallback = 1
)

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsCamera2D

func (self *Extension[T]) AsCamera2D() Instance

func (*Extension[T]) AsCanvasItem

func (self *Extension[T]) AsCanvasItem() CanvasItem.Instance

func (*Extension[T]) AsNode

func (self *Extension[T]) AsNode() Node.Instance

func (*Extension[T]) AsNode2D

func (self *Extension[T]) AsNode2D() Node2D.Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.Camera2D

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) Align

func (self Instance) Align()

Aligns the camera to the tracked node.

func (Instance) AnchorMode

func (self Instance) AnchorMode() AnchorMode

The Camera2D's anchor point.

func (Instance) AsCamera2D

func (self Instance) AsCamera2D() Instance

func (Instance) AsCanvasItem

func (self Instance) AsCanvasItem() CanvasItem.Instance

func (Instance) AsNode

func (self Instance) AsNode() Node.Instance

func (Instance) AsNode2D

func (self Instance) AsNode2D() Node2D.Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) CustomViewport

func (self Instance) CustomViewport() Node.Instance

The custom Viewport node attached to the Camera2D. If null or not a Viewport, uses the default viewport instead.

func (Instance) DragBottomMargin

func (self Instance) DragBottomMargin() Float.X

Bottom margin needed to drag the camera. A value of 1 makes the camera move only when reaching the bottom edge of the screen.

func (Instance) DragHorizontalEnabled

func (self Instance) DragHorizontalEnabled() bool

If true, the camera only moves when reaching the horizontal (left and right) drag margins. If false, the camera moves horizontally regardless of margins.

func (Instance) DragHorizontalOffset

func (self Instance) DragHorizontalOffset() Float.X

The relative horizontal drag offset of the camera between the right (-1) and left (1) drag margins.

Note: Used to set the initial horizontal drag offset; determine the current offset; or force the current offset. It's not automatically updated when DragHorizontalEnabled is true or the drag margins are changed.

func (Instance) DragLeftMargin

func (self Instance) DragLeftMargin() Float.X

Left margin needed to drag the camera. A value of 1 makes the camera move only when reaching the left edge of the screen.

func (Instance) DragRightMargin

func (self Instance) DragRightMargin() Float.X

Right margin needed to drag the camera. A value of 1 makes the camera move only when reaching the right edge of the screen.

func (Instance) DragTopMargin

func (self Instance) DragTopMargin() Float.X

Top margin needed to drag the camera. A value of 1 makes the camera move only when reaching the top edge of the screen.

func (Instance) DragVerticalEnabled

func (self Instance) DragVerticalEnabled() bool

If true, the camera only moves when reaching the vertical (top and bottom) drag margins. If false, the camera moves vertically regardless of the drag margins.

func (Instance) DragVerticalOffset

func (self Instance) DragVerticalOffset() Float.X

The relative vertical drag offset of the camera between the bottom (-1) and top (1) drag margins.

Note: Used to set the initial vertical drag offset; determine the current offset; or force the current offset. It's not automatically updated when DragVerticalEnabled is true or the drag margins are changed.

func (Instance) EditorDrawDragMargin

func (self Instance) EditorDrawDragMargin() bool

If true, draws the camera's drag margin rectangle in the editor.

func (Instance) EditorDrawLimits

func (self Instance) EditorDrawLimits() bool

If true, draws the camera's limits rectangle in the editor.

func (Instance) EditorDrawScreen

func (self Instance) EditorDrawScreen() bool

If true, draws the camera's screen rectangle in the editor.

func (Instance) Enabled

func (self Instance) Enabled() bool

Controls whether the camera can be active or not. If true, the Camera2D will become the main camera when it enters the scene tree and there is no active camera currently (see Viewport.GetCamera2d).

When the camera is currently active and Enabled is set to false, the next enabled Camera2D in the scene tree will become active.

func (Instance) ForceUpdateScroll

func (self Instance) ForceUpdateScroll()

Forces the camera to update scroll immediately.

func (Instance) GetScreenCenterPosition

func (self Instance) GetScreenCenterPosition() Vector2.XY

Returns the center of the screen from this camera's point of view, in global coordinates.

Note: The exact targeted position of the camera may be different. See GetTargetPosition.

func (Instance) GetScreenRotation

func (self Instance) GetScreenRotation() Float.X

Returns the current screen rotation from this camera's point of view.

Note: The screen rotation can be different from Node2D.GlobalRotation if the camera is rotating smoothly due to RotationSmoothingEnabled.

func (Instance) GetTargetPosition

func (self Instance) GetTargetPosition() Vector2.XY

Returns this camera's target position, in global coordinates.

Note: The returned value is not the same as Node2D.GlobalPosition, as it is affected by the drag properties. It is also not the same as the current position if PositionSmoothingEnabled is true (see GetScreenCenterPosition).

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IgnoreRotation

func (self Instance) IgnoreRotation() bool

If true, the camera's rendered view is not affected by its Node2D.Rotation and Node2D.GlobalRotation.

func (Instance) IsCurrent

func (self Instance) IsCurrent() bool

Returns true if this Camera2D is the active camera (see Viewport.GetCamera2d).

func (Instance) LimitBottom

func (self Instance) LimitBottom() int

Bottom scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.

func (Instance) LimitEnabled

func (self Instance) LimitEnabled() bool

If true, the limits will be enabled. Disabling this will allow the camera to focus anywhere, when the four limit_* properties will not work.

func (Instance) LimitLeft

func (self Instance) LimitLeft() int

Left scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.

func (Instance) LimitRight

func (self Instance) LimitRight() int

Right scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.

func (Instance) LimitSmoothed

func (self Instance) LimitSmoothed() bool

If true, the camera smoothly stops when reaches its limits.

This property has no effect if PositionSmoothingEnabled is false.

Note: To immediately update the camera's position to be within limits without smoothing, even with this setting enabled, invoke ResetSmoothing.

func (Instance) LimitTop

func (self Instance) LimitTop() int

Top scroll limit in pixels. The camera stops moving when reaching this value, but Offset can push the view past the limit.

func (Instance) MakeCurrent

func (self Instance) MakeCurrent()

Forces this Camera2D to become the current active one. Enabled must be true.

func (Instance) Offset

func (self Instance) Offset() Vector2.XY

The camera's relative offset. Useful for looking around or camera shake animations. The offsetted camera can go past the limits defined in LimitTop, LimitBottom, LimitLeft and LimitRight.

func (Instance) PositionSmoothingEnabled

func (self Instance) PositionSmoothingEnabled() bool

If true, the camera's view smoothly moves towards its target position at PositionSmoothingSpeed.

func (Instance) PositionSmoothingSpeed

func (self Instance) PositionSmoothingSpeed() Float.X

Speed in pixels per second of the camera's smoothing effect when PositionSmoothingEnabled is true.

func (Instance) ProcessCallback

func (self Instance) ProcessCallback() Camera2DProcessCallback

The camera's process callback.

func (Instance) ResetSmoothing

func (self Instance) ResetSmoothing()

Sets the camera's position immediately to its current smoothing destination.

This method has no effect if PositionSmoothingEnabled is false.

func (Instance) RotationSmoothingEnabled

func (self Instance) RotationSmoothingEnabled() bool

If true, the camera's view smoothly rotates, via asymptotic smoothing, to align with its target rotation at RotationSmoothingSpeed.

Note: This property has no effect if IgnoreRotation is true.

func (Instance) RotationSmoothingSpeed

func (self Instance) RotationSmoothingSpeed() Float.X

The angular, asymptotic speed of the camera's rotation smoothing effect when RotationSmoothingEnabled is true.

func (Instance) SetAnchorMode

func (self Instance) SetAnchorMode(value AnchorMode)

SetAnchorMode sets the property returned by [GetAnchorMode].

func (Instance) SetCustomViewport

func (self Instance) SetCustomViewport(value Node.Instance)

SetCustomViewport sets the property returned by [GetCustomViewport].

func (Instance) SetDragBottomMargin

func (self Instance) SetDragBottomMargin(value Float.X)

SetDragBottomMargin sets the property returned by [GetDragMargin].

func (Instance) SetDragHorizontalEnabled

func (self Instance) SetDragHorizontalEnabled(value bool)

SetDragHorizontalEnabled sets the property returned by [IsDragHorizontalEnabled].

func (Instance) SetDragHorizontalOffset

func (self Instance) SetDragHorizontalOffset(value Float.X)

SetDragHorizontalOffset sets the property returned by [GetDragHorizontalOffset].

func (Instance) SetDragLeftMargin

func (self Instance) SetDragLeftMargin(value Float.X)

SetDragLeftMargin sets the property returned by [GetDragMargin].

func (Instance) SetDragRightMargin

func (self Instance) SetDragRightMargin(value Float.X)

SetDragRightMargin sets the property returned by [GetDragMargin].

func (Instance) SetDragTopMargin

func (self Instance) SetDragTopMargin(value Float.X)

SetDragTopMargin sets the property returned by [GetDragMargin].

func (Instance) SetDragVerticalEnabled

func (self Instance) SetDragVerticalEnabled(value bool)

SetDragVerticalEnabled sets the property returned by [IsDragVerticalEnabled].

func (Instance) SetDragVerticalOffset

func (self Instance) SetDragVerticalOffset(value Float.X)

SetDragVerticalOffset sets the property returned by [GetDragVerticalOffset].

func (Instance) SetEditorDrawDragMargin

func (self Instance) SetEditorDrawDragMargin(value bool)

SetEditorDrawDragMargin sets the property returned by [IsMarginDrawingEnabled].

func (Instance) SetEditorDrawLimits

func (self Instance) SetEditorDrawLimits(value bool)

SetEditorDrawLimits sets the property returned by [IsLimitDrawingEnabled].

func (Instance) SetEditorDrawScreen

func (self Instance) SetEditorDrawScreen(value bool)

SetEditorDrawScreen sets the property returned by [IsScreenDrawingEnabled].

func (Instance) SetEnabled

func (self Instance) SetEnabled(value bool)

SetEnabled sets the property returned by [IsEnabled].

func (Instance) SetIgnoreRotation

func (self Instance) SetIgnoreRotation(value bool)

SetIgnoreRotation sets the property returned by [IsIgnoringRotation].

func (Instance) SetLimitBottom

func (self Instance) SetLimitBottom(value int)

SetLimitBottom sets the property returned by [GetLimit].

func (Instance) SetLimitEnabled

func (self Instance) SetLimitEnabled(value bool)

SetLimitEnabled sets the property returned by [IsLimitEnabled].

func (Instance) SetLimitLeft

func (self Instance) SetLimitLeft(value int)

SetLimitLeft sets the property returned by [GetLimit].

func (Instance) SetLimitRight

func (self Instance) SetLimitRight(value int)

SetLimitRight sets the property returned by [GetLimit].

func (Instance) SetLimitSmoothed

func (self Instance) SetLimitSmoothed(value bool)

SetLimitSmoothed sets the property returned by [IsLimitSmoothingEnabled].

func (Instance) SetLimitTop

func (self Instance) SetLimitTop(value int)

SetLimitTop sets the property returned by [GetLimit].

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetOffset

func (self Instance) SetOffset(value Vector2.XY)

SetOffset sets the property returned by [GetOffset].

func (Instance) SetPositionSmoothingEnabled

func (self Instance) SetPositionSmoothingEnabled(value bool)

SetPositionSmoothingEnabled sets the property returned by [IsPositionSmoothingEnabled].

func (Instance) SetPositionSmoothingSpeed

func (self Instance) SetPositionSmoothingSpeed(value Float.X)

SetPositionSmoothingSpeed sets the property returned by [GetPositionSmoothingSpeed].

func (Instance) SetProcessCallback

func (self Instance) SetProcessCallback(value Camera2DProcessCallback)

SetProcessCallback sets the property returned by [GetProcessCallback].

func (Instance) SetRotationSmoothingEnabled

func (self Instance) SetRotationSmoothingEnabled(value bool)

SetRotationSmoothingEnabled sets the property returned by [IsRotationSmoothingEnabled].

func (Instance) SetRotationSmoothingSpeed

func (self Instance) SetRotationSmoothingSpeed(value Float.X)

SetRotationSmoothingSpeed sets the property returned by [GetRotationSmoothingSpeed].

func (Instance) SetZoom

func (self Instance) SetZoom(value Vector2.XY)

SetZoom sets the property returned by [GetZoom].

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

func (Instance) Zoom

func (self Instance) Zoom() Vector2.XY

The camera's zoom. Higher values are more zoomed in. For example, a zoom of Vector2(2.0, 2.0) will be twice as zoomed in on each axis (the view covers an area four times smaller). In contrast, a zoom of Vector2(0.5, 0.5) will be twice as zoomed out on each axis (the view covers an area four times larger). The X and Y components should generally always be set to the same value, unless you wish to stretch the camera view.

Note: FontFile.Oversampling does not take Camera2D zoom into account. This means that zooming in/out will cause bitmap fonts and rasterized (non-MSDF) dynamic fonts to appear blurry or pixelated unless the font is part of a CanvasLayer that makes it ignore camera zoom. To ensure text remains crisp regardless of zoom, you can enable MSDF font rendering by enabling ProjectSettings "gui/theme/default_font_multichannel_signed_distance_field" (applies to the default project font only), or enabling Multichannel Signed Distance Field in the import options of a DynamicFont for custom fonts. On system fonts, SystemFont.MultichannelSignedDistanceField can be enabled in the inspector.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL