Menu

example_02

Benedict Jäggi
#include "engine.hpp"

// define some button ids
#define BTN_1 101
#define BTN_2 102
#define MENUBTN_NEW 103
#define MENUBTN_SAVE 104

// derive a class from qEventReceiver to receive single clicks on GUI buttons and other stuff.
class eventreceiver : public qEventReceiver
{
// you may need the engine
qEngine *engine;
public:
    // get the engine in constructor.
    eventreceiver() {engine=qEngine::getInstance();}

    // OnGUIButtonClick gets called when the mouse gets pressed on a GUI button.
    // it gets the button ID as parameter.
    virtual void OnGUIButtonClick(int id)
    {
            // do some stuff with the buttons
            switch(id)
            {
                case BTN_1:
                    printf("GUI BUTTON ONE CLICKED!\n"); break;
                case BTN_2:
                    printf("GUI BUTTON TWO CLICKED!\n"); break;

                  // note that menu items will also be catched here.
                  case MENUBTN_NEW:
                          engine->clearScene(); break;
                   case MENUBTN_SAVE:
                          engine->saveScene("MyScene.bsc"); break;
            }
    }

    // get button down and up ONCE
    virtual void OnMouseUp(qEMouseButton which, int x, int y)
    {
        printf("Mouse Button %d was left up at %d, %d",which, x, y);

        // get the entity under the mouse (and select it or something)
        // there are no entities in this example but this is how you get the one under the mouse.
        // get it on mouseup, not mousedown, so you can be really sure that you want this one.
        if(which==qEMouseButton::LEFT_BUTTON)
        {
            qEntity *ent=e->getMouseOverEntity();
            if(ent)
            {
                printf("Entity %s clicked.\n", ent->getName().c_str());
            }
        }
    }

    virtual void OnMouseDown(qEMouseButton which, int x, int y)
    {
        printf("Mouse Button %d was pressed down at %d, %d", which, x, y);
    }
};

int main()
{
  // get the engine singleton
  qEngine *e;
    e = qEngine::getInstance();

    // create the window.
    if(!e->createWindow("BeOS", screenWidth, screenHeight))
    {
        printf("Could not create the main Window. Aborting.\n");
        return 1;
    }

    // create an event receiver and pass it to the engine.
    eventreceiver gcr;
    e->setEventReceiver(&gcr);

    // add some static text.
    e->addGUIStaticText("BeOS v0.1",qRecti(0,0,200,44));

    // create a window
       qGUIWindow *w= e->addGUIWindow(qRecti(100,100,120,120), "This is a window.");

    // create button 1 and 2
    // the parameters are: id to give,
    // a rect with x, y, width and height values
    // the button text and a button hint.
    // and lastly the parent GUI element.
    qGUIButton* button=e->addGUIButton(BTN_1,qRecti(10,10,100,50), "Button One", "This is button one.");

    // add the second button to the window.
    e->addGUIButton(BTN_2, qRecti(10,70,100,50), "Button Two","This is button two", w);

    // create a menu.
    qGUIMenu *menu = e->addMenu();
    irr::s32 itemid=menu->addItem(L"File");
    menu->getSubMenu(itemid)->addItem(L"New", MENUBTN_NEW);
    menu->getSubMenu(itemid)->addItem(L"Save", MENUBTN_SAVE);
    // etc..

    while(e->isRunning()) 
    {
       // update the engine
        e->update();

        // get a key down.
        if(e->getKeyboardKey(qKey ::KEY_SPACE))
            printf("SPACE DOWN\n");

            // you can get a buttons down status also with isPressed.
            // but the gui click receiver gets them once and this gets it every frame.
            if(button->isPressed())
            {
                printf("GUI BUTTON %s [%d] PRESSED\n",button->getName(),button->getID());
            }

        // get mouse position and mouse buttons continuously
        printf("Mouse: %d %d %d %d %d\n", 
            e->getMouseX(), 
            e->getMouseY(),
            e->getMouseDown(qEMouseButton::LEFT_BUTTON),
            e->getMouseDown(qEMouseButton::MIDDLE_BUTTON),
            e->getMouseDown(qEMouseButton::RIGHT_BUTTON)
            );    
    }

    e->quit();
    return 0;
}

/*
That's it. Compile and run.
**/

Related

Wiki: Home
Wiki: qEngine