#Include Once "glfw3.bi"
#include once "AntTweakBar.bi"

sub _init_ constructor
	if glfwInit()=0 then
		dim as integer hFile = FreeFile()
		open err for output as #hFile
		print #hFile,"error: glfwInit() !"
		beep:sleep
		close #hFile
		end 1
	end if
end sub

sub _exit_ destructor
	glfwTerminate()
end sub

function CreateWindow(w as integer,h as integer,title as zstring ptr,fullscreen as integer=0) as GLFWwindow ptr
	dim as GLFWmonitor ptr mon=0
	if fullscreen then mon = glfwGetPrimaryMonitor()
	return glfwCreateWindow(w,h,title,mon,0)
end function

sub FramebufferSizeCallback CALLBACK (win as GLFWwindow ptr, w as integer, h as integer)
	? w,h
	glViewport(0, 0, w, h)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(40, w/h, 1, 10)
	gluLookAt(-1,0,3, 0,0,0, 0,1,0)

	' Send the new window size to AntTweakBar

end sub

sub WindowposCalloback CALLBACK(win as GLFWwindow ptr, x as integer, y as integer)

end sub

sub WindowsizeCallback CALLBACK(win as GLFWwindow ptr, w as integer, h as integer)
	TwWindowSize(w,h)
end sub
sub WindowcloseCallback CALLBACK(win as GLFWwindow ptr)

end sub
sub WindowrefreshCallback CALLBACK(win as GLFWwindow ptr)

end sub
sub WindowfocusCallback CALLBACK(win as GLFWwindow ptr, focused as integer)

end sub
sub WindowiconifyCallback CALLBACK(win as GLFWwindow ptr, iconified as integer)

end sub
sub MousebuttonCallback CALLBACK(win as GLFWwindow ptr, button as integer, action as integer, buttonbits as integer)
	dim as integer but
	select case button
		case GLFW_MOUSE_BUTTON_LEFT   : but or=TW_MOUSE_LEFT
		case GLFW_MOUSE_BUTTON_RIGHT  : but or=TW_MOUSE_RIGHT
		case GLFW_MOUSE_BUTTON_MIDDLE : but or=TW_MOUSE_MIDDLE
	end select
	select case action
		case GLFW_RELEASE : action = TW_MOUSE_RELEASED
		case GLFW_PRESS   : action = TW_MOUSE_PRESSED
	end select
	TwMouseButton(action,but)
end sub

sub CursorposCallback CALLBACK(win as GLFWwindow ptr, x as double, y as double)
	TwMouseMotion(x,y)
end sub

sub ScrollCallback CALLBACK(win as GLFWwindow ptr, xoff as double, yoff as double)
	TwMouseWheel(int(yoff))
end sub

sub KeyCallback CALLBACK(win as GLFWwindow ptr, key as integer, scancode as integer, action as integer, Modifier as integer)
	dim as integer modi = TW_KMOD_NONE
	if action=GLFW_PRESS then
		if key>=GLFW_KEY_F1 and key<=GLFW_KEY_F12 then
			key-=8
		elseif key=GLFW_KEY_ENTER then
			key=TW_KEY_RETURN
		end if
		if Modifier and GLFW_MOD_SHIFT   then modi or= TW_KMOD_SHIFT
		if Modifier and GLFW_MOD_CONTROL then modi or= TW_KMOD_CTRL
		if Modifier and GLFW_MOD_ALT     then modi or= TW_KMOD_ALT
		if Modifier and GLFW_MOD_SUPER   then modi or= TW_KMOD_META
		TwKeyPressed(key,modi)
	end if
end sub

sub CharCallback CALLBACK(win as GLFWwindow ptr, unicode as uinteger)

end sub

sub WMonitorCallback CALLBACK(win as GLFWmonitor ptr, event as integer)

end sub

sub MyButtonCallback TW_CALL(clientData as any ptr)

end sub

sub CursorenterCallback CALLBACK(win as GLFWwindow ptr, entered as integer)

end sub

dim as GLFWwindow ptr win = CreateWindow(800,600,"test01")
'glfwSetFramebufferSizeCallback(win,@FramebufferSizeCallback)
glfwSetWindowSizeCallback(win,@WindowsizeCallback)
glfwSetCursorPosCallback(win,@CursorPosCallback)
glfwSetMouseButtonCallback(win,@MouseButtonCallback)
glfwSetKeyCallback(win,@KeyCallback)
'glfwSetCharCallback(win,@CharCallback)
glfwSetScrollCallback(win,@ScrollCallback)

glfwMakeContextCurrent(win)

TwInit(TW_OPENGL,0)
TwWindowSize(800,600)

dim as double tim
dim as double dt
dim as double turn
dim as double speed = 0.3
dim as integer wire
dim as single position(...) = { 0.1, 0.2, 0.4 }
dim as single bgColor(...) = { 0.1, 0.2, 0.4 }
dim as ubyte cubeColor(...) = { 255, 0, 0, 128 }
dim as TwBar ptr bar



bar = TwNewBar("bar1")
TwDefine("bar1 label='This is a TweakBar!' color='255 128 0' text=light alpha=255")

'TwDefine("GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' ")
' 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, @speed,"label='Rot speed' min=0 max=2 step=0.01 keyIncr=s keyDecr=S help='Rotation speed (turns/second)' ")

' Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, @wire, " label='Wireframe mode' key=w help='Toggle wireframe display mode.' ")

' Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, @tim, " label='Time' precision=1 help='Time (in seconds).' ")

TwAddSeparator(bar, "sep1",0)
' Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, @bgColor(0), " label='Background color' ")
TwAddVarRO(bar, "px", TW_TYPE_FLOAT, @position(0), " label='Position X'")
TwAddVarRO(bar, "py", TW_TYPE_FLOAT, @position(1), " label='Position Y'")
TwAddVarRO(bar, "pz", TW_TYPE_FLOAT, @position(2), " label='Position Z'")
TwAddSeparator(bar, "sep2",0)
' Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, @cubeColor(0), " label='Cube color' alpha help='Color and transparency of the cube.' ")

TwAddButton(bar,"button1",@MyButtonCallback,0," label='label' help='a button.' ")


while glfwWindowShouldClose(win)=0 and glfwGetKey(win,GLFW_KEY_ESCAPE)=0
	glClear( GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	' ...
	' draw scene
	'...
	TwDraw()
	TwRefreshBar(bar)
	glfwSwapBuffers(win)
	position(1)+=0.1

	glfwPollEvents()
wend
TwTerminate()