Cacao: Convet Label to use Text

This commit is contained in:
Shadowfacts 2021-02-26 23:23:11 -05:00
parent ecfb696bc9
commit c1ecdacc06
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 45 additions and 36 deletions

View File

@ -1,5 +1,7 @@
package net.shadowfacts.cacao.view package net.shadowfacts.cacao.view
import net.minecraft.text.LiteralText
import net.minecraft.text.Text
import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.window.Window
import net.shadowfacts.cacao.geometry.Axis import net.shadowfacts.cacao.geometry.Axis
import net.shadowfacts.cacao.util.Color import net.shadowfacts.cacao.util.Color
@ -12,22 +14,22 @@ import net.shadowfacts.kiwidsl.dsl
* @author shadowfacts * @author shadowfacts
*/ */
class DialogView( class DialogView(
val title: String, val title: Text,
val message: String, val message: Text,
val buttonTypes: Array<ButtonType>, val buttonTypes: Array<ButtonType>,
val iconTexture: Texture?, val iconTexture: Texture?,
val buttonCallback: (ButtonType, Window) -> Unit val buttonCallback: (ButtonType, Window) -> Unit
): View() { ): View() {
interface ButtonType { interface ButtonType {
val localizedName: String val localizedName: Text
} }
enum class DefaultButtonType: ButtonType { enum class DefaultButtonType: ButtonType {
CANCEL, CONFIRM, OK, CLOSE; CANCEL, CONFIRM, OK, CLOSE;
override val localizedName: String override val localizedName: Text
get() = name.toLowerCase().capitalize() // todo: actually localize me get() = LiteralText(name.toLowerCase().capitalize()) // todo: actually localize me
} }
private lateinit var background: NinePatchView private lateinit var background: NinePatchView
@ -111,4 +113,4 @@ class DialogView(
} }
} }
} }

View File

@ -3,6 +3,8 @@ package net.shadowfacts.cacao.view
import net.minecraft.client.MinecraftClient import net.minecraft.client.MinecraftClient
import net.minecraft.client.font.TextRenderer import net.minecraft.client.font.TextRenderer
import net.minecraft.client.util.math.MatrixStack import net.minecraft.client.util.math.MatrixStack
import net.minecraft.text.OrderedText
import net.minecraft.text.Text
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.Color import net.shadowfacts.cacao.util.Color
@ -22,7 +24,7 @@ import net.shadowfacts.cacao.util.RenderHelper
* wrapping. * wrapping.
*/ */
class Label( class Label(
text: String, text: Text,
val shadow: Boolean = true, val shadow: Boolean = true,
val maxLines: Int = 0, val maxLines: Int = 0,
val wrappingMode: WrappingMode = WrappingMode.WRAP, val wrappingMode: WrappingMode = WrappingMode.WRAP,
@ -45,14 +47,14 @@ class Label(
/** /**
* The text of this label. Mutating this field will update the intrinsic content size and trigger a layout. * The text of this label. Mutating this field will update the intrinsic content size and trigger a layout.
*/ */
var text: String = text var text: Text = text
set(value) { set(value) {
field = value field = value
updateIntrinsicContentSize() updateIntrinsicContentSize()
// todo: setNeedsLayout instead of force unwrapping window // todo: setNeedsLayout instead of force unwrapping window
window!!.layout() window!!.layout()
} }
private lateinit var lines: List<String> private lateinit var lines: List<OrderedText>
var textColor = Color.WHITE var textColor = Color.WHITE
set(value) { set(value) {
@ -102,22 +104,11 @@ class Label(
} }
private fun computeLines() { private fun computeLines() {
var lines = text.split("\n") var lines = textRenderer.wrapLines(text, bounds.width.toInt())
if (wrappingMode == WrappingMode.WRAP) { if (maxLines > 0 && maxLines < lines.size) {
lines = lines.flatMap {
wrapStringToWidthAsList(it, bounds.width)
}
}
if (0 < maxLines && maxLines < lines.size) {
lines = lines.dropLast(lines.size - maxLines) lines = lines.dropLast(lines.size - maxLines)
} }
this.lines = lines this.lines = lines
} }
private fun wrapStringToWidthAsList(string: String, width: Double): List<String> { }
if (RenderHelper.disabled) return listOf(string)
// return textRenderer.wrapStringToWidthAsList(string, width.toInt())
TODO()
}
}

View File

@ -1,5 +1,6 @@
package net.shadowfacts.cacao.view.button package net.shadowfacts.cacao.view.button
import net.minecraft.text.Text
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.EnumHelper import net.shadowfacts.cacao.util.EnumHelper
import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.MouseButton
@ -15,7 +16,7 @@ import net.shadowfacts.cacao.view.Label
* @param initialValue The initial enum value for this button. * @param initialValue The initial enum value for this button.
* @param localizer A function that takes an enum value and converts into a string for the button's label. * @param localizer A function that takes an enum value and converts into a string for the button's label.
*/ */
class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> String): AbstractButton<EnumButton<E>>(Label(localizer(initialValue))) { class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> Text): AbstractButton<EnumButton<E>>(Label(localizer(initialValue))) {
private val label: Label private val label: Label
get() = content as Label get() = content as Label
@ -42,4 +43,4 @@ class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> String): Abs
return super.mouseClicked(point, mouseButton) return super.mouseClicked(point, mouseButton)
} }
} }

View File

@ -1,5 +1,7 @@
package net.shadowfacts.phycon.screen package net.shadowfacts.phycon.screen
import net.minecraft.client.MinecraftClient
import net.minecraft.text.LiteralText
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.shadowfacts.cacao.CacaoScreen import net.shadowfacts.cacao.CacaoScreen
import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.window.Window
@ -8,10 +10,8 @@ import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.Color import net.shadowfacts.cacao.util.Color
import net.shadowfacts.cacao.util.texture.NinePatchTexture import net.shadowfacts.cacao.util.texture.NinePatchTexture
import net.shadowfacts.cacao.util.texture.Texture import net.shadowfacts.cacao.util.texture.Texture
import net.shadowfacts.cacao.view.NinePatchView import net.shadowfacts.cacao.view.*
import net.shadowfacts.cacao.view.StackView import net.shadowfacts.cacao.view.button.Button
import net.shadowfacts.cacao.view.TextureView
import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.kiwidsl.dsl
@ -43,14 +43,26 @@ class TestCacaoScreen: CacaoScreen() {
backgroundColor = Color.RED backgroundColor = Color.RED
} }
val label = Label(LiteralText("Test"), wrappingMode = Label.WrappingMode.NO_WRAP).apply {
// textColor = Color.BLACK
}
// stack.addArrangedSubview(label)
val button = red.addSubview(Button(label))
view.solver.dsl { view.solver.dsl {
stack.topAnchor equalTo 0 stack.topAnchor equalTo 0
stack.centerXAnchor equalTo window!!.centerXAnchor stack.centerXAnchor equalTo window!!.centerXAnchor
stack.widthAnchor equalTo 100 stack.widthAnchor equalTo 100
button.centerXAnchor equalTo red.centerXAnchor
button.centerYAnchor equalTo red.centerYAnchor
// label.heightAnchor equalTo 9
button.heightAnchor equalTo 20
} }
} }
} }
addWindow(Window(viewController)) addWindow(Window(viewController))
} }
} }

View File

@ -1,5 +1,6 @@
package net.shadowfacts.cacao.view.button package net.shadowfacts.cacao.view.button
import net.minecraft.text.LiteralText
import net.shadowfacts.cacao.CacaoScreen import net.shadowfacts.cacao.CacaoScreen
import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.window.Window
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
@ -29,7 +30,9 @@ class EnumButtonTests {
} }
enum class MyEnum { enum class MyEnum {
ONE, TWO, THREE ONE, TWO, THREE;
val text = LiteralText(name)
} }
lateinit var screen: CacaoScreen lateinit var screen: CacaoScreen
@ -46,7 +49,7 @@ class EnumButtonTests {
} }
} }
window = screen.addWindow(Window(viewController)) window = screen.addWindow(Window(viewController))
button = viewController.view.addSubview(EnumButton(MyEnum.ONE, MyEnum::name)) button = viewController.view.addSubview(EnumButton(MyEnum.ONE, MyEnum::text))
window.solver.dsl { window.solver.dsl {
button.leftAnchor equalTo 0 button.leftAnchor equalTo 0
button.topAnchor equalTo 0 button.topAnchor equalTo 0
@ -92,4 +95,4 @@ class EnumButtonTests {
assertEquals(MyEnum.ONE, button.value) assertEquals(MyEnum.ONE, button.value)
} }
} }