I read the gui interface tutorial. Again one of those tutorials that try and do everything, but don't explain much.
I got two buttons to show on screen. I have to admit they are not very pretty buttons, but they show, and they don't do a lot.
Ok once I got the buttons on the screen I wanted them to do something. Now MyEventreciever class is already in a separate file, and so I went about adding my button code to that class, and yey behold my buttons appear on the screen. The question is how do I get them to work. I think I am missing the context, thing because if I comment out the context line the game runs, but the buttons don't actually do anything, but if I put it in it complains about scope. Now I know why it is complaining. It is because I haven't set up the context thing, and the thing is I don't know how to.
Now I can pretty much grasp C++ but I am having trouble getting engine stuff to work.
For instance I can never work out when to use irr::core irr::scene irr:video.
I can sometimes guess the right one based on it's function, but very often have to go through all of them until it works heh heh.
OK code follows.
First my header file
Code: Select all
#pragma once
#ifndef MYEVENTRECEIVER_H
#define MYEVENTRECEIVER_H
#include "irrlicht.h"
struct SAppContext
{
irr::IrrlichtDevice *device;
irr::s32 counter;
};
enum
{
GUI_ID_HELLO_BUTTON = 101,
GUI_ID_ANOTHER_BUTTON = 102
};
class MyEventReceiver : public irr::IEventReceiver
{
public:
MyEventReceiver();
// MyEventReceiver(SAppContext & context) : Context(context) { }
virtual bool OnEvent(const irr::SEvent& event);
virtual bool IsKeyDown(irr::EKEY_CODE keyCode) const;
void Buttons(irr::IrrlichtDevice *device);
private:
bool KeyIsDown[irr::KEY_KEY_CODES_COUNT];
irr::gui::IGUIEnvironment* env;
};
#endif // MYEVENTRECEIVER_H
Code: Select all
#include "MyEventReceiver.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
MyEventReceiver::MyEventReceiver()
{
for (irr::u32 i=0; i<irr::KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
bool MyEventReceiver::OnEvent(const irr::SEvent& event)
{
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
// env = Context.device->getGUIEnvironment();
switch(event.GUIEvent.EventType)
{
case GUI_ID_HELLO_BUTTON:
printf("Hey Hello There");
return true;
case GUI_ID_ANOTHER_BUTTON:
printf("Hey Hello There");
return true;
default:
return false;
}
}
return false;
}
// This is used to check whether a key is being held down
bool MyEventReceiver::IsKeyDown(irr::EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
void MyEventReceiver::Buttons(IrrlichtDevice *device)
{
env = device->getGUIEnvironment();
float x=10,y=560;
env->addButton(rect<s32>(x,y,x+100,y+30), 0, GUI_ID_HELLO_BUTTON,L"Hello", L"There");
x=130;
env->addButton(rect<s32>(x,y,x+100,y+30), 0, GUI_ID_ANOTHER_BUTTON,L"Another Button", L"There");
}