A monospace bitmap font where glyphs are stored in a tileset

This Font does not implement all settings of TextSprites.

Introduced properties

fun advance: Float

gamnit :: TileSetFont :: advance

Distance between the beginning of a letter tile and the beginning of the next letter tile
fun char(char: Char): nullable Texture

gamnit :: TileSetFont :: char

The glyph/tile/texture associated to char
fun chars=(chars: Text)

gamnit :: TileSetFont :: chars=

Set the characters present in texture
init defaultinit(texture: Texture, width: Numeric, height: Numeric, chars: Text)

gamnit :: TileSetFont :: defaultinit

fun hspace: Numeric

gamnit :: TileSetFont :: hspace

Additional space to insert horizontally between characters
fun hspace=(hspace: Numeric)

gamnit :: TileSetFont :: hspace=

Additional space to insert horizontally between characters
fun text_height(text: Text): Numeric

gamnit :: TileSetFont :: text_height

Distance between the top of the first line to the bottom of the last line in text
fun text_width(text: Text): Numeric

gamnit :: TileSetFont :: text_width

Distance between the beginning and the end of the longest line of text
fun vspace: Numeric

gamnit :: TileSetFont :: vspace

Additional space to insert vertically between characters
fun vspace=(vspace: Numeric)

gamnit :: TileSetFont :: vspace=

Additional space to insert vertically between characters

Redefined properties

redef type SELF: TileSetFont

gamnit $ TileSetFont :: SELF

Type of this instance, automatically specialized in every class
redef fun write_into(text_sprites: TextSprites, text: Text)

gamnit $ TileSetFont :: write_into

Backend writing service, clients should use TextSprites.text=

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 [](x: Int, y: Int): Texture

gamnit :: TileSet :: []

Subtexture for the tile at x, y
fun advance: Float

gamnit :: TileSetFont :: advance

Distance between the beginning of a letter tile and the beginning of the next letter tile
fun char(char: Char): nullable Texture

gamnit :: TileSetFont :: char

The glyph/tile/texture associated to char
fun chars=(chars: Text)

gamnit :: TileSetFont :: chars=

Set the characters present in texture
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.
init defaultinit(texture: Texture, width: Numeric, height: Numeric, chars: Text)

gamnit :: TileSetFont :: defaultinit

init defaultinit(texture: Texture, width: Numeric, height: Numeric)

gamnit :: TileSet :: defaultinit

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: Numeric

gamnit :: TileSet :: height

Height of a tile
protected fun height=(height: Numeric)

gamnit :: TileSet :: height=

Height of a tile
fun hspace: Numeric

gamnit :: TileSetFont :: hspace

Additional space to insert horizontally between characters
fun hspace=(hspace: Numeric)

gamnit :: TileSetFont :: hspace=

Additional space to insert horizontally between characters
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 nb_cols: Int

gamnit :: TileSet :: nb_cols

Number of columns of tiles in the texture
protected fun nb_cols=(nb_cols: Int)

gamnit :: TileSet :: nb_cols=

Number of columns of tiles in the texture
fun nb_rows: Int

gamnit :: TileSet :: nb_rows

Number of rows of tiles in the texture
protected fun nb_rows=(nb_rows: Int)

gamnit :: TileSet :: nb_rows=

Number of rows of tiles in the texture
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 partial_line_mod: Numeric

gamnit :: Font :: partial_line_mod

Line spacing modifier for pld and plu
fun partial_line_mod=(partial_line_mod: Numeric)

gamnit :: Font :: partial_line_mod=

Line spacing modifier for pld and plu
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun subtextures: Array[Texture]

gamnit :: TileSet :: subtextures

Cache of the subtextures of tiles
protected fun subtextures=(subtextures: Array[Texture])

gamnit :: TileSet :: subtextures=

Cache of the subtextures of tiles
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun text_height(text: Text): Numeric

gamnit :: TileSetFont :: text_height

Distance between the top of the first line to the bottom of the last line in text
fun text_width(text: Text): Numeric

gamnit :: TileSetFont :: text_width

Distance between the beginning and the end of the longest line of text
fun texture: Texture

gamnit :: TileSet :: texture

Texture containing the tileset
protected fun texture=(texture: Texture)

gamnit :: TileSet :: texture=

Texture containing the tileset
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 vspace: Numeric

gamnit :: TileSetFont :: vspace

Additional space to insert vertically between characters
fun vspace=(vspace: Numeric)

gamnit :: TileSetFont :: vspace=

Additional space to insert vertically between characters
fun width: Numeric

gamnit :: TileSet :: width

Width of a tile
protected fun width=(width: Numeric)

gamnit :: TileSet :: width=

Width of a tile
protected abstract fun write_into(text_sprites: TextSprites, text: Text)

gamnit :: Font :: write_into

Backend writing service, clients should use TextSprites.text=
package_diagram gamnit::TileSetFont TileSetFont gamnit::TileSet TileSet gamnit::TileSetFont->gamnit::TileSet gamnit::Font Font gamnit::TileSetFont->gamnit::Font core::Object Object gamnit::TileSet->core::Object gamnit::Font->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Font

gamnit :: Font

Abstract font, drawn by a TextSprites
class TileSet

gamnit :: TileSet

Efficiently retrieve tiles in a big texture

Class definitions

gamnit $ TileSetFont
# A monospace bitmap font where glyphs are stored in a tileset
#
# This `Font` does not implement all settings of `TextSprites`.
class TileSetFont
	super TileSet
	super Font

	# Set the characters present in `texture`
	#
	# Last all characters from left to right, then top to bottom.
	# Line skip `\n`, are ignored and space ' ' skips holes in the tileset.
	fun chars=(chars: Text)
	is autoinit do
		chars_cleaned = chars.replace("\n", "")
	end

	# Character present in the texture, set by `chars=`
	private var chars_cleaned: Text is noautoinit

	# Additional space to insert horizontally between characters
	#
	# A negative value may display overlapped tiles.
	var hspace: Numeric = 0.0 is writable

	# Additional space to insert vertically between characters
	#
	# A negative value may display overlapped tiles.
	var vspace: Numeric = 0.0 is writable

	# The glyph/tile/texture associated to `char`
	#
	# Returns null if `char` is not in `chars`.
	fun char(char: Char): nullable Texture
	do
		var i = chars_cleaned.index_of(char)
		if i == -1 then return null
		return subtextures[i]
	end

	# Distance between the beginning of a letter tile and the beginning of the next letter tile
	fun advance: Float do return width.to_f + hspace.to_f

	# Distance between the beginning and the end of the longest line of `text`
	fun text_width(text: Text): Numeric
	do
		var lines = text.split('\n')
		if lines.is_empty then return 0

		var longest = 0
		for line in lines do longest = longest.max(line.length)

		return longest.mul(advance)
	end

	# Distance between the top of the first line to the bottom of the last line in `text`
	fun text_height(text: Text): Numeric
	do
		if text.is_empty then return 0

		var n_lines = text.chars.count('\n')
		return (n_lines+1).mul(height.add(vspace)).sub(vspace)
	end

	redef fun write_into(text_sprites, text)
	do
		# Build new sprites
		var dx = advance/2.0
		var dy = hspace.to_f/2.0
		for c in text do
			if c == '\n' then
				dy -= height.to_f + vspace.to_f
				dx = advance/2.0
				continue
			else if c == pld then
				dy -= (height.to_f + vspace.to_f) * partial_line_mod.to_f
				continue
			else if c == plu then
				dy += (height.to_f + vspace.to_f) * partial_line_mod.to_f
				continue
			else if c.is_whitespace then
				dx += advance
				continue
			end

			var tex = char(c)
			if tex == null then
				# Try to fallback to '?'
				tex = char('?')
				if tex == null then continue
			end

			text_sprites.sprites.add new Sprite(tex, text_sprites.anchor.offset(dx, dy, 0))
			dx += advance
		end
	end
end
lib/gamnit/tileset.nit:60,1--155,3