2021-02-19 04:12:43 +00:00
|
|
|
package net.shadowfacts.cacao.view
|
|
|
|
|
2021-02-27 04:23:11 +00:00
|
|
|
import net.minecraft.text.LiteralText
|
|
|
|
import net.minecraft.text.Text
|
2021-02-19 04:27:18 +00:00
|
|
|
import net.shadowfacts.cacao.window.Window
|
2021-02-19 04:12:43 +00:00
|
|
|
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(
|
2021-02-27 04:23:11 +00:00
|
|
|
val title: Text,
|
|
|
|
val message: Text,
|
|
|
|
val buttonTypes: Array<ButtonType>,
|
|
|
|
val iconTexture: Texture?,
|
|
|
|
val buttonCallback: (ButtonType, Window) -> Unit
|
2021-02-19 04:12:43 +00:00
|
|
|
): View() {
|
|
|
|
|
|
|
|
interface ButtonType {
|
2021-02-27 04:23:11 +00:00
|
|
|
val localizedName: Text
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class DefaultButtonType: ButtonType {
|
|
|
|
CANCEL, CONFIRM, OK, CLOSE;
|
|
|
|
|
2021-02-27 04:23:11 +00:00
|
|
|
override val localizedName: Text
|
2021-12-22 23:59:51 +00:00
|
|
|
get() = LiteralText(name.lowercase().replaceFirstChar(Char::titlecase)) // todo: actually localize me
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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, spacing = 8.0))
|
|
|
|
|
|
|
|
if (iconTexture != null) {
|
|
|
|
iconView = hStack.addArrangedSubview(TextureView(iconTexture))
|
|
|
|
}
|
|
|
|
|
|
|
|
vStack = hStack.addArrangedSubview(StackView(Axis.VERTICAL, spacing = 4.0))
|
|
|
|
|
|
|
|
vStack.addArrangedSubview(Label(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 175
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 04:23:11 +00:00
|
|
|
}
|