diff --git a/korge-sandbox/src/Main.kt b/korge-sandbox/src/Main.kt index 1277c92ad..3b5012493 100644 --- a/korge-sandbox/src/Main.kt +++ b/korge-sandbox/src/Main.kt @@ -133,7 +133,8 @@ suspend fun main() = Korge( //Demo(::MainMasks), //Demo(::MainShape2dScene), //Demo(::MainUIStacks), - Demo(::MainAudioScene), + Demo(::MainTextBounds2), + //Demo(::MainAudioScene), //Demo(::MainAsteroids), //Demo(::MainSprites10k), //Demo(::MainStressMatrixMultiplication), diff --git a/korge-sandbox/src/samples/MainTextBounds2.kt b/korge-sandbox/src/samples/MainTextBounds2.kt new file mode 100644 index 000000000..ed8f8e518 --- /dev/null +++ b/korge-sandbox/src/samples/MainTextBounds2.kt @@ -0,0 +1,43 @@ +package samples + +import korlibs.image.color.* +import korlibs.image.font.* +import korlibs.image.text.* +import korlibs.korge.scene.* +import korlibs.korge.view.* +import korlibs.math.geom.* + +class MainTextBounds2 : Scene() { + override suspend fun SContainer.sceneMain() { + for ((tbindex, withTextBounds) in listOf(true, false).withIndex()) { + for ((tindex, text) in listOf("hello world", "hello\nworld").withIndex()) { + for ((findex, font) in listOf(DefaultTtfFontAsBitmap, DefaultTtfFont).withIndex()) { + for ((hindex, horizontal) in listOf(HorizontalAlign.LEFT, HorizontalAlign.CENTER, HorizontalAlign.RIGHT, HorizontalAlign.JUSTIFY).withIndex()) { + for ((vindex, vertical) in listOf(VerticalAlign.TOP, VerticalAlign.CENTER, VerticalAlign.BOTTOM, VerticalAlign.BASELINE).withIndex()) { + val textColor = if (findex == 0) Colors.RED else Colors.GREEN + container { + val W = 70 + val H = 60 + val PAD = 6 + position( + 10 + (W + PAD) * (hindex) + ((findex + (tindex * 2.1)) * ((W + PAD + 2) * 4)), + 10 + (H + PAD) * vindex + (tbindex * ((H + PAD + 4) * 4)) + ) + if (withTextBounds) { + solidRect(Size(W, H), Colors.DIMGREY) + text(text, alignment = TextAlignment(horizontal, vertical), color = textColor, font = font, textSize = 10) { + this.setTextBounds(Rectangle(0, 0, W, H)) + } + } else { + line(Point(-4, 0), Point(+4, 0), Colors.DIMGREY) + line(Point(0, -4), Point(0, +4), Colors.DIMGREY) + text(text, alignment = TextAlignment(horizontal, vertical), color = textColor, font = font, textSize = 10) + } + } + } + } + } + } + } + } +} diff --git a/korge/src/korlibs/korge/view/Text.kt b/korge/src/korlibs/korge/view/Text.kt index 8c4590fa5..943f1e02f 100644 --- a/korge/src/korlibs/korge/view/Text.kt +++ b/korge/src/korlibs/korge/view/Text.kt @@ -309,13 +309,25 @@ open class Text( //println("lineInfos=$lineInfos") //println("Text.BitmapFont: bounds=$bounds, firstBounds=$firstBounds, textWidth=$textWidth, textHeight=$textHeight, verticalAlign=$verticalAlign") - //val dx = (-_textBounds.width - textWidth) * horizontalAlign.ratio - val dx = _textBounds.x - val dy: Double = when (verticalAlign) { - VerticalAlign.BASELINE -> 0.0 - VerticalAlign.TOP -> firstLineInfos.maxTop - else -> firstLineInfos.maxTop - (totalHeight) * verticalAlign.ratioFake + val dx = when { + autoSize -> _textBounds.x + else -> _textBounds.x + 1 + ((_textBounds.width - textWidth) * horizontalAlign.ratio) } + val dy: Double = when { + autoSize -> { + _textBounds.y + firstLineInfos.maxTop + } + else -> { + when (verticalAlign) { + VerticalAlign.BASELINE -> _textBounds.y + else -> (_textBounds.y + firstLineInfos.maxTop) + ((_textBounds.height - totalHeight) * verticalAlign.ratioFake) + } + } + + } + + + //println("dx=$dx, dy=$dy, totalHeight=$totalHeight, textHeight=$textHeight, textWidth=$textWidth, _textBounds=$_textBounds") val program = font.agProgram //val program = null