2019-06-24 02:21:59 +00:00
|
|
|
package net.shadowfacts.cacao.view.button
|
|
|
|
|
|
|
|
import net.minecraft.util.Identifier
|
|
|
|
import net.shadowfacts.cacao.geometry.Point
|
|
|
|
import net.shadowfacts.cacao.geometry.Size
|
|
|
|
import net.shadowfacts.cacao.util.MouseButton
|
2019-06-26 23:32:06 +00:00
|
|
|
import net.shadowfacts.cacao.util.texture.Texture
|
2019-06-24 02:21:59 +00:00
|
|
|
import net.shadowfacts.cacao.view.TextureView
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A button for toggling between on/off states.
|
|
|
|
*
|
|
|
|
* @author shadowfacts
|
|
|
|
* @param initialState Whether the button starts as on or off.
|
|
|
|
*/
|
|
|
|
class ToggleButton(initialState: Boolean): AbstractButton<ToggleButton>(TextureView(if (initialState) ON else OFF).apply {
|
|
|
|
intrinsicContentSize = Size(19.0, 19.0)
|
|
|
|
}, padding = 0.0) {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val ON = Texture(Identifier("asmr", "textures/gui/toggle.png"), 0, 0)
|
|
|
|
val OFF = Texture(Identifier("asmr", "textures/gui/toggle.png"), 0, 19)
|
|
|
|
}
|
|
|
|
|
|
|
|
private val textureView: TextureView
|
|
|
|
get() = content as TextureView
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The button's current on/off state.
|
|
|
|
* Updating this property updates the button's texture.
|
|
|
|
*/
|
|
|
|
var state: Boolean = initialState
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
textureView.texture = if (value) ON else OFF
|
|
|
|
}
|
|
|
|
|
2019-06-24 02:34:12 +00:00
|
|
|
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
|
2019-06-24 02:21:59 +00:00
|
|
|
if (!disabled && (mouseButton == MouseButton.LEFT || mouseButton == MouseButton.RIGHT)) {
|
|
|
|
state = !state
|
|
|
|
}
|
|
|
|
|
2019-06-24 02:34:12 +00:00
|
|
|
return super.mouseClicked(point, mouseButton)
|
2019-06-24 02:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|