#import_plugin Nuklear as nk

// show all errors
SetErrorMode(2)

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

// set display properties
SetVirtualResolution( 1280, 720 ) // 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()

#constant POPUP_1 = 1
#constant POPUP_2 = 2
#constant FNC_SINUS = 0
#constant FNC_COSINUS = 1

global steps as integer = 32
global popup as integer = 0
global mode as integer = NK_CHART_COLUMN
global fnc as integer = 0
global chart_array as float[23]
global pop2_min as float = -25.0
global pop2_max as float = 100.0

// For the second chart the values are stored in an array.
i as integer
for i=0 to chart_array.length
	chart_array[i] = (Random(0, 1250)-250) / 10.0
next

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

	// open window
	if nk.WindowBegin("Chart and Popup Demo", 80, 50, 640, 360, NK_WINDOW_BORDER||NK_WINDOW_MOVABLE||NK_WINDOW_TITLE)
		client as nk_rect
		bounds as nk_rect
		
		// Get client area from the window.
		client = nkWindowGetContentRegion()
		nk.LayoutRowDynamic(120, 1)
		
		// Get area from the widget.
		bounds = nkWidgetBounds()
		min as float = 0.0

		// If fnc is set to GNC_COSINUS, the minimum value must be set to
		// -1. cos returns the value range -1.0 to 1.0.
		if fnc = FNC_COSINUS then min = -1.0
		
		// Start drawing the chart parameters are (from left to right) 
		// drawing mode (line- or bar- graphic), number of values minimum 
		// value and maximum value.
		nk.ChartBegin(mode, steps, min, 1.0)
		
		// Insert all values between 0 and 180.
		for i=0 to steps-1
			if fnc = FNC_SINUS
				nk.ChartPush(Sin((i*(180.0/steps))))
			else
				nk.ChartPush(Cos((i*(180.0/steps))))
			endif
		next
		nk.ChartEnd()

		// If the right mouse button was pressed on the chart, the position
		// of the mouse pointer is remembered so that one of the popup 
		// fields can be opened there. The popup variable is set so that 
		// the correct popup window can be opened later.
		if nkInputIsMouseClickInRect(NK_BUTTON_RIGHT, bounds)
			popup = POPUP_1
			
			// Calculate the upper left corner of the pop-up window.
			mx = GetRawMouseX() - client.x-3
			my = GetRawMouseY() - client.y-3
		endif

		// Get the next widget area.
		bounds = nkWidgetBounds()
		
		// Now another way to pass the values to a chart. Here it is 
		// done with an array.
		nkChartColoredArray(NK_CHART_COLUMN, nkRGB(255, 255, 0), nkRGB(0,255,255), chart_array, pop2_min, pop2_max)

		// Also here a query for the right mouse button to open a popup 
		// window for the second chart.
		if nkInputIsMouseClickInRect(NK_BUTTON_RIGHT, bounds)
			popup = POPUP_2
			mx = GetRawMouseX() - client.x-3
			my = GetRawMouseY() - client.y-3
		endif

		// Depending on the variable popup, a corresponding popup window 
		// is opened.
		select popup
			case POPUP_1:
				PopupChart1(mx, my)
			endcase
			case POPUP_2:
				PopupChart2(mx, my)
			endcase
		endselect
	endif
	nk.WindowEnd()

    nkSync()
loop


function PopupChart1(mx as float, my as float)
	// Like a normal window, a pop-up window is opened here. It requires 
	// a popup type, a title, the window flags, the position of the popup 
	// window and the size.
	if nk.PopupBegin(NK_POPUP_STATIC, "popup_chart_1", NK_WINDOW_BORDER||NK_WINDOW_DYNAMIC||NK_WINDOW_NO_SCROLLBAR, mx, my, 220,140)
		// give it a layout of one dynamic column and a row height of 
		// 20 pixels.
		nk.LayoutRowDynamic(20, 1)

		// let the user choose between a bar graph or a line graph
		if nk.OptionLabel("Bar Chart", mode = NK_CHART_COLUMN) then mode = NK_CHART_COLUMN
		if nk.OptionLabel("Line Chart", mode = NK_CHART_LINES) then mode = NK_CHART_LINES
		
		// Selection of the accuracy. Determines the number of steps how 
		// the sine/cosine curve is displayed.  
		label as String
		select steps
			case 8: label = "LOW - Resolution ": endcase
			case 16: label = "MED - Resolution ": endcase
			case 32: label = "HIGH - Resolution ": endcase
			case 64: label = "ULTRA - Resolution ": endcase
		endselect
		label = label + str(steps)

		ratio as float[2] = [0.1,0.8,0.1]
		nkLayoutRow(NK_DYNAMIC, 20, ratio)

		if nk.ButtonSymbol(NK_SYMBOL_TRIANGLE_LEFT) And steps > 8 then steps = steps / 2
		nk.Label(label, NK_TEXT_CENTERED)
		if nk.ButtonSymbol(NK_SYMBOL_TRIANGLE_RIGHT) And steps < 64 then steps = steps * 2

		// Again as option widget selection of the curve.
		nk.LayoutRowDynamic(20, 1)
		if nk.OptionLabel("Sinus Fnc", fnc = FNC_SINUS) then fnc = FNC_SINUS
		if nk.OptionLabel("Cosinus Fnc", fnc = FNC_COSINUS) then fnc = FNC_COSINUS

		// If the mouse cursor is moved outside the pop-up, the pop-up 
		// window should close.
		if nk.WindowIsHovered() = NK_FALSE
			popup = 0
			nk.PopupClose()
		endif
	endif
	
	// Closes the pop-up window
	nk.PopupEnd()
endfunction

// The construction of the second popup window is similar to the first, 
// but with other widgets inside. It offers the possibility to add new 
// random values and to change the limits min/max.
function PopupChart2(mx as float, my as float)
	if nk.PopupBegin(NK_POPUP_STATIC, "popup_chart_2", NK_WINDOW_BORDER||NK_WINDOW_DYNAMIC||NK_WINDOW_NO_SCROLLBAR, mx, my, 220,140)
		nk.LayoutRowDynamic(20, 1)
		
		if nk.ButtonLabel("Randomize")
			for i=0 to chart_array.length
				chart_array[i] = (Random(0, (pop2_max-pop2_min)*10.0)+(pop2_min*10.0)) / 10.0
			next
			popup = 0
		endif
		
		pop2_min = nk.PropertyFloat("Minimum", -500.0, pop2_min, 0.0, 2.5,1.0)
		pop2_max = nk.PropertyFloat("Maximum", 0.0, pop2_max, 500.0, 2.5,1.0)

		if nk.WindowIsHovered() = NK_FALSE And nk.InputIsMouseDown(NK_BUTTON_LEFT) = NK_FALSE
			popup = 0
			nk.PopupClose()
		endif
	endif
	nk.PopupEnd()
endfunction
