#import_plugin Nuklear as nk

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Nuklear Demo" )
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"

// constants for the tabs.
#constant	PICKER_TAB = 1
#constant	EXTRA_TAB = 2
#constant	DRAWING_TAB = 3

// basic nuklear initialization
nkInit()

nk.FontStashBegin()
	nk.FontAtlasAddFromSystem("arial", "arial.ttf", 24)
nk.FontStashEnd()

LoadImage(1, "tools.png")
nk.ImageToSlot(0, 1)

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

	// open the window
	if nk.WindowBegin("ColorPicker, Widget and Drawing Commands Demo", 40, 50, 1024, 576, NK_WINDOW_BORDER||NK_WINDOW_MOVABLE||NK_WINDOW_TITLE||NK_WINDOW_NO_SCROLLBAR)
		
		// Here is a simple way to make notetabs.
		nk.LayoutRowStatic(20, 150, 3)
		if nk.SelectableLabel("Colorpicker", NK_TEXT_CENTERED, selected = PICKER_TAB) then selected = PICKER_TAB
		if nk.SelectableLabel("Extra", NK_TEXT_CENTERED, (selected = EXTRA_TAB)) then selected = EXTRA_TAB
		if nk.SelectableLabel("Drawing", NK_TEXT_CENTERED, selected = DRAWING_TAB) then selected = DRAWING_TAB
		
		// depending on the selected tab the corresponding function is 
		// branched to. These functions define the remaining window 
		// structure.
		select selected
			case PICKER_TAB: do_picker_tab():endcase
			case EXTRA_TAB: do_extra_tab():endcase
			case DRAWING_TAB: do_drawing_tab():endcase
		endselect
	endif
	nk.WindowEnd()
	
    nkSync()
loop

// this demonstrates the colorpicker. The currently selected color is 
// passed as nk_colorf and with NK_RGBA/NKRGB you can choose whether the 
// colorpicker should be displayed with or without alpha channel. The 
// return value is the changed color as integer.
function do_picker_tab()
	global sel_color as nk_colorf
	result as integer
	
	// layout and the call of the colorpicker.
	nk.LayoutRowStatic(256, 256, 1)
	result = nkColorPicker(sel_color, NK_RGBA)
	
	// this displays the currently selected color.
	nk.LayoutRowStatic(25, 256, 1)
	nk.ButtonColor(result)
	
	// all color chanels as values.
	nk.LayoutRowStatic(20, 64, 4)
	nk.Label("R:"+str(GetColorRed(result)), NK_TEXT_CENTERED)
	nk.Label("G:"+str(GetColorGreen(result)), NK_TEXT_CENTERED)
	nk.Label("B:"+str(GetColorBlue(result)), NK_TEXT_CENTERED)
	nk.Label("A:"+str(GetColorAlpha(result)), NK_TEXT_CENTERED)
endfunction

// here you can see how to use the draw commands to draw over the widget.
function do_extra_tab()
	global value as integer
	
	// store window canvas in slot 0
	nk.WindowStoreCanvas(0)
	
	// prepare and display the info text
	nk.LayoutRowDynamic(25, 1)
	nk.Label("Here is an example of how to pimp a standard widget. Do you want to label the progress bar?", NK_TEXT_CENTERED)

	// prepare the Progressbar
	nk.LayoutRowDynamic(40, 1)
	bounds as nk_rect
	
	// get the widget bounds of the following widget.
	bounds = nkWidgetBounds()
	
	// draw the progressbar
	value = nk.Progress(value, 1000, nk_true)

	// build the label
	label as String
	label = str(value/10.0,1)+"% Processed"

	// set the label in the center of the widget
	inc bounds.x,(bounds.w-nk.FontGetLabelWidth(label))/2.0
	inc bounds.y,(40.0-nk.FontGetHeight())/2.0
	
	// and draw the text/label
	nkDrawText(0, bounds, label, "default", nkRGBA(0,0,0,0), nkRGB(192,225,225))
endfunction

function do_drawing_tab()
	white as nk_color
	black as nk_color
	red as nk_color
	green as nk_color
	blue as nk_color
	yellow as nk_color
	bounds as nk_rect
	txt_rect as nk_rect
	draw_rect as nk_rect

	// Store window canvas to slot 0.
	nk.WindowStoreCanvas(0)
	
	// get position and size of the whole windo content.
	bounds = nkWindowGetContentRegion()
	txt_rect.x = bounds.x+25
	txt_rect.y = bounds.x+60
	txt_rect.w = 128
	txt_rect.h = 20
	draw_rect = txt_rect
	inc draw_rect.y, 30
	draw_rect.w = 120
	draw_rect.h = 90
	 
	// define some default variables
	white = nkRGB(255,255,255)
	black = nkRGB(0,0,0)
	red = nkRGB(255,0,0)
	green = nkRGB(0,255,0)
	blue = nkRGB(0,0,255)
	yellow = nkRGB(255,255,0)
	
	// draw e rect around the entire drawable window space.
	nkStrokeRect(0, bounds, 15.0, 2.0, white)
	
	// draw the text and the corresponding line.
	nkDrawText(0, txt_rect, "Line", "arial", black, yellow)
	nkStrokeLine(0, nk_vec2(draw_rect.x,draw_rect.y), nk_vec2(draw_rect.x+draw_rect.w,draw_rect.y+draw_rect.h), 2, red)
	
	// move the Text-Rect and Drawable-Rect to the right.
	// draw the text and the corespnding curve.
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "Curve", "arial", black, yellow)	
	nkStrokeCurve(0, nk_vec2(draw_rect.x,draw_rect.y), nk_vec2(50,-30), nk_vec2(-50, 30), nk_vec2(draw_rect.x+draw_rect.w,draw_rect.y+draw_rect.h), 2.0, red)

	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "Rect", "arial", black, yellow)
	nkStrokeRect(0, draw_rect, 0, 2, red)
	
	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "FillRect", "arial", black, yellow)
	nkFillRect(0, draw_rect, 10, red)

	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "FillRectMC", "arial", black, yellow)
	nkFillRectMultiColor(0, draw_rect, red, green, blue, black)
	
	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "Circle", "arial", black, yellow)
	nkStrokeCircle(0, nk_vec2(draw_rect.x+60,draw_rect.y+45), 45.0, 2, red)
	
	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "FillCircle", "arial", black, yellow)
	nkFillCircle(0, nk_vec2(draw_rect.x+60,draw_rect.y+45), 45.0, red)
	

	
	// move the Text-Rect and Drawable-Rect under the StrokeLine demo.
	// draw the text and the corespnding arc.
	txt_rect.x = bounds.x+25:draw_rect.x=txt_rect.x:inc txt_rect.y, 128:inc draw_rect.y, 128
	nkDrawText(0, txt_rect, "Arc", "arial", black, yellow)
	nkStrokeArc(0, nk_vec2(draw_rect.x+60,draw_rect.y+45), 60.0,-37.5*(3.14/180.0),37.5*(3.14/180.0), 2, red)
	
	// move the Text-Rect and Drawable-Rect to the right.
	// draw the text and the corespnding filled arc.
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "FillArc", "arial", black, yellow)
	nkFillArc(0, nk_vec2(draw_rect.x+60,draw_rect.y+45), 60.0,-37.5*(3.14/180.0),37.5*(3.14/180.0), red)

	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "Triangle", "arial", black, yellow)
	nkStrokeTriangle(0, nk_vec2(draw_rect.x, draw_rect.y), nk_vec2(draw_rect.x+draw_rect.w, draw_rect.y), nk_vec2(draw_rect.x+(draw_rect.w/2), draw_rect.y+draw_rect.h), 2, red)
	
	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "FillTriangle", "arial", black, yellow)
	nkFillTriangle(0, nk_vec2(draw_rect.x, draw_rect.y), nk_vec2(draw_rect.x+draw_rect.w, draw_rect.y), nk_vec2(draw_rect.x+(draw_rect.w/2), draw_rect.y+draw_rect.h), red)
	
	// ...
	inc txt_rect.x, 128:inc draw_rect.x, 128
	nkDrawText(0, txt_rect, "Image", "arial", black, yellow)
	nkDrawImage(0, draw_rect, 0, red)
endfunction
