SimpleButton.kt
class SimpleButton(texture: Texture?, text: String?, x: Float, y: Float, width: Float, height: Float) {
var texture : Texture? = null
private var font : BitmapFont? = null
private var shadowFont : BitmapFont? = null
private var x : Float = 0f
var y : Float = 0f
private var width : Float = 0f
private var height : Float = 0f
var text : String? = null
private var fontSize : Int = 120
var fontColor : Color = Color.WHITE
private var fontName : String = "pricedown.otf"
private val shadowLabelGlyph = GlyphLayout()
private val labelGlyph = GlyphLayout()
fun checkIfClicked(ix: Float, iy: Float) {
if ((ix > (x - (width / 2f))) && (ix < (x + (width / 2f)))) {
if ((iy > (y - (height / 2f))) && (iy < (y + (height / 2f)))) {
// the button was clicked, perform an action
println("Button clicked !")
this.listener?.invoke()
}
}
}
init {
println("-------------------")
println("SimpleButton")
println("-------------------")
println("texture: ${texture}")
this.texture = texture // your image
setPosition(x, y)
setSize(width, height)
if (text != null) {
this.text = text
// font generator
val generator = FreeTypeFontGenerator(Gdx.files.internal(fontName))
// parameter
val parameter: FreeTypeFontGenerator.FreeTypeFontParameter =
FreeTypeFontGenerator.FreeTypeFontParameter()
parameter.size = fontSize // font size
font = generator.generateFont(parameter)
shadowFont = generator.generateFont(parameter)
}
}
fun draw(batch: SpriteBatch){
if (this.texture != null) {
batch.draw(
this.texture,
this.x - (this.width / 2f),
this.y - (this.height / 2f),
this.width,
this.height
)
}
if (this.font != null) {
// fontColor
font?.color = fontColor
shadowFont?.color = Color.BLACK
// shadowLabelGlyph
shadowLabelGlyph.setText(shadowFont, text)
// labelGlyphWidth
val shadowLabelGlyphWidth: Float = shadowLabelGlyph.width
val shadowLabelGlyphHeight: Float = shadowLabelGlyph.height
// draw shadowFont
shadowFont!!.draw(
batch,
shadowLabelGlyph,
this.x - (shadowLabelGlyphWidth / 2f) + 2f,
this.y + (shadowLabelGlyphHeight / 2f) - 2f
)
// labelGlyph
labelGlyph.setText(font, text)
// labelGlyphWidth
val labelGlyphWidth: Float = labelGlyph.width
val labelGlyphHeight: Float = labelGlyph.height
// draw font
font!!.draw(
batch,
labelGlyph,
this.x - (labelGlyphWidth / 2f),
this.y + (labelGlyphHeight / 2f)
)
}
}
private var listener: (() -> Unit)? = null
fun setListener(listener: () -> Unit) {
this.listener = listener
}
fun setPosition(x: Float, y: Float) {
this.x = x
this.y = y
}
fun setSize(width: Float, height: Float) {
this.width = width
this.height = height
}
fun setFontName(fontName: String) {
this.fontName = fontName
// font generator
val generator = FreeTypeFontGenerator(Gdx.files.internal(fontName))
// parameter
val parameter: FreeTypeFontGenerator.FreeTypeFontParameter =
FreeTypeFontGenerator.FreeTypeFontParameter()
parameter.size = fontSize // font size
font = generator.generateFont(parameter)
shadowFont = generator.generateFont(parameter)
}
fun setFontSize(fontSize: Int) {
this.fontSize = fontSize
// font generator
val generator = FreeTypeFontGenerator(Gdx.files.internal(fontName))
// parameter
val parameter: FreeTypeFontGenerator.FreeTypeFontParameter =
FreeTypeFontGenerator.FreeTypeFontParameter()
parameter.size = fontSize // font size
font = generator.generateFont(parameter)
shadowFont = generator.generateFont(parameter)
}
}
Game.kt
class Game() : ApplicationAdapter() {
// MARK: - Properties -
var camera : OrthographicCamera? = null
var viewport : ExtendViewport? = null
private lateinit var batch : SpriteBatch
var touchPoint : Vector3 = Vector3(0f, 0f, 0f)
var replayButton : SimpleButton? = null
// MARK: - Lifecycle -
override fun create() {
// camera
camera = OrthographicCamera()
viewport = ExtendViewport(1080f, 1920f, camera)
// game variables
batch = SpriteBatch()
// replayButton
replayButton = SimpleButton(Texture("button_green.png"), "Replay", 0f, 0f, 400f, 200f)
replayButton?.setFontSize(80)
replayButton?.fontColor = Color.BLUE
replayButton?.setFontName("ka1.ttf")
replayButton?.setListener {
println("Button Pressed")
}
}
// MARK: - Render Loop -
override fun render() {
// camera
camera?.update(); //update our camera every frame
// draw
batch.begin()
// replayButton
replayButton!!.setPosition((camera!!.viewportWidth / 2f), (camera!!.viewportHeight / 2f))
replayButton!!.draw(batch)
// touches
if (Gdx.input.justTouched()) {
camera!!.unproject(touchPoint!!.set(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f))
replayButton!!.checkIfClicked(touchPoint!!.x, touchPoint!!.y)
}
// end draw
batch.end()
}
// MARK: - Camera -
override fun resize(width: Int, height: Int) {
// aspect fill
viewport!!.update(width, height, true)
batch.projectionMatrix = camera!!.combined
}
}