Better animation

This commit is contained in:
LeshaInc 2019-09-07 20:40:29 +03:00
parent 95575b243f
commit c3a7ab5235
No known key found for this signature in database
GPG Key ID: B4855290FC36DE72
2 changed files with 51 additions and 12 deletions

View File

@ -136,6 +136,10 @@ class Graphics extends Logging {
stack.head.transform = stack.head.transform >> t stack.head.transform = stack.head.transform >> t
} }
def scale(v: Float): Unit = {
transform(Transform2D.scale(v))
}
def translate(x: Float, y: Float): Unit = { def translate(x: Float, y: Float): Unit = {
transform(Transform2D.translate(x, y)) transform(Transform2D.translate(x, y))
} }

View File

@ -3,16 +3,12 @@ package ocelot.desktop.ui.widget
import ocelot.desktop.color.RGBAColor import ocelot.desktop.color.RGBAColor
import ocelot.desktop.geometry.{Padding2D, Size2D, Vector2D} import ocelot.desktop.geometry.{Padding2D, Size2D, Vector2D}
import ocelot.desktop.graphics.Graphics import ocelot.desktop.graphics.Graphics
import ocelot.desktop.ui.UiHandler
import ocelot.desktop.ui.layout.LinearLayout import ocelot.desktop.ui.layout.LinearLayout
import ocelot.desktop.ui.workspace.ScreenNode import ocelot.desktop.ui.workspace.ScreenNode
import ocelot.desktop.util.{Animation, Orientation} import ocelot.desktop.util.{Animation, Orientation}
class NodeSelector extends Widget { class NodeSelector extends Widget {
private class Row extends Widget {
override val layout = new LinearLayout(this, Orientation.Horizontal)
}
override val layout = new LinearLayout(this) override val layout = new LinearLayout(this)
private val rowsWidget: Widget = new Widget { private val rowsWidget: Widget = new Widget {
@ -29,47 +25,83 @@ class NodeSelector extends Widget {
// noinspection ScalaUnusedSymbol // noinspection ScalaUnusedSymbol
private def rows_=(rows: Array[Widget]): Unit = rowsWidget.children = rows private def rows_=(rows: Array[Widget]): Unit = rowsWidget.children = rows
private var numNodes: Int = 0
private def add(widget: Widget): Unit = { private def add(widget: Widget): Unit = {
val row = if (rows.lastOption.exists(row => row.children.length < 4)) { val row = if (rows.lastOption.exists(row => row.children.length < 4)) {
rows.last rows.last
} else { } else {
val row = new Row() val row = new Row(rows.length)
rows :+= row rows :+= row
row row
} }
row.children :+= widget row.children :+= widget
numNodes += 1
} }
for (_ <- 0 to 100) for (_ <- 0 to 100)
add(new ScreenNode()) add(new ScreenNode())
private var _isShown: Boolean = false private var _isShown: Boolean = false
private var isClosing: Boolean = false
private var width = 0f private var width = 0f
private var finalHeight = 0f private var finalHeight = 0f
private var heightAnimation: Animation = _ private var heightAnimation: Animation = _
private var animationTime: Float = 0f
def isShown: Boolean = _isShown def isShown: Boolean = _isShown
override def update(): Unit = { override def update(): Unit = {
if (isShown) { if (isShown) {
heightAnimation.update() heightAnimation.update()
if (isClosing) {
animationTime -= UiHandler.dt
} else {
animationTime += UiHandler.dt
}
size = Size2D(width, heightAnimation.value * finalHeight) size = Size2D(width, heightAnimation.value * finalHeight)
if (size.height < 1) if (size.height < 1) {
_isShown = false _isShown = false
isClosing = false
}
} }
super.update() super.update()
} }
override def draw(g: Graphics): Unit = { override def draw(g: Graphics): Unit = {
if (isShown) { if (!isShown) return
g.foreground = RGBAColor(150, 150, 150, 60) g.foreground = RGBAColor(150, 150, 150, 60)
g.sprite = "Empty" g.sprite = "Empty"
g.rect(position.x, position.y, size.width, size.height) g.rect(position.x, position.y, size.width, size.height)
super.draw(g) super.draw(g)
} }
private class Row(rowIdx: Int) extends Widget {
override val layout = new LinearLayout(this, Orientation.Horizontal)
override def drawChildren(g: Graphics): Unit = {
if (!isShown) return
for ((child, i) <- children.zipWithIndex) {
val idx = rowIdx * 4 + i
val timeOffset = idx.toFloat * 0.02f
g.save()
val sx = child.position.x + child.size.width / 2
val sy = child.position.y + child.size.height / 2
g.translate(sx, sy)
g.scale(((animationTime - timeOffset) / 0.1f).max(0f).min(1f))
g.translate(-sx, -sy)
child.draw(g)
g.restore()
}
}
} }
def show(at: Vector2D): Unit = { def show(at: Vector2D): Unit = {
@ -77,6 +109,7 @@ class NodeSelector extends Widget {
position = at position = at
heightAnimation = Animation.easeOutQuad(0.2f) heightAnimation = Animation.easeOutQuad(0.2f)
heightAnimation.goUp() heightAnimation.goUp()
animationTime = 0f
width = inner.minimumSize.width width = inner.minimumSize.width
finalHeight = inner.minimumSize.height.min(250) finalHeight = inner.minimumSize.height.min(250)
} }
@ -85,5 +118,7 @@ class NodeSelector extends Widget {
heightAnimation = Animation.easeInQuad(0.1f) heightAnimation = Animation.easeInQuad(0.1f)
heightAnimation.time = 1 heightAnimation.time = 1
heightAnimation.goDown() heightAnimation.goDown()
animationTime = 0.16f
isClosing = true
} }
} }