Skip to content

Commit

Permalink
More work on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Nov 21, 2022
1 parent 6d88cdd commit ced7467
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 29 deletions.
4 changes: 2 additions & 2 deletions korge-sandbox/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ suspend fun main() = Korge(
//Demo(::MainAnimations),
//Demo(::MainCache),
//Demo(::MainEditor),
//Demo(::MainUI),
Demo(::MainTextMetrics),
Demo(::MainUI),
//Demo(::MainTextMetrics),
//Demo(::MainBunnymark),
//Demo(::MainBlur),
//Demo(::MainSDF),
Expand Down
3 changes: 2 additions & 1 deletion korge/src/commonMain/kotlin/com/soywiz/korge/ui/UIButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ open class UIButton(
var skin: UISkin? get() = uiSkin ; set(value) { uiSkin = value }

var forcePressed = false
var radius = 6.pt
var radius: Length = 6.pt
set(value) {
field = value
setInitialState()
Expand Down Expand Up @@ -161,6 +161,7 @@ open class UIButton(
val textView = textBlock(richText ?: RichTextData(text, font = DefaultTtfFontAsBitmap), align = TextAlignment.MIDDLE_CENTER)

@ViewProperty
@ViewPropertyProvider(TextAlignment.Provider::class)
var textAlignment: TextAlignment by textView::align

protected val iconView = image(Bitmaps.transparent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ open class UIComboBox<T>(
matchesFilter(index) -> {
val highlightColor = MaterialColors.BLUE_800
RichTextData.fromHTML(itemText.htmlspecialchars().replace(Regex(Regex.escapeReplacement(filter), RegexOption.IGNORE_CASE)) {
"<b><span color='${highlightColor.hexString}'>${it.value}</span></b>"
"<span color='${highlightColor.hexString}'>${it.value}</span>"
}, RichTextData.Style(color = Colors.BLACK, font = DefaultTtfFontAsBitmap))
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ interface UIFocusable {
var UIFocusable.focused: Boolean
get() = UIFocusManager.Scope.focusView.stage?.uiFocusedView == this
set(value) {
if (value) {
UIFocusManager.Scope.focusView.stage?.uiFocusManager?.uiFocusedView = this
} else {
UIFocusManager.Scope.focusView.stage?.uiFocusManager?.uiFocusedView = null
}
UIFocusManager.Scope.focusView.stage?.uiFocusManager?.uiFocusedView = if (value) this else null
}

@ThreadLocal
Expand Down
2 changes: 1 addition & 1 deletion korge/src/commonMain/kotlin/com/soywiz/korge/view/Text.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ open class Text(
_renderInternal(null)
if (cachedVersionGlyphMetrics != version) {
cachedVersionGlyphMetrics = version
_textMetricsResult = font.getOrNull()?.getTextBoundsWithGlyphs(fontSize, text, renderer)
_textMetricsResult = font.getOrNull()?.getTextBoundsWithGlyphs(fontSize, text, renderer, alignment)
}
return _textMetricsResult ?: error("Must ensure font is resolved before calling getGlyphMetrics")
}
Expand Down
17 changes: 6 additions & 11 deletions korim/src/commonMain/kotlin/com/soywiz/korim/font/Font.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,12 @@ data class PlacedGlyphMetrics constructor(
val boundsPath: VectorPath by lazy {
buildVectorPath {
this.optimize = false
val rect = Rectangle().copyFrom(metrics.bounds)
rect.y = -fontMetrics.ascent
rect.height = fontMetrics.ascent - fontMetrics.descent

//println("rect=$rect, ascent=${fontMetrics.ascent}, descent=${fontMetrics.descent}")

//rect.y = -rect.y
//rect.height = -rect.height
//rect.y = -rect.y
//rect.height = -rect.height
rect(rect)
rect(
metrics.left,
-fontMetrics.ascent,
metrics.xadvance,
fontMetrics.lineHeight
)
}.applyTransform(Matrix().translate(x, y).premultiply(transform))
}
val boundsPathCurves: Curves by lazy {
Expand Down
14 changes: 6 additions & 8 deletions korim/src/commonMain/kotlin/com/soywiz/korim/font/Metrics.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.soywiz.korim.font

import com.soywiz.kmem.toIntRound
import com.soywiz.korio.util.niceStr
import com.soywiz.korma.geom.Rectangle
import com.soywiz.korma.math.*
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
import com.soywiz.korio.util.*
import com.soywiz.korma.geom.*
import kotlin.math.*

/**
* ... [top] (Positive)
Expand Down Expand Up @@ -50,8 +46,10 @@ data class FontMetrics(
/** Bottom-most part of the glyphs (without the [lineGap] between lines) */
val rbottom: Double get() = min(descent, bottom)

val lineHeightWithoutGap: Double get() = rtop - rbottom

/** Total size of a line including from [top] to [bottom] + [lineGap] */
val lineHeight: Double get() = rtop - rbottom + lineGap // Including gap!
val lineHeight: Double get() = lineHeightWithoutGap + lineGap // Including gap!

fun copyFrom(other: FontMetrics): FontMetrics = this.copyFromScaled(other, 1.0)
fun copyFromNewSize(other: FontMetrics, size: Double): FontMetrics = this.copyFromScaled(other, size / other.size)
Expand Down

0 comments on commit ced7467

Please sign in to comment.