Page 1 of 1

getCursorControl() from TouchInput event

Posted: Mon Aug 25, 2014 1:26 pm
by sabotage3d
Hello ,
I am using the GLES branch of Irrlicht.
Is there an easy way to pass TouchInput event to getCursorControl ?
I am trying to get the getCursorControl to work under IOS for compatability with GUI's and FPS camera's.
If I do getCursorControl()->setPosition or getCursorControl()->getPosition it crashes under IOS.
I hope that I can use the existing GLES device without modifying it.
That's my event receiver for IOS:

Code: Select all

  if (event.EventType == irr::EET_TOUCH_INPUT_EVENT)
        {
            switch(event.TouchInput.Event)
            {
                case ETIE_MOVED:
                    MouseState.Position.X = event.TouchInput.X;
                    MouseState.Position.Y = event.TouchInput.Y;
                    break;
            }
         }
 
Thanks,

Alex

Re: getCursorControl() from TouchInput event

Posted: Mon Aug 25, 2014 9:11 pm
by CuteAlien
getCursorControl probably won't work. And even if it would - using the FPS camera just like that on a handheld device is unlikely to work well. But you can get the GUI mostly working. Checkout the Android example which does that.

Re: getCursorControl() from TouchInput event

Posted: Mon Aug 25, 2014 11:15 pm
by sabotage3d
What would be the best solution then ,to create my own CCursorControl and pass it manually to my own camera ?
I am using CIrrDeviceiOS and I just noticed the cursor control is not implemented at all.

Re: getCursorControl() from TouchInput event

Posted: Tue Aug 26, 2014 7:27 pm
by CuteAlien
The easier way is probably to write your own camera-controls instead.

Re: getCursorControl() from TouchInput event

Posted: Tue Aug 26, 2014 9:01 pm
by sabotage3d
Thanks a lot I just noticed how handy is the OnEvent function on the cameras :)

Re: getCursorControl() from TouchInput event

Posted: Wed Sep 10, 2014 10:05 pm
by FloatyBoaty
Anything that changes the cursor (hiding, moving, etc.) crashes the application on hand-held devices since they don't have cursors.
This may be something to change in the branch -> interrupt these actions in the engine on mobile devices: it will make the library more cross-platform compatible.

Re: getCursorControl() from TouchInput event

Posted: Mon Sep 15, 2014 10:16 am
by CuteAlien
Thanks, it should definitely not crash and likely easy to fix. I'm back to mobile dev this week, so I'll check it out.

Re: getCursorControl() from TouchInput event

Posted: Mon Sep 15, 2014 7:16 pm
by FloatyBoaty
ICursorControl was never implemented for Mobile.....
So, here is my contribution.

---------------------------------------------

CMobileCursorControl.h:

Code: Select all

 
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
// Jonathan Frisch : Sep. 15, 2014
 
#ifndef __C_MOBILE_CURSOR_CONTROL_H_INCLUDED__
#define __C_MOBILE_CURSOR_CONTROL_H_INCLUDED__
 
#include "IrrlichtDevice.h"
#include "ICursorControl.h"
#include "position2d.h"
#include "dimension2d.h"
#include "rect.h"
 
namespace irr
{
    namespace gui
    {
        //! The type of motion that the touch event will emulate.
        enum ECURSOR_TOUCH_MOTION_TYPE
        {
            ECTMT_DRAG, // getPosition returns difference between relative center and current touch point
            ECTMT_TRACE, // getPosition returns difference between last touch point and current touch point
            ECTMT_COUNT // maximum : for consistency
        };
 
        //! Mobile fake mouse cursor control.
        class CMobileCursorControl : public ICursorControl
        {
            public:
                CMobileCursorControl(IrrlichtDevice* device);
 
                virtual ~CMobileCursorControl();
 
                //! Manually update touch point.
                virtual void updateTouchPoint(core::position2di touch);
 
                //! Mobile device -- does nothing
                virtual void setVisible(bool visible) _IRR_OVERRIDE_;
 
                //! Mobile device -- return false
                virtual bool isVisible() _IRR_OVERRIDE_;
 
                //! Sets the new position of the cursor.
                /** The position must be
                between (0.0f, 0.0f) and (1.0f, 1.0f), where (0.0f, 0.0f) is
                the top left corner and (1.0f, 1.0f) is the bottom right corner of the
                render window.
                \param pos New position of the cursor. */
                virtual void setPosition(const core::position2df &pos) _IRR_OVERRIDE_;
 
                //! Sets the new position of the cursor.
                /** The position must be
                between (0.0f, 0.0f) and (1.0f, 1.0f), where (0.0f, 0.0f) is
                the top left corner and (1.0f, 1.0f) is the bottom right corner of the
                render window.
                \param x New x-coord of the cursor.
                \param y New x-coord of the cursor. */
                virtual void setPosition(f32 x, f32 y) _IRR_OVERRIDE_;
 
                //! Sets the new position of the cursor.
                /** \param pos: New position of the cursor. The coordinates are pixel units. */
                virtual void setPosition(const core::position2di &pos) _IRR_OVERRIDE_;
 
                //! Sets the new position of the cursor.
                /** \param x New x-coord of the cursor. The coordinates are pixel units.
                \param y New y-coord of the cursor. The coordinates are pixel units. */
                virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_;
 
                //! Returns the current position of the mouse cursor.
                /** \return Returns the current position of the cursor. The returned position
                is the position of the mouse cursor in pixel units. */
                virtual const core::position2di& getPosition() _IRR_OVERRIDE_;
 
                //! Returns the current position of the mouse cursor.
                /** \return Returns the current position of the cursor. The returned position
                is a value between (0.0f, 0.0f) and (1.0f, 1.0f), where (0.0f, 0.0f) is
                the top left corner and (1.0f, 1.0f) is the bottom right corner of the
                render window. */
                virtual core::position2df getRelativePosition() _IRR_OVERRIDE_;
 
                //! Sets an absolute reference rect for setting and retrieving the cursor position.
                /** If this rect is set, the cursor position is not being calculated relative to
                the rendering window but to this rect. You can set the rect pointer to 0 to disable
                this feature again. This feature is useful when rendering into parts of foreign windows
                for example in an editor.
                \param rect: A pointer to an reference rectangle or 0 to disable the reference rectangle.*/
                virtual void setReferenceRect(core::recti* rect=0) _IRR_OVERRIDE_;
 
                //! Set the touch motion behavior type.
                virtual void setTouchMotionType(ECURSOR_TOUCH_MOTION_TYPE type);
 
                //! Get the touch motion behavior type.
                virtual ECURSOR_TOUCH_MOTION_TYPE getTouchMotionType();
 
                //! Set the factor to scale the movement.
                virtual void setMovementScaleFactor(f32 scale);
 
                //! Get the scaling factor for movement.
                virtual f32 getMovementScaleFactor();
 
            private:
                ECURSOR_TOUCH_MOTION_TYPE mtype;
                core::recti* ref;
                f32 factor;
                core::position2di relCenter, ctouch, ltouch;
                IrrlichtDevice* iDevice;
        };
 
 
    } // end namespace gui
} // end namespace irr
 
#endif // __C_MOBILE_CURSOR_CONTROL_H_INCLUDED__
 
 
-------------------------------------------

CMobileCursorControl.cpp

Code: Select all

 
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
// Jonathan Frisch : Sep. 15, 2014
 
#include "CMobileCursorControl.h"
 
namespace irr
{
    namespace gui
    {
        CMobileCursorControl::CMobileCursorControl(IrrlichtDevice* device) : mtype(ECTMT_DRAG),
            ref(0), relCenter(0, 0),
            iDevice(device), ctouch(0, 0), ltouch(0, 0)
        {
            iDevice->grab();
 
            #if defined(_DEBUG) || defined(DEBUG)
            setDebugName("CMobileCursorControl");
            #endif // DEBUG
 
            setReferenceRect();
        }
 
        CMobileCursorControl::~CMobileCursorControl()
        {
            iDevice->drop();
            if(ref) delete ref;
        }
 
        //! Manually update touch point.
        void CMobileCursorControl::updateTouchPoint(core::position2di touch)
        {
            ltouch = ctouch;
            ctouch = touch;
        }
 
        //! Mobile device -- does nothing
        void CMobileCursorControl::setVisible(bool visible)
        {
        }
 
        //! Mobile device -- return false
        bool CMobileCursorControl::isVisible()
        {
            return false;
        }
 
        //! Sets the new position of the cursor.
        /** The position must be
        between (0.0f, 0.0f) and (1.0f, 1.0f), where (0.0f, 0.0f) is
        the top left corner and (1.0f, 1.0f) is the bottom right corner of the
        render window.
        \param pos New position of the cursor. */
        void CMobileCursorControl::setPosition(const core::position2df &pos)
        {
            setPosition(pos.X, pos.Y);
        }
 
        //! Sets the new position of the cursor.
        /** The position must be
        between (0.0f, 0.0f) and (1.0f, 1.0f), where (0.0f, 0.0f) is
        the top left corner and (1.0f, 1.0f) is the bottom right corner of the
        render window.
        \param x New x-coord of the cursor.
        \param y New x-coord of the cursor. */
        void CMobileCursorControl::setPosition(f32 x, f32 y)
        {
            setPosition(s32(x * (ref->LowerRightCorner.X - ref->UpperLeftCorner.X)), s32(y * (ref->LowerRightCorner.Y - ref->UpperLeftCorner.Y)));
        }
 
        //! Sets the new position of the cursor.
        /** \param pos: New position of the cursor. The coordinates are pixel units. */
        void CMobileCursorControl::setPosition(const core::position2di &pos)
        {
            relCenter = pos;
        }
 
        //! Sets the new position of the cursor.
        /** \param x New x-coord of the cursor. The coordinates are pixel units.
        \param y New y-coord of the cursor. The coordinates are pixel units. */
        void CMobileCursorControl::setPosition(s32 x, s32 y)
        {
            setPosition(core::position2di(x, y));
        }
 
        //! Returns the current position of the mouse cursor.
        /** \return Returns the current position of the cursor. The returned position
        is the position of the mouse cursor in pixel units. */
        const core::position2di& CMobileCursorControl::getPosition()
        {
            if(ref->isPointInside(ctouch))
            {
                core::position2di p;
                switch(mtype)
                {
                    case ECTMT_DRAG:
                        p.X = (relCenter.X - ctouch.X) * factor;
                        p.Y = (relCenter.Y - ctouch.Y) * factor;
                        break;
                    case ECTMT_TRACE:
                        p.X = (ltouch.X - ctouch.X) * factor;
                        p.Y = (ltouch.Y - ctouch.Y) * factor;
                        break;
                    default:
                        p.X = relCenter.X;
                        p.Y = relCenter.Y;
                        break;
                }
                return p;
            }
            return relCenter;
        }
 
        //! Returns the current position of the mouse cursor.
        /** \return Returns the current position of the cursor. The returned position
        is a value between (0.0f, 0.0f) and (1.0f, 1.0f), where (0.0f, 0.0f) is
        the top left corner and (1.0f, 1.0f) is the bottom right corner of the
        render window. */
        core::position2df CMobileCursorControl::getRelativePosition()
        {
            core::position2di t(getPosition());
            return core::position2df(t.X / (ref->LowerRightCorner.X - ref->UpperLeftCorner.X), t.Y / (ref->LowerRightCorner.Y - ref->UpperLeftCorner.Y));
        }
 
        //! Sets an absolute reference rect for setting and retrieving the cursor position.
        /** If this rect is set, the cursor position is not being calculated relative to
        the rendering window but to this rect. You can set the rect pointer to 0 to disable
        this feature again. This feature is useful when rendering into parts of foreign windows
        for example in an editor.
        \param rect: A pointer to an reference rectangle or 0 to disable the reference rectangle.*/
        void CMobileCursorControl::setReferenceRect(core::recti* rect)
        {
            if(!rect) rect = new core::recti(core::position2di(0, 0), iDevice->getVideoDriver()->getScreenSize());
            if(ref) delete ref;
            ref = rect;
        }
 
        //! Set the touch motion behavior type.
        void CMobileCursorControl::setTouchMotionType(ECURSOR_TOUCH_MOTION_TYPE type)
        {
            mtype = type;
        }
 
        //! Get the touch motion behavior type.
        ECURSOR_TOUCH_MOTION_TYPE CMobileCursorControl::getTouchMotionType()
        {
            return mtype;
        }
 
        //! Set the factor to scale the movement.
        void CMobileCursorControl::setMovementScaleFactor(f32 scale)
        {
            factor = scale;
        }
 
        //! Get the scaling factor for movement.
        f32 CMobileCursorControl::getMovementScaleFactor()
        {
            return factor;
        }
    } // end namespace gui
} // end namespace irr