import random
import math
import util
import sys
from demoscript import *
sched = SchedulerSingleton_instance() # setup scheduler

joypad1=0
#addMapping(joypad1,0,"taptap")	# map button a 
#addMapping(joypad1,0,"bass")	# map button a 
addMapping(joypad1,0,"logo")	# map button a 
#addMapping(joypad1,1,"next")	# map button a 





class CustomOver(Over):
	def __init__ (self):
		Over.__init__(self)

	def callback(self):
#		if getEvent(EC_NOTE_ON,"butB"):
			self.fillOpacity.setDamping(0.9)
			self.fillOpacity.setFixPointDamping(0.9)
			self.fillOpacity.setTargetValue(frand(0,1))

class CustomOver2(Over):
	def __init__ (self,red=0,green=0,blue=0,opacity=0,opacityEvents=[]):
		Over.__init__(self)
		self.red=FixedSlider(0,0)
		self.green=FixedSlider(0,0)
		self.blue=FixedSlider(0,0)
		self.opacityEvents=opacityEvents


	def callback(self):

		for event in self.opacityEvents:
			if getEvent(EC_NOTE_ON,event[1]):
				self.fillOpacity.setDamping(0.97)
				self.fillOpacity.setFixPointDamping(0.99)
				self.fillOpacity.setTargetValue(event[0])
	


class CustomFlocking(Flocking):
	def __init__ (self):
		Flocking.__init__(self)
		self.particleSize.setFixPointDamping(0.96)
		
	def callback(self):
		if getEvent(EC_NOTE_ON,"butB"):
			self.particleSize.setTargetValue(frand(400,800))

class CustomSweep(MSSweep):
	
	def __init__(self):
		MSSweep.__init__(self)
		self.segmentWidth=FixedSlider(1.5,1.5)
		self.segmentHeight=FixedSlider(1.5,1.5)


	def callback(self):
		if getEvent(EC_NOTE_ON,"butA_"):
			self.segmentWidth.setTargetValue(0.1)
			self.segmentHeight.setTargetValue(0.1)
#			self.starShape.setTargetValue(1)
			self.distance.setTargetValue(4)
#			self.red1.setTargetValue(4)

		if getEvent(EC_NOTE_ON,"sweep"):
			self.segmentWidth.setTargetValue(frand(0,4.5))
			self.segmentHeight.setTargetValue(frand(0,4.5))
			self.distance.setTargetValue(2)
			self.starShape.setTargetValue(frand(0,1))
			
			colors=[self.red1,self.blue1,self.green1]
			for i in colors:
				i.setTargetValue(10)
			#self.setBlendMode(random.choice([0,1]))			

			#self.red1.setTargetValue(10)



#imgnames=["d:\\demos\\demo\\strip1.tga","d:\\demos\\demo\\strip2.tga"]*4
#imgs=map (util.RandomImage,imgnames)
#map(lambda x:x.setAmpY(0),imgs)
#imgSeqTest=util.SimpleImageSeq(imgs,["butB"])


introScene=util.SimpleScene("d:\\demos\\demo\\bot.lzma")
introTransforms=[Scaling(),Scaling(),Scaling(),Scaling()]

ie="taptap"

introSceneTransforms=[]
for i in range(20):introSceneTransforms.append([i,Scaling(),ie])

introSceneOverlay=util.SimpleScene("d:\\demos\\demo\\road.lzma",transforms=introSceneTransforms)
introScene.connectOutput(introSceneOverlay,"in")
#introSceneOverlay.getScene().getObjects()[10].addTransform(kak)


sweepBack=Bitmap("d:\\demos\\demo\\backdrop1.png")
sweepBackOverlay=util.SimpleScene("d:\\demos\\demo\\tunnel.lzma",nextCamera=["bass"],reset=["bass"])
sweepBackOverlay.speed=FixedSlider(20000,20000)
sweepBack.connectOutput(sweepBackOverlay,"in")

sweepScene=util.SimpleScene("d:\\demos\\demo\sweep.lzma",nextCamera=["sweep"])
sweep=CustomSweep()
sweep.setBlendMode(1)
sweepScene.attachEffect(sweep,1)
sweepBackOverlay.connectOutput(sweepScene,"in") 

gamma=util.SimpleGamma(["butB"],[2,2,2])



flockBack=Bitmap("d:\\demos\\demo\\backdrop2.png")
flockScene=util.SimpleScene("d:\\demos\\demo\\flock.lzma")
flocking=CustomFlocking()
flockScene.attachEffect(flocking,1)
flockBack.connectOutput(flockScene,"in") 
flockOverlayScene=util.SimpleScene("d:\\demos\\demo\\bot2.lzma",speeds=[[0,"butA"]],offsets=[[4000,"shake"]])



exposure=Image("d:\\demos\\demo\\film1.png")
exposure.setAdditive(1)

flockExposure=imgSeqTest=util.SimpleImageSeq([exposure],["butB"])
flockScene.connectOutput(flockExposure,"in")
#tvOuttro=util.Image("d:\\demos\\demo\\tv.png");
#flockScene.connectOutput(tvOuttro,"in")
#transScene=util.SimpleScene("d:\\demos\\demo\\transsphere.lzma",nextCamera=["butB"])

transScene=IsoParticles()
transStubScene=util.SimpleScene("d:\\demos\\demo\\isoscene.lzma")
transStubScene.attachEffect(transScene,1)
transBlend=Blender()
transStubScene.connectOutput(transBlend,"blend")

rippleTexture=Bitmap("d:\\demos\\demo\\tiling2.png")
ripple=Ripple2()
rippleTexture.connectOutput(ripple,"in")
rippleOverlayScene=util.SimpleScene("d:\\demos\\demo\\bot3.lzma",offsets=[[4000,"snare"]])
rippleOver=CustomOver()
rippleOverlayScene.connectOutput(rippleOver,"a")
ripple.connectOutput(rippleOver,"b")

noiseBack=Bitmap("d:\\demos\\demo\\backdrop3.png")
noiseScene=util.SimpleScene("d:\\demos\\demo\\3dkaleido.lzma",nextCamera=["next"],reset=["reset"],cameraOrder=[0,1],defaultspeed=7000 )
noiseOver=CustomOver2(opacityEvents=[[1.0,"butB"]])
noiseLights=util.SimpleScene("d:\\demos\\demo\\lights.lzma")
noiseBack.connectOutput(noiseLights,"in")
noiseLights.connectOutput(noiseOver,"b")
noiseScene.connectOutput(noiseOver,"a")
noiseMap=Bitmap("d:\\demos\\demo\\noisemap.tga")
noiseMap.connectOutput(noiseOver,"map")
noiseBlur=util.SimpleBlur(["snare"])
#imgSeqTest.connectOutput(noiseOver,"map")

# -- start of demo



#testScene=util.SimpleScene("d:\\demos\\demo\\bendsplines16_bump.lzma",curveEvents=zip(range(4),["reset"]*4))
#mb=util.MotionBlur()
#testScene.connectOutput(mb,"in")
#testBlur=util.SimpleBlur()
#mb.connectOutput(testBlur,"in")


#kaleidoBack=util.SimpleScene("d:\\demos\\demo\\bendsplines16_bump.lzma",curveEvents=zip(range(16),["butB"]*16))
#kaleidoBack=util.SimpleScene("d:\\demos\\demo\\bot.lzma",nextCamera=["butB"])
kaleidoBack=util.SimpleScene("d:\\demos\\demo\\kaleidoscopic_curves.lzma",curveEvents=zip(range(16),["kaleido"]*16),reset=["reset1"])
#kaleidoScene=util.SimpleScene("d:\\demos\\demo\\nog_een_kaleidoscope.lzma",reset=["reset"])
kaleidoScene=util.SimpleScene("d:\\demos\\demo\\2dkaleido.lzma",reset=["reset2"])

#kaleidoBackreal=Bitmap("d:\\demos\\demo\\backdrop3.png")
#kaleidoBackreal.connectOutput(kaleidoScene,"in")
kaleidoBack.connectOutput(kaleidoScene,"a")

#kaleidoBlur=Blur()
#kaleidoBlur.mix=FixedSlider(1,1)
# kaleidoScene.connectOutput(kaleidoBlur,"in")

#glowScene1=util.SimpleScene("d:\\demos\\demo\\glowscene_pass1.lzma")
#glowScene2=util.SimpleScene("d:\\demos\\demo\\glowscene_pass2.lzma")
glowScene1=util.SimpleScene("d:\\demos\\demo\\lafglow.lzma")
glowScene2=util.SimpleScene("d:\\demos\\demo\\lafglow.lzma")
glowBlur=Blur()
glowBlur.mix=FixedSlider(1,1)
glowBlur.spread=FixedSlider(3,3)
glowAdder=util.Add()

glowScene1.connectOutput(glowAdder,"a")
glowScene2.connectOutput(glowBlur,"in")
glowBlur.connectOutput(glowAdder,"b")
glowBlur.connectOutput(glowAdder,"c")
glowBlur.connectOutput(glowAdder,"d")


#sched.schedule(0,	260.2,	testScene,		SI_A,		0)


""" 
#Lelijke kak
greebleball = util.SimpleScene("d:\\demos\\demo\\greebleball_2.lzma",defaultspeed=35000)
greebleballblend=Blender()
greebleballblend.setMode( BMODE_NOFADE )
greebleball.connectOutput(greebleballblend,"blend")
"""

#transSphereScene=util.SimpleScene("d:\\demos\\demo\\transsphere.lzma")
transSphereBlend=Blender()


transSphereScene=Ripple2()
rippleTexture.connectOutput(transSphereScene,"in")
transSphereScene.connectOutput(transSphereBlend,"blend")



#sched.schedule(1,	1,	transBlend,		SI_MIX,		0)
#sched.schedule(3,	1,	transBlend,		SI_MIX,		0)


logoImageSeq=util.SimpleImageSeq([Image("d:\\demos\\demo\\polarbear.png")],nextImage=["logo"])
logoDistort=util.SimpleDistort([[0,"logo"]],10)
logoBlur=util.SimpleBlur(["logo"],restv=3.0,highv=0.0)


endImageSeq=util.SimpleImageSeq([Image("d:\\demos\\demo\\kaleido.png")],nextImage=["logo"])
endDistort=util.SimpleDistort([[0,"logo"]],10)
endBlur=util.SimpleBlur(["logo"],restv=3.0,highv=0.0)



#logoBlur=Blur()
#logoBlur.mix=FixedSlider(1,1)
#logoBlur.setAdditive(1)
logoDistort.connectOutput(logoBlur,"in")
logoImageSeq.connectOutput(logoDistort,"in")


endDistort.connectOutput(endBlur,"in")
endImageSeq.connectOutput(endDistort,"in")


cityscene2=util.SimpleScene("d:\\demos\\demo\\city_glow.lzma",noiseEvents=zip(range(4),["deform"]*4),nextCamera=["next"],reset=["reset1"])
cityscene1=util.SimpleScene("d:\\demos\\demo\\city.lzma",noiseEvents=zip(range(4),["deform"]*4),nextCamera=["next"],reset=["reset1"])
cityBlur=Blur()
cityBlur.mix=FixedSlider(1,1)
cityBlur.spread=FixedSlider(1,1)
cityAdder=util.Multiply()
cityscene1.connectOutput(cityAdder,"a")
cityscene2.connectOutput(cityBlur,"in")
cityMBlur= util.MotionBlur()
cityBlur.connectOutput(cityMBlur,"in")
cityMBlur.connectOutput(cityAdder,"b")


boxScene=util.CurveScene("d:\\demos\\demo\\boxscene.lzma")
boxScene.speed=FixedSlider(16000,16000)

beats1=34.5
beats1l=45-34.5
#beats2=44

#boxXMV=util.SimpleXMV("d:\\demos\\demo\\countdown.xmv")
#boxXMV.connectOutput(boxScene,"in")

##sched.schedule(9,	26.2-9,	flockExposure,		SI_A,		0)
##sched.schedule(9,	26.2-9,	flockOverlayScene,	SI_SUPER,	0)


vintageNoise=VintageNoise()

vintageNoise = VintageNoise();
vintageNoise.addBitmap("d:\\demos\\demo\\dust1.tga");
vintageNoise.addBitmap("d:\\demos\\demo\\dust2.tga");
vintageNoise.addBitmap("d:\\demos\\demo\\dust3.tga");
vintageNoise.addBitmap("d:\\demos\\demo\\dust4.tga");


sched.schedule(0,112,vintageNoise,SI_SUPER,0)


sched.schedule(9,	26.2-9, boxScene, SI_A, 0)
sched.schedule(0,9+3,logoBlur,SI_B,0)
sched.schedule(9,	3,	transSphereBlend,	SI_MIX,		0)
sched.schedule(26.2,	beats1-26.2+0.6,		introSceneOverlay,		SI_B,		0)
sched.schedule(23.2,	3,	transBlend,		SI_MIX,		0)
sched.schedule(beats1,	0.6,	transBlend,		SI_MIX,		0)
sched.schedule(beats1,	beats1l,sweepScene,		SI_A,		0)

sched.schedule(64,	81-66+2,	rippleOver,			SI_B,		0)


sched.schedule(45.0,	21,	noiseOver,		SI_A,		0)
sched.schedule(45.0,	21,	noiseBlur,		SI_POST,	0)
sched.schedule(64  ,	2,	transSphereBlend,		SI_MIX,		0)

##sched.schedule(64,	81-66+2, boxScene, SI_B, 0)


sched.schedule(93.5  ,	1.5,	transSphereBlend,		SI_MIX,		0)

sched.schedule(95.0-1.5,112-95+1.5,	cityAdder,		SI_B,	0)
sched.schedule(112,	140-112+10,	kaleidoScene,		SI_A,	0)

sched.schedule(140 ,	10,	transSphereBlend,		SI_MIX,		0)
sched.schedule(140,16.5,endBlur,SI_B,0)


sched.schedule(79.5,	1.5,	transSphereBlend,		SI_MIX,		0)


#sched.schedule(83.0,	20,	glowAdder,		SI_A,	0)



refBlur=Blur()
refScene=util.SimpleScene("d:\\demos\\demo\\reflectie_scene.lzma",noiseEvents=[[2,"bass"]],reset=["reset1"],colors=[[1,"bass"]])
refScene.connectOutput(refBlur,"in")
refBlur.setAdditive(1)
sched.schedule(79.5,	95-79.5,	refBlur,		SI_A,	0)


#aapscene = util.SimpleScene("d:\\demos\\demo\\ile.lzma",noiseEvents=[[2,"butA"]],nextCamera=["butB"],reset=["next"],colors=[[1,"butB"]])
#sched.schedule(0, 30, aapscene, SI_A, 0 )

##############################
playSong("d:\\demos\\demo\\music.wma")
