Properly scale up a button depending on stage size!

This commit is contained in:
2026-03-20 12:14:20 +00:00
parent 5aba0fc70a
commit 858971d1db

View File

@@ -3,30 +3,22 @@ package com.iofferyoutea.WitchQueen
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.BitmapFont import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Cell
import com.badlogic.gdx.scenes.scene2d.ui.Container
import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup import com.badlogic.gdx.scenes.scene2d.ui.Value
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import com.sun.org.apache.bcel.internal.generic.CASTORE
import ktx.app.KtxScreen import ktx.app.KtxScreen
import ktx.app.clearScreen import ktx.app.clearScreen
import org.w3c.dom.Text
class MainMenu : KtxScreen { class MainMenu : KtxScreen {
val table = Table().apply { val stage = Stage()
debug = true
setFillParent(true)
}
val stage = Stage().apply {
Gdx.input.inputProcessor = this
addActor(table)
}
// Default val table = Table()
var tableWidthPadAmount = 0.8f
var tableHeightPadAmount = 0.8f
//region Buttons
val defaultButtonTrd = TextureRegionDrawable(Texture("default.png")) val defaultButtonTrd = TextureRegionDrawable(Texture("default.png"))
val flippedDefaultButtonTrd = TextureRegionDrawable(Texture("default_flipped.png")) val flippedDefaultButtonTrd = TextureRegionDrawable(Texture("default_flipped.png"))
val defaultTextButtonStyle = TextButton.TextButtonStyle ( val defaultTextButtonStyle = TextButton.TextButtonStyle (
@@ -35,78 +27,31 @@ class MainMenu : KtxScreen {
defaultButtonTrd, defaultButtonTrd,
BitmapFont() BitmapFont()
) )
val defaultButton = TextButton("Default Button", defaultTextButtonStyle)
// Map
// Lobby
// Start off only showing Host and Join button. When selected show menu for that option.
// Host is normal lobby
// Join will bring up menu with games on local network
val hostButton = TextButton("Host", defaultTextButtonStyle).apply {
//setFillParent(true)
}
val joinButton = TextButton("Join", defaultTextButtonStyle).apply {
//setFillParent(true)
}
val hostOrJoinTable = Table().apply { // Move this into a Table instead of a Group!
add(hostButton).growX().row()
add(joinButton).growX()
}
//region Play
val casualButton = TextButton("Casual", defaultTextButtonStyle)
//endregion //endregion
private fun update(delta: Float) {
if (hostButton.isPressed) {
Gdx.app.log("MainMenu", "Host button pressed")
Gdx.app.log("MainMenu", "table width is ${table.width}")
}
if (joinButton.isPressed) {
Gdx.app.log("MainMenu", "Join button pressed")
}
if (casualButton.isPressed) {
Gdx.app.log("MainMenu", "Casual button pressed")
Gdx.app.log("MainMenu", "New Button Size: ${table.width * 0.4f}, ${table.width * 0.2f}")
}
}
override fun show() { override fun show() {
table.setFillParent(true) // Stage Setup
table.validate() // THIS IS EXTREMELY IMPORTANT!! WE NEED TO MAKE SURE THE TABLE HAS A SIZE WHEN WE REFERENCE IT'S WIDTH/HEIGHT!! Gdx.input.inputProcessor = stage
stage.addActor(table)
//hostOrJoinVerticalGroup // Table Setup
// .size(table.width * 0.4f, table.width * 0.2f) table.debug = true
table.setPosition(
stage.width * ((1 - tableWidthPadAmount) / 2),
stage.height * ((1 - tableHeightPadAmount) / 2)
) // This centres the table
table.setSize(stage.width * tableWidthPadAmount, stage.height * tableHeightPadAmount)
table.add(defaultButton).growX().height(Value.percentWidth(9f / 32f, table)) // We use a FreeTypeFontGenerator to do font sizing maybe?
//table.getCell<VerticalGroup>(hostOrJoinVerticalGroup).size(table.width * 0.4f, table.width * 0.2f)
hostOrJoinTable.needsLayout()
for (cell in com.badlogic.gdx.utils.Array.ArrayIterator<Cell<Actor>>(hostOrJoinTable.cells)) {
//cell.height(cell.actorWidth)
Gdx.app.log("MainMenu", cell.actorWidth.toString())
Gdx.app.log("MainMenu", hostOrJoinTable.width.toString())
} }
//table.row()
table.add(hostOrJoinTable).size(table.width * 0.4f, table.width * 0.2f)
//table.getCell<TextButton>(casualButton)
}
override fun render(delta: Float) { override fun render(delta: Float) {
clearScreen(0f,0f, 0f) clearScreen(0f, 0f, 0f)
update(delta)
stage.act(delta) stage.act(delta)
stage.draw() stage.draw()
} }
override fun resize(width: Int, height: Int) {
Gdx.app.log("MainMenu", "resize called! New width: $width, new height $height")
//everythingVerticalGroup.setSize(width.toFloat(), height.toFloat())
}
override fun dispose() { override fun dispose() {
stage.dispose() stage.dispose()
} }