Accessing GUI events

Irrlicht.Net is no longer developed or supported, Irrlicht.Net Cross Platform is a much more complete wrapper. Please ask your C# related questions on their forums first.
Locked
Scionwest
Posts: 15
Joined: Sun Jul 30, 2006 4:01 am

Accessing GUI events

Post by Scionwest »

My current code is as follows

Code: Select all

    Public Sub Show()
        Dim Background As Irrlicht.Video.ITexture = RenderingDevice.device.VideoDriver.GetTexture("Background.jpg")
        Dim pos As New Position2D


        pos.X = RenderingDevice.device.VideoDriver.ScreenSize.Width / 4 * 3
        pos.Y = RenderingDevice.device.VideoDriver.ScreenSize.Height / 2

        Menus.CurrentMenu = "MainMenu"

        ''disable the camera
        CameraFPS.disable(RenderingDevice.device)
        ''enable the cursor
        RenderingDevice.device.CursorControl.Visible = True

        ''start displaying the objects
        ''background
        RenderingDevice.device.GUIEnvironment.AddImage(Background, New Position2D(0, 0), False, RenderingDevice.device.GUIEnvironment.RootGUIElement, 100, Nothing)

        ''play game button
        RenderingDevice.device.GUIEnvironment.AddButton(New Rect(pos, New Dimension2D(64, 32)), RenderingDevice.device.GUIEnvironment.RootGUIElement, 101, "Play Game")

    End Sub
When I compile and run it the engine starts up with my background and my button in the correct locations. Now, once I have created the button how do I write event handlers? I want it to start playing the game when I click on the button but I don't know how to do so. Any help would be great, either in C# or VB I don't care, I can figure out the differances.

Thanks!
matiae
Posts: 16
Joined: Wed May 31, 2006 8:06 am
Location: Chile

Post by matiae »

hi,
you should implement IEventReceiver.OnEvent. There you can look if the event is GUIEvent, GUIEvent.BUTTON_CLICKED and if the e.GUIEventCaller.ID == 101.. and then you could invoke a delegate.
shurijo
Posts: 148
Joined: Sat Feb 21, 2004 3:04 am

Post by shurijo »

Are you using buttons in the Irrlicht.GUI namespace (Irrlicht GUI objects) or Windows Forms buttons (the ones in the VS's toolbox)?

If you are using Win Form buttons, then you have to program the on click event of the button. The IEventReceiver doesn't work when draw to a WinForm. It works fine when draw to its own window (that can't use the .NET Form controls).

In VB.NET just double click on the button in design mode. In C#, select the button, then click the lightbolt in the property grid, scroll to MouseClick, and finally double click the text on the left.
Scionwest
Posts: 15
Joined: Sun Jul 30, 2006 4:01 am

Post by Scionwest »

I'm using the Irlicht gui controls. I will try using the IEventReciever again. I was messing with it and couldn't fin any documentation on it. Anyone have any simple examples?
shurijo
Posts: 148
Joined: Sat Feb 21, 2004 3:04 am

Post by shurijo »

Does the wiki have all of the examples for C# and VB.NET? I'm sure there's an example there.
Scionwest
Posts: 15
Joined: Sun Jul 30, 2006 4:01 am

Post by Scionwest »

I checked over there but couldn't find anything. I will check again right now incase I missed it (probably did)
shurijo
Posts: 148
Joined: Sat Feb 21, 2004 3:04 am

Post by shurijo »

http://www.irrforge.org/index.php/VBNET_Tutorial_5

Just make a new class like below

Code: Select all

class MyEventReceiver
  Implements IEventReceiver

Public Function OnEvent(ByVal p_e As Irrlicht.Event) As Boolean Implements IEventReceiver.OnEvent
          'do stuff here, check event type whatever
          'if handled event
           '  return true
          ' else
           '    Return False
        End Function
end class
then where ever you make your device just dim a new MyEventReceiver class and set the device.EventReceiver = myVariableOfEventReceiverClass

That's just off the top of my head, so the code isn't 100% correct, but it should point you in the right direction.
Scionwest
Posts: 15
Joined: Sun Jul 30, 2006 4:01 am

Post by Scionwest »

Awesome! this is perfect, I will give it a shot when I get home tomarrow thanks! This should work looking at how you wrote it.
Scionwest
Posts: 15
Joined: Sun Jul 30, 2006 4:01 am

Post by Scionwest »

This worked Perfectly. Here is the code I used for anyone else that might need to use it.

My Handler class:

Code: Select all

Public Class CHandler
    Implements Irrlicht.IEventReceiver


    Public Function OnEvent(ByVal e As Irrlicht.Event) As Boolean Implements Irrlicht.IEventReceiver.OnEvent

        ''Handle all of the GUI related events
        If e.Type = Irrlicht.EventType.GUIEvent Then
            ''Store the event's ID
            Dim id As Integer = e.GUIEventCaller.ID

            ''See what kind of event was executed
            Select Case e.GUIEventType
                ''A button was clicked
            Case Irrlicht.GUI.GUIEvent.BUTTON_CLICKED
                    ''1001 = MainMenu.Play Game
                    If id = 1001 Then
                        MessageBox.Show("Test!")
                        ''1003 = MainMenu.Quit
                    ElseIf id = 1003 Then
                        ''If true the engine closes the rendering device and the application ends
                        Menus.MainMenu.Disable()
                        RenderingDevice.bQuiting = True
                    End If
            End Select
        End If
    End Function
End Class
My Menu class, this will contain a seperate class for each menu i need created.

Code: Select all

Public Class CMenus

    ''All of the menu classes
    Public MainMenu As New CMainMenu
    'Public OptionsMenu as New COptionsMenu

    ''So we can check which menu is loaded, and skip creating with each of the engines loop cycles
    Public CurrentMenu As String = ""
    Public Environment As Irrlicht.GUI.IGUIEnvironment

    ''Sends the Rendering Devices gui environment back to the caller
    ''Each menu will have it's own environment to modify
    Public Function SetEnvironment() As Irrlicht.GUI.IGUIEnvironment
        Return RenderingDevice.device.GUIEnvironment
    End Function

    Public Sub disableMenu()
        Environment.RootGUIElement.Visible = False
    End Sub

End Class
And then my main menu code.

Code: Select all

Public Class CMainMenu
    'Inherits CMenus

    ''Allows me to show\hide the gui when i want to
    Public env As GUI.IGUIEnvironment

    Public Sub Show()
        Dim Background As Irrlicht.Video.ITexture = RenderingDevice.device.VideoDriver.GetTexture("Background.jpg")
        Dim pos As New Position2D


        pos.X = RenderingDevice.device.VideoDriver.ScreenSize.Width / 4 * 3
        pos.Y = RenderingDevice.device.VideoDriver.ScreenSize.Height / 2

        Menus.CurrentMenu = "MainMenu"
        env = Menus.SetEnvironment

        ''disable the camera
        CameraFPS.disable(RenderingDevice.device)
        ''enable the cursor
        RenderingDevice.device.CursorControl.Visible = True

        ''start displaying the objects
        '        Dim window As Irrlicht.GUI.IGUIElement = Menus.Environment.AddWindow(New Rect(New Position2D(800, 600), New Dimension2D(800, 600)), False, "Trinity", RenderingDevice.device.GUIEnvironment.RootGUIElement, 1000)
        ''background
        env.AddImage(Background, New Position2D(0, 0), False, env.RootGUIElement, 100, Nothing)

        ''Play game button
        env.AddButton(New Rect(pos, New Dimension2D(98, 24)), env.RootGUIElement, 1001, "Play Game")


        ''options button
        pos.Y += pos.Y / 8
        RenderingDevice.device.GUIEnvironment.AddButton(New Rect(pos, New Dimension2D(98, 24)), env.RootGUIElement, 1002, "Options")

        ''Quit button
        pos.Y += pos.Y / 8
        RenderingDevice.device.GUIEnvironment.AddButton(New Rect(pos, New Dimension2D(98, 24)), env.RootGUIElement, 1003, "Quit")
    End Sub

    Public Sub Disable()
        env.RootGUIElement.Visible = False
    End Sub
End Class
Thanks again for all the help!
Locked