Skip to content

Commit

Permalink
revert change that broke animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Stivais committed Nov 8, 2024
1 parent 95a2c4d commit c12b33b
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ class Animatable(
return from.get(element, type)
}

override fun animate(duration: Float, type: Animations): Animation? {
if (duration == 0f) {
swap()
} else {
if (animation != null) {
before = current
swap()
animation = Animation(duration * animation!!.get(), type)
} else {
animation = Animation(duration, type)
}
}
return animation
}

override fun swap() {
val temp = to
to = from
from = temp
}

override fun reliesOnChild(): Boolean {
return from.reliesOnChild() || to.reliesOnChild()
}
Expand Down
24 changes: 4 additions & 20 deletions src/main/kotlin/com/github/stivais/ui/elements/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.stivais.ui.UI.Companion.logger
import com.github.stivais.ui.color.Color
import com.github.stivais.ui.constraints.Constraints
import com.github.stivais.ui.constraints.Type
import com.github.stivais.ui.constraints.measurements.Animatable
import com.github.stivais.ui.constraints.measurements.Undefined
import com.github.stivais.ui.constraints.positions.Center
import com.github.stivais.ui.elements.scope.ElementScope
Expand Down Expand Up @@ -58,20 +57,6 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
field = value.coerceAtLeast(0f)
}

//


// todo: move to different element
var scrollY: Animatable.Raw? = null

var sy = 0f
set(value) {
if (field == value) return
redraw = true
field = value
}
//

private var transforms: ArrayList<Transforms>? = null

// this is needed to track current scale
Expand All @@ -98,7 +83,7 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
if (!enabled) return
preSize()
if (!constraints.width.reliesOnChild()) width = constraints.width.get(this, Type.W)
if (!constraints.height.reliesOnChild()) height = constraints.height.get(this, Type.H) + sy
if (!constraints.height.reliesOnChild()) height = constraints.height.get(this, Type.H)
elements?.loop { it.size() }
}

Expand All @@ -113,12 +98,11 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
it.position(x, y)
it.positionChildren()
}
// this causes it to redraw every frame, it will be fixed when official repo comes out with code reorganization
val widthRelies = constraints.width.reliesOnChild()
val heightRelies = constraints.height.reliesOnChild()

if (widthRelies) width = constraints.width.get(this, Type.W)
if (heightRelies) height = constraints.height.get(this, Type.H) + sy

if (heightRelies) height = constraints.height.get(this, Type.H)
if (widthRelies || heightRelies) parent?.redrawInternal = true
}

Expand Down Expand Up @@ -268,7 +252,7 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
fun isInside(x: Float, y: Float): Boolean {
val tx = this.x
val ty = this.y
return x in tx..tx + (width) * scale && y in ty..ty + (height - sy) * scale
return x in tx..tx + (width) * scale && y in ty..ty + (height) * scale
}

fun intersects(other: Element): Boolean {
Expand Down
78 changes: 22 additions & 56 deletions src/main/kotlin/com/github/stivais/ui/elements/impl/Column.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.github.stivais.ui.elements.impl

import com.github.stivais.ui.color.Color
import com.github.stivais.ui.constraints.Constraints
import com.github.stivais.ui.constraints.Size
import com.github.stivais.ui.constraints.Type
import com.github.stivais.ui.constraints.*
import com.github.stivais.ui.constraints.measurements.Undefined
import com.github.stivais.ui.constraints.plus
import com.github.stivais.ui.constraints.sizes.Bounding
import com.github.stivais.ui.constraints.sizes.Copying
import com.github.stivais.ui.elements.Element
import com.github.stivais.ui.elements.scope.ElementScope
import com.github.stivais.ui.elements.scope.ScopeDSL
import com.github.stivais.ui.utils.loop
import com.github.stivais.ui.utils.replaceUndefined

// rework start
class Column(
constraints: Constraints?,
constraints: Constraints,
private val paddingX: Size?,
private val paddingY: Size?,
) : Element(constraints?.replaceUndefined(w = Bounding, h = Bounding)) {
) : Element(constraints.replaceUndefined(w = Bounding, h = Bounding)) {

private val positionedElements = hashSetOf<Element>()

Expand Down Expand Up @@ -51,60 +50,27 @@ class Column(
it.positionChildren()
}

if (constraints.width.reliesOnChild()) width = constraints.width.get(this, Type.W)
if (constraints.height.reliesOnChild()) height = constraints.height.get(this, Type.H) + sy
val widthRelies = constraints.width.reliesOnChild()
val heightRelies = constraints.height.reliesOnChild()
if (widthRelies) width = constraints.width.get(this, Type.W)
if (heightRelies) height = constraints.height.get(this, Type.H)
if (widthRelies || heightRelies) parent?.redrawInternal = true
}

override fun draw() {
renderer.hollowRect(x, y, width, height, 1f, Color.WHITE.rgba)
}
}


class Row(
constraints: Constraints?,
private val paddingX: Size?,
private val paddingY: Size?,
) : Element(constraints?.replaceUndefined(w = Bounding, h = Bounding)) {

private val positionedElements = hashSetOf<Element>()

init {
this.constraints.apply {
width += paddingX
height += paddingY
}
}

override fun onElementAdded(element: Element) {
val constraints = element.constraints
if (constraints.x is Undefined) {
positionedElements.add(element)
}
}

override fun positionChildren() {
if (!enabled) return

val px = paddingX?.get(this, Type.W) ?: 0f
val py = paddingY?.get(this, Type.H) ?: 0f

var increment = 0f
elements?.loop {
if (positionedElements.contains(it)) {
it.position(x + px + increment, y + py)
increment += (it.width * it.scale) + py
} else {
it.position(x + px, y + py)
}
it.positionChildren()
}

if (constraints.width.reliesOnChild()) width = constraints.width.get(this, Type.W)
if (constraints.height.reliesOnChild()) height = constraints.height.get(this, Type.H) + sy
}

override fun draw() {
renderer.hollowRect(x, y, width, height, 1f, Color.WHITE.rgba)
}
class ColumnScope(column: Column) : ElementScope<Column>(column) {
@ScopeDSL
fun section(
size: Size,
block: ElementScope<Group>.() -> Unit
) = group(size(Copying, size), block)

@ScopeDSL
fun divider(
size: Size,
) = group(size(Copying, size))
}
7 changes: 5 additions & 2 deletions src/main/kotlin/com/github/stivais/ui/elements/impl/Grid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ class Grid(
it.positionChildren()
}

if (constraints.width.reliesOnChild()) width = constraints.width.get(this, Type.W)
if (constraints.height.reliesOnChild()) height = constraints.height.get(this, Type.H) + sy
val widthRelies = constraints.width.reliesOnChild()
val heightRelies = constraints.height.reliesOnChild()
if (widthRelies) width = constraints.width.get(this, Type.W)
if (heightRelies) height = constraints.height.get(this, Type.H)
if (widthRelies || heightRelies) parent?.redrawInternal = true
}

override fun draw() {
Expand Down
63 changes: 63 additions & 0 deletions src/main/kotlin/com/github/stivais/ui/elements/impl/Row.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.stivais.ui.elements.impl

import com.github.stivais.ui.color.Color
import com.github.stivais.ui.constraints.Constraints
import com.github.stivais.ui.constraints.Size
import com.github.stivais.ui.constraints.Type
import com.github.stivais.ui.constraints.measurements.Undefined
import com.github.stivais.ui.constraints.plus
import com.github.stivais.ui.constraints.sizes.Bounding
import com.github.stivais.ui.elements.Element
import com.github.stivais.ui.utils.loop
import com.github.stivais.ui.utils.replaceUndefined

class Row(
constraints: Constraints?,
private val paddingX: Size?,
private val paddingY: Size?,
) : Element(constraints?.replaceUndefined(w = Bounding, h = Bounding)) {

private val positionedElements = hashSetOf<Element>()

init {
this.constraints.apply {
width += paddingX
height += paddingY
}
}

override fun onElementAdded(element: Element) {
val constraints = element.constraints
if (constraints.x is Undefined) {
positionedElements.add(element)
}
}

override fun positionChildren() {
if (!enabled) return

val px = paddingX?.get(this, Type.W) ?: 0f
val py = paddingY?.get(this, Type.H) ?: 0f

var increment = 0f
elements?.loop {
if (positionedElements.contains(it)) {
it.position(x + px + increment, y + py)
increment += (it.width * it.scale) + py
} else {
it.position(x + px, y + py)
}
it.positionChildren()
}

val widthRelies = constraints.width.reliesOnChild()
val heightRelies = constraints.height.reliesOnChild()
if (widthRelies) width = constraints.width.get(this, Type.W)
if (heightRelies) height = constraints.height.get(this, Type.H)
if (widthRelies || heightRelies) parent?.redrawInternal = true
}

override fun draw() {
renderer.hollowRect(x, y, width, height, 1f, Color.WHITE.rgba)
}
}
Loading

0 comments on commit c12b33b

Please sign in to comment.