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,110 +3,55 @@ package com.iofferyoutea.WitchQueen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Texture
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.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.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.sun.org.apache.bcel.internal.generic.CASTORE
import ktx.app.KtxScreen
import ktx.app.clearScreen
import org.w3c.dom.Text
class MainMenu : KtxScreen {
val table = Table().apply {
debug = true
setFillParent(true)
}
val stage = Stage().apply {
Gdx.input.inputProcessor = this
addActor(table)
}
val stage = Stage()
// Default
val table = Table()
var tableWidthPadAmount = 0.8f
var tableHeightPadAmount = 0.8f
//region Buttons
val defaultButtonTrd = TextureRegionDrawable(Texture("default.png"))
val flippedDefaultButtonTrd = TextureRegionDrawable(Texture("default_flipped.png"))
val defaultTextButtonStyle = TextButton.TextButtonStyle (
defaultButtonTrd,
flippedDefaultButtonTrd,
defaultButtonTrd,
BitmapFont()
defaultButtonTrd,
flippedDefaultButtonTrd,
defaultButtonTrd,
BitmapFont()
)
// 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)
val defaultButton = TextButton("Default Button", defaultTextButtonStyle)
//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() {
table.setFillParent(true)
table.validate() // THIS IS EXTREMELY IMPORTANT!! WE NEED TO MAKE SURE THE TABLE HAS A SIZE WHEN WE REFERENCE IT'S WIDTH/HEIGHT!!
// Stage Setup
Gdx.input.inputProcessor = stage
stage.addActor(table)
//hostOrJoinVerticalGroup
// .size(table.width * 0.4f, table.width * 0.2f)
// Table Setup
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.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)
table.add(defaultButton).growX().height(Value.percentWidth(9f / 32f, table)) // We use a FreeTypeFontGenerator to do font sizing maybe?
}
override fun render(delta: Float) {
clearScreen(0f,0f, 0f)
update(delta)
clearScreen(0f, 0f, 0f)
stage.act(delta)
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() {
stage.dispose()
}