From 3e282edb3ece269204b12affce4897c8d6cc0cb3 Mon Sep 17 00:00:00 2001 From: jeffshee Date: Sat, 15 Jun 2019 11:07:29 +0900 Subject: [PATCH] More modifiers --- .idea/misc.xml | 2 +- .../visualizer/painters/misc/Background.kt | 33 ++++++++++ .../visualizer/painters/modifier/Move.kt | 30 +++++++++ .../visualizer/painters/modifier/Scale.kt | 35 +++++++++++ .../modifier/{Group.kt => Sequential.kt} | 2 +- .../visualizer/painters/modifier/Shake.kt | 61 +++++++++++++++++++ .../visualizer/painters/modifier/Zoom.kt | 48 +++++++++++++++ 7 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 visualizer/src/main/java/io/github/jeffshee/visualizer/painters/misc/Background.kt create mode 100644 visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Move.kt create mode 100644 visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Scale.kt rename visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/{Group.kt => Sequential.kt} (85%) create mode 100644 visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Shake.kt create mode 100644 visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Zoom.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 7bfef59..37a7509 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/misc/Background.kt b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/misc/Background.kt new file mode 100644 index 0000000..0662403 --- /dev/null +++ b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/misc/Background.kt @@ -0,0 +1,33 @@ +package io.github.jeffshee.visualizer.painters.misc + +import android.graphics.* +import io.github.jeffshee.visualizer.painters.Painter +import io.github.jeffshee.visualizer.utils.VisualizerHelper +import kotlin.math.max + +class Background( + var bitmap: Bitmap, + var xR: Float = .5f, + var yR: Float = .5f, + var wR: Float = 1f +) : Painter() { + + private val matrix = Matrix() + + override fun draw(canvas: Canvas, helper: VisualizerHelper) { + bitmap.apply bitmap@{ + matrix.apply { + val scale = max( + canvas.width.toFloat() * wR / this@bitmap.width, + canvas.height.toFloat() * wR / this@bitmap.height + ) + postScale(scale, scale) + postTranslate(-scale * this@bitmap.width / 2f, -scale * this@bitmap.height.toFloat() / 2f) + } + drawHelper(canvas, "a", xR, yR) { + canvas.drawBitmap(this, matrix, null) + } + matrix.reset() + } + } +} diff --git a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Move.kt b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Move.kt new file mode 100644 index 0000000..91eb1d5 --- /dev/null +++ b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Move.kt @@ -0,0 +1,30 @@ +package io.github.jeffshee.visualizer.painters.modifier + +import android.graphics.Canvas +import io.github.jeffshee.visualizer.painters.Painter +import io.github.jeffshee.visualizer.utils.VisualizerHelper + +class Move : Painter { + var painters: List + var xR: Float + var yR: Float + + constructor(painters: List, xR: Float = 0f, yR: Float = 0f) { + this.painters = painters + this.xR = xR + this.yR = yR + } + + constructor(painter: Painter, xR: Float = 0f, yR: Float = 0f) : this( + listOf(painter), xR, yR + ) + + override fun draw(canvas: Canvas, helper: VisualizerHelper) { + canvas.save() + canvas.translate(canvas.width * xR, canvas.height * yR) + painters.forEach { painter -> + painter.draw(canvas, helper) + } + canvas.restore() + } +} \ No newline at end of file diff --git a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Scale.kt b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Scale.kt new file mode 100644 index 0000000..77635c2 --- /dev/null +++ b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Scale.kt @@ -0,0 +1,35 @@ +package io.github.jeffshee.visualizer.painters.modifier + +import android.graphics.Canvas +import io.github.jeffshee.visualizer.painters.Painter +import io.github.jeffshee.visualizer.utils.VisualizerHelper + +class Scale : Painter { + var painters: List + var wR: Float + var hR: Float + var pxR: Float + var pyR: Float + + constructor(painters: List, wR: Float = 1f, hR: Float = 1f, pxR: Float = .5f, pyR: Float = .5f) { + this.painters = painters + this.wR = wR + this.hR = hR + this.pxR = pxR + this.pyR = pyR + + } + + constructor(painter: Painter, wR: Float = 1f, hR: Float = 1f, pxR: Float = .5f, pyR: Float = .5f) : this( + listOf(painter), wR, hR, pxR, pyR + ) + + override fun draw(canvas: Canvas, helper: VisualizerHelper) { + canvas.save() + canvas.scale(wR, hR, pxR * canvas.width, pyR * canvas.height) + painters.forEach { painter -> + painter.draw(canvas, helper) + } + canvas.restore() + } +} \ No newline at end of file diff --git a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Group.kt b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Sequential.kt similarity index 85% rename from visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Group.kt rename to visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Sequential.kt index 53fa2bc..57a600d 100644 --- a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Group.kt +++ b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Sequential.kt @@ -4,7 +4,7 @@ import android.graphics.Canvas import io.github.jeffshee.visualizer.painters.Painter import io.github.jeffshee.visualizer.utils.VisualizerHelper -class Group(var painters: List) : Painter() { +class Sequential(var painters: List) : Painter() { override fun draw(canvas: Canvas, helper: VisualizerHelper) { painters.forEach { painter -> painter.draw(canvas, helper) } diff --git a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Shake.kt b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Shake.kt new file mode 100644 index 0000000..2d270a4 --- /dev/null +++ b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Shake.kt @@ -0,0 +1,61 @@ +package io.github.jeffshee.visualizer.painters.modifier + +import android.graphics.Canvas +import io.github.jeffshee.visualizer.painters.Painter +import io.github.jeffshee.visualizer.utils.VisualizerHelper +import kotlin.math.* +import kotlin.random.Random + +class Shake : Painter { + var painters: List + var xR: Float + var yR: Float + var shaker: Shaker + + constructor(painters: List, xR: Float = .05f, yR: Float = .05f, shaker: Shaker = Shaker()) { + this.painters = painters + this.xR = xR + this.yR = yR + this.shaker = shaker + } + + constructor(painter: Painter, xR: Float = .05f, yR: Float = .05f, shaker: Shaker = Shaker()) : this( + listOf(painter), xR, yR, shaker + ) + + override fun draw(canvas: Canvas, helper: VisualizerHelper) { + shaker.update() + drawHelper(canvas, "a", xR * shaker.x, yR * shaker.y) { + painters.forEach { painter -> + painter.draw(canvas, helper) + } + } + } + + class Shaker { + var x = 0f + var y = 0f + var v = .01f + var theta = 0f + + private val range = toRad(60f) + private val pi = PI.toFloat() + + fun update() { + if (x * x + y * y >= 1f) { + theta = toCenter() + range * Random.nextFloat() - range / 2f + } + x += v * cos(theta) + y += v * sin(theta) + } + + private fun toCenter(): Float { + return pi + atan2(y, x) + } + + private fun toRad(deg: Float): Float { + return deg / 180f * PI.toFloat() + } + + } +} \ No newline at end of file diff --git a/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Zoom.kt b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Zoom.kt new file mode 100644 index 0000000..0f09a6a --- /dev/null +++ b/visualizer/src/main/java/io/github/jeffshee/visualizer/painters/modifier/Zoom.kt @@ -0,0 +1,48 @@ +package io.github.jeffshee.visualizer.painters.modifier + +import android.graphics.Canvas +import io.github.jeffshee.visualizer.painters.Painter +import io.github.jeffshee.visualizer.utils.VisualizerHelper +import kotlin.math.* +import kotlin.random.Random + +class Zoom : Painter { + var painters: List + var xR: Float + var yR: Float + var zoomer: Zoomer + + constructor(painters: List, xR: Float = .05f, yR: Float = .05f, zoomer: Zoomer = Zoomer()) { + this.painters = painters + this.xR = xR + this.yR = yR + this.zoomer = zoomer + } + + constructor(painter: Painter, xR: Float = .05f, yR: Float = .05f, zoomer: Zoomer = Zoomer()) : this( + listOf(painter), xR, yR, zoomer + ) + + override fun draw(canvas: Canvas, helper: VisualizerHelper) { + zoomer.update() + canvas.save() + canvas.scale(1f + .1f*zoomer.x,1f + .1f*zoomer.x, canvas.width/2f, canvas.height/2f) + painters.forEach { painter -> + painter.draw(canvas, helper) + } + canvas.restore() + } + + class Zoomer { + var x = 0f + var v = .01f + + fun update() { + if (abs(x) >= 1f) { + v = -v + } + x += v + } + + } +} \ No newline at end of file