Start DialogView

This commit is contained in:
Shadowfacts 2019-06-27 19:12:35 -04:00
parent 91c512c4e9
commit db06b454b1
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 128 additions and 0 deletions

View File

@ -41,6 +41,18 @@ class TestCacaoScreen: CacaoScreen() {
).apply {
handler = {
println("dropdown value: ${it.value}")
val dialog = DialogView(
"New redstone mode",
it.value.name,
arrayOf(DialogView.DefaultButtonType.OK),
Texture(Identifier("textures/block/birch_log_top.png"), 0, 0, 16, 16),
buttonCallback = { _, window ->
window.removeFromScreen()
}
)
val dialogWindow = Window()
dialogWindow.addView(dialog)
this@TestCacaoScreen.addWindow(dialogWindow)
}
})

View File

@ -38,6 +38,7 @@ open class CacaoScreen: Screen(TextComponent("CacaoScreen")) {
fun <T: Window> addWindow(window: T, index: Int = _windows.size): T {
_windows.add(index, window)
window.screen = this
window.resize(width, height)
return window
}

View File

@ -0,0 +1,115 @@
package net.shadowfacts.cacao.view
import net.minecraft.ChatFormat
import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Axis
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.button.Button
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
): View() {
interface ButtonType {
val localizedName: String
}
enum class DefaultButtonType: ButtonType {
CANCEL, CONFIRM, OK, CLOSE;
override val localizedName: String
get() = name.toLowerCase().capitalize() // todo: actually localize me
}
private lateinit var background: NinePatchView
private lateinit var hStack: StackView
private var iconView: TextureView? = null
private lateinit var vStack: StackView
private lateinit var messageLabel: Label
private var buttonContainer: View? = null
private var buttonStack: StackView? = null
override fun wasAdded() {
background = addSubview(NinePatchView(NinePatchTexture.PANEL_BG).apply { zIndex = -1.0 })
hStack = addSubview(StackView(Axis.HORIZONTAL, StackView.Distribution.LEADING))
if (iconTexture != null) {
iconView = hStack.addArrangedSubview(TextureView(iconTexture))
}
vStack = hStack.addArrangedSubview(StackView(Axis.VERTICAL))
vStack.addArrangedSubview(Label(ChatFormat.BOLD.toString() + title, shadow = false).apply {
textColor = Color(0x404040)
})
messageLabel = vStack.addArrangedSubview(Label(message, shadow = false).apply {
textColor = Color(0x404040)
})
if (buttonTypes.isNotEmpty()) {
buttonContainer = vStack.addArrangedSubview(View())
buttonStack = buttonContainer!!.addSubview(StackView(Axis.HORIZONTAL))
for (type in buttonTypes) {
buttonStack!!.addArrangedSubview(Button(Label(type.localizedName)).apply {
handler = {
this@DialogView.buttonCallback(type, this@DialogView.window)
}
})
}
}
super.wasAdded()
}
override fun createInternalConstraints() {
super.createInternalConstraints()
solver.dsl {
centerXAnchor equalTo window.centerXAnchor
centerYAnchor equalTo window.centerYAnchor
widthAnchor greaterThanOrEqualTo 150
background.leftAnchor equalTo leftAnchor - 8
background.rightAnchor equalTo rightAnchor + 8
background.topAnchor equalTo topAnchor - 8
background.bottomAnchor equalTo bottomAnchor + 8
hStack.leftAnchor equalTo leftAnchor
hStack.rightAnchor equalTo rightAnchor
hStack.topAnchor equalTo topAnchor
hStack.bottomAnchor equalTo bottomAnchor
if (iconView != null) {
hStack.bottomAnchor greaterThanOrEqualTo iconView!!.bottomAnchor
}
hStack.bottomAnchor greaterThanOrEqualTo vStack.bottomAnchor
if (iconView != null) {
iconView!!.widthAnchor equalTo 30
iconView!!.heightAnchor equalTo 30
}
messageLabel.heightAnchor greaterThanOrEqualTo 50
if (buttonContainer != null) {
buttonStack!!.heightAnchor equalTo buttonContainer!!.heightAnchor
buttonStack!!.centerYAnchor equalTo buttonContainer!!.centerYAnchor
buttonStack!!.rightAnchor equalTo buttonContainer!!.rightAnchor
}
}
}
}