TGC Codebase Backup



Keyframe Editor by Jack

3rd Nov 2016 23:08
Summary

This function will provide a keyframe editor with mousewheel support. The value can be changed if a keyframe is pressed.



Description

This function will provide a keyframe editor with mousewheel support. The value can be changed if a keyframe is pressed.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
// Project: Keyframe Editor 			  Created: 2016-01-29
// ==========================================================
// Alex Schmidt - Online Arts 2016	www.movies.online-arts.de

// you can use the Mousewheel to set a cutout
// the silder at the bottom will also help 
// the spacekey shows the current val# 's of the shown keyframes

// you can use update_KeyframeEditor() even if no keyframe
// editor is created, no crash will happen



// set window properties
SetWindowTitle( "Keyframe Editor" )
SetWindowSize( 1280, 720, 0 )

// set display properties
SetVirtualResolution( 1280, 720 )
SetOrientationAllowed( 1, 1, 1, 1 )






type keyframetype
	val# as float
	spr as integer
	txt as integer
endtype

type keyframeeditortype
	
	// sprite media
		bg as integer
	// text media
		txtmin as integer
		txtzero as integer
		txtmax as integer	
		txt1 as integer
		txt2 as integer
		txt3 as integer
	// slider media
		sprPush as integer
		sprSL as integer
	// stuff
		sel as integer
		curmy as integer
		showtext as integer
	// cutout
		startP as integer
		endP as integer
	// text and max bars
		max# as float
		min# as float
		zero# as float
		multi# as float

endtype






px=40
py=60
sx=1200
sy=600
max#=100.0
min#=0.0
zero#=0
multiplier#=2.0
showtext=0
values=64
create_KeyframeEditor(px,py,sx,sy,max#,min#,zero#,multiplier#,showtext,values)



// delete_KeyframeEditor()	// this will delete the keyframe editor including all arrays and media


do
    

update_KeyframeEditor()

   
    Sync()
loop






// ====================== Keyframe Editor functions  ==================================
// =============================== easy as peanuts ====================================
// =============== Online Arts 2016 =========== movies.online-arts.de =================



function create_KeyframeEditor(px,py,sx,sy,max#,min#,zero#,multiplier#,showtext,values)

	dim keyframes[] as keyframetype
	dim keyframeEditor[0] as keyframeeditortype
	
	// background window
		bg=CreateSprite(0)
		SetSpriteColor(bg,50,50,50,150)
		SetSpriteSize(bg,sx,sy)
		SetSpritePosition(bg,px,py)
		SetSpriteDepth(bg,100)

	// min max zero text
	if showtext=1
		mintxt=CreateText(GetStringToken(str(min#),".",1))
		SetTextPosition(mintxt,px,py+sy/2-(min#*multiplier#))
		SetTextSize(mintxt,20)
		
		maxtxt=CreateText(GetStringToken(str(max#),".",1))
		SetTextPosition(maxtxt,px,py+sy/2-(max#*multiplier#))
		SetTextSize(maxtxt,20)
			
		zerotxt=CreateText(GetStringToken(str(zero#),".",1))
		SetTextPosition(zerotxt,px,py+sy/2-(zero#*multiplier#))
		SetTextSize(zerotxt,20)
	endif


	// information text (txt1 and txt3 can be used for other information, for me txt2 is enough)
	
	
		// txt1=CreateText("debug 1")
		// settextsize(txt1,20)
		// SetTextAlignment(txt1,1)
		// SetTextPosition(txt1,px+sx/4,py+sy-70)
		// SetTextSize(txt1,20)

		txt2=CreateText("debug 2")
		settextsize(txt2,20)
		SetTextAlignment(txt2,1)
		SetTextPosition(txt2,px+sx/2,py+sy-70)
		SetTextSize(txt2,20)

		// txt3=CreateText("debug 3")
		// settextsize(txt3,20)
		// SetTextPosition(txt3,px+sx*0.75,py+sy-70)
		// SetTextAlignment(txt3,1)
		// SetTextSize(txt3,20)
		

	// silder
		sprSL=createsprite(0)
		setspritecolor(sprSL,92,92,92,255)
		setspritesize(sprSL,sx-40,20)
		setspriteposition(sprSL,px+20,py+sy-40)
	 
		sprPush=createsprite(0)
		setspritecolor(sprPush,192,192,192,220)
		setspritesize(sprPush,sx-40,20)
		setspriteposition(sprPush,px+20,py+sy-40)

	

			keyframeEditor[0].bg=bg
			keyframeEditor[0].showtext=showtext
			
			keyframeEditor[0].max# = max#
			keyframeEditor[0].min# = min#
			keyframeEditor[0].zero# = zero#
			keyframeEditor[0].multi# = multiplier#
			
			keyframeEditor[0].txtmin = mintxt
			keyframeEditor[0].txtmax = maxtxt
			keyframeEditor[0].txtzero = zerotxt
			
			// keyframeEditor[0].txt1 = txt1
			keyframeEditor[0].txt2 = txt2
			// keyframeEditor[0].txt3 = txt3
			

			keyframeEditor[0].sprSL = sprSL
			keyframeEditor[0].sprPush = sprPush
			
						
keyframeEditor[0].startP	=	0
keyframeEditor[0].endP		=	values

			
	// fill keyframes with random values
	for i=0 to values
		
		keyframes.length=keyframes.length+1
		p=keyframes.length
		
		// create a sprite for each keyframe
		key=CreateSprite(0)
		SetSpriteColor(key,255,255,0,255)
		SetSpriteSize(key,10,10)
		
		// create an information text for each Keyframe
		text=CreateText("")
		SetTextSize(text,16)

		keyframes[p].val#=10.0
		keyframes[p].spr=key
		keyframes[p].txt=text
		
		if keyframes[p].val#>max# then keyframes[p].val#=max#
		if keyframes[p].val#<min# then keyframes[p].val#=min#
		

				
		
	next i
	
	
	
	// position the keyframes inside the keyframe window
	
	for i=0 to keyframes.length
		
		
		xoff#=(sx/keyframes.length*1.0)*i
		
		SetSpritePosition(keyframes[i].spr,px+xoff#,(py+sy/2-(keyframes[i].val#*multiplier#)))
		

		
	next i

set_Keyframe_cutout()

endfunction





function update_KeyframeEditor()




	// if the keyframes are filled
	if keyframes.length>-1


		
		// draw the ground lines min, 0, max
	
			// zero
			drawline( GetSpriteX(keyframeEditor[0].bg), GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-keyframeEditor[0].zero#*keyframeEditor[0].multi#+10, GetSpriteX(keyframeEditor[0].bg)+GetSpriteWidth(keyframeEditor[0].bg), GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-keyframeEditor[0].zero#*keyframeEditor[0].multi#+10,255,255,255)
	
			// min
			drawline( GetSpriteX(keyframeEditor[0].bg), GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-keyframeEditor[0].min#*keyframeEditor[0].multi#+10, GetSpriteX(keyframeEditor[0].bg)+GetSpriteWidth(keyframeEditor[0].bg), GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-keyframeEditor[0].min#*keyframeEditor[0].multi#+10,255,255,255)
			
			// max
			drawline( GetSpriteX(keyframeEditor[0].bg), GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-keyframeEditor[0].max#*keyframeEditor[0].multi#, GetSpriteX(keyframeEditor[0].bg)+GetSpriteWidth(keyframeEditor[0].bg), GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-keyframeEditor[0].max#*keyframeEditor[0].multi#,255,255,255)
			
		// 

	for i=keyframeEditor[0].startP to keyframeEditor[0].endP
		
		

		
		
		// draw lines to connect dots
		if i>0 and i>keyframeEditor[0].startP and i=<keyframeEditor[0].endP

			
			DrawLine(GetSpriteX(keyframes[i-1].spr)+GetSpriteWidth(keyframes[i-1].spr)/2,GetSpriteY(keyframes[i-1].spr)+GetSpriteHeight(keyframes[i-1].spr)/2,GetSpriteX(keyframes[i].spr)+GetSpriteWidth(keyframes[i].spr)/2,GetSpriteY(keyframes[i].spr)+GetSpriteHeight(keyframes[i].spr)/2,255,255,0)
		endif
		
		// manange collision
		

		if i<>keyframeEditor[0].sel then SetSpriteColor(keyframes[i].spr,255,255,0,255)
		

		
		
			if GetSpriteHit(GetPointerX(),GetPointerY())=keyframes[i].spr
				SetSpriteColor(keyframes[i].spr,255,100,255,255)

			endif
		
		if GetRawMouseLeftState()=1 
			
			
			if GetSpriteHit(GetPointerX(),GetPointerY())=keyframes[i].spr
			
				SetSpriteColor(keyframes[i].spr,255,0,0,255)
				keyframeEditor[0].sel=i
				keyframeEditor[0].curmy=getpointery()
				

			endif
			
				// information for only the selected one
			if keyframeEditor[0].sel>-1
				SetTextPosition(keyframes[keyframeEditor[0].sel].txt, GetSpriteX(keyframes[keyframeEditor[0].sel].spr),GetSpriteY(keyframes[keyframeEditor[0].sel].spr)+20)
				SetTextString(keyframes[keyframeEditor[0].sel].txt, left(str(keyframes[keyframeEditor[0].sel].val#),5))	
			endif
			
		else
			
		if GetRawMouseLeftReleased()=1
			if keyframeEditor[0].sel>-1
						for i=0 to keyframes.length
							SetTextPosition(keyframes[i].txt, -200,-200 )
						next i	
			endif
		endif
			
			keyframeEditor[0].sel=-1
			keyframeEditor[0].curmy=0
		endif
		

		
		
		

		
		
	next i
	
	// space key to show information
	if GetRawKeyPressed(32)=1
		for i=0 to keyframes.length
			
			SetTextPosition(keyframes[i].txt, GetSpriteX(keyframes[i].spr),GetSpriteY(keyframes[i].spr)+20)
			SetTextString(keyframes[i].txt, left(str(keyframes[i].val#),5))
			
		next i
	endif
	
	
	
	// hide text
	if GetRawKeyReleased(32)=1
		for i=0 to keyframes.length
			
			SetTextPosition(keyframes[i].txt, -200,-200 )
			
		next i		
	endif
	

	endif



// offset of the clicked keyframe
if keyframeEditor.length>-1 
	if keyframeEditor[0].curmy>0
	// print(GetPointerY()-keyframeEditor[0].curmy)		// offset of the keyframe


		// the new y position of the keyframe
		keyframes[keyframeEditor[0].sel].val#=keyframes[keyframeEditor[0].sel].val#-(GetPointerY()-keyframeEditor[0].curmy)/keyframeEditor[0].multi#
		keyframeEditor[0].curmy=GetPointerY()


		// check for the boundaries
		if keyframes[keyframeEditor[0].sel].val# > keyframeEditor[0].max# then keyframes[keyframeEditor[0].sel].val# = keyframeEditor[0].max#
		if keyframes[keyframeEditor[0].sel].val# < keyframeEditor[0].min# then keyframes[keyframeEditor[0].sel].val# = keyframeEditor[0].min#


		// reposition the keyframe sprite
		px=GetSpriteX(keyframeEditor[0].bg)
		py=GetSpriteY(keyframeEditor[0].bg)
		sx=getspritewidth(keyframeEditor[0].bg)
		sy=getspriteheight(keyframeEditor[0].bg)
		
		
		
		SetSpritePosition(keyframes[keyframeEditor[0].sel].spr, GetSpriteX(keyframes[keyframeEditor[0].sel].spr), (py+sy/2)-(keyframes[keyframeEditor[0].sel].val#*keyframeEditor[0].multi#))
	endif








// handle the slider


 mx#=getpointerx()
 my#=getpointery()
 
 spr=getspritehit(mx#,my#)


			
			delta#=GetRawMouseWheelDelta()*6
			
				if delta#<>0.0 and GetSpriteWidth( keyframeEditor[0].sprPush)+delta#>20.0
				
					// print("change")
				change=1
				setspritesize( keyframeEditor[0].sprPush,GetSpriteWidth( keyframeEditor[0].sprPush)+delta#,GetSpriteHeight( keyframeEditor[0].sprPush))

				if GetSpriteWidth( keyframeEditor[0].sprPush)=>GetSpriteWidth( keyframeEditor[0].sprSL) 
					SetSpriteSize(keyframeEditor[0].sprPush, GetSpriteWidth( keyframeEditor[0].sprSL),GetSpriteHeight(keyframeEditor[0].sprPush))
				endif
				
				x#=mx#
				if x#<getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprPush)/2.0 then x#=getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprPush)/2.0
				if x#>getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprSL)-getspritewidth( keyframeEditor[0].sprPush)/2.0 then x#=getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprSL)-getspritewidth( keyframeEditor[0].sprPush)/2.0
				setspritepositionbyoffset( keyframeEditor[0].sprPush,x#,getspriteybyoffset( keyframeEditor[0].sprPush))
				
			endif
			
			
		 
		 
		 if getpointerstate()=1
			 if spr= keyframeEditor[0].sprPush or spr= keyframeEditor[0].sprSL
				x#=mx#
					
					// print("change")
					      change=1
				if x#<getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprPush)/2.0 then x#=getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprPush)/2.0
				if x#>getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprSL)-getspritewidth( keyframeEditor[0].sprPush)/2.0 then x#=getspritex( keyframeEditor[0].sprSL)+getspritewidth( keyframeEditor[0].sprSL)-getspritewidth( keyframeEditor[0].sprPush)/2.0
				setspritepositionbyoffset( keyframeEditor[0].sprPush,x#,getspriteybyoffset( keyframeEditor[0].sprPush))
			 endif
		 endif
 
 
 
 // update text information
 
// SetTextString(keyframeEditor[0].txt1,"Slider: "+str(GetValue( keyframeEditor[0].sprSL, keyframeEditor[0].sprPush))+"  /  "+str(GetSpriteWidth( keyframeEditor[0].sprPush)/GetSpriteWidth( keyframeEditor[0].sprSL)))
// SetTextString(keyframeEditor[0].txt2,"Max: "+str(0)+" / "+str(keyframes.length))
 
 
 view_maxwidth	=	keyframes.length*(GetSpriteWidth( keyframeEditor[0].sprPush)/GetSpriteWidth( keyframeEditor[0].sprSL))
 view_start#		=	(getspritexbyoffset(keyframeEditor[0].sprPush)-getspritewidth(keyframeEditor[0].sprPush)/2.0)-getspritex(keyframeEditor[0].sprSL) 
 
 

	new			=	keyframes.length*((100.0/getspritewidth(keyframeEditor[0].sprSL))*view_start#)*0.01

 SetTextString(keyframeEditor[0].txt2,str(new)+" - "+str(new + view_maxwidth )+" ("+str(keyframes.length)+")")


keyframeEditor[0].startP	=	new
keyframeEditor[0].endP		=	new + view_maxwidth

	// update the graph to the slider values
		if change=1
			set_Keyframe_cutout()
		endif

endif


endfunction 



function GetValue(sprSL,sprPush)
 
    x#=(getspritexbyoffset(sprPush)-getspritewidth(sprPush)/2.0)-getspritex(sprSL)
    w#=getspritewidth(sprSL)-getspritewidth(sprPush)

    v#=x#/w#
 if w#=0 then v#=0 
endfunction v#


function set_Keyframe_cutout()

// hide the rest
for i=0 to keyframes.length
SetSpritePosition(keyframes[i].spr,-200,-200)
next i

// show the cutout
for i=keyframeEditor[0].startP to keyframeEditor[0].endP
	
	
	xoff#=((GetSpriteWidth(keyframeEditor[0].bg)-10)/(keyframeEditor[0].endP-keyframeEditor[0].startP)*1.0)*(i-keyframeEditor[0].startP)
		
		
		
		
	SetSpritePosition(keyframes[i].spr,GetSpriteX(keyframeEditor[0].bg)+xoff#,(GetSpriteY(keyframeEditor[0].bg)+GetSpriteHeight(keyframeEditor[0].bg)/2-(keyframes[i].val#*keyframeEditor[0].multi#)))

next i


endfunction



function delete_KeyframeEditor()
	
	
	
	
for i=0 to keyframes.length

	if GetSpriteExists(keyframes[i].spr)=1 then DeleteSprite(keyframes[i].spr)
 	 if GetTextExists(keyframes[i].txt)=1 then DeleteText(keyframes[i].txt)

next i




	
	

	// sprite media
		if GetSpriteExists(keyframeEditor[0].bg)=1 then DeleteSprite(keyframeEditor[0].bg)


	// text media
		if GetTextExists(keyframeEditor[0].txtmin)=1 then DeleteText(keyframeEditor[0].txtmin)
		if GetTextExists(keyframeEditor[0].txtzero)=1 then DeleteText(keyframeEditor[0].txtzero)
		if GetTextExists(keyframeEditor[0].txtmax)=1 then DeleteText(keyframeEditor[0].txtmax)
		
		if GetTextExists(keyframeEditor[0].txt1)=1 then DeleteText(keyframeEditor[0].txt1)
		if GetTextExists(keyframeEditor[0].txt2)=1 then DeleteText(keyframeEditor[0].txt2)
		if GetTextExists(keyframeEditor[0].txt3)=1 then DeleteText(keyframeEditor[0].txt3)

	// slider media
		if GetSpriteExists(keyframeEditor[0].sprPush)=1 then DeleteSprite(keyframeEditor[0].sprPush)
		if GetSpriteExists(keyframeEditor[0].sprSL)=1 then DeleteSprite(keyframeEditor[0].sprSL)

	
	
	
undim keyframeEditor[]
undim keyframes[]
	
	
	
	

endfunction



// Keyframe Editor end