Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes UIWindow scale handlers + improve close button icon #1776

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed docs/korge/reference/ui/img_23.png
Binary file not shown.
Binary file added docs/korge/reference/ui/img_24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/korge/reference/ui/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ uiImage(Size(120f, 32f), KR.korge.read().slice(), scaleMode = ScaleMode.FIT, con

A draggable, resizable and closeable window.

![img_23.png](img_23.png)
![img_24.png](img_24.png)

## `UIProgressBar`

Expand Down
22 changes: 22 additions & 0 deletions korge/src/commonMain/kotlin/korlibs/korge/ui/UIIcons.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package korlibs.korge.ui

import korlibs.image.atlas.*
import korlibs.image.bitmap.*
import korlibs.image.color.*
import korlibs.image.vector.*
import korlibs.math.geom.*

object UIIcons {
val atlas = MutableAtlasUnit()

fun createIcon(size: Size = Size(32, 32), block: Context2d.() -> Unit): BmpSlice =
atlas.add(Bitmap32(size.width.toInt(), size.height.toInt()).context2d { block() }).slice

val CROSS = createIcon {
val padding = 8
stroke(Colors.WHITE, lineWidth = 4f) {
line(Point(padding, padding), Point(32 - padding, 32 - padding))
line(Point(32 - padding, padding), Point(padding, 32 - padding))
}
}
}
17 changes: 14 additions & 3 deletions korge/src/commonMain/kotlin/korlibs/korge/ui/UIWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import korlibs.image.color.*
import korlibs.image.text.*
import korlibs.korge.annotations.*
import korlibs.korge.input.*
import korlibs.korge.internal.*
import korlibs.korge.render.*
import korlibs.korge.tween.*
import korlibs.korge.view.*
Expand All @@ -14,6 +15,7 @@ import korlibs.memory.*
import korlibs.render.*
import korlibs.time.*


@KorgeExperimental
inline fun Container.uiWindow(
title: String,
Expand Down Expand Up @@ -52,7 +54,7 @@ class UIWindow(title: String, size: Size = Size(256, 256)) : UIContainer(size) {
})
private val titleContainer = fixedSizeContainer(Size(width, titleHeight))
private val titleView = titleContainer.textBlock(RichTextData(title), align = TextAlignment.MIDDLE_LEFT).xy(12, 0).size(width, titleHeight)
private val closeButton = titleContainer.uiButton("X", size = Size(titleHeight - buttonSeparation * 2, titleHeight - buttonSeparation * 2)) {
private val closeButton = titleContainer.uiButton(icon = UIIcons.CROSS, size = Size(titleHeight - buttonSeparation * 2, titleHeight - buttonSeparation * 2)) {
radiusRatio = Ratio.ONE
elevation = false
bgColorOut = MaterialColors.RED_600
Expand All @@ -72,8 +74,10 @@ class UIWindow(title: String, size: Size = Size(256, 256)) : UIContainer(size) {
class ScaleHandler(val window: UIWindow, val anchor: Anchor) {
val isCorner = (anchor.doubleX == anchor.doubleY)

@OptIn(KorgeUntested::class)
val view = window.solidRect(Size.ZERO, Colors.TRANSPARENT) {
val sh = this
val anchor = [email protected]
anchor(Anchor.CENTER)
cursor = GameWindow.Cursor.fromAnchorResize(anchor)
// @TODO: clamping shouldn't affect (we should use it.start and get initial values to compute based on start and not on deltas)
Expand All @@ -88,7 +92,7 @@ class UIWindow(title: String, size: Size = Size(256, 256)) : UIContainer(size) {
}
}

anchor.floatY > 0.5f -> {
anchor.floatX > 0.5f -> {
bounds = bounds.copyBounds(right = it.cx)
bounds = bounds.copy(width = bounds.width.clamp(window.minWidth, window.maxWidth))
}
Expand All @@ -110,7 +114,10 @@ class UIWindow(title: String, size: Size = Size(256, 256)) : UIContainer(size) {

else -> Unit
}
window.setGlobalBounds(bounds.immutable)
window.setGlobalBounds(bounds)
if (it.end) {
resized()
}
}
}

Expand All @@ -125,6 +132,10 @@ class UIWindow(title: String, size: Size = Size(256, 256)) : UIContainer(size) {
else -> 0f
}

private fun resized() {
resized(window.widthD, window.heightD)
}

fun resized(width: Double, height: Double) {
view
.position(getExpectedX(), getExpectedY())
Expand Down