GUI event receiver . Is it possible?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fernando24691
Posts: 10
Joined: Thu Jan 22, 2009 8:12 pm

GUI event receiver . Is it possible?

Post by fernando24691 »

Hi.

I'm making my own project, so I picked up the MastEventReceiver as a receiver. Now I've developed my project a little more and I want to include a GUI (buttons,bars,menus,etc), but I've realised that the MastEventReceiver hasn't got any code for GUI elements. Due to this lack, I've started out to code some lines that let me handle GUI events. I've put these lines inside OnEvent block, as keyboarevents and mouse events are. In the header, I declared a new keystatesEnum variable for my guiStates:

Code: Select all

 // GUI buttons
   keyStatesENUM guiState[100]; 
Here's the code I've put inside the OnEvent function:

Code: Select all

//////////////////////////////
	/////// GUI Input Event///////
    //////////////////////////////

	   if (event.EventType == EET_GUI_EVENT){

     /* !!!BREAK!!!*/      int id = event.GUIEvent.Caller->getID();
            

        switch(event.GUIEvent.EventType)
		{
			case EGET_BUTTON_CLICKED:
               if (guiState[id] == UP || guiState[id] == RELEASED) guiState[id] = PRESSED;
			   else guiState[id] = DOWN;    
			default: break;
        }
		 
        eventprocessed = true;

      }


      return eventprocessed;
   }

Later I made this function in order to use it in my main loop.

Code: Select all

 bool ButtonPressed(int id)
   {
	   if (guiState[id] == PRESSED || guiState[id] == DOWN)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
Then, when I try to run this function on debug mode, it doesn't start. Event if I put a break point as you can see above (!!!BREAK!!!), it doesn't break. Why??
:shock:

I hope you could help me! :roll: [/code]
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I think this is a really weird way to do GUI events.... GUI events are not like key presses on a keyboard... there are widely ranging types of GUI events like button presses, checkbox changes, scrollbar changes, windows closing etc... they're not all On/Off states like keys are.
Image Image Image
fernando24691
Posts: 10
Joined: Thu Jan 22, 2009 8:12 pm

Post by fernando24691 »

Okey. Thanks. But there's one more question. If I don't use an event receiver to handle GUI events, what should I use to handle them. I've looked for one guide or tutorial about handling GUI events, but I've only found out the one that comes with irrlicht (5th tutorial on the web).

Should I use the method shown in that tutorial or theres another one??????????
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Your handler is fine; what puzzling JP (and me) is why you'd want to store the state for a GUI element.

Unlike with key states, you'd generally want to act on the actual GUI events themselves when they're received, rather than storing a state and acting on it later. Do you have some particular requirement that has led you to store states?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
fernando24691
Posts: 10
Joined: Thu Jan 22, 2009 8:12 pm

Post by fernando24691 »

No, I just want to set up a simple GUI receiver, but I didn't know how to so I decided to make this type of receiver. I don't want to store any data.
I would know which method should I use to set up a simple GUI receiver (like buttons, labels, windows,etc.)
fernando24691
Posts: 10
Joined: Thu Jan 22, 2009 8:12 pm

Post by fernando24691 »

The problem is that I include a GUI code lines inside the MastEventReceiver.
Eventually, what I did was to put the 5th tutorial on the web (UserInterface) into my project. Because I have to modify my source code to include the MastEventReceiver in my main.cpp file (using the Sapp context). I think I have implemented it well since I don't get any code mistakes and my program runs well.

The problem comes when debugging. I have put a breakpoint inside Keyboard Input Process, another inside Mouse Input Process and one last inside GUI Input Process (the code I copied from the tutorial). Well, when run it, the program breaks at Keyboard and Mouse Process (I guess they launched well), but when I click a button, or a toolbar, it doesn't break.

Why comes this error?

For more precission, I will put here all my code, so you (if have some minutes) could see it (some comments are in spanish, don't care :oops: )

Code: Select all


#include <irrlicht.h>
#include <iostream>

using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////---Estructura del contexto---/////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct SAppContext
{
        IrrlichtDevice *device;
        s32                             counter;
        IGUIListBox*    listbox;
};

enum
{
        GUI_ID_QUIT_BUTTON = 101,
        GUI_ID_NEW_WINDOW_BUTTON,
        GUI_ID_FILE_OPEN_BUTTON,
        GUI_ID_TRANSPARENCY_SCROLL_BAR
};


////////////////////////////////////////////-----------------------//////////////////////////////////////////////
////////////////////////////////////////////--MAST EVENT RECEIVER--//////////////////////////////////////////////
////////////////////////////////////////////----------------------///////////////////////////////////////////////

class MastEventReceiver : public IEventReceiver
{
   public:
			
	   MastEventReceiver(SAppContext & context) : Context(context) { }


   // Enumeration for UP, DOWN, PRESSED and RELEASED key states. Also used for mouse button states.
   enum keyStatesENUM {UP, DOWN, PRESSED, RELEASED};
			keyStatesENUM mouseButtonState[2]; //Left(0), Middle(1) and Right(2) Buttons.
			 keyStatesENUM keyState[KEY_KEY_CODES_COUNT];

   // Enumeration for Event Handling State.
   enum processStateENUM {STARTED, ENDED};
   processStateENUM processState; // STARTED = handling events, ENDED = not handling events

   // Mouse X/Y coordinates and Wheel data.
   struct mouseData
   {
   int X;
   int Y;
   float wheel; //wheel is how far the wheel has moved
   };
   struct mouseData mouse;
  

   virtual bool OnEvent(const SEvent& event)
   {
      bool eventprocessed = false;

	  //////////////////////////////
      // GUI Input Event
      //////////////////////////////

	if (event.EventType == EET_GUI_EVENT)
                {
                        s32 id = event.GUIEvent.Caller->getID();
                        IGUIEnvironment* guienv = Context.device->getGUIEnvironment();

                        switch(event.GUIEvent.EventType)
                        {
							case EGET_SCROLL_BAR_CHANGED:
                                if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
                                {
                                        s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
                                        
                                        for (u32 i=0; i<EGDC_COUNT ; ++i)
                                        {
                                                SColor col = guienv->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
                                                col.setAlpha(pos);
                                                guienv->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
                                        }
                                        
                                }
                                break;
							case EGET_BUTTON_CLICKED:
                                switch(id)
                                {
                                case GUI_ID_QUIT_BUTTON:
                                        Context.device->closeDevice();
                                        return true;

                                case GUI_ID_NEW_WINDOW_BUTTON:
                                        {
                                        Context.listbox->addItem(L"Window created");
                                        Context.counter += 30;
                                        if (Context.counter > 200)
                                                Context.counter = 0;

                                        IGUIWindow* window = guienv->addWindow(
                                                rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
                                                false, // modal?
                                                L"Test window");

                                        guienv->addStaticText(L"Please close me",
                                                rect<s32>(35,35,140,50),
                                                true, // border?
                                                false, // wordwrap?
                                                window);
                                        }
                                        return true;

                                case GUI_ID_FILE_OPEN_BUTTON:
                                        Context.listbox->addItem(L"File open");
                                        guienv->addFileOpenDialog(L"Please choose a file.");
                                        return true;

                                default:
                                        return false;
                                }
                                break;

                        default:
                                break;
                        }
	}

      //////////////////////////////
      // Keyboard Input Event
      //////////////////////////////
      if (event.EventType == EET_KEY_INPUT_EVENT)
      {
         if (processState == STARTED)
         {
            // if key is Pressed Down
            if (event.KeyInput.PressedDown == true)
            {
               // If key was not down before
               if (keyState[event.KeyInput.Key] != DOWN)
               {
                  keyState[event.KeyInput.Key] = PRESSED; // Set to Pressed
               }
               else
               {
                  // if key was down before
                  keyState[event.KeyInput.Key] = DOWN; // Set to Down
               }
            }
            else
            {

                  // if the key is down
                  if (keyState[event.KeyInput.Key] != UP)
                  {
                     keyState[event.KeyInput.Key] = RELEASED; // Set to Released
                  }
            }
         }


         eventprocessed = true;
      }

      //////////////////////////////
      // Mouse Input Event
      //////////////////////////////

      if (event.EventType == EET_MOUSE_INPUT_EVENT)
      {
         if (processState == STARTED)
         {
            //Mouse changed position
            if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
            {
               mouse.Y = event.MouseInput.Y;
               mouse.X = event.MouseInput.X;
            }

            //Wheel moved.
            if (event.MouseInput.Event == EMIE_MOUSE_WHEEL)
            {
               mouse.wheel += event.MouseInput.Wheel;
				
            }

            //Left Mouse Button Pressed
            if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
            {
               //
               if (mouseButtonState[0] == UP || mouseButtonState[0] == RELEASED)
               {
                  mouseButtonState[0] = PRESSED;
               }
               else
               {
                  mouseButtonState[0] = DOWN;
               }
            }

            //Left Mouse Button Rleased
            if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
            {
               //
               if (mouseButtonState[0] != UP)
               {
                  mouseButtonState[0] = RELEASED;
               }
            }

            //Middle Mouse Button Pressed
            if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
            {
               //
               if (mouseButtonState[1] == UP || mouseButtonState[1] == RELEASED)
               {
                  mouseButtonState[1] = PRESSED;
               }
               else
               {
                  mouseButtonState[1] = DOWN;
               }
            }

            //Middle Mouse Button Rleased
            if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
            {
               //
               if (mouseButtonState[1] != UP)
               {
                  mouseButtonState[1] = RELEASED;
               }
            }

            //Right Mouse Button Pressed
            if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
            {
               //
               if (mouseButtonState[2] == UP || mouseButtonState[2] == RELEASED)
               {
                  mouseButtonState[2] = PRESSED;
               }
               else
               {
                  mouseButtonState[2] = DOWN;
               }
            }

            //Right Mouse Button Rleased
            if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
            {
               //
               if (mouseButtonState[2] != UP)
               {
                  mouseButtonState[2] = RELEASED;
               }
            }
         }


         eventprocessed = true;
      }

      return eventprocessed;
   }


   //////////////////////
   // Public functions
   //////////////////////
   public:

   float mouseWheel()
   {
      return mouse.wheel;
   }

   int mouseX()
   {
      return mouse.X;
   }

   int mouseY()
   {
      return mouse.Y;
   }

   bool leftMouseReleased()
   {
      if (mouseButtonState[0] == RELEASED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
   bool leftMouseUp()
   {
      if (mouseButtonState[0] == RELEASED || mouseButtonState[0] == UP)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool leftMousePressed()
   {
      if (mouseButtonState[0] == PRESSED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool leftMouseDown()
   {
      if (mouseButtonState[0] == PRESSED || mouseButtonState[0] == DOWN)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool middleMouseReleased()
   {
      if (mouseButtonState[1] == RELEASED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
   bool middleMouseUp()
   {
      if (mouseButtonState[1] == RELEASED || mouseButtonState[1] == UP)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool middleMousePressed()
   {
      if (mouseButtonState[1] == PRESSED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool middleMouseDown()
   {
      if (mouseButtonState[1] == PRESSED || mouseButtonState[1] == DOWN)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool rightMouseReleased()
   {
      if (mouseButtonState[2] == RELEASED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
   bool rightMouseUp()
   {
      if (mouseButtonState[2] == RELEASED || mouseButtonState[2] == UP)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool rightMousePressed()
   {
      if (mouseButtonState[2] == PRESSED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool rightMouseDown()
   {
      if (mouseButtonState[2] == PRESSED || mouseButtonState[2] == DOWN)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool keyPressed(char keycode)
   {
      if (keyState[keycode] == PRESSED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool keyDown(char keycode)
   {
      if (keyState[keycode] == DOWN || keyState[keycode] == PRESSED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool keyUp(char keycode)
   {
      if (keyState[keycode] == UP || keyState[keycode] == RELEASED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   bool keyReleased(char keycode)
   {
      if (keyState[keycode] == RELEASED)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
   

   // This is used so that the Key States will not be changed during execution of your Main game loop.
   // Place this at the very START of your Main Loop
   void endEventProcess()
   {
      processState = ENDED;
   }

   // This is used so that the Key States will not be changed during execution of your Main game loop.
   // Place this function at the END of your Main Loop.

   void startEventProcess()
   {

      processState = STARTED;
      //Keyboard Key States
      for (int i = 0; i < KEY_KEY_CODES_COUNT; i++)
      {
         if (keyState[i] == RELEASED)
         {
            keyState[i] = UP;
         }

         if (keyState[i] == PRESSED)
         {
            keyState[i] = DOWN;
         }
      }
      //Mouse Button States
      for (int i = 0; i <= 2; i++)
      {
         if (mouseButtonState[i] == RELEASED)
         {
            mouseButtonState[i] = UP;
         }

         if (mouseButtonState[i] == PRESSED)
         {
            mouseButtonState[i] = DOWN;
         }
      }
      //Mouse Wheel state
      mouse.wheel = 0.0f;

	
   }
   void init()
   {
      //KeyBoard States.
      for (int i = 0; i <= KEY_KEY_CODES_COUNT; i++)
      {
         keyState[i] = UP;
      }
      //Mouse states
      for (int i = 0; i <= 2; i++)
      {
         mouseButtonState[i] = UP;
      }
      //Mouse X/Y coordenates.
      mouse.X = 0;
      mouse.Y = 0;
      mouse.wheel = 0.0f;
   
}







   private:
        SAppContext & Context;

   };



////////////////////////////////////////////-----------------------//////////////////////////////////////////////
////////////////////////////////////////////------FUNCIONES-------/////////////////////////////////////////////
////////////////////////////////////////////----------------------///////////////////////////////////////////////



 void move(scene::ISceneNode *node,core::vector3df vel){
    core::matrix4 m;
    m.setRotationDegrees(node->getRotation());
    m.transformVect(vel);
    node->setPosition(node->getAbsolutePosition() + vel);
}
 void rotate(ISceneNode* seeker, vector3df target){
 // Where target and seeker are of type ISceneNode*

	vector3df toTarget(target - seeker->getPosition());
	vector3df requiredRotation = toTarget.getHorizontalAngle();
	seeker->setRotation(vector3df(0,requiredRotation.Y,0));


 }




 //////////////////////////////////////////-----------------/////////////////////////////////////////////////////
//////////////////////////////////////////-------INICIO-----/////////////////////////////////////////////////////
//////////////////////////////////////////------------------/////////////////////////////////////////////////////

int main()
{
    
  IrrlichtDevice *device = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(1024, 768),16,0,0,0,0);

   if(device==0)
   {
      return 1;
   }
   
   IGUIEnvironment *guienv = device->getGUIEnvironment();
   ISceneManager *smgr = device->getSceneManager();
   IVideoDriver *driver = device->getVideoDriver(); 
   ISceneCollisionManager  *collman  = smgr->getSceneCollisionManager(); 



 

/*********************///////////////////////////////////////////////////////////////////////////************************************
/*********************/////////////////////DECLARACION VARIABLES/////////////////////////////////************************************
/*********************///////////////////////////////////////////////////////////////////////////************************************


//////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES DEL ENTORNO///////////////////////////////
//////////////////////////////////////////////////////////////////////////

    IMeshSceneNode* suelo = smgr->addCubeSceneNode(10,0,-1,vector3df(0,0,0),vector3df(0,0,0),vector3df(100,1,100));
	suelo->setMaterialFlag(video::EMF_LIGHTING,false);
	suelo->setMaterialTexture(0,driver->getTexture("../../media/wall.bmp"));

	//Añadimos un selector de triangulos

		ITriangleSelector* selector = smgr->createOctTreeTriangleSelector(suelo->getMesh(),suelo,32);
		suelo->setTriangleSelector(selector);
















////////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES DEL PERSONAJE///////////////////////////////
////////////////////////////////////////////////////////////////////////////

   //Añadimos la malla y su colisionador

        IAnimatedMesh* mesh = smgr->getMesh("../../media/ninja.b3d");
			

        IAnimatedMeshSceneNode* enano = smgr->addAnimatedMeshSceneNode(mesh,0,-1,vector3df(0,40,0),vector3df(0,0,0),vector3df(3,3,3));

				enano->animateJoints(true);
				enano->setPosition(vector3df(0,10,0));
                enano->setMaterialFlag(EMF_LIGHTING, false);
				enano->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
				enano->setMaterialTexture( 0, driver->getTexture("../../media/nskinrd.jpg") );

				enano->setAnimationSpeed(15);
                enano->setFrameLoop(206,250);

				

		ISceneNodeAnimator* Benano = smgr->createCollisionResponseAnimator(selector, enano,vector3df(4,10,4),vector3df(0,-9,0),vector3df(0,-9,0));
                enano->addAnimator(Benano);
                Benano->drop();















//////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES NPC///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

					//Añadimos el enemigo y el colisionador
        IAnimatedMeshSceneNode* enemigo = smgr->addAnimatedMeshSceneNode(mesh,0,-1,vector3df(0,40,0),vector3df(0,-9.81f,0),vector3df(3,3,3));

				
				enemigo->setPosition(vector3df(300,80,60));
                enemigo->setMaterialFlag(EMF_LIGHTING, false);
				enemigo->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
				enemigo->setMaterialTexture( 0, driver->getTexture("../../media/nskinbl.jpg") );

				enemigo->setAnimationSpeed(15);
                enemigo->setFrameLoop(206,250);

				enemigo->addShadowVolumeSceneNode();
			

		ISceneNodeAnimator* Denano = smgr->createCollisionResponseAnimator(selector, enemigo,vector3df(2,10,2),vector3df(0,-9.81f,0),vector3df(0,-9,0));
                enemigo->addAnimator(Denano);
                Denano->drop();

















		
////////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES DE LA CAMARA////////////////////////////////
////////////////////////////////////////////////////////////////////////////
	
	ICameraSceneNode* camara = smgr->addCameraSceneNode(0,enano->getPosition(),enano->getPosition(),0);
	ISceneNodeAnimator* Bcamara = smgr->createCollisionResponseAnimator(selector, camara,vector3df(5,5,5),vector3df(0,0,0),vector3df(0,0,0));
                camara->addAnimator(Bcamara);
                Bcamara->drop();



















//////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES FX////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
				
	
     IVolumeLightSceneNode * n = smgr->addVolumeLightSceneNode(0, -1,32,32,SColor(0, 255, 255, 255), SColor(0, 0, 0, 0));   

       n->setScale(vector3df(40,10,40));
	   n->setVisible(false);

                array<video::ITexture*> textures;
                for (s32 g=7; g > 0; --g)
                {
                        core::stringc tmp;
                        tmp = "../../media/portal";
                        tmp += g;
                        tmp += ".bmp";
                        video::ITexture* t = driver->getTexture( tmp.c_str() );
                        textures.push_back(t);
                }

                // create texture animator
                scene::ISceneNodeAnimator* glow = smgr->createTextureAnimator(textures, 60);

                // add the animator
                n->addAnimator(glow);

                // drop the animator because it was created with a create() function
                glow->drop();











        
////////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES DEL GUI///////////////////////////////
////////////////////////////////////////////////////////////////////////////


         IGUISkin* skin = guienv->getSkin();
        IGUIFont* font = guienv->getFont("../../media/fonthaettenschweiler.bmp");
        if (font)
                skin->setFont(font);

        skin->setFont(guienv->getBuiltInFont(), EGDF_TOOLTIP);

		guienv->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
                        L"Quit", L"Exits Program");
        guienv->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
                        L"New Window", L"Launches a new Window");
        guienv->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
                        L"File Open", L"Opens a file");
		guienv->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
        IGUIScrollBar* scrollbar = guienv->addScrollBar(true,
                        rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
        scrollbar->setMax(255);

        // set scrollbar position to alpha value of an arbitrary element
        scrollbar->setPos(guienv->getSkin()->getColor(EGDC_WINDOW).getAlpha());

        guienv->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
        IGUIListBox * listbox = guienv->addListBox(rect<s32>(50, 140, 250, 210));
        guienv->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));

		guienv->addImage(driver->getTexture("../../media/irrlichtlogo2.png"),
                        position2d<int>(10,10));


////////////////////////////////////////////////////////////////////////////
//////////////////////DECLARACION EVENT RECEIVER////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

     
	        SAppContext context;
        context.device = device;
        context.counter = 0;
        context.listbox = listbox;

   
  MastEventReceiver receiver(context);
  device->setEventReceiver(&receiver);

	

	
////////////////////////////////////////////////////////////////////////////
//////////////////////VARIABLES DEL LOOP////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

        //Variables del personaje
		float angulo = 180;
		float velocidad = .2f;
		vector3df intersection;
		line3df linia;
		line3df collisionray;
		position2di clickPosition;
		ISceneNode *focus = 0;
		bool ismoving = false;
		bool istargeted = false;

		//Variables de la camara
		float inclinacion = 150;
		float Cangulo = 180;

		//Variables del gui
		int lastFPS = -1;
		position2d<s32> etiqueta(collman->getScreenCoordinatesFrom3DPosition(enano->getPosition(),camara));
	
		

		/////Variables basura

		triangle3df tribasura;
		vector3df vectorbasura;
		




		
/*************************//////////////////////////////////////////////////////////////////////////////////******************************
/*************************///////////////////////////// Loop del juego//////////////////////////////////////******************************
/*************************//////////////////////////////////////////////////////////////////////////////////******************************




	while(device->run() && device) {
		if (device->isWindowActive()){


	receiver.endEventProcess();

	driver->beginScene(true, true, SColor(255,255,255,255));






//////////////////////////////////////////////////////////////////////////
//////////////////////EVENTOS DEL PERSONAJE///////////////////////////////
//////////////////////////////////////////////////////////////////////////

	/////////////////////Eventos de movimento del personaje/////////////////////
	
	if (receiver.keyDown(irr::KEY_KEY_S))  {move(enano,vector3df(0,0,-velocidad));ismoving=false;}	
	if (receiver.keyDown(irr::KEY_KEY_W))  {move(enano,vector3df(0,0,velocidad));ismoving=false;}	
	if (receiver.keyDown(irr::KEY_KEY_A)) {angulo -=.25f; enano->setRotation(enano->getRotation() + vector3df(0,-.25f, 0) );}
	if (receiver.keyDown(irr::KEY_KEY_D)) {angulo +=.25f; enano->setRotation(enano->getRotation() + vector3df(0,.25f,0) );}	
		
		
	////////////////////////////Clasificacion de los movimientos////////////////////////////

	if (receiver.keyPressed(irr::KEY_KEY_W)) {enano->setFrameLoop(1,14);n->setVisible(false);}
	if (receiver.keyReleased(irr::KEY_KEY_W)) {enano->setFrameLoop(206,250);n->setVisible(false);}
	if (receiver.keyPressed(irr::KEY_KEY_S)) {enano->setFrameLoop(1,14);n->setVisible(false);}

	if (receiver.keyReleased(irr::KEY_KEY_S)) {enano->setFrameLoop(206,250);n->setVisible(false);}
	if (receiver.keyPressed(irr::KEY_KEY_A)) {enano->setFrameLoop(1,14);n->setVisible(false);}
	if (receiver.keyReleased(irr::KEY_KEY_A)) {
		if(receiver.keyDown(irr::KEY_KEY_W)) enano->setFrameLoop(1,14);
		else enano->setFrameLoop(206,250);
		n->setVisible(false);}
	if (receiver.keyPressed(irr::KEY_KEY_D)) {enano->setFrameLoop(1,14);n->setVisible(false);}
	if (receiver.keyReleased(irr::KEY_KEY_D)) {
		if(receiver.keyDown(irr::KEY_KEY_W)) enano->setFrameLoop(1,14);
		else enano->setFrameLoop(206,250);
		n->setVisible(false);}

	////////////////////////////Evento "GoTo"////////////////////////////

	if(receiver.leftMousePressed()){
	
	clickPosition = device->getCursorControl()->getPosition();
	line3d<f32> line = collman->getRayFromScreenCoordinates(clickPosition,camara);
	triangle3df tri;
		
	smgr->getSceneCollisionManager()->getCollisionPoint(line,selector,intersection,tri);
    focus = collman->getSceneNodeFromRayBB(line,0,false);

		//Comprobamos si clicamos en el suelo

	if (focus == suelo){
			n->setVisible(true);
			n->setPosition(intersection);
			rotate(enano,intersection);
			enano->setFrameLoop(1,14);
			istargeted = false;
			ismoving = true;}
	else if(focus == enemigo){
		//target->setVisible(true);
		istargeted =true;}
	
	intersection.Y += 15;

	
 }











     //////////////////////////////////////////////////////////////////////////////////////////
	 //////////////// OTRAS FUNCIONES DEL PERSONAJE////////////////////////////////////////////
	 //////////////////////////////////////////////////////////////////////////////////////////

	//Loop de la funcion GoTo

	if(ismoving){
		line3df distancia(enano->getPosition(),intersection);
	
		if(distancia.getLength() > 15){
			move(enano,vector3df(0,0,velocidad));
		}
		else {
			ismoving = false;	
			enano->setFrameLoop(206,250);
			n->setVisible(false);
		}
	}

	if(istargeted){
		
	}

	
	

	/////////////////////////////////////////////////////////////////////////////////
	//////////////////////EVENTOS DE LA CAMARA///////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////

	/////////////////////Eventos de movimiento de la cámara//////////////////////

	if (receiver.keyDown(irr::KEY_RIGHT)) Cangulo -=.25f;
	if (receiver.keyDown(irr::KEY_LEFT))  Cangulo +=.25f;






	vector3df colision;
	triangle3df Tcolision;
	line3df rayo(camara->getAbsolutePosition(),enano->getAbsolutePosition());

	collman->getCollisionPoint(rayo,selector,colision,Tcolision);
	driver->draw3DTriangle(Tcolision,SColor(2552,255,0,0));

	angulo = enano->getRotation().Y + 180 ;




	//Dibujamos la camara y la hacemos orbitar mediante las funciones trigonometricas.
	camara->setPosition(enano->getPosition() + vector3df(inclinacion*sinf(Cangulo*PI/180),70,inclinacion*cosf(Cangulo*PI/180)));
    camara->setTarget(enano->getPosition()+vector3df(0,5,0));














	/////////////////////////////////////////////////////////////////////////////////
	//////////////////////Eventos del GUI////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////







	///////////////////////////////////////FIN DE LOS EVENTOS/////////////////////////////////////////////



	smgr->drawAll();
	guienv->drawAll();
	driver->endScene();

	receiver.startEventProcess();
		}
	}
 
	
	device->drop();

	return 0;


}


	
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

when I test your code I have 2 problems:

1st: I don't have your assets so I have to comment many lines (all lines for the 3d objects), but that doesn't matter for the gui though... ;)

2nd: you always return true from the event receiver, that causes Irrlicht to ignore all gui events !!!

so try to return false from the event receiver and look what happens then...
maybe this already solves your problem (I don't realy know though) ??? ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
fernando24691
Posts: 10
Joined: Thu Jan 22, 2009 8:12 pm

Post by fernando24691 »

You're GOD :oops:

When my event receiver returns false, the it always runs ok!
fernando24691
Posts: 10
Joined: Thu Jan 22, 2009 8:12 pm

Post by fernando24691 »

And there's any way to override GUI events from mouse/keyboard events??

That's because when I click a button for example, I make my character move under the button proyection in the 3d world.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Acki wrote:2nd: you always return true from the event receiver, that causes Irrlicht to ignore all gui events !!!
Maybe that should be added to the official FAQ? (the one here on the forums)
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Alternatively, we could have OnEvent() return an enumerated type that very explicitly documents the handling of the event e.g. (an extreme candidate):

Code: Select all

typedef enum EventHandledBy
{
    IrrlichtCanHandleEvent,
    EventFullyHandledInUserApp
};
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply