#option_explicit
#import_plugin Nuklear as nk

type connector
	source_index as integer
	dest_index as integer
endtype

type node
	name as string
	bounds as nk_rect
	link_index as integer
endtype


// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Nuklear Window" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 0, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

#insert "../Nuklear.agc"

// basic nuklear initialization
nkInit()

// init nodes
global node_array as node[]
global connector_array as connector[]

new_node as node
new_node.name = "component alpha"
new_node.bounds = nk_rect(10,50,150,150)
node_array.insert(new_node)

new_node.name = "component beta"
new_node.bounds = nk_rect(10,250,150,150)
node_array.insert(new_node)

new_node.name = "delta"
new_node.bounds = nk_rect(350,150,150,150)
node_array.insert(new_node)

LinkNode(node_array[0], node_array[2])
LinkNode(node_array[1], node_array[2])

// begin main loop
do
	// pass agk inputs to nuklear
    nk.HandleInput()

	// open a window
	if nk.WindowBegin("Window", 20, 50, 600, 540, NK_WINDOW_BORDER||NK_WINDOW_MOVABLE||NK_WINDOW_TITLE||NK_WINDOW_CLOSABLE||NK_WINDOW_NO_SCROLLBAR)
		region as nk_rect
		
		// window canvas is stored in slot '0' for later use to draw on 
		// the window surface.
		nk.WindowStoreCanvas(0)
		
		// get the area of the available space of the window.
		region = nkWindowGetContentRegion()

		// give the layout the entire window area. with a defined number 
		// of widgets.
		nk.LayoutSpaceBegin(NK_STATIC, region.h, node_array.length+1)

		// loop all available nodes
		i as integer
		for i=0 to node_array.length
			//  give the next widget a defined place on the window/panel.
			nk.LayoutSpacePush(node_array[i].bounds.x,node_array[i].bounds.y,node_array[i].bounds.w,node_array[i].bounds.h)
			
			// now a widget, in this case a group is packed into the 
			// layout. In this group more widgets can be placed.
			if nk.GroupBegin(node_array[i].name, NK_WINDOW_MOVABLE||NK_WINDOW_NO_SCROLLBAR||NK_WINDOW_BORDER||NK_WINDOW_TITLE)
				// store group-window panel for later use.
				nk.WindowStorePanel(1)
				
				// give the next widget (label) a place.
				nk.LayoutRowDynamic(25, 1)
				nk.Label("pos:"+str(trunc(node_array[i].bounds.x))+","+str(trunc(node_array[i].bounds.y)), NK_TEXT_CENTERED)
				
				// give each group an end mark, but only if it was opened.
				nk.GroupEnd()
			endif

			// here we get the new stored group-window panel bounds
			bound0 as nk_rect
			bound0 = nkLayoutSpaceRectToLocal(nkWindowGetStoredPanelBounds(1))

			// copy the new position to the node description.
			node_array[i].bounds.x = bound0.x
			node_array[i].bounds.y = bound0.y
		next

		// now draw all connectors
		for i=0 to connector_array.length
			DrawConnection(node_array[connector_array[i].source_index], node_array[connector_array[i].dest_index])
		next

		nk.LayoutSpaceEnd()
	endif
	nk.WindowEnd()

	if nk.WindowIsClosed("Window") then exit

    nkSync()
loop

function LinkNode(src ref as node, dst ref as node)
	new_link as connector
	new_link.source_index = node_array.find(src.name)
	new_link.dest_index = node_array.find(dst.name)

	connector_array.insert(new_link)
	src.link_index = connector_array.length
endfunction

function DrawConnection(src as node, dst as node)
	pos0 as nk_vec2
	pos1 as nk_vec2
	pos0.x = src.bounds.x + src.bounds.w
	pos0.y = src.bounds.y + src.bounds.h/2
	pos1.x = dst.bounds.x
	pos1.y = dst.bounds.y + dst.bounds.h/2

	pos0 = nkLayoutSpaceToScreen(pos0)
	pos1 = nkLayoutSpaceToScreen(pos1)

	nk.StrokeCurve(0, pos0.x,pos0.y, pos0.x+50,pos0.y, pos1.x-50,pos1.y, pos1.x,pos1.y, 1.5, 0xffff00ff)
endfunction
