Strict

'Framework BRL.EventQueue
Import pantson.morph
'Import BRL.Win32MaxGUI

' modules which may be required:
' Import BRL.PNGLoader
' Import BRL.BMPLoader
' Import BRL.TGALoader
' Import BRL.JPGLoader

Global main_wndw:tgadget
Global source_cnvs:tgadget
'Global dest_cnvs:tgadget
Global source_p:TPixmap
'Global dest_p:TPixmap

Global gridx_tfld:tgadget
Global gridy_tfld:tgadget

Global scale_sx:Float,scale_sy:Float
'Global scale_dx:Float,scale_dy:Float

Global preview_bttn:tgadget

Const MENU_EXIT=105
Const MENU_LOADIS=101
'Const MENU_LOADID=102
Const MENU_LOADMS=103
'Const MENU_LOADMD=104
Const MENU_SAVEMS=106
Const MENU_NEWM=107

Global gridx:Int,gridy:Int
Global s_m:tmorph,d_m:tmorph

Global s_p:TPixmap ', d_p:TPixmap

Init_Window()
'dest_p = loadguiimage("02a.png",scale_dx,scale_dy)
'source_p = loadguiimage("01b.png",scale_sx,scale_sy)
's_m = guiloadmorph("01b.txt")
'dest_p = loadguiimage("02b.png",scale_dx,scale_dy)
'd_m = guiloadmorph("02b.txt")

draw_stuff()
Local i:Int,j:Int,c:Int,x:Int,y:Int
Global selected:Int

selected = -1

' do itj
While WaitEvent()
'	Print CurrentEvent.ToString()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
		Case EVENT_MENUACTION
			Select EventData()
				Case MENU_NEWM
					newmorph()
				Case MENU_EXIT
					End
				Case MENU_LOADIS
					source_p = loadguiimage(RequestFile("Open Image","image:png,jpg,bmp",False,CurrentDir()+"/"),scale_sx,scale_sy)
					draw_stuff()	
'				Case MENU_LOADID
'					dest_p = loadguiimage(RequestFile("Select Image","image:png,jpg,bmp",False,CurrentDir()),scale_dx,scale_dy)
				Case MENU_LOADMS
					s_m = LoadMorph(RequestFile("Open Morph file","",False,CurrentDir()+"/"))
					draw_stuff()	
'				Case MENU_LOADMD
'					d_m = loadmorph(RequestFile("Select morph file"))
				Case MENU_SAVEMS
					If s_m <> Null Then SaveMorph(s_m,RequestFile("Save Morph file","",True))
			End Select
		Case EVENT_MOUSEDOWN
			If ActiveGadget() = source_cnvs
			x = EventX()
			y = EventY()
			
			i = 0
			While i < s_m.w
				j = 0 
				While j < s_m.h
					c = (j*s_m.w) + i
					If s_m.x[c]/scale_sx > x-2
					If s_m.x[c]/scale_sx < x+2
					If s_m.y[c]/scale_sy > y-2
					If s_m.y[c]/scale_sy < y+2
						selected = c
					EndIf
					EndIf
					EndIf
					EndIf
					j:+1
				Wend
				i:+1
			Wend
			EndIf
'			Print "sel" + selected
		Case EVENT_MOUSEUP
			selected = -1
		Case EVENT_MOUSEMOVE
			If selected > -1
				s_m.x[selected] = Min(198,Max(0,EventX()))*scale_sx
				s_m.y[selected] = Min(198,Max(0,EventY()))*scale_sy
'				Print "set"
			EndIf
	End Select
	
	draw_stuff()	
Wend

Function newmorph()
	Local window:tgadget
	Local ok_bttn:tgadget
	Local cncl_bttn:tgadget
	
	Local x_txt:tgadget
	Local y_txt:tgadget	
	
	Local EXT:Int
	
	Local i:Int,j:Int
	Local w:Int,h:Int,mx:Int,my:Int,c:Int
	
'	Local cnvs:tgadget
'	Local slider:tgadget
		
	window = CreateWindow("New morph",200,200,200,170,main_wndw,WINDOW_TITLEBAR)

	x_txt = CreateTextField(90,5,40,20,window)
	CreateLabel("X",70,8,20,20,window)
	SetGadgetText x_txt,"4"

	y_txt = CreateTextField(90,45,40,20,window)
	CreateLabel("Y",70,48,20,20,window)
	SetGadgetText y_txt,"4"

	OK_bttn = CreateButton("OK",10,100,60,30,window)
	cncl_bttn = CreateButton("CANCEL",120,100,60,30,window)
	
	EXT = 0
	While WaitEvent() And EXT=0
		Select EventID()
		Case EVENT_WINDOWCLOSE
			EXT = 1 ' exit
		Case EVENT_GADGETACTION
			Select EventSource()
				Case ok_bttn
					EXT = 2
				Case cncl_bttn
					EXT = 1
			End Select
		End Select
		
		If Int(TextFieldText(x_txt))=0 Or Int(TextFieldText(y_txt))=0
			DisableGadget ok_bttn
		Else
			EnableGadget ok_bttn
		EndIf
	Wend
	
	If EXT = 2
		s_m = Null
		GCCollect()
		w = Int(TextFieldText(x_txt))
		h = Int(TextFieldText(y_txt))
		s_m = CreateMorph(w,h)
		If source_p = Null
			scale_sx = 1
			scale_sy = 1
		EndIf
		
		mx = 200/(w-1) 
		my = 200/(h-1)
		
		i = 0
		While i < w
			j = 0
			While j < h
				c = i + (j*w)
				s_m.x[c] = Min(199,i*mx)*scale_sx
				s_m.y[c] = Min(199,j*my)*scale_sy
				
				j:+1
			Wend
			i:+1
		Wend

	EndIf
	
	FreeGadget window
	window = Null
	
End Function

Function Draw_Stuff()
	Local x:Int,y:Int,i:Int
	
'	SetBlend solidblend
	SetColor 255,255,255
	SetGraphics CanvasGraphics(source_cnvs)
	Cls
	
	If source_p <>  Null Then DrawPixmap source_p,0,0
	
'	SetBlend solidblend
	SetColor 255,255,255
	If s_m <> Null		
		x = 0
		While x < s_m.w-1
			y = 0
			While y < s_m.h-1
				i = y*s_m.w + x
				
				SetColor 255,255,255
				DrawLine s_m.x[i]/scale_sx,s_m.y[i]/scale_sy,s_m.x[i+1]/scale_sx,s_m.y[i+1]/scale_sy
				DrawLine s_m.x[i]/scale_sx,s_m.y[i]/scale_sy,s_m.x[i+s_m.w]/scale_sx,s_m.y[i+s_m.w]/scale_sy
				
				If selected = i Then SetColor 255,0,0
				DrawRect s_m.x[i]/scale_sx-2,s_m.y[i]/scale_sy-2,5,5
				y:+1
			Wend
			x:+1
		Wend

		y = s_m.h-1
		x = 0
		While x < s_m.w-1	
			i = y*s_m.w + x
			
			SetColor 255,255,255
			DrawLine s_m.x[i]/scale_sx,s_m.y[i]/scale_sy,s_m.x[i+1]/scale_sx,s_m.y[i+1]/scale_sy
			
			If selected = i Then SetColor 255,0,0
			DrawRect s_m.x[i]/scale_sx-2,s_m.y[i]/scale_sy-2,5,5
			x:+1
		Wend

		x = s_m.w-1
		y = 0
		While y < s_m.h-1
			i = y*s_m.w + x

			SetColor 255,255,255			
			DrawLine s_m.x[i]/scale_sx,s_m.y[i]/scale_sy,s_m.x[i+s_m.w]/scale_sx,s_m.y[i+s_m.w]/scale_sy
			
			If selected = i Then SetColor 255,0,0
			DrawRect s_m.x[i]/scale_sx-2,s_m.y[i]/scale_sy-2,5,5
			y:+1
		Wend

		i = s_m.w*s_m.h - 1
		SetColor 255,255,255
		If selected = i Then SetColor 255,0,0
		DrawRect s_m.x[i]/scale_sx-2,s_m.y[i]/scale_sy-2,5,5
	EndIf
	Flip

Rem	
'	SetBlend solidblend
	SetGraphics CanvasGraphics(dest_cnvs)
	Cls
	If dest_p <>  Null Then DrawPixmap dest_p,0,0

	If d_m <> Null		
		x = 0
		While x < d_m.w-1
			y = 0
			While y < d_m.h-1
				i = y*d_m.w + x

				SetColor 255,255,255				
				DrawLine d_m.x[i]/scale_dx,d_m.y[i]/scale_dy,d_m.x[i+1]/scale_dx,d_m.y[i+1]/scale_dy
				DrawLine d_m.x[i]/scale_dx,d_m.y[i]/scale_dy,d_m.x[i+d_m.w]/scale_dx,d_m.y[i+d_m.w]/scale_dy
				
				If selected = i Then SetColor 255,0,0
				DrawRect d_m.x[i]/scale_dx-2,d_m.y[i]/scale_dy-2,5,5
				y:+1
			Wend
			x:+1
		Wend

		y = d_m.h-1	
		x = 0
		While x < d_m.w-1
			i = y*d_m.w + x
				
			SetColor 255,255,255
			DrawLine d_m.x[i]/scale_dx,d_m.y[i]/scale_dy,d_m.x[i+1]/scale_dx,d_m.y[i+1]/scale_dy		

			If selected = i Then SetColor 255,0,0
			DrawRect d_m.x[i]/scale_dx-2,d_m.y[i]/scale_dy-2,5,5
			x:+1
		Wend


		x = d_m.w-1
		y = 0
		While y < d_m.h-1
			i = y*d_m.w + x

			SetColor 255,255,255			
			DrawLine d_m.x[i]/scale_dx,d_m.y[i]/scale_dy,d_m.x[i+d_m.w]/scale_dx,d_m.y[i+d_m.w]/scale_dy
	
			If selected = i Then SetColor 255,0,0
			DrawRect d_m.x[i]/scale_dx-2,d_m.y[i]/scale_dy-2,5,5
			y:+1
		Wend
		i = d_m.w*d_m.h - 1
		SetColor 255,255,255
		If selected = i Then SetColor 255,0,0
		DrawRect d_m.x[i]/scale_dx-2,d_m.y[i]/scale_dy-2,5,5

	EndIf
	Flip
EndRem

End Function

Function Init_Window()
	Local menu:tgadget
	
	' window setup
	main_wndw = CreateWindow("Morph GUI",100,100,218,255,Desktop(),WINDOW_TITLEBAR+WINDOW_MENU)
	
	source_cnvs = CreateCanvas(5,5,200,200,main_wndw)
	SetGraphics CanvasGraphics(source_cnvs)
	Cls

Rem	
	dest_cnvs = CreateCanvas(300,5,200,200,main_wndw)
	SetGraphics CanvasGraphics(dest_cnvs)
	Cls
EndRem

	' menu setup
	menu=CreateMenu("&File",0,WindowMenu(main_wndw))
	
'	menu=CreateMenu("&Source",0,WindowMenu(main_wndw))
	CreateMenu "&New Morph",MENU_NEWM,menu
	CreateMenu"",0,menu
	CreateMenu "&Open Image",MENU_LOADIS,menu
	CreateMenu "&Open Morph",MENU_LOADMS,menu
	CreateMenu"",0,menu
	CreateMenu "&Save Morph",MENU_SAVEMS,menu
	CreateMenu"",0,menu
	CreateMenu "&Exit",MENU_EXIT,menu

'	menu=CreateMenu("&Destination",0,WindowMenu(main_wndw))
'	CreateMenu "&Load Image",MENU_LOADID,menu
'	CreateMenu "&Load Morph",MENU_LOADMD,menu

	UpdateWindowMenu main_wndw
	
'	preview_bttn = CreateButton("Preview",275,200,60,30,main_wndw)
End Function

Function LoadGUIimage:TPixmap(file:String,scalex:Float Var,scaley:Float Var)
	Local p:TPixmap
	Local n:TPixmap
	
'	Print "here"
	p = LoadPixmap(file)
	If p = Null Then Return Null
'	Print "there"
	
	n = ResizePixmap(p,200,200)
	scalex = Float(PixmapWidth(p))/200.0
	scaley = Float(PixmapHeight(p))/200.0
	
	p = Null
	Return n
End Function

Function GUIloadMorph:tmorph(file:String)
	Local m:tmorph
	
	m = LoadMorph(file)
	If m = Null Then Return Null
	
	Return m
End Function