diff --git a/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala b/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala index b974f12..57160d7 100644 --- a/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala +++ b/src/main/scala/ocelot/desktop/ui/widget/TextInput.scala @@ -7,9 +7,9 @@ import ocelot.desktop.graphics.{Font, Graphics} import ocelot.desktop.ui.UiHandler import ocelot.desktop.ui.event.handlers.MouseHandler import ocelot.desktop.ui.event.sources.KeyEvents -import ocelot.desktop.ui.event.{ClickEvent, KeyEvent, MouseEvent} +import ocelot.desktop.ui.event.{KeyEvent, MouseEvent} import ocelot.desktop.ui.widget.traits.HoverAnimation -import ocelot.desktop.util.DrawUtils +import ocelot.desktop.util.{DrawUtils, Register} import ocelot.desktop.util.animation.ColorAnimation import org.lwjgl.input.Keyboard @@ -32,7 +32,8 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w private var isFocused = false private var scroll = 0f private var blinkTimer = 0f - private var prevEnabled = enabled + + private val enabledRegister = Register.sampling(enabled) private val foregroundAnimation = new ColorAnimation(targetForegroundColor, 7f) private val borderAnimation = new ColorAnimation(targetBorderColor, 7f) @@ -81,22 +82,22 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w // ------------------------------------------------------------------------------------------------------------------- override def minimumSize: Size2D = Size2D(200, 24) override def maximumSize: Size2D = Size2D(Float.PositiveInfinity, 24) - - override protected def receiveClickEvents: Boolean = true - override protected def receiveDragEvents: Boolean = true - override protected def allowClickReleaseOutsideThreshold: Boolean = false override def receiveAllMouseEvents = true - protected def font(): Font = Font.NormalFont + protected def font: Font = Font.NormalFont - private def charWidth(c: Char): Int = font().charWidth(c) + private def charWidth(c: Char): Int = font.charWidth(c) - private def charsWidth(chars: Array[Char]): Int = charsWidth(chars, 0, chars.length - 1) - - private def charsWidth(chars: Array[Char], first: Int, last: Int): Int = { + /** + * Calculates given text width in pixels. + * @param from inclusive + * @param to exclusive + */ + //noinspection SameParameterValue + private def charsWidth(chars: Array[Char], from: Int, to: Int): Int = { var width = 0 - for (index <- (first max 0) to (last min (chars.length - 1))) { - width += font().charWidth(chars(index)) + for (index <- (from max 0) until (to min chars.length)) { + width += font.charWidth(chars(index)) } width } @@ -107,9 +108,9 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w unfocus() } - case ClickEvent(MouseEvent.Button.Left, mouse) if enabled => + case MouseEvent(MouseEvent.State.Pressed, MouseEvent.Button.Left) if enabled => focus() - val absoluteX = mouse.x - bounds.x + scroll - 4 + val absoluteX = UiHandler.mousePosition.x - bounds.x + scroll - 4 var width = 0 var pos = 0 while (width < absoluteX && pos < chars.length) { @@ -195,7 +196,7 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w private def adjustCursor(position: Int): Unit = { cursorPos = position - cursorOffset = charsWidth(chars, 0, cursorPos - 1) + cursorOffset = charsWidth(chars, 0, cursorPos) blinkTimer = 0 adjustScroll() } @@ -210,22 +211,20 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w override def update(): Unit = { super.update() + // process state changes if (textChanged) { onInput(text) updateAnimationTargets() textChanged = false } - - val nextEnabled = enabled - if (nextEnabled != prevEnabled) { + if (enabledRegister.update()) { updateAnimationTargets() - prevEnabled = nextEnabled } - if (isFocused && !enabled) { unfocus() } + // update everything foregroundAnimation.update() borderAnimation.update() blinkTimer = (blinkTimer + UiHandler.dt) % CursorBlinkTime diff --git a/src/main/scala/ocelot/desktop/util/Register.scala b/src/main/scala/ocelot/desktop/util/Register.scala index 6bb2757..8b37097 100644 --- a/src/main/scala/ocelot/desktop/util/Register.scala +++ b/src/main/scala/ocelot/desktop/util/Register.scala @@ -1,7 +1,5 @@ package ocelot.desktop.util -import ocelot.desktop.ui.widget.Updatable - /** * Stores a value updated by calls to [[update]]. */