Skip to content

Commit

Permalink
More modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffshee committed Jun 15, 2019
1 parent a32be29 commit 3e282ed
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Painter>
var xR: Float
var yR: Float

constructor(painters: List<Painter>, 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()
}
}
Original file line number Diff line number Diff line change
@@ -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<Painter>
var wR: Float
var hR: Float
var pxR: Float
var pyR: Float

constructor(painters: List<Painter>, 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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>) : Painter() {
class Sequential(var painters: List<Painter>) : Painter() {

override fun draw(canvas: Canvas, helper: VisualizerHelper) {
painters.forEach { painter -> painter.draw(canvas, helper) }
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Painter>
var xR: Float
var yR: Float
var shaker: Shaker

constructor(painters: List<Painter>, 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()
}

}
}
Original file line number Diff line number Diff line change
@@ -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<Painter>
var xR: Float
var yR: Float
var zoomer: Zoomer

constructor(painters: List<Painter>, 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
}

}
}

0 comments on commit 3e282ed

Please sign in to comment.