Orthogonal camera to draw UI objects with services to work with screens of different sizes

X axis: left to right of the screen, from position.x to position.x + width Y axis: top to bottom of the screen, from position.y to position.y + height Z axis: far to near the camera (usually when values are higher), from far to near

Introduced properties

fun bottom: IPoint3d[Float]

gamnit :: UICamera :: bottom

Center of the bottom of the screen, at z = 0
protected fun bottom=(bottom: IPoint3d[Float])

gamnit :: UICamera :: bottom=

Center of the bottom of the screen, at z = 0
fun bottom_left: IPoint3d[Float]

gamnit :: UICamera :: bottom_left

Bottom left corner of the screen, at z = 0
protected fun bottom_left=(bottom_left: IPoint3d[Float])

gamnit :: UICamera :: bottom_left=

Bottom left corner of the screen, at z = 0
fun bottom_right: IPoint3d[Float]

gamnit :: UICamera :: bottom_right

Bottom right corner of the screen, at z = 0
protected fun bottom_right=(bottom_right: IPoint3d[Float])

gamnit :: UICamera :: bottom_right=

Bottom right corner of the screen, at z = 0
fun camera_to_ui(x: Numeric, y: Numeric): Point3d[Float]

gamnit :: UICamera :: camera_to_ui

Convert the position x, y on screen, to UI coordinates
fun center: IPoint3d[Float]

gamnit :: UICamera :: center

Center of the screen, from the point of view of the camera, at z = 0
protected fun center=(center: IPoint3d[Float])

gamnit :: UICamera :: center=

Center of the screen, from the point of view of the camera, at z = 0
fun far: Float

gamnit :: UICamera :: far

Clipping wall the farthest of the camera, defaults to -100.0
fun far=(far: Float)

gamnit :: UICamera :: far=

Clipping wall the farthest of the camera, defaults to -100.0
fun height: Float

gamnit :: UICamera :: height

Height in world units, defaults to 1080.0
protected fun height=(height: Float)

gamnit :: UICamera :: height=

Height in world units, defaults to 1080.0
fun left: IPoint3d[Float]

gamnit :: UICamera :: left

Center of the left border of the screen, at z = 0
protected fun left=(left: IPoint3d[Float])

gamnit :: UICamera :: left=

Center of the left border of the screen, at z = 0
fun near: Float

gamnit :: UICamera :: near

Clipping wall near the camera, defaults to 100.0
fun near=(near: Float)

gamnit :: UICamera :: near=

Clipping wall near the camera, defaults to 100.0
fun reset_height(height: nullable Float)

gamnit :: UICamera :: reset_height

Reset the camera position so that height world units are visible on the Y axis
fun right: IPoint3d[Float]

gamnit :: UICamera :: right

Center of the right border of the screen, at z = 0
protected fun right=(right: IPoint3d[Float])

gamnit :: UICamera :: right=

Center of the right border of the screen, at z = 0
fun top: IPoint3d[Float]

gamnit :: UICamera :: top

Center of the top of the screen, at z = 0
protected fun top=(top: IPoint3d[Float])

gamnit :: UICamera :: top=

Center of the top of the screen, at z = 0
fun top_left: IPoint3d[Float]

gamnit :: UICamera :: top_left

Top left corner of the screen, at z = 0
protected fun top_left=(top_left: IPoint3d[Float])

gamnit :: UICamera :: top_left=

Top left corner of the screen, at z = 0
fun top_right: IPoint3d[Float]

gamnit :: UICamera :: top_right

Top right corner of the screen, at z = 0
protected fun top_right=(top_right: IPoint3d[Float])

gamnit :: UICamera :: top_right=

Top right corner of the screen, at z = 0
fun width: Float

gamnit :: UICamera :: width

Width in world units, calculated from height and the screen aspect ratio

Redefined properties

redef type SELF: UICamera

gamnit $ UICamera :: SELF

Type of this instance, automatically specialized in every class
redef fun far=(value: Float)

gamnit :: cameras_cache $ UICamera :: far=

Clipping wall the farthest of the camera, defaults to -100.0
redef fun height=(value: Float)

gamnit :: cameras_cache $ UICamera :: height=

Height in world units, defaults to 1080.0
redef fun mvp_matrix: Matrix

gamnit $ UICamera :: mvp_matrix

The Model-View-Projection matrix created by this camera
redef fun mvp_matrix: Matrix

gamnit :: cameras_cache $ UICamera :: mvp_matrix

The returned matrix must not be modified as it is cached.
redef fun near=(value: Float)

gamnit :: cameras_cache $ UICamera :: near=

Clipping wall near the camera, defaults to 100.0

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun bottom: IPoint3d[Float]

gamnit :: UICamera :: bottom

Center of the bottom of the screen, at z = 0
protected fun bottom=(bottom: IPoint3d[Float])

gamnit :: UICamera :: bottom=

Center of the bottom of the screen, at z = 0
fun bottom_left: IPoint3d[Float]

gamnit :: UICamera :: bottom_left

Bottom left corner of the screen, at z = 0
protected fun bottom_left=(bottom_left: IPoint3d[Float])

gamnit :: UICamera :: bottom_left=

Bottom left corner of the screen, at z = 0
fun bottom_right: IPoint3d[Float]

gamnit :: UICamera :: bottom_right

Bottom right corner of the screen, at z = 0
protected fun bottom_right=(bottom_right: IPoint3d[Float])

gamnit :: UICamera :: bottom_right=

Bottom right corner of the screen, at z = 0
fun camera_to_ui(x: Numeric, y: Numeric): Point3d[Float]

gamnit :: UICamera :: camera_to_ui

Convert the position x, y on screen, to UI coordinates
fun center: IPoint3d[Float]

gamnit :: UICamera :: center

Center of the screen, from the point of view of the camera, at z = 0
protected fun center=(center: IPoint3d[Float])

gamnit :: UICamera :: center=

Center of the screen, from the point of view of the camera, at z = 0
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun display: GamnitDisplay

gamnit :: Camera :: display

The host GamnitDisplay
protected fun display=(display: GamnitDisplay)

gamnit :: Camera :: display=

The host GamnitDisplay
fun far: Float

gamnit :: UICamera :: far

Clipping wall the farthest of the camera, defaults to -100.0
fun far=(far: Float)

gamnit :: UICamera :: far=

Clipping wall the farthest of the camera, defaults to -100.0
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun height: Float

gamnit :: UICamera :: height

Height in world units, defaults to 1080.0
protected fun height=(height: Float)

gamnit :: UICamera :: height=

Height in world units, defaults to 1080.0
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun left: IPoint3d[Float]

gamnit :: UICamera :: left

Center of the left border of the screen, at z = 0
protected fun left=(left: IPoint3d[Float])

gamnit :: UICamera :: left=

Center of the left border of the screen, at z = 0
abstract fun mvp_matrix: Matrix

gamnit :: Camera :: mvp_matrix

The Model-View-Projection matrix created by this camera
fun near: Float

gamnit :: UICamera :: near

Clipping wall near the camera, defaults to 100.0
fun near=(near: Float)

gamnit :: UICamera :: near=

Clipping wall near the camera, defaults to 100.0
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun position: Point3d[Float]

gamnit :: Camera :: position

Position of this camera in world space
protected fun position=(position: Point3d[Float])

gamnit :: Camera :: position=

Position of this camera in world space
fun reset_height(height: nullable Float)

gamnit :: UICamera :: reset_height

Reset the camera position so that height world units are visible on the Y axis
fun right: IPoint3d[Float]

gamnit :: UICamera :: right

Center of the right border of the screen, at z = 0
protected fun right=(right: IPoint3d[Float])

gamnit :: UICamera :: right=

Center of the right border of the screen, at z = 0
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun top: IPoint3d[Float]

gamnit :: UICamera :: top

Center of the top of the screen, at z = 0
protected fun top=(top: IPoint3d[Float])

gamnit :: UICamera :: top=

Center of the top of the screen, at z = 0
fun top_left: IPoint3d[Float]

gamnit :: UICamera :: top_left

Top left corner of the screen, at z = 0
protected fun top_left=(top_left: IPoint3d[Float])

gamnit :: UICamera :: top_left=

Top left corner of the screen, at z = 0
fun top_right: IPoint3d[Float]

gamnit :: UICamera :: top_right

Top right corner of the screen, at z = 0
protected fun top_right=(top_right: IPoint3d[Float])

gamnit :: UICamera :: top_right=

Top right corner of the screen, at z = 0
fun width: Float

gamnit :: UICamera :: width

Width in world units, calculated from height and the screen aspect ratio
package_diagram gamnit::UICamera UICamera gamnit::Camera Camera gamnit::UICamera->gamnit::Camera core::Object Object gamnit::Camera->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Camera

gamnit :: Camera

A camera with a point of view on the world

Class definitions

gamnit $ UICamera
# Orthogonal camera to draw UI objects with services to work with screens of different sizes
#
# X axis: left to right of the screen, from `position.x` to `position.x + width`
# Y axis: top to bottom of the screen, from `position.y` to `position.y + height`
# Z axis: far to near the camera (usually when values are higher), from `far` to `near`
class UICamera
	super Camera

	# Clipping wall near the camera, defaults to 100.0
	var near = 100.0 is writable

	# Clipping wall the farthest of the camera, defaults to -100.0
	var far: Float = -100.0 is writable

	# Width in world units, calculated from `height` and the screen aspect ratio
	fun width: Float do return height * display.aspect_ratio

	# Height in world units, defaults to 1080.0
	#
	# Set this value using `reset_height`.
	var height = 1080.0

	# Reset the camera position so that `height` world units are visible on the Y axis
	#
	# This can be used to set standardized UI units independently from the screen resolution.
	fun reset_height(height: nullable Float)
	do
		if height == null then height = display.height.to_f
		self.height = height
	end

	# Convert the position `x, y` on screen, to UI coordinates
	fun camera_to_ui(x, y: Numeric): Point3d[Float]
	do
		# FIXME this kind of method should use something like a canvas
		# instead of being hard coded on the display.

		var wx = x.to_f * width / display.width.to_f - position.x
		var wy = y.to_f * height / display.height.to_f - position.y
		return new Point3d[Float](wx, -wy, 0.0)
	end

	# Center of the screen, from the point of view of the camera, at z = 0
	var center: IPoint3d[Float] = new CameraAnchor(self, 0.5, -0.5)

	# Center of the top of the screen, at z = 0
	var top: IPoint3d[Float] = new CameraAnchor(self, 0.5, 0.0)

	# Center of the bottom of the screen, at z = 0
	var bottom: IPoint3d[Float] = new CameraAnchor(self, 0.5, -1.0)

	# Center of the left border of the screen, at z = 0
	var left: IPoint3d[Float] = new CameraAnchor(self, 0.0, -0.5)

	# Center of the right border of the screen, at z = 0
	var right: IPoint3d[Float] = new CameraAnchor(self, 1.0, -0.5)

	# Top left corner of the screen, at z = 0
	var top_left: IPoint3d[Float] = new CameraAnchor(self, 0.0, 0.0)

	# Top right corner of the screen, at z = 0
	var top_right: IPoint3d[Float] = new CameraAnchor(self, 1.0, 0.0)

	# Bottom left corner of the screen, at z = 0
	var bottom_left: IPoint3d[Float] = new CameraAnchor(self, 0.0, -1.0)

	# Bottom right corner of the screen, at z = 0
	var bottom_right: IPoint3d[Float] = new CameraAnchor(self, 1.0, -1.0)

	redef fun mvp_matrix
	do
		var view = new Matrix.identity(4)

		# Translate the world away from the camera
		view.translate(-position.x, -position.y, -position.z)

		# Use a projection matrix with a depth
		var projection = new Matrix.orthogonal(0.0, width, -height, 0.0, near, far)

		return view * projection
	end
end
lib/gamnit/cameras.nit:196,1--277,3

gamnit :: cameras_cache $ UICamera
redef class UICamera
	# The returned matrix must not be modified as it is cached.
	redef fun mvp_matrix
	do
		var m = mvp_matrix_cache
		if m == null or check_position_changed then
			m = super
			mvp_matrix_cache = m
		end
		return m
	end

	redef fun near=(value)
	do
		super
		mvp_matrix_cache = null
	end

	redef fun far=(value)
	do
		super
		mvp_matrix_cache = null
	end

	redef fun height=(value)
	do
		super
		mvp_matrix_cache = null
	end
end
lib/gamnit/cameras_cache.nit:89,1--118,3