Add hover animation to icon buttons

This commit is contained in:
UnicornFreedom 2025-01-03 03:23:13 +01:00
parent 7a255ec631
commit 73d41071ee
2 changed files with 6 additions and 14 deletions

View File

@ -91,25 +91,17 @@ class DiskEditWindow(item: DiskItem) extends PanelWindow {
model = IconButton.ReadOnlyModel(isColorSelected),
)
{
private val backgroundAnimation = new ColorAnimation(targetBackgroundColor, IconButton.AnimationSpeed)
private val borderAnimation = new ColorAnimation(targetBorderColor, IconButton.AnimationSpeed)
private def targetBackgroundColor: Color =
if (isColorSelected) ColorScheme("FloppyIconButtonBackgroundSelected")
else ColorScheme("ButtonBackground")
private def targetBorderColor: Color =
if (isColorSelected) ColorScheme("FloppyIconButtonBorderSelected")
else ColorScheme("ButtonBorder")
override protected def updateAnimationTargets(): Unit = {
super.updateAnimationTargets()
backgroundAnimation.goto(targetBackgroundColor)
borderAnimation.goto(targetBorderColor)
}
override def backgroundColor: Color = backgroundAnimation.color
override def borderColor: Color = borderAnimation.color
override def onPressed(): Unit = {
@ -120,7 +112,6 @@ class DiskEditWindow(item: DiskItem) extends PanelWindow {
override def update(): Unit = {
super.update()
backgroundAnimation.update()
borderAnimation.update()
}
}

View File

@ -8,6 +8,7 @@ import ocelot.desktop.graphics.Graphics
import ocelot.desktop.ui.event.handlers.{ClickHandler, HoverHandler}
import ocelot.desktop.ui.event.{HoverEvent, MouseEvent}
import ocelot.desktop.ui.widget.IconButton.{AnimationSpeed, Mode}
import ocelot.desktop.ui.widget.`trait`.HoverAnimation
import ocelot.desktop.ui.widget.tooltip.LabelTooltip
import ocelot.desktop.util.animation.{ColorAnimation, ValueAnimation}
import ocelot.desktop.util.{DrawUtils, Spritesheet}
@ -26,8 +27,9 @@ class IconButton(
) extends Widget
with ClickHandler
with HoverHandler
with ClickSoundSource {
with HoverAnimation
with ClickSoundSource
{
override def receiveMouseEvents = true
private var prevModelValue = model.pressed
@ -96,12 +98,11 @@ class IconButton(
private val labelTooltip = tooltip.map(label => new LabelTooltip(label))
def backgroundColor: Color = ColorScheme("ButtonBackground")
def borderColor: Color = ColorScheme("ButtonBorder")
override def draw(g: Graphics): Unit = {
if (drawBackground) {
g.rect(bounds, backgroundColor)
g.rect(bounds, hoverAnimation.color)
DrawUtils.ring(g, position.x, position.y, width, height, 2, borderColor)
}