Problem with EventReceiver and mouse-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
bakkdoor
Posts: 49
Joined: Fri May 07, 2004 4:31 pm
Location: home

Problem with EventReceiver and mouse-events

Post by bakkdoor »

hi, i've got a problem with my eventhandling. i use irrlicht's eventsystem for input and forward my own events into my own eventsystem based on some input to use this data throughout my gameengine.
my problem is that while my keyboard input works fine, my mouseinput doesnt. first i had the problem that all mouseclicks for example get posted twice, or better said, that the code concerning leftmouse-click is executed twice, which is, well, kinda stupid ^^
i noticed that if i simply returned true right after that code, the mouse-click-code only was executed once.
but now i have the problem that if i do it this way, all gui-events arent passed anymore. they only work if i take out that return true after my mouse-click-code. but then again, these events are processed twice, although i only clicked once for example.

maybe i'm missing something here? could someone post his mouse-handling eventcode here that works? or is it a bug in irrlicht.net? i cant find the solution here...

code:

Code: Select all


namespace Game.Events
{
    /// <summary>
    /// Takes care of all Irrlicht related events and forwards them, if needed, into the game engine.
    /// </summary>
    class IrrEventHandler : IEventReceiver
    {
        public bool OnEvent(Irrlicht.Event evt)
        {

            if (evt.Type == Irrlicht.EventType.GUIEvent)
            {
                if (evt.GUIEventType == Irrlicht.GUI.GUIEvent.BUTTON_CLICKED)
                {
                    Console.WriteLine("buttonclicked, id: " + evt.GUIEventCaller.ID);
                    switch (evt.GUIEventCaller.ID)
                    {
                        // LeaveMission-Dialog - YES!
                        case 100:
                            EventManager.QueueEvent(new Evt_LeaveMission());

                            break;

                        case 101:
                            break;
                    }
                }
            }

            // handle keyinput!
            else if (evt.Type == Irrlicht.EventType.KeyInput)
            {
                if (evt.KeyPressedDown)
                {
                    switch (evt.Key)
                    {
                        case KeyCode.KEY_RETURN:
                            EventManager.Trigger(new Evt_EntityCreated(new EvtData_EntityCreated(-1, EntityType.Environment_Static, new Vector3D())));
                            break;

                        case KeyCode.KEY_ESCAPE:
                            EventManager.QueueEvent(new Evt_ESCPressed());
                            break;
                    }
                }
            }
            else if (evt.Type == Irrlicht.EventType.MouseInput)
            {
                switch (evt.MouseInputType)
                {
                    case MouseInputEvent.PressedDownLeft:
                        Console.WriteLine("mouse pressed!");
                        // send the mouseclicked-event into the game engine event system
                        EventManager.QueueEvent(new Evt_MouseClicked(
                            new EvtData_MouseClicked(evt.MousePos)));

                        // entitySelection:
                        ISceneNode node = GameApplication.SceneMgr.SceneCollisionManager.GetSceneNodeFromScreenCoordinatesBB(evt.MousePos, 0, false);
                        if (node != null)
                        {
                            node.DebugDataVisible = !node.DebugDataVisible;
                        }

                        break;
                  

                    case MouseInputEvent.MouseWheel:

                        if (GameApplication.Game.State == GameState.Running)
                        {
                            // update cam-position via wheel-movement
                            Vector3D camPos = GameApplication.Device.SceneManager.ActiveCamera.Position;
                            Vector3D camTarget = GameApplication.Device.SceneManager.ActiveCamera.Rotation;
                            camPos.Y += evt.MouseWheelDelta * 100.0f;
                            GameApplication.Device.SceneManager.ActiveCamera.Position = camPos;
                        }

                        break;
                }
            }

            return false;
        }
    }
}
bakkdoor
Posts: 49
Joined: Fri May 07, 2004 4:31 pm
Location: home

Post by bakkdoor »

ok, i managed to fix the problem on my own..
i dont think this is the way it "should be", but oh well, it works ^^

code:

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using Irrlicht;
using Game.Entities;
using Irrlicht.Core;
using Game.Processes;
using Irrlicht.Scene;

namespace Game.Events
{
    /// <summary>
    /// Takes care of all Irrlicht related events and forwards them, if needed, into the game engine.
    /// </summary>
    class IrrEventHandler : IEventReceiver
    {
        private bool mousePressedDown;
        public bool OnEvent(Irrlicht.Event evt)
        {
            if (evt.Type == Irrlicht.EventType.GUIEvent)
            {
                if (evt.GUIEventType == Irrlicht.GUI.GUIEvent.MESSAGEBOX_YES)
                {
                    switch (evt.GUIEventCaller.ID)
                    {
                            // leave mission!
                        case 100:
                            EventManager.QueueEvent(new Evt_LeaveMission());
                            break;
                    }
                }
            }

            // handle keyinput!
            else if (evt.Type == Irrlicht.EventType.KeyInput)
            {
                if (evt.KeyPressedDown)
                {
                    switch (evt.Key)
                    {
                        case KeyCode.KEY_RETURN:
                            EventManager.Trigger(new Evt_EntityCreated(new EvtData_EntityCreated(-1, EntityType.Environment_Static, new Vector3D())));
                            break;

                        case KeyCode.KEY_ESCAPE:
                            EventManager.QueueEvent(new Evt_ESCPressed());
                            break;
                    }
                }
            }
            else if (evt.Type == Irrlicht.EventType.MouseInput)
            {
                switch (evt.MouseInputType)
                {
                    case MouseInputEvent.PressedDownLeft:

                        if (!mousePressedDown)
                        {
                            // send the mouseclicked-event into the game engine event system
                            EventManager.QueueEvent(new Evt_MouseClicked(
                                new EvtData_MouseClicked(evt.MousePos)));

                            // entitySelection:
                            ISceneNode node = GameApplication.SceneMgr.SceneCollisionManager.GetSceneNodeFromScreenCoordinatesBB(evt.MousePos, 0, false);
                            if (node != null)
                            {
                                node.DebugDataVisible = !node.DebugDataVisible;
                            }


                            mousePressedDown = true;
                        }
                        else
                        {
                            mousePressedDown = false;
                        }

                        break;

                    case MouseInputEvent.MouseWheel:

                        if (GameApplication.Game.State == GameState.Running)
                        {
                            // update cam-position via wheel-movement
                            Vector3D camPos = GameApplication.Device.SceneManager.ActiveCamera.Position;
                            Vector3D camTarget = GameApplication.Device.SceneManager.ActiveCamera.Rotation;
                            camPos.Y += evt.MouseWheelDelta * 100.0f;
                            GameApplication.Device.SceneManager.ActiveCamera.Position = camPos;
                        }

                        break;
                }
            }

            return false;
        }
    }
}
all i did was added a boolean to make sure the mouse events arent processed twice.. :)
maybe someone can tell me how it should be done though? ^^
Locked