forked from iSoron/uhabits
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HtmlCanvas; move some tests to commonTest
- Loading branch information
Showing
17 changed files
with
288 additions
and
64 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
core/src/commonTest/kotlin/org/isoron/platform/gui/CanvasTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]> | ||
* | ||
* 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 | ||
|
||
class CanvasTest(val platform: Platform) { | ||
interface Platform { | ||
fun createCanvas(width: Int, height: Int): Canvas | ||
fun writePng(canvas: Canvas, filename: String) | ||
} | ||
|
||
fun testDrawing() { | ||
val canvas = platform.createCanvas(500, 400) | ||
|
||
canvas.setColor(Color(0x303030)) | ||
canvas.fillRect(0.0, 0.0, 500.0, 400.0) | ||
|
||
canvas.setColor(Color(0x606060)) | ||
canvas.setStrokeWidth(25.0) | ||
canvas.drawRect(100.0, 100.0, 300.0, 200.0) | ||
|
||
canvas.setColor(Color(0xFFFF00)) | ||
canvas.setStrokeWidth(1.0) | ||
canvas.drawRect(0.0, 0.0, 100.0, 100.0) | ||
canvas.fillCircle(50.0, 50.0, 30.0) | ||
canvas.drawRect(0.0, 100.0, 100.0, 100.0) | ||
canvas.fillArc(50.0, 150.0, 30.0, 90.0, 135.0) | ||
canvas.drawRect(0.0, 200.0, 100.0, 100.0) | ||
canvas.fillArc(50.0, 250.0, 30.0, 90.0, -135.0) | ||
canvas.drawRect(0.0, 300.0, 100.0, 100.0) | ||
canvas.fillArc(50.0, 350.0, 30.0, 45.0, 90.0) | ||
|
||
canvas.setColor(Color(0xFF0000)) | ||
canvas.setStrokeWidth(2.0) | ||
canvas.drawLine(0.0, 0.0, 500.0, 400.0) | ||
canvas.drawLine(500.0, 0.0, 0.0, 400.0) | ||
|
||
canvas.setFont(Font.BOLD) | ||
canvas.setFontSize(50.0) | ||
canvas.setColor(Color(0x00FF00)) | ||
canvas.setTextAlign(TextAlign.CENTER) | ||
canvas.drawText("HELLO", 250.0, 100.0) | ||
|
||
canvas.setTextAlign(TextAlign.RIGHT) | ||
canvas.drawText("HELLO", 250.0, 150.0) | ||
|
||
canvas.setTextAlign(TextAlign.LEFT) | ||
canvas.drawText("HELLO", 250.0, 200.0) | ||
|
||
canvas.setFont(Font.FONT_AWESOME) | ||
canvas.drawText(FontAwesome.CHECK, 250.0, 300.0) | ||
|
||
platform.writePng(canvas, "CanvasTest.png") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
110 changes: 110 additions & 0 deletions
110
core/src/jsMain/kotlin/org/isoron/platform/gui/HtmlCanvas.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]> | ||
* | ||
* 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 org.w3c.dom.* | ||
import kotlin.browser.* | ||
import kotlin.math.* | ||
|
||
class HtmlCanvas(val canvas: HTMLCanvasElement) : Canvas { | ||
|
||
val ctx = canvas.getContext("2d") as CanvasRenderingContext2D | ||
var fontSize = 12.0 | ||
var fontWeight = "" | ||
var fontFamily = "sans-serif" | ||
var align = CanvasTextAlign.CENTER | ||
|
||
override fun setColor(color: Color) { | ||
val c = "rgb(${color.red * 255}, ${color.green * 255}, ${color.blue * 255})" | ||
ctx.fillStyle = c; | ||
ctx.strokeStyle = c; | ||
} | ||
|
||
override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) { | ||
ctx.beginPath() | ||
ctx.moveTo(x1 + 0.5, y1 + 0.5) | ||
ctx.lineTo(x2 + 0.5, y2 + 0.5) | ||
ctx.stroke() | ||
} | ||
|
||
override fun drawText(text: String, x: Double, y: Double) { | ||
ctx.font = "${fontWeight} ${fontSize}px ${fontFamily}" | ||
ctx.textAlign = align | ||
ctx.textBaseline = CanvasTextBaseline.MIDDLE | ||
ctx.fillText(text, x, y) | ||
} | ||
|
||
override fun fillRect(x: Double, y: Double, width: Double, height: Double) { | ||
ctx.fillRect(x - 0.5, y - 0.5, width + 1.0, height + 1.0) | ||
} | ||
|
||
override fun drawRect(x: Double, y: Double, width: Double, height: Double) { | ||
ctx.strokeRect(x - 0.5, y - 0.5, width + 1.0, height + 1.0) | ||
} | ||
|
||
override fun getHeight(): Double { | ||
return canvas.height.toDouble() | ||
} | ||
|
||
override fun getWidth(): Double { | ||
return canvas.width.toDouble() | ||
} | ||
|
||
override fun setFont(font: Font) { | ||
fontWeight = if (font == Font.BOLD) "bold" else "" | ||
fontFamily = if (font == Font.FONT_AWESOME) "FontAwesome" else "sans-serif" | ||
} | ||
|
||
override fun setFontSize(size: Double) { | ||
fontSize = size | ||
} | ||
|
||
override fun setStrokeWidth(size: Double) { | ||
ctx.lineWidth = size | ||
} | ||
|
||
override fun fillArc(centerX: Double, | ||
centerY: Double, | ||
radius: Double, | ||
startAngle: Double, | ||
swipeAngle: Double) { | ||
val from = startAngle / 180 * PI | ||
val to = (startAngle + swipeAngle) / 180 * PI | ||
ctx.beginPath() | ||
ctx.moveTo(centerX, centerY) | ||
ctx.arc(centerX, centerY, radius, -from, -to, swipeAngle >= 0) | ||
ctx.lineTo(centerX, centerY) | ||
ctx.fill() | ||
} | ||
|
||
override fun fillCircle(centerX: Double, centerY: Double, radius: Double) { | ||
ctx.beginPath() | ||
ctx.arc(centerX, centerY, radius, 0.0, 2 * PI) | ||
ctx.fill() | ||
} | ||
|
||
override fun setTextAlign(align: TextAlign) { | ||
this.align = when(align) { | ||
TextAlign.LEFT -> CanvasTextAlign.LEFT | ||
TextAlign.CENTER -> CanvasTextAlign.CENTER | ||
TextAlign.RIGHT -> CanvasTextAlign.RIGHT | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
core/src/jsTest/kotlin/org/isoron/platform/gui/HtmlCanvasTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]> | ||
* | ||
* 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 org.w3c.dom.* | ||
|
||
class HtmlCanvasTest(val canvas: HTMLCanvasElement) : CanvasTest.Platform { | ||
|
||
override fun createCanvas(width: Int, height: Int): Canvas { | ||
return HtmlCanvas(canvas) | ||
} | ||
|
||
override fun writePng(canvas: Canvas, filename: String) { | ||
} | ||
|
||
fun testDrawing() { | ||
val test = CanvasTest(this) | ||
test.testDrawing() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.