About Store Forum Documentation Contact
Donations:
207$/mo



Post Reply 
Need help understanding GUI event functions
Author Message
EvilNoodle Offline
Member

Post: #1
Need help understanding GUI event functions
Hi,

I downloaded this engine yesterday and having looked through the tutorial source I am very impressed but there are one or two things I don't understand.

In the GameMenu GUI example there is code like...

Code:
struct MenuLoad // load-game menu
{
   Window window;
   Text   text;
   Button close;
  
   static void Close(Ptr){Ml.window.fadeOut();}

   void create()
   {
      Gui   +=window.create(Rect_C(0,0,0.9,0.5)).barVisible(false).hide();
      window+=text  .create(Vec2  (window.crect.w()/2,-0.15),"Load Game");
      window+=close .create(Rect_C(window.crect.w()/2,-window.crect.h()/2-0.1,0.4,0.08),"Close").func(Close);
   }
}Ml;

My main question is what does "Ptr" correspond to in the Close function arguments? When I run this with the debugger it shows in the watch list as "Ptr CXX0030: Error: expression cannot be evaluated".

The logical guess would be a reference to the control that was activated.

My second question is suppose I wanted to define a class that implements a GUI window for a console like...

Code:
class Console
{
private:
    Window mWnd;
    Button mBtnClose;
    //Other components...

    void Close();

public:
    void create();
};

and implemented like

Code:
void Console::create()
{
    Gui += mWnd.create(Rect_C(0,0,0.9,0.5)).barVisible(false).hide();
    mWnd += mBtnClose.create(Rect_C(mWnd.crect.w()/2,-mWnd.crect.h()/2-0.1,0.4,0.08),"Close").func(Close);
    //Init other controls and add...
}

void Console::Close()
{
    this->mWnd.fadeOut();
}

So I could have two instances like...

Code:
//...
Console mainConsole;
mainConsole.create();

Console altConsole;
altConsole.create();
//...

Can I do this? and if so what would I need to change about the code above to facilitate it?

Any help on this greatly appreciated.

EvilNoodle
OK I have looked at this a bit more and it seems that user data for the callback is passed through using the "func(void(*func)(TYPE *), TYPE *user)" function assuming that TYPE can be an arbitrary struct. Am I right on this and if so does anybody have an example?

If this is correct then the missing link in what I want to do is what is required to use a non-static function for the button callback.

Any ideas?
(This post was last modified: 02-11-2010 11:29 PM by EvilNoodle.)
02-11-2010 11:04 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Need help understanding GUI event functions
yes, Ptr in this case is a custom pointer to custom user data (set by 'user')
02-12-2010 12:08 AM
Find all posts by this user Quote this message in a reply
EvilNoodle Offline
Member

Post: #3
RE: Need help understanding GUI event functions
Thanks for that.

How is this data passed and what, if any, format does it need to be in?

Do you have an example of using this with custom data?

Cheers

EvilNoodle
02-12-2010 12:28 AM
Find all posts by this user Quote this message in a reply
Masterxilo Offline
Member

Post: #4
RE: Need help understanding GUI event functions
Code:
#include "stdafx.h"

Button button_a;

void InitPre() {
    App.name("Buttons"); App.flag=APP_NO_FX;
    Paks.add("../data/engine.pak"); D.mode(800,600);
}

void Func(Ptr userdata) {
    Int* pValue = static_cast<Int*>(userdata);
    *pValue += 1;
}

Bool Init() {
    Text_ds.color =BLACK;
    Text_ds.shadow=0;

    Gui+=button_a.create(Rect_C( 0.0,0, 0.45,0.08),"Button Func").func(Func);
    button_a.user = new Int; *static_cast<Int*>(button_a.user) = 0;

    return true;
}


void Shut() {
    delete button_a.user;
}

Bool Update() {
    if(Kb.bp(KB_ESC))return false;
    Gui.update();
    return true;
}

void Draw() {
    D  .clear(WHITE);
    Gui.draw ();

    Int* pValue = static_cast<Int*>(button_a.user);
    D.text(0,0.6,S+"value = "     + *pValue);
}

System: Windows 7 Ultimate 64 bit, Q6600 2.4 GHZ, 4GB RAM, nVidia GeForce 260 GTX 896 MB DDR3

Visit my site: hurricane-eye.webs.com
And read my blog: hurricane-eyeent.blogspot.com
02-12-2010 12:36 AM
Visit this user's website Find all posts by this user Quote this message in a reply
EvilNoodle Offline
Member

Post: #5
RE: Need help understanding GUI event functions
Thank you for that, that is absolutley brilliant!!

Will it work with anything that can be static cast back to the right type or does it need to be a primitive?

EvilNoodle
02-12-2010 01:32 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: Need help understanding GUI event functions
actually please don't allocate memory and set the pointer to 'user' as it may cause errors in a rare situtation when copying 1 object to another

it should be done other way:

Code:
struct Struct
{
...
}Object;

static void Func(Struct &obj)
{
}

button.func(Func,Object); // 'user' must point to constant memory address
02-12-2010 02:31 AM
Find all posts by this user Quote this message in a reply
Masterxilo Offline
Member

Post: #7
RE: Need help understanding GUI event functions
Quote:Will it work with anything that can be static cast back to the right type or does it need to be a primitive?
With anything, since you can get a pointer to anything and convert pointers back to pointers to anything.

@Esenthel
Ah ok, so you suggest to use it only to point to memory that will be freed by itself.

I didn't know that you can cast:
void (*func)(TYPE&)
into
void (*func)(Ptr )

Never seen that before. Are pointers and references in c++ about the same? Is there any reason to "still" use pointers instead of references?

System: Windows 7 Ultimate 64 bit, Q6600 2.4 GHZ, 4GB RAM, nVidia GeForce 260 GTX 896 MB DDR3

Visit my site: hurricane-eye.webs.com
And read my blog: hurricane-eyeent.blogspot.com
02-12-2010 02:58 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply