2016-11-10 26 views
0

私は、ボタン上でマウス入力を行うための例を書いた。ユーザーが画面をクリックすると、マウスの位置xとマウスの位置Yがわかります。しかし、XとYには常にジャンク値があります。これは私のコードです:irrlichtでマウスの位置を取得するにはどうすればよいですか?

#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; 


// Declare a structure to hold some context for the event receiver so that it 
// has it available inside its OnEvent() method. 
struct SAppContext 
{ 
    IrrlichtDevice *device; 
}; 

// Define some values that we'll use to identify individual GUI controls. 
enum 
{ 
    GUI_ID_BUTTON = 101, 
}; 


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

     virtual bool OnEvent(const SEvent& event) 
     { 
      if (event.EventType == EET_GUI_EVENT) 
      { 
       s32 id = event.GUIEvent.Caller->getID(); 

       switch(event.GUIEvent.EventType) 
       { 
        case EGET_BUTTON_CLICKED: 
         switch(id) 
         { 
          case GUI_ID_BUTTON: 
           cout << "X: "<< event.MouseInput.X << endl; 
           cout << "Y: "<< event.MouseInput.Y << endl << endl; 
           return true; 

          default: 
           return false; 
         } 
         break; 

        default: 
         break; 
       } 
      } 
      return false; 
     } 

    private: 
     SAppContext & Context; 
}; 


int main(int argc, char** argv) 
{ 


    // create device and exit if creation failed 

    IrrlichtDevice * device = createDevice(video::EDT_SOFTWARE, core::dimension2d<u32>(1280, 720)); 

    if (device == 0) 
     return 1; // could not create selected driver. 

    device->setWindowCaption(L"Irrlicht Engine - User Interface Demo"); 
    device->setResizable(true); 

    video::IVideoDriver* driver = device->getVideoDriver(); 
    IGUIEnvironment* env = device->getGUIEnvironment(); 

    // add button 
    env->addButton(rect<s32>(0,0,640,360), 0, GUI_ID_BUTTON, 
      L"Button", L"Button"); 

    // Store the appropriate data in a context structure. 
    SAppContext context; 
    context.device = device; 

    // Then create the event receiver, giving it that context structure. 
    MyEventReceiver receiver(context); 

    // And tell the device to use our custom event receiver. 
    device->setEventReceiver(&receiver); 

    /* 
    That's all, we only have to draw everything. 
    */ 

    while(device->run() && driver) 
    if (device->isWindowActive()) 
    { 
     driver->beginScene(true, true, SColor(0,200,200,200)); 

     env->drawAll(); 

     driver->endScene(); 
    } 

    device->drop(); 

    return 0; 
} 

そして、結果画像:

https://i.stack.imgur.com/dQEMs.png

どのように私はこの問題を解決することができますか?マウスの入力位置を取る別の方法はありますか?

答えて

2

Irrlichtのマニュアルには、マウスとジョイスティックを入力に使用する方法に関するチュートリアルがあります。 Read here

EMIE_MOUSE_MOVEDのイベントを処理し、マウスの現在の位置をどこかに格納する必要があります。

if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) 
{ 
    switch(event.MouseInput.Event) 
    { 
     ... 
     case EMIE_MOUSE_MOVED: 
      MouseState.Position.X = event.MouseInput.X; 
      MouseState.Position.Y = event.MouseInput.Y; 
      break; 
     ... 
    } 
} 
+0

ありがとうございました。 –

関連する問題