aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui')
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/FirmButtonComponent.kt15
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/FirmHoverComponent.kt59
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/ImageComponent.kt33
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/TickComponent.kt18
4 files changed, 119 insertions, 6 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/gui/FirmButtonComponent.kt b/src/main/kotlin/moe/nea/firmament/gui/FirmButtonComponent.kt
index d1e223e..09025af 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/FirmButtonComponent.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/FirmButtonComponent.kt
@@ -18,8 +18,9 @@ import io.github.notenoughupdates.moulconfig.observer.GetSetter
open class FirmButtonComponent(
child: GuiComponent,
val isEnabled: GetSetter<Boolean> = GetSetter.constant(true),
+ val noBackground: Boolean = false,
val action: Runnable,
-) : PanelComponent(child) {
+) : PanelComponent(child, if (noBackground) 0 else 2, DefaultBackgroundRenderer.TRANSPARENT) {
/* TODO: make use of vanillas built in nine slicer */
val hoveredBg =
@@ -44,7 +45,6 @@ open class FirmButtonComponent(
.cornerUv(5 / 200F, 5 / 20F)
.mode(NinePatch.Mode.STRETCHING)
.build()
-
var isClicking = false
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
if (!isEnabled.get()) return false
@@ -54,12 +54,14 @@ open class FirmButtonComponent(
if (context.isHovered) {
action.run()
}
+ blur()
return true
}
}
if (!context.isHovered) return false
if (mouseEvent !is MouseEvent.Click) return false
if (mouseEvent.mouseState && mouseEvent.mouseButton == 0) {
+ requestFocus()
isClicking = true
return true
}
@@ -73,10 +75,11 @@ open class FirmButtonComponent(
override fun render(context: GuiImmediateContext) {
context.renderContext.pushMatrix()
- context.renderContext.drawNinePatch(
- getBackground(context),
- 0f, 0f, context.width, context.height
- )
+ if (!noBackground)
+ context.renderContext.drawNinePatch(
+ getBackground(context),
+ 0f, 0f, context.width, context.height
+ )
context.renderContext.translate(insets.toFloat(), insets.toFloat(), 0f)
element.render(getChildContext(context))
context.renderContext.popMatrix()
diff --git a/src/main/kotlin/moe/nea/firmament/gui/FirmHoverComponent.kt b/src/main/kotlin/moe/nea/firmament/gui/FirmHoverComponent.kt
new file mode 100644
index 0000000..b1792ce
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/FirmHoverComponent.kt
@@ -0,0 +1,59 @@
+package moe.nea.firmament.gui
+
+import io.github.notenoughupdates.moulconfig.gui.GuiComponent
+import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
+import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent
+import io.github.notenoughupdates.moulconfig.gui.MouseEvent
+import java.util.function.BiFunction
+import java.util.function.Supplier
+import kotlin.time.Duration
+import moe.nea.firmament.util.TimeMark
+
+class FirmHoverComponent(
+ val child: GuiComponent,
+ val hoverLines: Supplier<List<String>>,
+ val hoverDelay: Duration,
+) : GuiComponent() {
+ override fun getWidth(): Int {
+ return child.width
+ }
+
+ override fun getHeight(): Int {
+ return child.height
+ }
+
+ override fun <T : Any?> foldChildren(
+ initial: T,
+ visitor: BiFunction<GuiComponent, T, T>
+ ): T {
+ return visitor.apply(child, initial)
+ }
+
+ override fun render(context: GuiImmediateContext) {
+ if (context.isHovered && (permaHover || lastMouseMove.passedTime() > hoverDelay)) {
+ context.renderContext.scheduleDrawTooltip(hoverLines.get())
+ permaHover = true
+ } else {
+ permaHover = false
+ }
+ if (!context.isHovered) {
+ lastMouseMove = TimeMark.now()
+ }
+ child.render(context)
+
+ }
+
+ var permaHover = false
+ var lastMouseMove = TimeMark.farPast()
+
+ override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
+ if (mouseEvent is MouseEvent.Move) {
+ lastMouseMove = TimeMark.now()
+ }
+ return child.mouseEvent(mouseEvent, context)
+ }
+
+ override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
+ return child.keyboardEvent(event, context)
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/ImageComponent.kt b/src/main/kotlin/moe/nea/firmament/gui/ImageComponent.kt
new file mode 100644
index 0000000..bba7dee
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/ImageComponent.kt
@@ -0,0 +1,33 @@
+package moe.nea.firmament.gui
+
+import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
+import io.github.notenoughupdates.moulconfig.gui.GuiComponent
+import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
+import java.util.function.Supplier
+
+class ImageComponent(
+ private val width: Int,
+ private val height: Int,
+ val resourceLocation: Supplier<MyResourceLocation>,
+ val u1: Float,
+ val u2: Float,
+ val v1: Float,
+ val v2: Float,
+) : GuiComponent() {
+ override fun getWidth(): Int {
+ return width
+ }
+
+ override fun getHeight(): Int {
+ return height
+ }
+
+ override fun render(context: GuiImmediateContext) {
+ context.renderContext.bindTexture(resourceLocation.get())
+ context.renderContext.drawTexturedRect(
+ 0f, 0f,
+ context.width.toFloat(), context.height.toFloat(),
+ u1, v1, u2, v2
+ )
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/TickComponent.kt b/src/main/kotlin/moe/nea/firmament/gui/TickComponent.kt
new file mode 100644
index 0000000..d1879b1
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/TickComponent.kt
@@ -0,0 +1,18 @@
+package moe.nea.firmament.gui
+
+import io.github.notenoughupdates.moulconfig.gui.GuiComponent
+import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
+
+class TickComponent(val onTick: Runnable) : GuiComponent() {
+ override fun getWidth(): Int {
+ return 0
+ }
+
+ override fun getHeight(): Int {
+ return 0
+ }
+
+ override fun render(context: GuiImmediateContext) {
+ onTick.run()
+ }
+}