Finish Kotlin implementation of IosCanvas

This commit is contained in:
2019-08-12 06:01:32 -05:00
parent 74475bd191
commit 563aa8b7b4
8 changed files with 109 additions and 274 deletions

View File

@@ -19,8 +19,11 @@
package org.isoron.platform.gui
import kotlinx.cinterop.*
import platform.CoreGraphics.*
import platform.Foundation.*
import platform.UIKit.*
import kotlin.math.*
val Color.uicolor: UIColor
get() = UIColor.colorWithRed(this.red, this.green, this.blue, this.alpha)
@@ -30,9 +33,13 @@ val Color.cgcolor: CGColorRef?
class IosCanvas(val width: Double,
val height: Double,
val pixelScale: Double = 2.0
val scale: Double = 2.0
) : Canvas {
var textColor = UIColor.blackColor
var font = Font.REGULAR
var fontSize = 12.0
var textAlign = TextAlign.CENTER
val ctx = UIGraphicsGetCurrentContext()!!
override fun setColor(color: Color) {
@@ -42,28 +49,46 @@ class IosCanvas(val width: Double,
}
override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) {
CGContextMoveToPoint(ctx, x1 * pixelScale, y1 * pixelScale)
CGContextAddLineToPoint(ctx, x2 * pixelScale, y2 * pixelScale)
CGContextMoveToPoint(ctx, x1 * scale, y1 * scale)
CGContextAddLineToPoint(ctx, x2 * scale, y2 * scale)
CGContextStrokePath(ctx)
}
@Suppress("CAST_NEVER_SUCCEEDS")
override fun drawText(text: String, x: Double, y: Double) {
val sx = scale * x
val sy = scale * y
val nsText = (text as NSString)
val uiFont = when (font) {
Font.REGULAR -> UIFont.systemFontOfSize(fontSize)
Font.BOLD -> UIFont.boldSystemFontOfSize(fontSize)
Font.FONT_AWESOME -> UIFont.fontWithName("FontAwesome", fontSize)
}
val size = nsText.sizeWithFont(uiFont)
val width = size.useContents { width }
val height = size.useContents { height }
val origin = when (textAlign) {
TextAlign.CENTER -> CGPointMake(sx - width / 2, sy - height / 2)
TextAlign.LEFT -> CGPointMake(sx, sy - height / 2)
TextAlign.RIGHT -> CGPointMake(sx - width, sy - height / 2)
}
nsText.drawAtPoint(origin, uiFont)
}
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
CGContextFillRect(ctx,
CGRectMake(x * pixelScale,
y * pixelScale,
width * pixelScale,
height * pixelScale))
CGRectMake(x * scale,
y * scale,
width * scale,
height * scale))
}
override fun drawRect(x: Double, y: Double, width: Double, height: Double) {
CGContextStrokeRect(ctx,
CGRectMake(x * pixelScale,
y * pixelScale,
width * pixelScale,
height * pixelScale))
CGRectMake(x * scale,
y * scale,
width * scale,
height * scale))
}
override fun getHeight(): Double {
@@ -75,14 +100,15 @@ class IosCanvas(val width: Double,
}
override fun setFont(font: Font) {
this.font = font
}
override fun setFontSize(size: Double) {
CGContextSetFontSize(ctx, size * pixelScale)
this.fontSize = size * scale
}
override fun setStrokeWidth(size: Double) {
CGContextSetLineWidth(ctx, size * pixelScale)
CGContextSetLineWidth(ctx, size * scale)
}
override fun fillArc(centerX: Double,
@@ -90,12 +116,31 @@ class IosCanvas(val width: Double,
radius: Double,
startAngle: Double,
swipeAngle: Double) {
val a1 = startAngle / 180 * PI * (-1)
val a2 = a1 - swipeAngle / 180 * PI
CGContextBeginPath(ctx)
CGContextMoveToPoint(ctx, centerX * scale, centerY * scale)
CGContextAddArc(ctx,
centerX * scale,
centerY * scale,
radius * scale,
a1,
a2,
if (swipeAngle > 0) 1 else 0)
CGContextClosePath(ctx)
CGContextFillPath(ctx)
}
override fun fillCircle(centerX: Double, centerY: Double, radius: Double) {
val rect = CGRectMake(scale * (centerX - radius),
scale * (centerY - radius),
scale * radius * 2.0,
scale * radius * 2.0)
CGContextFillEllipseInRect(ctx, rect)
}
override fun setTextAlign(align: TextAlign) {
this.textAlign = align
}
override fun toImage(): Image {

View File

@@ -26,7 +26,7 @@ fun sqlite3_errstr(db: CPointer<sqlite3>): String {
return "SQLite3 error: " + sqlite3_errmsg(db).toString()
}
class IosDatabaseOpener() : DatabaseOpener {
class IosDatabaseOpener : DatabaseOpener {
override fun open(file: UserFile): Database = memScoped {
val db = alloc<CPointerVar<sqlite3>>()
val path = (file as IosFile).path

View File

@@ -19,7 +19,6 @@
package org.isoron
import kotlinx.cinterop.*
import org.isoron.platform.gui.*
import org.isoron.platform.io.*
import org.isoron.platform.time.*
@@ -40,7 +39,7 @@ actual object DependencyResolver {
actual fun createCanvas(width: Int, height: Int): Canvas {
val scale = 2.0
UIGraphicsBeginImageContext(CGSizeMake(width * scale, height * scale))
return IosCanvas(width * scale, height * scale, pixelScale = scale)
return IosCanvas(width * scale, height * scale, scale = scale)
}
actual suspend fun getDatabase(): Database {