Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Created November 13, 2016 15:53
Show Gist options
  • Save ArcticLight/223f61de85c49443dd5ca01d18410fed to your computer and use it in GitHub Desktop.
Save ArcticLight/223f61de85c49443dd5ca01d18410fed to your computer and use it in GitHub Desktop.
package me.arcticlight.cs.databases
import me.arcticlight.animations.Eases
import scala.languageFeature.implicitConversions
import me.arcticlight.animations.ScalaTween._
import me.arcticlight.cs.databases.Animatables.PVectorIsVectorLike
import me.arcticlight.animations.ScalaTween.DefaultInterpolations._
import me.arcticlight.cs.databases.Animatables.Color
import processing.core.{PConstants, PGraphics, PVector}
import scala.language.implicitConversions
object AnimatablePieChart {
case class Datum(data: AnimationTarget[Float], color: AnimationTarget[Color])
def apply(data: Seq[Float], color: Seq[Color], x: Float, y: Float, r: Float): AnimatablePieChart = {
require(data.length == color.length)
new AnimatablePieChart(data.zip(color).map {case (z,c) => Datum(AnimationTarget(z),AnimationTarget(c))},
AnimationTarget(new PVector(x,y)),
AnimationTarget(r),
AnimationTarget(r)
)
}
}
class AnimatablePieChart(val data: Seq[AnimatablePieChart.Datum],
val pos: AnimationTarget[PVector],
val width: AnimationTarget[Float],
val height: AnimationTarget[Float]) {
def getPortionList: Seq[((Float, Float), Color)] = {
val sum = data.map(x=>x.data.target).sum
val z = data.scanLeft(0f)({case (total, x) => total + (x.data/sum)})
(z zip z.tail) zip data.map(x=>x.color.target)
}
def draw(g: PGraphics): Unit = {
g.ellipseMode(PConstants.CENTER)
g.pushMatrix()
g.translate(pos.x, pos.y)
g.rotate(-PConstants.HALF_PI)
g.noStroke()
getPortionList.foreach {case ((start,stop), color) =>
g.fill(color.r,color.g,color.b,color.a)
g.arc(0,0,width,height,start*PConstants.TWO_PI,stop*PConstants.TWO_PI,PConstants.PIE)
}
g.noFill()
g.stroke(0)
g.ellipse(0,0,width,height)
g.popMatrix()
}
def morphToMatchData(newData: Seq[Float]): Animatable = {
require(newData.length == this.data.length)
ParTimeline(
data.zip(newData).map {case (x,to) => Tween(x.data,x.data.target,to,0.8f).ease(Eases.EaseOutQuad)} :_*
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment