;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper - Michael Taupitz (2007)
;; PureBasic-Examples by Michael Taupitz (neotoma) & Marius Eckardt (Thalius) 
;; ----------------------------------------------------------------------------
;; Example 17 : Selecting Nodes With the Mouse
;; This example allows you To move the mouse around the display And To Select
;; nodes on the screen by hovering over them.
;; ----------------------------------------------------------------------------

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


#TEST_NODES = 25

; irrlicht objects
*MeshTexture.irr_texture
Dim *TestNode.irr_node(#TEST_NODES)
*SelectedNode.irr_node
*OurCamera.irr_camera
*BitmapFont.irr_font
x.l
y.l
*MouseEvent.IRR_MOUSE_EVENT
selected_node.s


;; ////////////////////////////////////////////////////////////////////////////
;; GDB debugger main() function

; -----------------------------------------------------------------------------
; 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()

  ; send the window caption
  IrrSetWindowCaption( "Example 17: Selecting Nodes with the Mouse" )
  
  ; load the bitmap font as a texture
  *BitmapFont = IrrGetFont ( "media\bitmapfont.bmp" )
  
  ; load the test cube texture
  *MeshTexture = IrrGetTexture( "media\texture.jpg" )
  
  ; itterate all of the test nodes
  For i = 1 To #TEST_NODES
      ; add a test node to the scene
      *TestNode(i) = IrrAddCubeSceneNode(10)
      
      ; move the node so that it is arranged into a square grid
      yy.l = ((i-1)/5)*20
      zz.l = ((i-1) % 5)*20    
      IrrSetNodePosition( *TestNode(i), 00, yy,zz )
  
      ; apply a material to the node
      IrrSetNodeMaterialTexture( *TestNode(i), *MeshTexture, 0 )
  Next i
  
  
  
  
  ; add a camera into the scene and move it into position
  *OurCamera = IrrAddCamera( 120,40,40, 0,40,40)
  
  ; start with the phrase saying nothing is selected
  selected_node = "NOTHING SELECTED"
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; begin the scene, erasing the canvas with grey before rendering
      IrrBeginScene( 128,128,128 )
  
      ; draw the scene
      IrrDrawScene()
  
      ; if there is a mouse event waiting
      ; while there are mouse events waiting
      While IrrMouseEventAvailable()
          ; read the mouse event out
          *MouseEvent = IrrReadMouseEvent()
  
          ; if this is a mouse move event
          If *MouseEvent\action = #IRR_EMIE_MOUSE_MOVED
              
              ; Get the node object that is under the mouse
              *SelectedNode = IrrGetCollisionNodeFromScreenCoordinates(*MouseEvent\x, *MouseEvent\y )
  
              ; if the node object that was return is 0
              If *SelectedNode = 0
                  ; there is no node under the mouse
                  selected_node = "NOTHING SELECTED AT "+Str(*MouseEvent\x)+","+Str(*MouseEvent\y)
              Else
                  ; scan through the list of nodes to find which one is selected
                  For i = 1 To #TEST_NODES
                      ; if this is the selected node
                      If  *SelectedNode = *TestNode(i)
                          ; display the node number
                          selected_node = "NODE "+Str(i)+" SELECTED AT "+Str(*MouseEvent\x)+","+Str(*MouseEvent\y)
                          ; switch lighting off on the node making it appear black
                          IrrSetNodeMaterialFlag( *SelectedNode, #IRR_EMF_LIGHTING, #IRR_ON )
                      Else
                          ; switch lighting on for this node
                          IrrSetNodeMaterialFlag( *TestNode(i), #IRR_EMF_LIGHTING, #IRR_OFF )
                      EndIf
                  Next i
              EndIf
          EndIf
                  
      Wend
  
      ; draw the text indicating which node was selected
      Irr2DFontDraw ( *BitmapFont, selected_node, 0, 0, 100, 16 )
  
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
EndIf  

End

; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; CursorPosition = 50
; FirstLine = 48
; Folding = -