Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Created November 13, 2016 16:10
Show Gist options
  • Save ArcticLight/ab8a33f7c34f284ff5a19b5da1fe95ba to your computer and use it in GitHub Desktop.
Save ArcticLight/ab8a33f7c34f284ff5a19b5da1fe95ba to your computer and use it in GitHub Desktop.
package me.arcticlight.cs.databases
import me.arcticlight.animations.Eases
import me.arcticlight.animations.ScalaTween.{Animatable, AnimationTarget, ParTimeline, SeqTimeline, Tween}
import me.arcticlight.animations.ScalaTween.DefaultInterpolations._
import me.arcticlight.cs.databases.Animatables.Color
import me.arcticlight.cs.databases.Animatables.StockAnimations.TDelay
import processing.core.{PGraphics, PShape}
/**
* Created by bogos on 11/12/2016.
*/
class AnimatableStateMap(val width: AnimationTarget[Float], val height: AnimationTarget[Float], svg: PShape) {
private val states = Elections.getStates
var stateColors: Seq[AnimationTarget[Color]] = {
makeStateColors(1956).map(x=>AnimationTarget(x))
}
def makeStateColors(year: Int): Seq[Color] = {
val v = Elections.getElectionsForYear(year)
Seq.range(0, 51).map(x=>{
v.find(z=>z.stateName == states(x).name) match {
case Some(state) =>
if(state.evDemocrat == 0 && state.evRepublican == 0) Color(220,225,40)
else if (state.evRepublican == state.evDemocrat) Color(200)
else if (state.evDemocrat > state.evRepublican) Color(0,0,255)
else Color(255,0,0)
case None => Color(128,200,140)
}
})
}
def makeRippleDelayMap(x: Float, y: Float): Seq[Float] = {
states.map(state=>{
val (stateX, stateY) = unprojectVirtualPoint(state.xpos, state.ypos)
val a = stateX - x
val b = stateY - y
(java.lang.Math.sqrt(a*a + b*b)/java.lang.Math.sqrt(width*width+height*height)).toFloat*0.8f
})
}
def animateIntoElectionYear(year: Int, direction: Boolean = false): Animatable = {
val delayMap = if(!direction) makeRippleDelayMap(width*1.25f, height/4)
else makeRippleDelayMap(-width/4, height/2f)
ParTimeline(
stateColors.zip(makeStateColors(year)).zip(delayMap).map({case ((a,b),delay) =>
SeqTimeline(
TDelay(delay),
Tween(a, a.target, b, 0.5f).ease(Eases.EaseInQuad)
)
}):_*
)
}
def unprojectVirtualPoint(x: Float, y: Float): (Float, Float) =
(x*0.655f*width,y*1.05f*height)
def projectVirtualPoint(x: Float, y: Float): (Float, Float) =
(x/(0.655f*width),y/1.05f/height)
def draw(g: PGraphics): Unit = {
g.background(255,255,255,0)
g.shape(svg,0,0,width,height)
g.loadPixels()
g.pixels.zipWithIndex.foreach({case (x,i) =>
if(100 <= g.red(x).toInt && g.red(x).toInt <= 150) {
val c = stateColors(g.red(x).toInt - 100)
g.pixels(i) = g.color(c.r, c.g, c.b, c.a)
} else {
g.pixels(i) = x
}
})
g.updatePixels()
}
}
object AnimatableStateMap {
def apply(width: Float, height: Float, svg: PShape) = new AnimatableStateMap(AnimationTarget(width), AnimationTarget(height), svg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment