mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-27 13:55:54 -04:00
Fix map editor resource label wrapped to about 1 char width (#9162)
* Fix potential problems with making ColorMarkupLabel wrappable * Fix map editor resource label wrapped to about 1 char width
This commit is contained in:
parent
f31d2a9a2a
commit
6efb4926af
@ -3,6 +3,7 @@
|
|||||||
package com.unciv.ui.components
|
package com.unciv.ui.components
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color
|
import com.badlogic.gdx.graphics.Color
|
||||||
|
import com.badlogic.gdx.graphics.Colors
|
||||||
import com.badlogic.gdx.graphics.g2d.GlyphLayout
|
import com.badlogic.gdx.graphics.g2d.GlyphLayout
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Label
|
import com.badlogic.gdx.scenes.scene2d.ui.Label
|
||||||
import com.unciv.Constants
|
import com.unciv.Constants
|
||||||
@ -34,6 +35,10 @@ class ColorMarkupLabel private constructor(
|
|||||||
fontSize: Int = Constants.defaultFontSize)
|
fontSize: Int = Constants.defaultFontSize)
|
||||||
: this (fontSize, prepareText(text, textColor, symbolColor))
|
: this (fontSize, prepareText(text, textColor, symbolColor))
|
||||||
|
|
||||||
|
/** Only if wrap was turned on, this is the prefWidth before.
|
||||||
|
* Used for getMaxWidth as better estimate than the default 0. */
|
||||||
|
private var unwrappedPrefWidth = 0f
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (fontSize != Constants.defaultFontSize) {
|
if (fontSize != Constants.defaultFontSize) {
|
||||||
val labelStyle = LabelStyle(style) // clone otherwise all default-styled Labels affected
|
val labelStyle = LabelStyle(style) // clone otherwise all default-styled Labels affected
|
||||||
@ -57,6 +62,31 @@ class ColorMarkupLabel private constructor(
|
|||||||
style.font.data.markupEnabled = originalMarkupEnabled
|
style.font.data.markupEnabled = originalMarkupEnabled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getPrefWidth(): Float {
|
||||||
|
if (!wrap) return super.getPrefWidth()
|
||||||
|
// Label has a Quirk that together with bad choices in Table become a bug:
|
||||||
|
// Label.getPrefWidth will always return 0 if wrap is on, and Table will NOT
|
||||||
|
// interpret that as "unknown" like it should but as "I want to be 0 wide".
|
||||||
|
super.getPrefHeight() // Ensure scaleAndComputePrefSize has been run
|
||||||
|
// private field prefWidth now has the correct value. However, there is no way
|
||||||
|
// to get it because temporarily turning wrap off (to circumvent the bad check)
|
||||||
|
// will invalidate which will run scaleAndComputePrefSize again without wrap...
|
||||||
|
val field = Label::class.java.getDeclaredField("prefWidth")
|
||||||
|
field.isAccessible = true
|
||||||
|
val result = field.getFloat(this)
|
||||||
|
// That prefWidth we got still might have to be wrapped in some background metrics
|
||||||
|
if (style.background == null) return result
|
||||||
|
return style.background.run { (result + leftWidth + rightWidth).coerceAtLeast(minWidth) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setWrap(wrap: Boolean) {
|
||||||
|
if (!this.wrap)
|
||||||
|
unwrappedPrefWidth = super.getPrefWidth()
|
||||||
|
super.setWrap(wrap)
|
||||||
|
}
|
||||||
|
override fun getMinWidth() = 48f
|
||||||
|
override fun getMaxWidth() = unwrappedPrefWidth // If unwrapped, we return 0 same as super
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private fun mapMarkup(text: String): String {
|
private fun mapMarkup(text: String): String {
|
||||||
val translated = text.tr()
|
val translated = text.tr()
|
||||||
@ -64,24 +94,12 @@ class ColorMarkupLabel private constructor(
|
|||||||
return translated.replace('«', '[').replace('»', ']')
|
return translated.replace('«', '[').replace('»', ']')
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Color.toMarkup() = when {
|
private val inverseColorMap = Colors.getColors().associate { it.value to it.key }
|
||||||
this == Color.CLEAR -> "CLEAR"
|
private fun Color.toMarkup(): String {
|
||||||
this == Color.BLACK -> "BLACK"
|
val mapEntry = inverseColorMap[this]
|
||||||
this == Color.BLUE -> "BLUE"
|
if (mapEntry != null) return mapEntry
|
||||||
this == Color.CYAN -> "CYAN"
|
if (a < 1f) return "#" + toString()
|
||||||
this == Color.GOLD -> "GOLD"
|
return "#" + toString().substring(0,6)
|
||||||
this == Color.GRAY -> "GRAY"
|
|
||||||
this == Color.GREEN -> "GREEN"
|
|
||||||
this == Color.LIME -> "LIME"
|
|
||||||
this == Color.NAVY -> "NAVY"
|
|
||||||
this == Color.PINK -> "PINK"
|
|
||||||
this == Color.RED -> "RED"
|
|
||||||
this == Color.SKY -> "SKY"
|
|
||||||
this == Color.TAN -> "TAN"
|
|
||||||
this == Color.TEAL -> "TEAL"
|
|
||||||
this == Color.WHITE -> "WHITE"
|
|
||||||
a < 1f -> "#" + toString()
|
|
||||||
else -> "#" + toString().substring(0,6)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun prepareText(text: String, textColor: Color, symbolColor: Color): String {
|
private fun prepareText(text: String, textColor: Color, symbolColor: Color): String {
|
||||||
|
@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table
|
|||||||
import com.badlogic.gdx.scenes.scene2d.ui.TextField
|
import com.badlogic.gdx.scenes.scene2d.ui.TextField
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter.DigitsOnlyFilter
|
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter.DigitsOnlyFilter
|
||||||
import com.badlogic.gdx.utils.Align
|
import com.badlogic.gdx.utils.Align
|
||||||
|
import com.unciv.UncivGame
|
||||||
import com.unciv.logic.map.MapGeneratedMainType
|
import com.unciv.logic.map.MapGeneratedMainType
|
||||||
import com.unciv.logic.map.MapParameters
|
import com.unciv.logic.map.MapParameters
|
||||||
import com.unciv.logic.map.MapResources
|
import com.unciv.logic.map.MapResources
|
||||||
@ -17,6 +18,7 @@ import com.unciv.logic.map.mapgenerator.MapGenerationRandomness
|
|||||||
import com.unciv.ui.components.ExpanderTab
|
import com.unciv.ui.components.ExpanderTab
|
||||||
import com.unciv.ui.components.UncivSlider
|
import com.unciv.ui.components.UncivSlider
|
||||||
import com.unciv.ui.components.UncivTextField
|
import com.unciv.ui.components.UncivTextField
|
||||||
|
import com.unciv.ui.components.WrappableLabel
|
||||||
import com.unciv.ui.components.extensions.onChange
|
import com.unciv.ui.components.extensions.onChange
|
||||||
import com.unciv.ui.components.extensions.onClick
|
import com.unciv.ui.components.extensions.onClick
|
||||||
import com.unciv.ui.components.extensions.pad
|
import com.unciv.ui.components.extensions.pad
|
||||||
@ -277,7 +279,11 @@ class MapParametersTable(
|
|||||||
|
|
||||||
if (forMapEditor) {
|
if (forMapEditor) {
|
||||||
val comment = "This is used for painting resources, not in map generator steps:"
|
val comment = "This is used for painting resources, not in map generator steps:"
|
||||||
add(comment.toLabel(Color.GOLD, 14, Align.center).apply { wrap=true }).colspan(2).row()
|
val expectedWidth = (UncivGame.Current.screen?.stage?.width ?: 1200f) * 0.4f
|
||||||
|
val label = WrappableLabel(comment, expectedWidth, Color.GOLD, 14)
|
||||||
|
label.setAlignment(Align.center)
|
||||||
|
label.wrap = true
|
||||||
|
add(label).colspan(2).row()
|
||||||
}
|
}
|
||||||
add("{Resource Setting}:".toLabel()).left()
|
add("{Resource Setting}:".toLabel()).left()
|
||||||
add(resourceSelectBox).fillX().row()
|
add(resourceSelectBox).fillX().row()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user