Ctrl+K

Viewport

Declaration

public class Strawberry.Graphics.Viewport

Represents a viewport for a game engine, defining screen and scene coordinates.

Remarks: This class is serializable to support saving and loading viewport configurations. Percent-based positioning is used when UsePercent is true.

Constructors

Viewport

Creates a new viewport with specified screen and scene coordinates.

void Viewport(string name, Vector2 screenPos, Vector2 screenSize, Vector2 scenePos, Vector2 sceneSize)

Parameters:

Name Type Description
name System.String Unique identifier for the viewport
screenPos Strawberry.Math.Vector2 Position on the screen in pixel coordinates
screenSize Strawberry.Math.Vector2 Size of the viewport area on the screen
scenePos Strawberry.Math.Vector2 Position in scene/world coordinates
sceneSize Strawberry.Math.Vector2 Size of the viewport area in world units

Properties

ScreenPos

Gets or sets position on the screen in pixel coordinates.

Vector2 ScreenPos { get set }

ScreenSize

Gets or sets the size of the viewport area on the screen in pixel units.

Vector2 ScreenSize { get set }

ScenePos

Gets or sets the position in scene/world coordinates (e.g., for camera targets).

Vector2 ScenePos { get set }

SceneSize

Gets or sets the size of the viewport area in scene/world units.

Vector2 SceneSize { get set }

UsePercent

Gets or sets whether to use percent-based positioning should be used instead of absolute pixels.

bool UsePercent { get set }

PercentPos

Gets or sets the position offset when using percent-based coordinates (0-100% of the screen).

Vector2 PercentPos { get set }

PercentSize

Gets or sets the size percentage when using percent-based coordinates (0-100% of the screen).

Vector2 PercentSize { get set }

Bounds

Get the scene bounds of the viewport.

Rectangle Bounds { get }

Name

Gets or sets the unique identifier name for this viewport.

string Name { get set }

Methods

Center

Centers the viewport on the screen

void Center(Vector2 displaySize)

Parameters:

Name Type Description
displaySize Strawberry.Math.Vector2

ScreenToScene

Converts a screen-space pixel position to scene coordinates.

Vector2 ScreenToScene(Vector2 screenPos)

Parameters:

Name Type Description
screenPos Strawberry.Math.Vector2

SceneToScreen

Converts a scene position to screen-space pixel coordinates.

Vector2 SceneToScreen(Vector2 scenePos)

Parameters:

Name Type Description
scenePos Strawberry.Math.Vector2

ApplyPercent

Resolves percent-based positioning into actual pixel coordinates. Call this when the display size changes or before rendering.

void ApplyPercent(Vector2 displaySize)

Parameters:

Name Type Description
displaySize Strawberry.Math.Vector2

FitWidth

Fits the viewport to a given width. Keeps the aspect ratio. If your game runs in landscape mode, call this.

void FitWidth(Vector2 displaySize)

Parameters:

Name Type Description
displaySize Strawberry.Math.Vector2

FitHeight

Fits the viewport to a given height. Keeps the aspect ratio. If your game runs in portrait mode, call this.

void FitHeight(Vector2 displaySize)

Parameters:

Name Type Description
displaySize Strawberry.Math.Vector2