Skip to content

Commit

Permalink
Make UIMaterialLayer anchorable (#1932)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Oct 11, 2023
1 parent e572ae3 commit 841f265
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
27 changes: 22 additions & 5 deletions korge/src/common/korlibs/korge/ui/UIMaterialLayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package korlibs.korge.ui
import korlibs.datastructure.*
import korlibs.image.color.*
import korlibs.korge.animate.*
import korlibs.korge.annotations.*
import korlibs.korge.internal.*
import korlibs.korge.render.*
import korlibs.korge.tween.*
import korlibs.korge.view.*
import korlibs.korge.view.property.*
import korlibs.math.geom.*
import korlibs.math.geom.collider.*
import korlibs.math.interpolation.*
import korlibs.time.*

Expand Down Expand Up @@ -48,9 +51,10 @@ class MaterialLayerHighlights(val view: View) {
}
}

@OptIn(KorgeInternal::class, KorgeExperimental::class)
class UIMaterialLayer(
size: Size = Size(100, 100),
) : UIView(size), ViewLeaf {
) : UIView(size), ViewLeaf, Anchorable {
@ViewProperty
var bgColor: RGBA = Colors.WHITE; set(value) { field = value; invalidateRender() }
@ViewProperty
Expand All @@ -74,14 +78,27 @@ class UIMaterialLayer(

private val highlights = MaterialLayerHighlights(this)

override var anchor: Anchor = Anchor.TOP_LEFT
set(value) {
if (field != value) {
field = value
invalidateLocalBounds()
}
}

@KorgeInternal override val anchorDispX: Float get() = (width * anchor.sx).toFloat()
@KorgeInternal override val anchorDispY: Float get() = (height * anchor.sy).toFloat()

override fun getLocalBoundsInternal(): Rectangle = Rectangle(-anchorDispX.toDouble(), -anchorDispY.toDouble(), width, height)

override fun renderInternal(ctx: RenderContext) {
if (!visible) return

renderCtx2d(ctx) { ctx2d ->
//println("context.multiplyColor=${ctx2d.multiplyColor}")
ctx2d.materialRoundRect(
x = 0.0,
y = 0.0,
x = -anchorDispX.toDouble(),
y = -anchorDispY.toDouble(),
width = width,
height = height,
color = bgColor,
Expand All @@ -98,8 +115,8 @@ class UIMaterialLayer(
)
highlights.fastForEach {
ctx2d.materialRoundRect(
x = 0.0,
y = 0.0,
x = -anchorDispX.toDouble(),
y = -anchorDispY.toDouble(),
width = width,
height = height,
color = Colors.TRANSPARENT,
Expand Down
16 changes: 16 additions & 0 deletions korge/test/common/korlibs/korge/ui/UIMaterialLayerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package korlibs.korge.ui

import korlibs.korge.view.*
import korlibs.math.geom.*
import kotlin.test.*

class UIMaterialLayerTest {
@Test
fun testBounds() {
val container = Container()
val layer = container.uiMaterialLayer(Size(100, 100))
assertEquals(Rectangle(0, 0, 100, 100).toInt(), layer.getBounds(container).toInt())
layer.anchor = Anchor.CENTER
assertEquals(Rectangle(-50, -50, 100, 100).toInt(), layer.getBounds(container).toInt())
}
}

0 comments on commit 841f265

Please sign in to comment.