-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added text rendering utilities. #183
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ktx.graphics | ||
|
||
import com.badlogic.gdx.graphics.g2d.BitmapFont | ||
import com.badlogic.gdx.graphics.g2d.GlyphLayout | ||
import com.badlogic.gdx.math.Vector2 | ||
|
||
/** | ||
* Calculates center position of the chosen [text] written with this [BitmapFont] on an object | ||
* with given [width] and [height] drawn at [x] and [y] coordinates. | ||
* | ||
* The passed [text] rendered with the selected [BitmapFont] at the returned coordinates should | ||
* be in the middle of the object. | ||
* | ||
* Note that [x] and [y] are essentially an optional offset added to the calculated position. | ||
*/ | ||
fun BitmapFont.center(text: String, width: Float, height: Float, x: Float = 0f, y: Float = 0f): Vector2 { | ||
val layout = GlyphLayout(this, text) | ||
return Vector2(x + (width - layout.width) / 2f, y + (height + layout.height) / 2f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ktx.graphics | ||
|
||
import com.badlogic.gdx.Gdx | ||
import com.badlogic.gdx.backends.lwjgl.LwjglFiles | ||
import com.badlogic.gdx.backends.lwjgl.LwjglNativesLoader | ||
import com.badlogic.gdx.graphics.g2d.BitmapFont | ||
import com.badlogic.gdx.utils.Array as GdxArray | ||
import com.nhaarman.mockitokotlin2.mock | ||
import org.junit.After | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
/** | ||
* Loads the .fnt settings file of the default LibGDX Arial font, but | ||
* omits loading the textures. For testing purposes. | ||
*/ | ||
class FakeFont : BitmapFont( | ||
BitmapFontData(Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.fnt"), true), | ||
GdxArray.with(mock()), | ||
true) | ||
{ | ||
override fun load(data: BitmapFontData?) { | ||
// Do nothing. | ||
} | ||
} | ||
|
||
class TextUtilitiesTest { | ||
@Before | ||
fun `setup files`() { | ||
Gdx.files = LwjglFiles() | ||
} | ||
|
||
@Test | ||
fun `should center text on a rectangle`() { | ||
val font = FakeFont() | ||
val width = 100f | ||
val height = 200f | ||
|
||
val position = font.center("text", width, height) | ||
|
||
assertEquals(38.5f, position.x, 0.1f) | ||
assertEquals(105.5f, position.y, 0.1f) | ||
} | ||
@Test | ||
fun `should center text on a rectangle at given position`() { | ||
val font = FakeFont() | ||
|
||
val position = font.center("text", x = 100f, y = 200f, width = 100f, height = 200f) | ||
|
||
assertEquals(138.5f, position.x, 0.1f) | ||
assertEquals(305.5f, position.y, 0.1f) | ||
} | ||
|
||
@After | ||
fun `dispose of files`() { | ||
Gdx.files = null | ||
} | ||
} |