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
import net.minecraft.text.LiteralText
import net.minecraft.text.Text
import net.shadowfacts.cacao.window.Window
import net.shadowfacts.cacao.geometry.Axis
import net.shadowfacts.cacao.util.Color
@ -12,22 +14,22 @@ import net.shadowfacts.kiwidsl.dsl
* @author shadowfacts
*/
class DialogView(
val title: String,
val message: String,
val buttonTypes: Array<ButtonType>,
val iconTexture: Texture?,
val buttonCallback: (ButtonType, Window) -> Unit
val title: Text,
val message: Text,
val buttonTypes: Array<ButtonType>,
val iconTexture: Texture?,
val buttonCallback: (ButtonType, Window) -> Unit
): View() {
interface ButtonType {
val localizedName: String
val localizedName: Text
}
enum class DefaultButtonType: ButtonType {
CANCEL, CONFIRM, OK, CLOSE;
override val localizedName: String
get() = name.toLowerCase().capitalize() // todo: actually localize me
override val localizedName: Text
get() = LiteralText(name.toLowerCase().capitalize()) // todo: actually localize me
}
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.font.TextRenderer
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.Size
import net.shadowfacts.cacao.util.Color
@ -22,7 +24,7 @@ import net.shadowfacts.cacao.util.RenderHelper
* wrapping.
*/
class Label(
text: String,
text: Text,
val shadow: Boolean = true,
val maxLines: Int = 0,
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.
*/
var text: String = text
var text: Text = text
set(value) {
field = value
updateIntrinsicContentSize()
// todo: setNeedsLayout instead of force unwrapping window
window!!.layout()
}
private lateinit var lines: List<String>
private lateinit var lines: List<OrderedText>
var textColor = Color.WHITE
set(value) {
@ -102,22 +104,11 @@ class Label(
}
private fun computeLines() {
var lines = text.split("\n")
if (wrappingMode == WrappingMode.WRAP) {
lines = lines.flatMap {
wrapStringToWidthAsList(it, bounds.width)
}
}
if (0 < maxLines && maxLines < lines.size) {
var lines = textRenderer.wrapLines(text, bounds.width.toInt())
if (maxLines > 0 && maxLines < lines.size) {
lines = lines.dropLast(lines.size - maxLines)
}
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
import net.minecraft.text.Text
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.EnumHelper
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 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
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)
}
}
}

View File

@ -1,5 +1,7 @@
package net.shadowfacts.phycon.screen
import net.minecraft.client.MinecraftClient
import net.minecraft.text.LiteralText
import net.minecraft.util.Identifier
import net.shadowfacts.cacao.CacaoScreen
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.texture.NinePatchTexture
import net.shadowfacts.cacao.util.texture.Texture
import net.shadowfacts.cacao.view.NinePatchView
import net.shadowfacts.cacao.view.StackView
import net.shadowfacts.cacao.view.TextureView
import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.view.*
import net.shadowfacts.cacao.view.button.Button
import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.kiwidsl.dsl
@ -43,14 +43,26 @@ class TestCacaoScreen: CacaoScreen() {
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 {
stack.topAnchor equalTo 0
stack.centerXAnchor equalTo window!!.centerXAnchor
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))
}
}
}

View File

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