'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006)
'' ----------------------------------------------------------------------------
'' Converted To BlitzMax and Irrlicht 1.8.3 by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 84: GUI Functions
'' This example demonstrates the use of a range of GUI objects
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' Includes For extension libraries
Framework usr.birrlicht


'' ////////////////////////////////////////////////////////////////////////////
'' Global variables
Global GUIEvent:Int
Global guiStatic:Int
Global guiEdit:Int
Global guiPassword:Int
Global guiWindow:Int
Global guiListbox:Int
Global redBackground:Int = 255
Global greenBackground:Int = 255
Global imageTexture:Int
Global fileName:String
Global displayString:String
Global BitmapFont:Int

Const MY_GUI_BUTTON_FILE     = 101
Const MY_GUI_BUTTON_WINDOW   = 102
Const MY_GUI_BUTTON_QUIT     = 103
Const MY_GUI_BUTTON_CLOSE    = 104
Const MY_GUI_SCROLLBAR_RED   = 105
Const MY_GUI_SCROLLBAR_GREEN = 106
Const MY_GUI_LISTBOX         = 107
Const MY_GUI_CHECKBOX        = 108
Const MY_GUI_EDITBOX         = 109
Const MY_GUI_PASSWORD        = 110
Const MY_GUI_FILEOPEN        = 111

'' ////////////////////////////////////////////////////////////////////////////
' start the irrlicht interface. The scene will be rendered using the Irrlicht,
' software renderer, the display will be a window 400 x 200 pixels in size, the
' display will Not support shadows And we will Not capture any keyboard And
' mouse events And finally vertical syncronisation To smoothen the display
' is switched on, by Default it is off
iIrr3D( 512, 256, 32, False )

' Set the title of the display
iAppTitle( "Example 84: GUI Functions" )

' set the font used by the GUI
BitmapFont = iLoadFont ( "./media/fonthaettenschweiler.bmp" )
iGUISetFont( BitmapFont )

' set the colors used by GUI controls
iGUISetColor( EGDC_3D_FACE, 192, 255, 255, 255 )
iGUISetColor( EGDC_BUTTON_TEXT, 0, 64, 64, 255 )

' add a static text Object To the graphical user interface. The text will be
' drawn inside the defined rectangle, the box will Not have a border And the
' text will Not be wrapped around If it runs off the End
guiStatic = iStaticText( "Hello World", 4,0,256,16, False, False, 0 )

' add a button To the graphical user interface
iCreateButton( 16, 16, 112, 32, MY_GUI_BUTTON_FILE, "Select File", "Select a bitmap", 0 )

' add a couple of more buttons To the graphical user interface
iCreateButton( 128, 16, 224, 32,   MY_GUI_BUTTON_WINDOW, "Open a window", "", 0 )
iCreateButton( 240, 16, 336, 32,  MY_GUI_BUTTON_QUIT, "Close the GUI", "",0 )

' add a horizontal scroll bar To the graphical user interface
iCreateScrollBar( IRR_GUI_HORIZONTAL, 32, 48, 320, 64, MY_GUI_SCROLLBAR_RED, 255, 255, 0 )

' add a vertical scroll bar To the graphical user interface
iCreateScrollBar( IRR_GUI_VERTICAL, 16, 64, 32, 240, MY_GUI_SCROLLBAR_GREEN, 255, 255, 0 )

' add a listbox To the display
guiListbox = iCreateListBox( 48, 64, 320, 128, MY_GUI_LISTBOX, IRR_GUI_DRAW_BACKGROUND, 0 )

' add three items To the listbox
iAddGadgetItem( guiListbox, "Apples" )
iAddGadgetItem( guiListbox, "Oranges" )
iAddGadgetItem( guiListbox, "Pears" )

' Select item 1 oranges
iSelectGadgetItem( guiListbox, 1 )

' add a normal editbox To the display
guiEdit = iCreateEditBox( "Editable text", 48,128, 320,144, MY_GUI_EDITBOX, True, False, 0 )

' add a password editbox To the display
guiPassword = iCreateEditBox( "Hidden Password", 48,144, 320,160, MY_GUI_PASSWORD, True, True, 0 )

' add a checkbox To the display
iCreateCheckBox( "Clickable Option", 48,160, 320,176, MY_GUI_CHECKBOX, 1, 0 )

' Let the GUI system handle the events
'IrrGUIEvents( 1 )

' While the scene is still running
While iRun() And (Not iKeyHit( KEY_ESCAPE ))
    ' begin the scene, erasing the canvas To white before rendering

	iCameraCLSColor(redBackground, greenBackground, 255 )
    iBeginScene()

    ' draw the Graphical User Interface
    iDrawGUI()

    ' If there are GUI events available
    If iGUIEventAvailable() Then

        ' read the GUI event out 
        iReadGUIEvent()
		GUIEventID = iGUIEventID()
		GUIEvent = iGUIEvent()
		GUIEventData = iGUIEventData()
		DebugLog "ID= "+GUIEventID 

        ' process the particular control
        Select GUIEventID
 
        Case MY_GUI_BUTTON_FILE
            ' If the button has been pressed
            If GUIEvent = EGET_BUTTON_CLICKED Then
                ' open a file open dialog
                iAddFileOpen( "Select an image file", MY_GUI_FILEOPEN, IRR_GUI_MODAL, 0  )
            End If

        Case MY_GUI_BUTTON_WINDOW
            ' If the button has been pressed
            If GUIEvent = EGET_BUTTON_CLICKED Then
                ' open a small modal window
                guiWindow = iCreateWindow( "Window", 80, 80, 160, 144, IRR_GUI_MODAL, 0 )
                ' with a button that can close the window
                iCreateButton( 16, 32, 64, 48, 104, "Close me", "", guiWindow )
            End If

        Case MY_GUI_BUTTON_QUIT
            ' If the button has been pressed
            If GUIEvent = EGET_BUTTON_CLICKED Then
                ' all GUI elements from the display
                iGUIClear()
                ' let the system handle mouse And key events now
                'IGUIEvents( 0 )
            End If

        Case MY_GUI_BUTTON_CLOSE
            ' If the button has been pressed
            If GUIEvent = EGET_BUTTON_CLICKED Then
                ' remove the window And its child buttons from the display
                iGUIRemove( guiWindow )
            End If

        Case MY_GUI_SCROLLBAR_RED
            ' If the position of the scrollbar has been changed
            If GUIEvent = EGET_SCROLL_BAR_CHANGED Then
                ' set the red value of the background color
                redBackground = GUIEventData
            End If

        Case MY_GUI_SCROLLBAR_GREEN
            ' If the position of the scrollbar has been changed
            If GUIEvent = EGET_SCROLL_BAR_CHANGED Then
                ' set the red value of the background color
                greenBackground = GUIEventData
            End If

        Case MY_GUI_LISTBOX
            ' If the listbox selection has changed
            If GUIEvent = EGET_LISTBOX_CHANGED Then
                ' change the text To show which item is selected
                If GUIEventData = 0 Then iGUISetText( guiStatic, "Selected apples" )
                If GUIEventData = 1 Then iGUISetText( guiStatic, "Selected oranges" )
                If GUIEventData = 2 Then iGUISetText( guiStatic, "Selected pears" )
            End If

        Case MY_GUI_EDITBOX
            ' If the editbox contents have changed
            If GUIEvent = EGET_EDITBOX_CHANGED Then
                ' set the static text Object To the contents of the edit box
                iGUISetText( guiStatic, iGUIGetText( guiEdit ))
            End If

        Case MY_GUI_PASSWORD
            ' If the password editbox contents have changed
            If GUIEvent = EGET_EDITBOX_CHANGED Then
                ' set the static text Object To the contents of the edit box
                iGUISetText( guiStatic, iGUIGetText( guiPassword ))
            End If

        Case MY_GUI_CHECKBOX
            ' If the checkbox state has changed
            If GUIEvent = EGET_CHECKBOX_CHANGED Then
                ' set the static text Object To represent the checkbox state
                If GUIEventData = 0 Then
                    iGUISetText( guiStatic, "Checkbox is Cleared" )
                Else
                    iGUISetText( guiStatic, "Checkbox is Checked" )
                End If
            End If

        Case MY_GUI_FILEOPEN
            ' If the event was from a file being selected
            If GUIEvent = EGET_FILE_SELECTED Then
				DebugLog iGetLastSelectedFile()
                imageTexture = iLoadTexture( iGetLastSelectedFile() )
                If Not imageTexture = 0 Then
                    iStaticImage( imageTexture, 352, 16, True, 140, 0 )
                End If
            End If

        End Select
    End If

    ' End drawing the scene And render it
    iEndScene()

Wend

' -----------------------------------------------------------------------------
' Stop the irrlicht engine And Release resources
iEndIrr3D()


End