GUI help

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
Tek
Posts: 7
Joined: Thu Sep 15, 2011 9:06 am

GUI help

Post by Tek »

I was following tutorial 5, making the gui, and i wanted to add a cancel button to the new windows you can open. But after 4+ hours of browsing the forums and google, i still cant find anything :x . if anyone could give me some help/code?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: GUI help

Post by CuteAlien »

Basically in a similar way in which the static-text is added. Search for the env->addStaticText with the "please close me" text. Notice that it has "window" passed as parent parameter - making the statictext a child of the window created there. Instead of static-text you need addButton for a button.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Tek
Posts: 7
Joined: Thu Sep 15, 2011 9:06 am

Re: GUI help

Post by Tek »

I tried what you suggested, but there was an error, it says there are too many arguments in the function call.
Tek
Posts: 7
Joined: Thu Sep 15, 2011 9:06 am

Re: GUI help

Post by Tek »

Code: Select all

#include <irrlicht.h>
#include "driverChoice.h"
 
using namespace irr;
 
using namespace core;
using namespace video;
using namespace gui;
using namespace io;
using namespace scene;
 
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
 
//declare a sturcture to hold some context for the event reciever so that it
//has it available inside its OnEvent() method.
struct SAppContext
{
        IrrlichtDevice *device;
        s32                       counter;
        IGUIListBox* listbox;
};
 
//Define some values that will be used to identify indivual GUI controls
enum
{
        GUI_ID_QUIT_BUTTON = 101,
        GUI_ID_NEW_WINDOW_BUTTON,
        GUI_ID_FILE_OPEN_BUTTON,
        GUI_ID_TRANSPARENCY_SCROLL_BAR,
        GUI_ID_CLOSE_BUTTON
};
 
/* The Event Receiver is not only capable of getting keyboard and mouse input events, 
but also events of the graphical user interface (gui). 
There are events for almost everything: Button click, Listbox selection change, 
events that say that a element was hovered and so on. 
To be able to react to some of these events, we create an event receiver. 
We only react to gui events, and if it's such an event, 
we get the id of the caller (the gui element which caused the event) and get the pointer to the gui environment. 
*/
//creates a class called "MyEventReceiver"
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();
             IGUIEnvironment* env = 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=10; i<EGDC_COUNT ; ++i)
                     {
                      SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
                      col.setAlpha(pos);
                      env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
                                         }
                                        
                 }
                 break;
 
                                 //If a button was clicked, it could be one of 'our' three buttons. 
                                 //If it is the first, we shut down the engine. 
                                 //If it is the second, we create a little window with some text on it. 
                                 //We also add a string to the list box to log what happened. 
                                 //And if it is the third button, we create a file open dialog, 
                                 //and add also this as string to the list box. That's all for the event receiver. 
                                 case EGET_BUTTON_CLICKED:
                                switch(id)
                                {
                                case GUI_ID_QUIT_BUTTON:
                                                                        //closes the window
                                        Context.device->closeDevice();
                                        return true;
 
                                case GUI_ID_NEW_WINDOW_BUTTON:
                                        {
                                                                                //creates a window and puts "Window creatd" in the log
                                        Context.listbox->addItem(L"Window created");
                                        Context.counter += 30;
                                        if (Context.counter > 200)
                                                Context.counter = 0;
                                                                                 
                                        IGUIWindow* window = env->addWindow(rect<s32>(100 + Context.counter,
                                                                                                100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
                                                false, // modal?
                                                L"Test window");
 
                                                                                env->addButton(rect<s32>(35,35,140,50), 0, GUI_ID_CLOSE_BUTTON,
                                                                                        L"Close", L"");
 
                                                                                   env->addButton(rect<s32>(35,35,140,50),  0, GUI_ID_CLOSE_BUTTON,
                                                                                        L"Close"), window;
                                                                                   if (GUI_ID_CLOSE_BUTTON)
                                                                                   {
                                                                                           Context.device->closeDevice(), window;
                                                                                   }
                                        }
 
                                        return true;
 
                                case GUI_ID_FILE_OPEN_BUTTON:
                                        Context.listbox->addItem(L"File open");
                                        env->addFileOpenDialog(L"Please choose a file.");
                                        return true;
 
                                default:
                                        return false;
                                }
                                break;
 
                        default:
                                break;
                        }
                }
 
                return false;
        }
 
private:
        SAppContext & Context;
};
 
int main()
{
        //ask user for driver
        video::E_DRIVER_TYPE driverType=driverChoiceConsole();
        if (driverType==video::EDT_COUNT)
                return 1;
        
        //create device, and exit if creation fialed
        IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
 
        if (device == 0)
                return 1; // could not create selected driver
 
        //The creation was successful, now we set the event receiver and store pointers to the driver 
        //and to the gui environment.
        device->setWindowCaption(L"Irrlicht Engine - User interface Demo");
        device->setResizable(true);
 
        video::IVideoDriver* driver = device->getVideoDriver();
        IGUIEnvironment* env = device->getGUIEnvironment();
 
        //Adds 3 buttons. First closes the engine, second creates a window, and third opens a file open dialog.
        //The third parameter is the id of the button, with which can easily be used to identify 
        //the button in the event receiver. 
        env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
                                                L"Quit", L"Exits Program");
        env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
                        L"New Window", L"Launches a new Window");
    env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
                        L"File Open", L"Opens a file");
 
        //this adds some static text and a scrollbar which modifies the transparency of all gui elements. 
        //set the maximum value of the scrollbar to 255, 
        //because that's the maximal value for a color value. 
        //Then create an other static text and a list box. 
        env->addStaticText(L"Transpaent Control:", rect<s32>(150,20,350,40), true);
        IGUIScrollBar* scrollbar = env->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 arbitary element (i dont really know what this means
        //is what the tutorial said) :)
        scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());
 
    env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
    IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
    env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
 
    // Store the appropriate data in a context structure.
        SAppContext context;
    context.device = device;
    context.counter = 0;
    context.listbox = listbox;
 
    // 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);
        
        //this draws everything in
         while(device->run() && driver)
        if (device->isWindowActive())
        {
                driver->beginScene(true, true, SColor(0,200,200,200));
 
                env->drawAll();
        
                driver->endScene();
        }
 
        device->drop();
 
        return 0;
}
 
This is the code i am using, no errors, but it just closes the program straight away when i open a new window.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: GUI help

Post by CuteAlien »

That line makes no sense:

Code: Select all

 
 env->addButton(rect<s32>(35,35,140,50),  0, GUI_ID_CLOSE_BUTTON, L"Close"), window;
 
Adding a ",window" behind the function call - that is just not how you pass parameters in c++. Function parameters are _always_ passed within the brackets in a fixed order. If you check the documentation, for buttons for example: http://irrlicht.sourceforge.net/docu/cl ... 42199e54a0
you can see the order in which they have to be passed.

Next line ain't better:

Code: Select all

 
if (GUI_ID_CLOSE_BUTTON)
 
This 'if' command would _always_ be true as GUI_ID_CLOSE_BUTTON is a non-zero value. What you most likely would have wanted is using it as case-label.

But all in all - you are not programming but guessing. This will not work work - you have to understand code before you can use it. Start with a c++ book and simple examples before you go into 3d programming.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Tek
Posts: 7
Joined: Thu Sep 15, 2011 9:06 am

Re: GUI help

Post by Tek »

it's how i learn, i cant get tutorials that work for my compiler (well not basic ones other then all the way up to loops which i have done many times), so i find a working one, read through it, then experiment with the code to see what happens when i do something.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: GUI help

Post by CuteAlien »

You should at least learn the syntax of c++ first. You can't learn that by try&error!
Buy a book like "Programming: Principles and Practice Using C++" from Stroustrup so you can learn the basics first. There are others books, but this one is from the creator of the c++ language and written for beginners, so probably not the worst.

There's also a bunch of free books online (http://www.computer-books.us/cpp.php), some people recommend those from Bruce Eckel for learning c++. And I can only urge you not to avoid books at this stage - otherwise you are going to spend years falling into beginner traps.

If your compiler is the problem: With gcc there is a completely free and very good compiler available on nearly every system. If you are on Windows then there is also VisualStudio Express which does not cost anything and comes with a nice IDE.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Tek
Posts: 7
Joined: Thu Sep 15, 2011 9:06 am

Re: GUI help

Post by Tek »

ok, thank you, i will try that.
Post Reply