Skip to content

Commit

Permalink
Number constructors. (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kietyo committed Feb 14, 2024
1 parent df41c99 commit 3e92eb8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
11 changes: 6 additions & 5 deletions korge-core/src/korlibs/image/font/BitmapFont.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,22 @@ interface BitmapFont : Font {
*/
operator fun invoke(
font: Font,
fontSize: Double,
fontSize: Number,
chars: CharacterSet = CharacterSet.LATIN_ALL,
fontName: String = font.name,
paint: Paint = Colors.WHITE,
mipmaps: Boolean = true,
effect: BitmapEffect? = null,
): BitmapFont {
val fmetrics = font.getFontMetrics(fontSize)
val glyphMetrics = chars.codePoints.map { font.getGlyphMetrics(fontSize, it, reader = null) }
val fontSizeD = fontSize.toDouble()
val fmetrics = font.getFontMetrics(fontSizeD)
val glyphMetrics = chars.codePoints.map { font.getGlyphMetrics(fontSizeD, it, reader = null) }
val requiredArea = glyphMetrics.map { (it.width + 4) * (fmetrics.lineHeight + 4) }.sum().toIntCeil()
val requiredAreaSide = sqrt(requiredArea.toFloat()).toIntCeil()
val matlas = MutableAtlas<TextToBitmapResult>(requiredAreaSide.nextPowerOfTwo, requiredAreaSide.nextPowerOfTwo)
val border = 2
for (codePoint in chars.codePoints) {
val result = font.renderGlyphToBitmap(fontSize, codePoint, paint = paint, fill = true, border = 1, effect = effect)
val result = font.renderGlyphToBitmap(fontSizeD, codePoint, paint = paint, fill = true, border = 1, effect = effect)
//val result = font.renderGlyphToBitmap(fontSize, codePoint, paint = DefaultPaint, fill = true)
//println("codePoint[${codePoint.toChar()}]: $result")
matlas.add(result.bmp.toBMP32().premultipliedIfRequired(), result)
Expand All @@ -163,7 +164,7 @@ interface BitmapFont : Font {
val g = it.data.glyphs.first()
//val fm = it.data.fmetrics
val m = g.metrics
g.codePoint to Glyph(fontSize, g.codePoint, slice, -border, (border - m.height - m.top).toInt() + fm.ascent.toInt(), m.xadvance.toIntCeil())
g.codePoint to Glyph(fontSizeD, g.codePoint, slice, -border, (border - m.height - m.top).toInt() + fm.ascent.toInt(), m.xadvance.toIntCeil())
}.toIntMap(),
kernings = IntMap(),
name = fontName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ interface VectorBuilder {

fun write(curves: List<Curves>) = write(curves.toVectorPath())
fun write(curves: Curves) = write(curves.toVectorPath())
fun arc(center: Point, r: Double, start: Angle, end: Angle, counterclockwise: Boolean = false) = Arc.arcPath(this, center, r, start, end, counterclockwise)
fun arc(center: Point, r: Number, start: Angle, end: Angle, counterclockwise: Boolean = false) = Arc.arcPath(this, center, r.toDouble(), start, end, counterclockwise)

fun circle(circle: Circle) = circle(circle.center, circle.radius)
fun circleHole(circle: Circle) = circleHole(circle.center, circle.radius)

fun circle(center: Point, radius: Double): Unit = arc(center, radius, Angle.ZERO, Angle.FULL)
fun circleHole(center: Point, radius: Double) = arc(center, radius, Angle.ZERO, Angle.FULL, counterclockwise = true)
fun circle(center: Point, radius: Number): Unit = arc(center, radius, Angle.ZERO, Angle.FULL)
fun circleHole(center: Point, radius: Number) = arc(center, radius, Angle.ZERO, Angle.FULL, counterclockwise = true)

fun ellipse(bounds: Rectangle) = ellipse(bounds.center, bounds.size / 2)
fun ellipse(ellipse: Ellipse) = ellipse(ellipse.center, ellipse.radius)
Expand Down
10 changes: 5 additions & 5 deletions korge/src/korlibs/korge/ui/UIContainerLayouts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ inline fun Container.uiVerticalStack(
) = UIVerticalStack(width, padding, adjustSize).addTo(this).apply(block)

open class UIVerticalStack(
forcedWidth: Double? = null,
padding: Double = UI_DEFAULT_PADDING,
forcedWidth: Number? = null,
padding: Number = UI_DEFAULT_PADDING,
adjustSize: Boolean = true,
) : UIVerticalHorizontalStack(Size(forcedWidth ?: 100.0, 0.0), padding, adjustSize) {
var forcedWidth: Double? = forcedWidth
var forcedWidth: Double? = forcedWidth?.toDouble()
set(value) {
if (field != value) {
field = value
Expand Down Expand Up @@ -216,8 +216,8 @@ open class UIHorizontalStack(
}
}

abstract class UIVerticalHorizontalStack(size: Size = UI_DEFAULT_SIZE, padding: Double = UI_DEFAULT_PADDING, val adjustSize: Boolean) : UIContainer(size) {
var padding: Double = padding
abstract class UIVerticalHorizontalStack(size: Size = UI_DEFAULT_SIZE, padding: Number = UI_DEFAULT_PADDING, val adjustSize: Boolean) : UIContainer(size) {
var padding: Double = padding.toDouble()
set(value) {
field = value
relayout()
Expand Down
6 changes: 3 additions & 3 deletions korge/src/korlibs/korge/view/Circle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ inline fun Container.circle(
* The [autoScaling] property determines if the underlying texture will be updated when the hierarchy is scaled.
*/
open class Circle(
radius: Double = 16.0,
radius: Number = 16.0,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 0.0,
strokeThickness: Number = 0.0,
autoScaling: Boolean = true,
renderer: GraphicsRenderer = GraphicsRenderer.GPU,
) : ShapeView(shape = VectorPath(), fill = fill, stroke = stroke, strokeThickness = strokeThickness, autoScaling = autoScaling, renderer = renderer) {
/** Radius of the circle */
var radius: Double by uiObservable(radius) { updateGraphics() }
var radius: Double by uiObservable(radius.toDouble()) { updateGraphics() }
/** Color of the circle. Internally it uses the [colorMul] property */
var color: RGBA by ::colorMul

Expand Down
4 changes: 2 additions & 2 deletions korge/src/korlibs/korge/view/RoundRect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ inline fun Container.roundRect(
radius: RectCorners,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 0.0,
strokeThickness: Number = 0.0,
autoScaling: Boolean = true,
callback: @ViewDslMarker RoundRect.() -> Unit = {}
) = RoundRect(size, radius, fill, stroke, strokeThickness, autoScaling).addTo(this, callback)
Expand All @@ -28,7 +28,7 @@ class RoundRect(
radius: RectCorners,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 0.0,
strokeThickness: Number = 0.0,
autoScaling: Boolean = true
) : ShapeView(shape = VectorPath(), fill = fill, stroke = stroke, strokeThickness = strokeThickness, autoScaling = autoScaling) {

Expand Down
6 changes: 3 additions & 3 deletions korge/src/korlibs/korge/view/ShapeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ inline fun Container.shapeView(
shape: VectorPath? = null,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 1.0,
strokeThickness: Number = 1.0,
autoScaling: Boolean = true,
renderer: GraphicsRenderer = GraphicsRenderer.GPU,
callback: @ViewDslMarker ShapeView.() -> Unit = {}
Expand All @@ -21,7 +21,7 @@ open class ShapeView(
shape: VectorPath? = null,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 1.0,
strokeThickness: Number = 1.0,
autoScaling: Boolean = true,
//renderer: GraphicsRenderer = GraphicsRenderer.SYSTEM
renderer: GraphicsRenderer = GraphicsRenderer.GPU
Expand Down Expand Up @@ -72,7 +72,7 @@ open class ShapeView(
_updateShapeGraphics()
}
@ViewProperty
var strokeThickness: Double = strokeThickness
var strokeThickness: Double = strokeThickness.toDouble()
set(value) {
if (field == value) return
field = value
Expand Down
4 changes: 2 additions & 2 deletions korge/src/korlibs/korge/view/Text.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inline fun Container.text(
= Text(text, textSize.toDouble(), color, font, alignment, renderer, autoScaling, fill, stroke).addTo(this, block)

open class Text(
text: String, textSize: Double = DEFAULT_TEXT_SIZE,
text: String, textSize: Number = DEFAULT_TEXT_SIZE,
color: RGBA = Colors.WHITE, font: Resourceable<out Font> = DefaultTtfFontAsBitmap,
alignment: TextAlignment = TextAlignment.TOP_LEFT,
renderer: TextRenderer<String> = DefaultStringTextRenderer,
Expand Down Expand Up @@ -87,7 +87,7 @@ open class Text(
var font: Resourceable<out Font> = font; set(value) { if (field != value) { field = value; invalidate() } }

@ViewProperty(min = 1.0, max = 300.0)
var textSize: Double = textSize; set(value) { if (field != value) { field = value; invalidate() } }
var textSize: Double = textSize.toDouble(); set(value) { if (field != value) { field = value; invalidate() } }
var fontSize: Double by ::textSize

override fun invalidate() {
Expand Down

0 comments on commit 3e92eb8

Please sign in to comment.