PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

isPointInRect - Relative coords?

Hi,
I was expecting (and very much hoping) that the Rect class would take into account the Panel hierarchy when using isPointInRect. However, it seems that even though the Rect will be drawn relative to it's Panel, the point checking considers the Rect's coordinates to be absolute.

Is my understanding/attempt correct? And if so, is this desirable behavior? Maybe my approach is way off? I'm also fairly new to c/c++ so please forgive any odd coding attempt.

Thanks.

Code:

//onInit()
basePanel = Panel(pBackBuffer);
basePanel.addChild(&menuPanel;, Utility::Rect(50, 500, 400, 600));
projButton = Utility::Rect(5,5,50,50);
...
//onNextFrame()
//draws rect at bottom left of screen (relative coords)
application->menuPanel.drawRect(RGB(100,100,100), &application-;>projButton);
...
//onStylusDown()
//"if" only true when I tap upper left of screen (absolute coords)
if (application->projButton.isPointInRect(p)) {
return 1;
}
return 0;
...
Hi billston,

Let me see if I get you right (if not, please let me know, and we'll try again).
The "isPointInRect" doesn't take into account the Panel hierarchy, but this is a desirable behavior, and I'll tell you why:
I see that you are using the Multi state game solution. When you are adding the event handlers, you can assign a panel to it, and the point that you'll get as parameter in onStylusDown, will be relative to that panel. in that way, when you use the "isPointInRect" method, it is going to be using coords relative to the panel, instead if absolute coords.

For example, if you do:

Code:
state->addEventHandler(menuEventHandler, menuPanel);


the event handling methods of menuEventHandler will be called using coords relative to the menuPanel. Also, the onNextFrame method will be passing the menuPanel as parameter, so you can write something like:

Code:
HRESULT MenuEventHandler::onNextFrame(Surface* pBackBuffer, Panel* panel)
{
//panel is the menuPanel
panel->drawRect(RGB(100,100,100), &application-;>projButton);
return ReturnCode::OK;
}

HRESULT MenuEventHandler::onStylusDown(POINT p)
{
//p is relative to menuPanel
if (application->projButton.isPointInRect(p)) {
return 1;
}
return 0;
}


Also, remember that you can add as many event handlers as you want, so you can separate the behavior related to the menu and drawing into its panel, from the rest of the game, in a different event handler object.

I hope this explanation was useful, if not, just let me know.

Best regards,
Sergio.
Hi Sergio,

That explains it very well! I originally was looking at the event handlers as being much more coarse grained, figuring there to be a 1-1 relationship between a "screen" and a state. I've now had a look at the documentation regarding the State class and my understanding of it has been consistent with your explanation here. It almost seems obvious now! ;)

Out of curiosity (and maybe this is partly how I became confused), it doesn't seem possible to do this if I were to use the Application class. Am I correct on that understanding? I'm fine using the StateMachine, but it seems the functionality of adding an event handler to an object should be available regardless of implications of state management.

Anyway, thank you very much for your help and quick reply. I'm very happy with GledPlay so far! :)
I'm fine using the StateMachine, but it seems the functionality of adding an event handler to an object should be available regardless of implications of state management.


That's an interesting idea, we'll discuss this in the next development road map definition meeting.

I'm very happy with GledPlay so far! :)


And that makes us happy. If you have any doubts or suggestions, please let us know. We appreciate every piece of feedback from developers.

Best regards,
Sergio.