mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Partial implementation of IosCanvas in Kotlin
This commit is contained in:
@@ -31,8 +31,8 @@ interface Image {
|
|||||||
suspend fun export(path: String)
|
suspend fun export(path: String)
|
||||||
|
|
||||||
fun diff(other: Image) {
|
fun diff(other: Image) {
|
||||||
if (width != other.width) error("Width must match")
|
if (width != other.width) error("Width must match: $width !== ${other.width}")
|
||||||
if (height != other.height) error("Height must match")
|
if (height != other.height) error("Height must match: $height !== ${other.height}")
|
||||||
|
|
||||||
for (x in 0 until width) {
|
for (x in 0 until width) {
|
||||||
for (y in 0 until height) {
|
for (y in 0 until height) {
|
||||||
|
|||||||
@@ -28,8 +28,12 @@ val Color.uicolor: UIColor
|
|||||||
val Color.cgcolor: CGColorRef?
|
val Color.cgcolor: CGColorRef?
|
||||||
get() = uicolor.CGColor
|
get() = uicolor.CGColor
|
||||||
|
|
||||||
class IosCanvas(val ctx: CGContextRef) : Canvas {
|
class IosCanvas(val width: Double,
|
||||||
|
val height: Double,
|
||||||
|
val pixelScale: Double = 2.0
|
||||||
|
) : Canvas {
|
||||||
var textColor = UIColor.blackColor
|
var textColor = UIColor.blackColor
|
||||||
|
val ctx = UIGraphicsGetCurrentContext()!!
|
||||||
|
|
||||||
override fun setColor(color: Color) {
|
override fun setColor(color: Color) {
|
||||||
CGContextSetStrokeColorWithColor(ctx, color.cgcolor)
|
CGContextSetStrokeColorWithColor(ctx, color.cgcolor)
|
||||||
@@ -38,32 +42,47 @@ class IosCanvas(val ctx: CGContextRef) : Canvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) {
|
override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) {
|
||||||
|
CGContextMoveToPoint(ctx, x1 * pixelScale, y1 * pixelScale)
|
||||||
|
CGContextAddLineToPoint(ctx, x2 * pixelScale, y2 * pixelScale)
|
||||||
|
CGContextStrokePath(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun drawText(text: String, x: Double, y: Double) {
|
override fun drawText(text: String, x: Double, y: Double) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
|
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
|
||||||
|
CGContextFillRect(ctx,
|
||||||
|
CGRectMake(x * pixelScale,
|
||||||
|
y * pixelScale,
|
||||||
|
width * pixelScale,
|
||||||
|
height * pixelScale))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun drawRect(x: Double, y: Double, width: Double, height: Double) {
|
override fun drawRect(x: Double, y: Double, width: Double, height: Double) {
|
||||||
|
CGContextStrokeRect(ctx,
|
||||||
|
CGRectMake(x * pixelScale,
|
||||||
|
y * pixelScale,
|
||||||
|
width * pixelScale,
|
||||||
|
height * pixelScale))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getHeight(): Double {
|
override fun getHeight(): Double {
|
||||||
return 0.0
|
return height
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getWidth(): Double {
|
override fun getWidth(): Double {
|
||||||
return 0.0
|
return width
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setFont(font: Font) {
|
override fun setFont(font: Font) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setFontSize(size: Double) {
|
override fun setFontSize(size: Double) {
|
||||||
|
CGContextSetFontSize(ctx, size * pixelScale)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setStrokeWidth(size: Double) {
|
override fun setStrokeWidth(size: Double) {
|
||||||
|
CGContextSetLineWidth(ctx, size * pixelScale)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun fillArc(centerX: Double,
|
override fun fillArc(centerX: Double,
|
||||||
@@ -80,6 +99,6 @@ class IosCanvas(val ctx: CGContextRef) : Canvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun toImage(): Image {
|
override fun toImage(): Image {
|
||||||
TODO()
|
return IosImage(UIGraphicsGetImageFromCurrentImageContext()!!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
55
core/src/main/ios/org/isoron/platform/gui/IosImage.kt
Normal file
55
core/src/main/ios/org/isoron/platform/gui/IosImage.kt
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.platform.gui
|
||||||
|
|
||||||
|
import platform.UIKit.*
|
||||||
|
import platform.CoreGraphics.*
|
||||||
|
import platform.Foundation.*
|
||||||
|
|
||||||
|
class IosImage(val image: UIImage) : Image {
|
||||||
|
|
||||||
|
override val width: Int
|
||||||
|
get() {
|
||||||
|
return CGImageGetWidth(image.CGImage).toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
override val height: Int
|
||||||
|
get() {
|
||||||
|
return CGImageGetHeight(image.CGImage).toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getPixel(x: Int, y: Int): Color {
|
||||||
|
return Color(1.0, 0.0, 0.0, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setPixel(x: Int, y: Int, color: Color) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("CAST_NEVER_SUCCEEDS")
|
||||||
|
override suspend fun export(path: String) {
|
||||||
|
val tmpPath = "${NSTemporaryDirectory()}/$path"
|
||||||
|
val dir = (tmpPath as NSString).stringByDeletingLastPathComponent
|
||||||
|
NSFileManager.defaultManager.createDirectoryAtPath(dir, true, null, null)
|
||||||
|
val data = UIImagePNGRepresentation(image)!!
|
||||||
|
val success = data.writeToFile(tmpPath, true)
|
||||||
|
if (!success) throw RuntimeException("could not write to $tmpPath")
|
||||||
|
println(tmpPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ package org.isoron.platform.io
|
|||||||
|
|
||||||
import org.isoron.platform.gui.*
|
import org.isoron.platform.gui.*
|
||||||
import platform.Foundation.*
|
import platform.Foundation.*
|
||||||
|
import platform.UIKit.*
|
||||||
|
|
||||||
class IosFileOpener : FileOpener {
|
class IosFileOpener : FileOpener {
|
||||||
override fun openResourceFile(path: String): ResourceFile {
|
override fun openResourceFile(path: String): ResourceFile {
|
||||||
@@ -62,6 +63,6 @@ class IosFile(val path: String) : UserFile, ResourceFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun toImage(): Image {
|
override suspend fun toImage(): Image {
|
||||||
TODO()
|
return IosImage(UIImage.imageWithContentsOfFile(path)!!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ import org.isoron.uhabits.*
|
|||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
class CanvasTest: BaseViewTest() {
|
class CanvasTest: BaseViewTest() {
|
||||||
//@Test
|
@Test
|
||||||
fun run() = asyncTest{
|
fun run() = asyncTest{
|
||||||
val canvas = DependencyResolver.createCanvas(500, 400)
|
val canvas = DependencyResolver.createCanvas(500, 400)
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.isoron
|
package org.isoron
|
||||||
|
|
||||||
|
import kotlinx.cinterop.*
|
||||||
import org.isoron.platform.gui.*
|
import org.isoron.platform.gui.*
|
||||||
import org.isoron.platform.io.*
|
import org.isoron.platform.io.*
|
||||||
import org.isoron.platform.time.*
|
import org.isoron.platform.time.*
|
||||||
@@ -37,9 +38,9 @@ actual object DependencyResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actual fun createCanvas(width: Int, height: Int): Canvas {
|
actual fun createCanvas(width: Int, height: Int): Canvas {
|
||||||
UIGraphicsBeginImageContext(CGSizeMake(width.toDouble(), height.toDouble()))
|
val scale = 2.0
|
||||||
val ctx = UIGraphicsGetCurrentContext()!!
|
UIGraphicsBeginImageContext(CGSizeMake(width * scale, height * scale))
|
||||||
return IosCanvas(ctx)
|
return IosCanvas(width * scale, height * scale, pixelScale = scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
actual suspend fun getDatabase(): Database {
|
actual suspend fun getDatabase(): Database {
|
||||||
|
|||||||
Reference in New Issue
Block a user