#import_plugin Nuklear as nk

#constant DEMO_COMBO = 1
#constant DEMO_EDIT = 2
#constant DEMO_SELECTABLE = 3
// 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()

tab as integer = 0
cmb_sel0 as String
cmb_sel1 as integer = 0
vehicles as string[5] = ["Bike","Motor Bike","Car","Truck","Airplane","Ship"]
ratio as float[1] = [0.1,0.9]
color as nk_color
color.a =  255
text as String
selected as integer[15]
do
	// pass agk inputs to nuklear
    nk.HandleInput()

	// open the window
	if nk.WindowBegin("Nuklear Window - Selectables, Combos & EditString", 40, 50, 480, 240, NK_WINDOW_BORDER||NK_WINDOW_MOVABLE||NK_WINDOW_TITLE)

		// here you can see how notetabs can be made.Three tabs are to be
		// created to demonstrate the widgets combo, editstring and
		// selctables.
		// First we divide the line into three parts and set a selectable
		// for each part to switch the tabs.
		nk.LayoutRowDynamic(20, 3)

		// first tab 'Combo Tab'. Together with a label it should show a
		// triangle to the right when folded and a triangle to the bottom
		// when unfolded. The variable 'tab' will be set according to the
		// tab to show the content of the tab later.
		symbol = NK_SYMBOL_TRIANGLE_RIGHT
		if tab = DEMO_COMBO then symbol = NK_SYMBOL_TRIANGLE_DOWN
		if nk.SelectableSymbolLabel(symbol, "Combo Tab", NK_TEXT_CENTERED, tab = DEMO_COMBO)
			tab = DEMO_COMBO
			symbol = NK_SYMBOL_TRIANGLE_RIGHT
		endif

		// second tab 'Edit Tab'
		if tab = DEMO_EDIT then symbol = NK_SYMBOL_TRIANGLE_DOWN
		if nk.SelectableSymbolLabel(symbol, "Edit Tab", NK_TEXT_CENTERED, tab = DEMO_EDIT)
			tab = DEMO_EDIT
			symbol = NK_SYMBOL_TRIANGLE_RIGHT
		endif

		// third tab 'Selectable Tab'
		if tab = DEMO_SELECTABLE then symbol = NK_SYMBOL_TRIANGLE_DOWN
		if nk.SelectableSymbolLabel(symbol, "Selectable Tab", NK_TEXT_CENTERED, tab = DEMO_SELECTABLE)
			tab = DEMO_SELECTABLE
			symbol = NK_SYMBOL_TRIANGLE_RIGHT
		endif

		// in dependency of the 'tab' variable the content is displayed.
		select(tab)
			// the combo demo
			case DEMO_COMBO:
				nk.LayoutRowDynamic(60, 1)
				nk.GroupBegin("ComboGrp", NK_WINDOW_BORDER||NK_WINDOW_NO_SCROLLBAR)
				nk.LayoutRowDynamic(20, 4)
				nk.Label("Vehicle 1:", NK_TEXT_RIGHT)

				// A normal combo widget. The text is displayed in the
				// variable 'cmb_sel'. The combo widget opens a panel of
				// 120 x 200 pixels when activated.
				if nk.ComboBeginLabel(cmb_sel0, 120, 200)
					// The opened panel must also have a layout.
					nk.LayoutRowDynamic(18,1)

					// Now all items of the combo widget are set. If one
					// of the items is selected, 'ComboItemLabel' returns
					// 'nk_true' (1). And cmb_sel0 is set to the new value.
					if nk.ComboItemLabel("Bike", NK_TEXT_LEFT) then cmb_sel0 = "Bike"
					if nk.ComboItemLabel("Motor Bike", NK_TEXT_LEFT) then cmb_sel0 = "Motor Bike"
					if nk.ComboItemLabel("Car", NK_TEXT_LEFT) then cmb_sel0 = "Car"
					if nk.ComboItemLabel("Truck", NK_TEXT_LEFT) then cmb_sel0 = "Truck"
					if nk.ComboItemLabel("Airplane", NK_TEXT_LEFT) then cmb_sel0 = "Airplane"
					if nk.ComboItemLabel("Ship", NK_TEXT_LEFT) then cmb_sel0 = "Ship"
					nk.ComboEnd()
				endif

				// This is another way to fill a combo widget. The string
				// array 'vehicles' contains all items and is passed to
				// the function 'nkComboArray'. As return value you get
				// the selected array index.
				nk.Label("Vehicle 2:", NK_TEXT_RIGHT)
				cmb_sel1 = nkComboArray(vehicles, cmb_sel1, 18, 120, 200)

				// This way a Color Combo Widget is created. The parameters
				// should be self-explanatory. Furthermore the widget
				// behaves similar to the first described combo widget.
				nk.Label("Color-Combo:", NK_TEXT_RIGHT)
				if nk.ComboBeginColor(color.r, color.g, color.b, color.a, 120, 100)
					nkLayoutRow(NK_DYNAMIC, 18, ratio)
					nk.Label("R:", NK_TEXT_RIGHT)
					color.r = nk.Progress(color.r, 255, NK_MODIFIABLE)
					nk.Label("G:", NK_TEXT_RIGHT)
					color.g = nk.Progress(color.g, 255, NK_MODIFIABLE)
					nk.Label("B:", NK_TEXT_RIGHT)
					color.b = nk.Progress(color.b, 255, NK_MODIFIABLE)
					nk.Label("A:", NK_TEXT_RIGHT)
					color.a = nk.Progress(color.a, 255, NK_MODIFIABLE)
					nk.ComboEnd()
				endif

				// The same as before except that a colorpicker opens.
				nk.Label("Combo-Picker:", NK_TEXT_RIGHT)
				if nk.ComboBeginColor(color.r, color.g, color.b, color.a, 200, 400)
					nk.LayoutRowDynamic(120, 1)
					color = nkColorFromInt(nk.ColorPicker(color.r/255.0, color.g/255.0, color.b/255.0, color.a/255.0, NK_RGBA))
					nk.ComboEnd()
				endif
				nk.GroupEnd()
				endcase

			// the edit demo
			case DEMO_EDIT:
				// this demo shows one input field and several buttons to
				// change the text within the input field. marks are read
				// and characters are inserted before and after the marks.
				nk.LayoutRowDynamic(200, 1)
				nk.GroupBegin("EditGrp", NK_WINDOW_BORDER||NK_WINDOW_NO_SCROLLBAR)
				ratio[0] = 0.25
				ratio[1] = 0.75
				nkLayoutRow(NK_DYNAMIC, 190, ratio)
				nk.GroupBegin("EditBtnGrp",NK_WINDOW_NO_SCROLLBAR)
				nk.LayoutRowDynamic(20,1)
				// Button creation
				if nk.ButtonLabel("Italic")
					// read the beginning of the marker
					pos = nk.EditGetMarkStart()

					// insert text on the readed position
					text = Left(text, pos) + "[i]"+Right(text, len(text)-pos)

					// read the ending of the marker and add the length of
					// the inserted text. because the insertion of the
					// text takes place in a string. The widget has no
					// knowledge of this.
					pos = nk.EditGetMarkEnd()+3

					// now insert a text at the end of the marker.
					text = Left(text, pos) + "[\i]"+Right(text, len(text)-pos)
				endif

				// same as above
				if nk.ButtonLabel("Bold")
					pos = nk.EditGetMarkStart()
					text = Left(text, pos) + "[b]"+Right(text, len(text)-pos)
					pos = nk.EditGetMarkEnd()+3
					text = Left(text, pos) + "[\b]"+Right(text, len(text)-pos)
				endif

				// same as above
				if nk.ButtonLabel("Underline")
					pos = nk.EditGetMarkStart()
					text = Left(text, pos) + "[u]"+Right(text, len(text)-pos)
					pos = nk.EditGetMarkEnd()+3
					text = Left(text, pos) + "[\u]"+Right(text, len(text)-pos)
				endif

				// same as above
				if nk.ButtonLabel("Center")
					pos = nk.EditGetMarkStart()
					text = Left(text, pos) + "[center]"+Right(text, len(text)-pos)
					pos = nk.EditGetMarkEnd()+8
					text = Left(text, pos) + "[\center]"+Right(text, len(text)-pos)
				endif

				// same as above
				if nk.ButtonLabel("Code")
					pos = nk.EditGetMarkStart()
					text = Left(text, pos) + "[code]"+Right(text, len(text)-pos)
					pos = nk.EditGetMarkEnd()+6
					text = Left(text, pos) + "[\code]"+Right(text, len(text)-pos)
				endif
				nk.GroupEnd()

				// now display EditString with the content of 'text', a
				// maximum text length of 1024 characters, a default
				// filter and an edit field style.
				// The changed text is saved by 'EditString' back to text.
				text = nk.EditString(NK_EDIT_BOX, text, 1024, NK_FILTER_DEFAULT)
				nk.GroupEnd()
				endcase

			case DEMO_SELECTABLE:
				// this demo is very simple. It shows a grid and remembers
				// the responsible of the selectables. The logic only
				// changes the symbol.
				nk.LayoutRowDynamic(150, 1)
				nk.GroupBegin("SelectableGrp", NK_WINDOW_BORDER||NK_WINDOW_NO_SCROLLBAR)
				nk.LayoutRowStatic(30,30,4)
				for i=0 to 15
					if selected[i] then symbol = NK_SYMBOL_X else symbol = NK_SYMBOL_CIRCLE_OUTLINE
					selected[i] = nk.SelectableSymbolLabel(symbol, " ", NK_TEXT_CENTERED, selected[i])
				next
				nk.GroupEnd()
				endcase
		endselect
	endif
	nk.WindowEnd()

    nkSync()
loop
