#option_explicit
#import_plugin Nuklear as nk

// 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()


show_menu as Integer = nk_true
overwrite as Integer = nk_false
usage as float = 768.25
max_mem as float = 1024
show_as_mb as integer = nk_true
global chart_values as float[13]
chart_range as float = 300
// begin main loop
do
	// pass agk inputs to nuklear
    nk.HandleInput()
	
	// Get the mouse data to display in a chartwidget
	GetMouseMoveData()
	
	// 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)
		if show_menu
			// Start of the menubar
			nk.MenubarBegin()

			// First menu to display common menu items
			// The menu bar is also a panel that can contain all kinds of 
			// widgets. Therefore widgets must be inserted in a layout.
			// We have 3 menu entries with a menu bar size of 25 px.
			nk.LayoutRowBegin(NK_STATIC, 25, 3)
			
			// The first menubar item is 45px width.
			nk.LayoutRowPush(45)
			
			// Start of the menubar item. MenuBeginLabel' opens a panel, 
			// in this case 120px width and 150px height. 'File' is the 
			// title of the item and 'NK_TEXT_CENTERED' is the alignment 
			// of the title.
			if(nk.MenuBeginLabel("File", NK_TEXT_CENTERED, 120, 150))
				
				// Since a panel has been opened, the widgets must be 
				// added to a layout.
				nk.LayoutRowDynamic(25, 1)
				
				// Now the individual menu items for 'File' follow. 
				// These are simple items. The last one with the 
				// function to leave the main loop to end the program.
				nk.MenuItemLabel("New", NK_TEXT_LEFT)
				nk.MenuItemLabel("Open", NK_TEXT_LEFT)
				nk.MenuItemLabel("Save", NK_TEXT_LEFT)
				nk.MenuItemLabel("Close", NK_TEXT_LEFT)
				if nk.MenuItemLabel("Exit", NK_TEXT_LEFT) then exit
				
				// End of the first menubar item.
				nk.MenuEnd()
			endif

			// Now follows the second menubar item.
			nk.LayoutRowPush(45)
			if(nk.MenuBeginLabel("Edit", NK_TEXT_CENTERED, 150, 300))
				nk.LayoutRowDynamic(25, 1)
				nk.MenuItemLabel("Copy", NK_TEXT_LEFT)
				nk.MenuItemLabel("Delete", NK_TEXT_LEFT)
				nk.MenuItemLabel("Cut", NK_TEXT_LEFT)
				nk.MenuItemLabel("Paste", NK_TEXT_LEFT)
				
				// Insert a label as seperator.
				nk.Label("~~~~~~~~~~~~~~~~~~~~~", NK_TEXT_CENTERED)
				
				// Remember: The 'MenuBegin...' command opens a panel and 
				// the all items can be a widgets. Here you see how to 
				// insert a checkox, Progressbar, Slider and option widgets.
				overwrite = nk.CheckboxLabel("Overwrite", overwrite)
				nk.LayoutRowDynamic(25, 2)
				nk.Progress(usage, max_mem, NK_FIXED)
				if show_as_mb
					nk.Label(str(usage,2)+" MB", NK_TEXT_LEFT)
				else
					nk.Label(str((usage/max_mem)*100,2)+" %", NK_TEXT_LEFT)
				endif
				nk.LayoutRowDynamic(25, 1)
				max_mem = nk.SliderFloat(usage, max_mem, 2048, 64)
				if nk.OptionLabel("Show as MegByte", show_as_mb) then show_as_mb = nk_true
				if nk.OptionLabel("Show as Percent", 1 - show_as_mb) then show_as_mb = nk_false
				
				// End of the first menubar item.
				nk.MenuEnd()
			endif

			// The third menubar item. Here you can see a chart widget 
			// with live data. The procedure is the same as for the last 
			// menu item.
			nk.LayoutRowPush(45)
			if(nk.MenuBeginLabel("Data", NK_TEXT_CENTERED, 400, 300))
				nk.LayoutRowDynamic(25, 1)
				nk.Label("Move Mouse", NK_TEXT_CENTERED)

				nk.LayoutRowDynamic(100,1)
				nk.ChartBegin(NK_CHART_LINES, chart_values.length, 0, chart_range)
				i as integer
				for i=0 to chart_values.length
					nk.ChartPush(chart_values[i])
				next
				nk.ChartEnd()

				nk.LayoutRowBegin(NK_DYNAMIC, 25, 3)
				nk.LayoutRowPush(0.1)
				nk.Label("range:", NK_TEXT_LEFT)
				nk.LayoutRowPush(0.8)
				chart_range = nk.SliderFloat(100, chart_range, 700, 1.0)
				nk.LayoutRowPush(0.1)
				nk.Label(str(chart_range, 0), NK_TEXT_LEFT)				

				nk.LayoutRowDynamic(25, 1)
				nk.MenuItemLabel("Save Data", NK_TEXT_LEFT)
				nk.MenuEnd()
			endif
			nk.MenubarEnd()
		endif
	endif
	nk.WindowEnd()

	if nk.WindowIsClosed("Window") then exit

    nkSync()
loop

// Create mouse movement data. It calculates the average mouse movement 
// distance every 200ms and stores it in a global variable (chart_values).
function GetMouseMoveData()
	global dist as float
	global updt as integer
	now as integer
	
	x as float
	y as float
	x = nk.InputGetMouseDeltaX()
	y = nk.InputGetMouseDeltaY()

	dist = dist + Sqrt(x * x + y * y)

	now = GetMilliseconds()
	if now > updt
		updt = now + 200
		
		i as integer
		for i=0 to chart_values.length-1
			chart_values[i] = chart_values[i+1]
		next i

		chart_values[chart_values.length] = dist
		dist = Sqrt(x * x + y * y)
	endif 
	
endfunction
