;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; Previous IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper and PureBasic-Example by Michael Taupitz (2006)
;; ----------------------------------------------------------------------------
;; Example 30: Using the Image-GUI
;; This example show some features of Irrlicht. It uses a 'own' EventReceiver,
;; to capture the Events directly. It also uses the XML-Interface to REad a XML-File,
;; and you can see how to use the Image-Gui.
;; ----------------------------------------------------------------------------

;; ////////////////////////////////////////////////////////////////////////////
;; Include the Irr3D-Requester
XIncludeFile "Irr3DRequester.pb"

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables

Global MessageText.s
Global Caption.s 
Global StartUpModelFile.s
Global *Model.l
Global *SkyBox.l
Global g_Quit.l = #False
Global *skin.l = #Null

Procedure showAboutText() 
	; create modal message box With the text
	; loaded from the xml file.
  IrrGuiAddMessageBox(Caption,MessageText,#True,#IRR_EMBF_OK,0,-1)

EndProcedure


Procedure loadSkin(*skin.l, skinNum.l)

	If(skinNum < 0 Or skinNum > 2 Or *skin = #Null)
		ProcedureReturn;
  EndIf
	;Bubbly blue
	If skinNum =  0
		;Here we load a nice glass button image To use when drawing all 3D button faces
		;he skin supports animated images, so we specify * 1 indicating there is only one frame
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_BUTTON_PANE_STANDARD, "media/skins/bb1.tga", 0,0,114*1,114,114,114,#False);
		
		;Here we load an image used For animating the button when it's pressed
		;The animation has 8 frames laid out horizontally, so we specify that As *8 in the Ax parameter
		;If the animation was laid out vertically, you would specify *8 in the Ay parameter
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_BUTTON_PANE_PRESSED, "media/skins/bbp2.tga", 0,0,114*8,114,114,114,#False);

		;There are 8 frames, so we want them To animate pretty quickly on a button press
		IrrGuiImageSkinSetAnimSpeed(*skin,#IRR_EGEIT_BUTTON_PANE_PRESSED, 10);

		;We only want the animation To happen once, so we turn looping off.
		;To make the animation happen again, skin->resetAnimation() must be called when the 
		;mouse button is clicked. Check the event receiver For info on how To do that.
		IrrGuiImageSkinSetLoop(*skin,#IRR_EGEIT_BUTTON_PANE_PRESSED, #False);

		;Here we load an animated image For the window titles
		;4 frames, laid out horizontally.
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_WINDOW_BACKGROUND_TITLEBAR, "media/skins/fireline_e0.tga", 0,0,600*4,50,600,50,#False);
    IrrGuiImageSkinSetAnimSpeed(*skin,#IRR_EGEIT_WINDOW_BACKGROUND_TITLEBAR, 70);	
		
		;Finally, load the image To be used For window backgrounds.
		;It's a simple 2 frame animation of some lighting flickering
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_WINDOW_BACKGROUND, "media/skins/bg4.tga", 0,0,232*1,232,232,232,#False);

		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_TAB_BODY, "media/skins/bg4.tga", 0,0,232*1,232,232,232,#False);
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_TAB_BUTTON, "media/skins/bb1.tga", 0,0,114*1,114,114,114,#False);
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_MENU_PANE, "media/skins/bg4.tga", 0,0,232*1,232,232,232,#False);
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_TOOLBAR, "media/skins/bg4.tga", 0,0,232*1,232,232,232,#False);
	ElseIf skinNum = 1

		;Aero Glass
		;Here we load a nice glass button image To use when drawing all 3D button faces
		;The skin supports animated images, so we specify * 1 indicating there is only one frame
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_BUTTON_PANE_STANDARD, "media/skins/blackbsq.tga", 0,0,145*1,145,145,145,#False);
		

		;Here we load an image used For animating the button when it's pressed
		;The animation has 8 frames laid out horizontally, so we specify that As *8 in the Ax parameter
		;If the animation was laid out vertically, you would specify *8 in the Ay parameter
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_BUTTON_PANE_PRESSED, "media/skins/blackbsqpush.tga", 0,0,145*8,145,145,145,#False);

		;There are 8 frames, so we want them To animate pretty quickly on a button press
		IrrGuiImageSkinSetAnimSpeed(*skin,#IRR_EGEIT_BUTTON_PANE_PRESSED, 10);

		;We only want the animation To happen once, so we turn looping off.
		;To make the animation happen again, skin->resetAnimation() must be called when the 
		;mouse button is clicked. Check the event receiver For info on how To do that.
		IrrGuiImageSkinSetLoop(*skin,#IRR_EGEIT_BUTTON_PANE_PRESSED, #False);

		;Here we load a 'blank' image so we don't have titles 
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_WINDOW_BACKGROUND_TITLEBAR, "media/skins/blank.tga", 0,0,32*1,32,32,32,#False);
		
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_WINDOW_BACKGROUND, "media/skins/bg5.tga", 0,0,399*1,399,399,399,#False);
		
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_TAB_BODY, "media/skins/bg5.tga", 0,0,399*1,399,399,399,#False);
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_TAB_BUTTON, "media/skins/blackbsq.tga", 0,0,145*1,145,145,145,#False);
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_MENU_PANE, "media/skins/bg5.tga", 0,0,399*1,399,399,399,#False);
		IrrGuiImageSkinLoad(*skin,#IRR_EGEIT_TOOLBAR, "media/skins/bg5.tga", 0,0,399*1,399,399,399,#False);
  EndIf

EndProcedure

; 	Finally, the third function creates a toolbox window. In this simple mesh viewer,
; 	this toolbox only contains a tab control With three edit boxes For changing
; 	the scale of the displayed model.
Procedure createToolBox()
; 
; 
  *rooElm.l = IrrGuiGetRootGUIElement();
 	; remove tool box if already there
  *tbox.l = IrrGuiGetElementFromId(*rooElm,5000,#True);
  If *tbox <> #Null
    IrrGuiRemoveElement(*tbox)
  EndIf
; 
; 	// create the toolbox window
  *wnd.l = IrrGuiAddWindow(450,25,640,480, "Toolset")
  
; 	// create tab control And tabs
  *tab.l = IrrGuiAddTabControl(2,20,640-452,480-7,#True,#True,*wnd,9999)
  *t1.l = IrrGuiTabControlAddTab(*tab,"Scale")
  *t2.l = IrrGuiTabControlAddTab(*tab,"Empty Tab")
  
; 	// add some edit boxes And a button To tab one
  IrrGuiAddEditBox("1.0",40,50,130,70,#True,*t1,901)
  IrrGuiAddEditBox("1.0",40,80,130,100,#True,*t1,902)
  IrrGuiAddEditBox("1.0",40,110,130,130,#True,*t1,903)

  IrrGuiAddButton("set",10,150,100,190,*t1,1101)
  
; 	// add senseless checkbox
  IrrGuiAddCheckBox(#True,10,220,200,240,"Senseless Checkbox",*t1,-1);

; 	// add undocumentated transparent control
  IrrGuiAddStaticText("Transparent Control:", 10,240,150,260, #True, #False, #False, *t1);
  
  *scrollbar.l = IrrGuiAddScrollBar(#True,10,260,150,275, *t1, 104)
  IrrGuiScrollBarSetMax(*scrollbar,255)
; 
; 	// bring irrlicht engine logo To front, because it
; 	// now may be below the newly created toolbox
  *elm.l = IrrGuiGetElementFromId(*rooElm,666,#True)
  IrrGuiBringToFront(*rooElm,*elm)
; 	root->bringToFront(root->getElementFromId(666, true));
; }
EndProcedure

ProcedureCDLL.l eventCallback(*event.SEvent)
  
;  Debug("EventCB")
  If *event\EventType = #IRR_EET_GUI_EVENT    
    id.l = IrrGuiGetId(*event\GuiEvent\Caller)
    Debug("ID="+Str(id))
    Select *event\GuiEvent\EventType 
      Case #IRR_EGET_MENU_ITEM_SELECTED
        item.l = IrrGuiContextMenuGetSelectedItem(*event\GuiEvent\Caller)
        id.l = IrrGuiContextMenuGetItemCommandId(*event\GuiEvent\Caller,item);
          Select id 
            Case 100:  ;// File -> Open Model
              IrrGuiAddFileOpenDialog("Please select a model file to open",#True,0,0)
            Case 200:  ;// File -> Quit
              g_Quit = #True; 
   					Case 300: ;// View -> Skybox
   					  IrrSetNodeVisibility(*SkyBox,1!IrrGetNodeVisibility(*SkyBox))
; 						SkyBox->setVisible(!SkyBox->isVisible());
; 						Break;
   					Case 400: ;// View -> Debug Information
   						If *Model <> 0
;   						  IrrSetNodeDebugData(*Model,1)
  ; 							Model->setDebugDataVisible(!Model->isDebugDataVisible());
               EndIf 
  					Case 500: ;// Help->About
   						showAboutText();
  					Case 610: ;// View -> Material -> Solid
  						If *Model<>0
  						  IrrSetNodeMaterialType(*Model,#IRR_EMT_SOLID)
  						EndIf
  					Case 620: ;// View -> Material -> Transparent
  						If *Model<>0
  						  IrrSetNodeMaterialType(*Model,#IRR_EMT_TRANSPARENT_ADD_COLOR)
  						EndIf
  					Case 630: ;// View -> Material -> Reflection
  						If *Model<>0
  						  IrrSetNodeMaterialType(*Model,#IRR_EMT_SPHERE_MAP)
  						EndIf
          EndSelect          
        Case #IRR_EGET_FILE_SELECTED:
            *adress.l =      IrrGuiFileOpenDialogGetFilename(*event\GuiEvent\Caller);
            fname.s = PeekS(*adress,-1,#PB_Unicode)
            Debug(fname)
        
   			Case #IRR_EGET_SCROLL_BAR_CHANGED:; 
; 				// control skin transparency 
   				If (id = 104)
            pos.l = IrrGuiScrollBarGetPos(*event\GuiEvent\Caller)
            IrrGuiSetAlpha(pos);
          EndIf
        
			  Case #IRR_EGET_COMBO_BOX_CHANGED:
				  If(id = 456)
				    selected.l = IrrGuiComboBoxGetSelected(*event\GuiEvent\Caller)
				    Debug("Selcetd SkiN="+Str(selected))
					  loadSkin(*skin, selected );
					EndIf
			  Case #IRR_EGET_BUTTON_CLICKED:
 				  Select (id)

            Case 1101:
; 						// set scale
; 						gui::IGUIElement* root = env->getRootGUIElement();
; 						core::vector3df scale;
; 						core::stringc s;
              *rooElm.l = IrrGuiGetRootGUIElement();

              *elm.l = IrrGuiGetElementFromId(*rooElm,901,#True)
              Debug("*elm="+Str(*elm))
              si.s = IrrGuiGetText(*elm)
              x.f = ValF(si)
              *elm.l = IrrGuiGetElementFromId(*rooElm,902,#True)
              Debug("*elm="+Str(*elm))              
              si.s = IrrGuiGetText(*elm)
              y.f = ValF(si)
              *elm.l = IrrGuiGetElementFromId(*rooElm,903,#True)
              Debug("*elm="+Str(*elm))              
              si.s = IrrGuiGetText(*elm)
              z.f = ValF(si)

              Debug("Scaling="+StrF(x)+","+StrF(y)+","+StrF(z))            
              If *Model <> 0
                IrrSetNodeScale(*Model,x,y,z)
              EndIf
     				Case 1102:
     				  IrrGuiAddFileOpenDialog("Please select a model file to open",#True,0,0)
     				Case 1103:
     					showAboutText();
     				Case 1104:
     					createToolBox();

          EndSelect
      
    EndSelect 
        
  EndIf
  ProcedureReturn #False
EndProcedure 



; -----------------------------------------------------------------------------
; To Start the Irrlicht-Device we use the Irr3DRequester, to make it more comfortable
; for the Users, and also easier to test different resolutions and devices.
If Irr3DRequester()
  
  IrrSetResizeAble(#True)
 
 

; Set the title of the display
  IrrSetWindowCaption( "Example 30: Using the Image-GUI" )



  IrrSetTextureCreationFlag(#IRR_ETCF_ALWAYS_32_BIT,#True)
  IrrSetTextureCreationFlag(#IRR_ETCF_CREATE_MIP_MAPS, #False);
  *skin.l = IrrGuiCreateImageSkin()
 	loadSkin(*skin, 0); //load bubbly blue as default
  IrrGuiSetSkin(*skin)


    ;Read configuration from xml file
  
    *xml.l = IrrXmlCreateReader("media/config.xml")
    
  	While *xml And IrrXmlRead(*xml)
  	  Select IrrXmlGetNodeType(*xml) 
  	    Case #IRR_EXN_TEXT
  			  ;in this xml file, the only text which occurs is the messageText
  			  MessageText = PeekS(IrrXmlGetNodeData(*xml),-1,#PB_Unicode)
  			  Debug(MessageText)
        Case #IRR_EXN_ELEMENT
  		    *nodename.l = IrrXmlGetNodeName(*xml)
  		    If *nodename <> 0
    			  If PeekS(IrrXmlGetNodeName(*xml),-1,#PB_Unicode) = "startUpModel"
  	  		    StartUpModelFile = PeekS(IrrXmlGetAttributeValueByName(*xml,"file"),-1,#PB_Unicode);
  	  		    Debug(StartUpModelFile)
  		  		Else
  			  	  If PeekS(IrrXmlGetNodeName(*xml),-1,#PB_Unicode) = "messageText"
  				  	  Caption = PeekS(IrrXmlGetAttributeValueByName(*xml,"caption"),-1,#PB_Unicode);
  				  	  Debug(Caption)
              EndIf
            EndIf
          EndIf
        Case #IRR_EXN_ELEMENT_END 
  		    *nodename.l = IrrXmlGetNodeName(*xml)
  		    If *nodename <> 0
    			  If PeekS(IrrXmlGetNodeName(*xml),-1,#PB_Unicode) = "config"
              Break;
            EndIf
          EndIf
          
              
      EndSelect
    Wend
    IrrXmlFreeReader(*xml)
    
    
    
  
    *font.l = IrrGetFont("media/fonthaettenschweiler.bmp");
  	If *font
  	  IrrGuiSetSkinFont(*font)
  	EndIf
  
  	; create menu
  
    *menu.l = IrrGuiAddMenu(0,-1);
    IrrGuiContextMenuAddItem(*menu, "File", -1, #True, #True)
    IrrGuiContextMenuAddItem(*menu, "View", -1, #True, #True)
    IrrGuiContextMenuAddItem(*menu, "Help", -1, #True, #True)
  
    *submenu.l = IrrGuiContextMenuGetSubMenu(*menu,0)
    IrrGuiContextMenuAddItem(*submenu,"Open Model File...", 100,#True,#False);
    IrrGuiContextMenuAddSeparator(*submenu)
    IrrGuiContextMenuAddItem(*submenu,"Quit", 200,#True,#False);
    
    *submenu.l = IrrGuiContextMenuGetSubMenu(*menu,1)
    IrrGuiContextMenuAddItem(*submenu,"toggle sky box visibility", 300,#True,#False);
    IrrGuiContextMenuAddItem(*submenu,"toggle model debug information", 400,#True,#False);
  
    *submenu.l = IrrGuiContextMenuGetSubMenu(*menu,2)
    IrrGuiContextMenuAddItem(*submenu,"Solid", 610,#True,#False););
    IrrGuiContextMenuAddItem(*submenu,"Transparent", 620,#True,#False););
    IrrGuiContextMenuAddItem(*submenu,"Reflection", 630,#True,#False););
    IrrGuiContextMenuAddItem(*submenu,"About", 500,#True,#False););
  
  
  	; create toolbar
  
    *bar.l = IrrGuiAddToolBar(0,0);
  
  	*Image.l = IrrGetTexture("media/open.bmp");	
    IrrColorKeyTextureFromPoint(*Image.l,0,0) 	
    IrrGuiToolBarAddButton(*bar,1102, "", *Image, 0, #False, #True);
  
  	*Image.l = IrrGetTexture("media/help.bmp");	
    IrrColorKeyTextureFromPoint(*Image.l,0,0) 	
    IrrGuiToolBarAddButton(*bar,1103, "", *Image, 0, #False, #True);
  
  	*Image.l = IrrGetTexture("media/tools.bmp");	
    IrrColorKeyTextureFromPoint(*Image.l,0,0) 	
    IrrGuiToolBarAddButton(*bar,1104, "", *Image, 0, #False, #True);
  
  
  	*box.l = IrrGuiAddComboBox( 100,5,200,25, *bar,0);
  	IrrGuiComboBoxAddItem(*box,"Bilinear")
  	IrrGuiComboBoxAddItem(*box,"Trilinear");
  	IrrGuiComboBoxAddItem(*box,"Anisotropic");
  	IrrGuiComboBoxAddItem(*box,"Isotropic");
  	IrrGuiComboBoxAddItem(*box,"Psychedelic");
  	IrrGuiComboBoxAddItem(*box,"No filtering");
  
  	; create a combobox To allow user To change skin
  	IrrGuiAddStaticText("Skin",290,5,315,25,#False,#False,#False,*bar)
  
  	
  	*skinbox.l = IrrGuiAddComboBox( 320,5,420,25, *bar,456);
  	IrrGuiComboBoxAddItem(*skinbox,"Bubbly Blue");
  	IrrGuiComboBoxAddItem(*skinbox,"Aero Glass");
  
    IrrSetTextureCreationFlag(#IRR_ETCF_CREATE_MIP_MAPS, #False);
    *SkyBox = IrrAddSkyBoxToScene( IrrGetTexture("./media/irrlicht2_up.jpg"),IrrGetTexture("./media/irrlicht2_dn.jpg"),IrrGetTexture("./media/irrlicht2_lf.jpg"),      IrrGetTexture("./media/irrlicht2_rt.jpg"),        IrrGetTexture("./media/irrlicht2_ft.jpg"),        IrrGetTexture("./media/irrlicht2_bk.jpg"))
    IrrSetTextureCreationFlag(#IRR_ETCF_CREATE_MIP_MAPS, #True);
  
  
    createToolBox()
  
   
    *er = IrrCreateEventReceiver(@eventCallback())
    IrrSetEventReceiver(*er)

  	;// add a camera scene node 
  	*OurCamera = IrrAddCameraMaya(0)


    ;	// load the irrlicht engine logo  
    *irrLogo.l = IrrGetTexture("./media/irrlichtlogoaligned.jpg");


  
  ; while the scene is still running
  While g_Quit= #False And IrrRunning()
      ; begin the scene, erasing the canvas to white before rendering
  
      IrrBeginScene( 255,255,255 )
  
      IrrDrawScene()
      ; draw the Graphical User Interface
      IrrDrawGUI()
          
      IrrDraw2DImageElement(*irrLogo, 10, (480-50), 0,0,108-20,460-429, #True, 255,255,255,255)
  
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
  
EndIf

End

; jaPBe Version=3.7.5.643
; Build=1
; Language=0x0000 Language Neutral
; FirstLine=108
; CursorPosition=35
; ExecutableFormat=Windows
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 244
; FirstLine = 209
; Folding = -