PocketMatrix forums

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

Dynamic Panels and State Requirements

Hi,

After my previous post I learned that I had to associate a Panel with an event handler in order to use relative coordinates. My attempts here are within the multi state game template found in the gledplay distribution.

Now I'm attempting to build panels dynamically where a Panel will have zero or more Panels, which each in turn will have zero or more Panels themselves. The quantity of both levels of Panels needs to change dynamically.

The documentation for the Panel class shows examples of creating Panels within the onNextFrame event.

Code:

HRESULT LaurasQuest::onNextFrame(Surface* pBackBuffer)
{
Panel mainPanel(pBackBuffer);
Panel inventory, playerMenu, msgConsole;
mainPanel.addChild(inventory, Utility::Rect(0, 20, 280, 240));
mainPanel.addChild(playerMenu, Utility::Rect(280, 0, 320, 240));
mainPanel.addChild(msgConsole, Utility::Rect(0, 0, 280, 60));

drawInventory(inventory);
drawPlayerMenu(playerMenu);
drawMsgConsole(msgConsole);
}


This seems to be an inefficient approach (I'm just guessing, not accusing) and contradictory to the EventHandler onNextFrame documentation which simply states that drawing should be done here

Code:

HRESULT TestApplication::onNextFrame(Surface* pBackBuffer, Panel* panel)
{
pBackBuffer->fillRect(RGB(128,128,128));
pBackBuffer->drawText(120, 100, Font::getSystemFontWithBorder(),
Font::CENTER_ALIGN, TEXT("Hello World!"));

return ReturnCode::OK;
}





Also, within this method there is no access to the current state for the application, and therefore I don't seem to have the ability to assign an event handler to any newly created Panel.

Can someone please provide some clarification on the differences of the documented approaches for onNextFrame (is it ok to be constructing objects and so forth), and also, how to go about creating Panels with associated event handlers at runtime?

Thanks.
Hi billston,

I'm afraid to say that the example of the Panel documentation is out of date. Prior to the addition of the Multi state game solution, the onNextFrame method didn't have a panel parameter where to write.
It was not very inefficient to create the panel structure right in there, but it was not certainly the best either. So we changed the onNextFrame method (something we didn't want to do, but have to) and we added the panel as parameter. Thank you for letting us know of this "out of date" example.

About dynamically changing the panels, in the next release we are adding some functionality to do so. You'll be able to resize and move panels on runtime.

About adding event handlers to a panel dynamically: The event handler doesn't keep a copy of its state because we didn't want to add an (in most cases) unnecessary dependency to that class. But as you are creating your own event handlers, extending from the EventHandler class, you can add a parameter to its constructor, or add a method to set and save an instance of a state. Then, you can use that instance to add event handlers in runtime.

Sorry for the long post, here you have a summary:

1- The panel example is out of date. We'll fix it right away
2- In next release (next week) you'll have methods for moving and resizing panels dynamically
3- You can save the state instance in your eventhandler, and use it to add event handlers in runtime.

Best regards,
Sergio.
Thanks Sergio! I'll take the approach for adding a state instance to the event handler and I'll look forward to your next release with added Panel functionality. :)

billston
Hi Sergio,

I'm just waiting now for the next release and was doing a bit of pondering...

"The event handler doesn't keep a copy of its state because we didn't want to add an (in most cases) unnecessary dependency to that class."

Don't you always need a state to use an EventHandler? It seems from my limited experience that I MUST have a state if I wish to use an EventHandler. So it seems completely dependent all of the time.

Are there cases where EventHandlers can be used without a state? I'm curious because I'm new to much of this and want to make sure that I have sane intentions for Panels. i.e. What is abnormal about what I'm trying to do?

Thanks.
Hi billston,

I understand that what I said was a little confusing, I'll try to explain it:

Don't you always need a state to use an EventHandler?


No, not really. You need EventHandler to use State, but not the other way around. State "depends" on the EventHandler class, but the EventHandler doesn't depend on State.
An EventHandler is an object that represents the actions that will be performed when an event occurs, and it is not strictly linked to the "State" concept.
For example, the Application class extends EventHandler, and it doesn't use states.
You could also create you own "game loop" class, using EventHandler, with no State class at all. In fact, with next release, we are providing 2 new classes: Display and Device, so people can create more easily their own "Application" or "StateMachine / State" classes.
Another use could be extending the EventHandler class with your game entities. For example, in a shoot'em up game, your "PlayerShip" can extend the EventHandler class.
And even if you are using State and StateMachine, the same EventHandler class may be added to several states. For example a menu bar on the top of the screen, for doing things such as pausing/resuming, open save/load menu, go to main menu, open help dialog, all of them different game states, but the menu bar is present in all of them.

I hope this is clearer, if you have any doubt, please let me know and I'll try to explain it further.

Thank you very much,
Sergio.
Thanks again Sergio. I'm still a bit confused.

Ok, so if I'm not to use a state, and I wanted to use EventHandlers for my game entities, how would I register the EventHandlers with the application so that they receive events? Maybe I don't need to register them at all? Do event handlers just automatically receive events?

I really like the idea of extending the EventHandler for each game entity, but I also wish each entity to be a Panel, or at least have relative coords for drawing and point detection. But it is not possible to assign the EventHandler a Panel without using a state? Correct?

The way I'd see it is to have a Panel that is an EventHandler. Then the event handler methods would be a part of, for example, playerShip. OnNextFrame could then just have the Panel's parent surface as a class member to draw on and all coordinates would automatically be relative to the panel. And whether a developer chose to override the event handler methods or not would be up to them.

I don't assume to really know what I'm talking about, so this isn't necessarily a suggestion for how it should be. Instead I'm just trying to communicate how I'm looking at the scenario and what I'm expecting to see.

Thanks.
Hi billston,

Let me clarify this a bit.

Ok, so if I'm not to use a state, and I wanted to use EventHandlers for my game entities


You can extend your game entities from EventHandler in both scanerios: Using state, and not using it. And that's because, as I told you before, the EventHandler class does not depend on the State class.

Do event handlers just automatically receive events?


No, they don't. Extending the EventHandler class doesn't really add any behavior to your class, until you actually implement its methods. In fact, if the coding language would had been C# or Java, instead of C++, EventHandler would be an Interface, not a class. But that doesn't mean it isn't good to use it anyway. A nice example is when you use the Strategy Design Pattern. For example: imagine that you have several different entities (tanks, ships, troopers), every kind of unit will be implemented with its own class. If all of this classes inherit from the EventHandler class, you can save them all in a EventHandler pointers array. And then, you'll be able to do something like:

Code:
HRESULT TestApplication::onNextFrame(Surface* pBackBuffer, Panel* panel)
{
for(int i=0; i<units_quantity; i++)
unitsArray->onNextFrame(pBackBuffer, panel);
return ReturnCode::OK;
}


Even if each of them (tanks, ships, and troopers) are drawn in a very different way (each of them will have its own implementation of onNextFrame) they are managed as the same thing. So you can use heterogeneous things, as if they where the same.

I really like the idea of extending the EventHandler for each game entity, but I also wish each entity to be a Panel


I'm sorry, but I don't think that's going to get you good results. A player ship IS an EventHandler because it is going to take actions when an event arise. But I don't see how a player ship IS a Panel. I'd rather say that the player ship USE a Panel, to draw itself in it. But the same panel is going to be used for drawing enemy ships, explosions, bullets, etc.
I advice you to try doing something like this:

Code:
class PlayerShip : public EventHandler, public Animation {

PlayerShip():Animation(TEXT("playership.png")) {}
...

}


So PlayerShip IS an EventHandler and an Animation, and it's going to use a Panel (in its onNextFrame) to draw itself. The same panel will be used to draw bullets, enemy ships, etc.

You can use this approach adding this eventhandlers in a State, or without using the multi state game solution and calling the event handler methods manually. In both cases, code will be tidier.

I hope this could clarify things a little bit, please let me know if you have any doubts.

Best regards,
Sergio.
Ok I'm sorry if I am missing things here.

"You can extend your game entities from EventHandler in both scanerios: Using state, and not using it. And that's because, as I told you before, the EventHandler class does not depend on the State class. "

Ok I understand that what you said earlier. What I need are relative coordinates. And I'm confused as to how relative coordinates can be used in "stylusDown" or any other implemented EventHandler method without the given context of a Panel to be relative to. The only method I see for handling this functionality is of the State class. Is is this that I need to have cleared up. :?

"In fact, if the coding language would had been C# or Java, instead of C++, EventHandler would be an Interface, not a class...."

Yes, I understand and appreciate this approach. :)

"I'm sorry, but I don't think that's going to get you good results.... "

I guess some background is necessary. I originally developed my program on the desktop using a combination of windows forms and GDI functions in c#. In that realm, a "Control" (iirc) is the most generic ui object. A Control knows how to paint itself and as well handles any events that concern it. So that's what I'm trying to describe here when desiring an object that can draw itself and also handle events. So whether it IS a Panel, or internally uses a Panel, it doesn't matter much to me.

My actual game entities are already written and all they do is model what they logically represent. What I would like to have is a UI object that wraps the model object. So if I have a Tank model, then I would have a corresponding UITank object to handle my GledPlay concerns. I would like to see that object be able to draw itself, move itself, and handle it's own input concerns.

I'm not stuck on this approach but I have already implemented a class that inherits both from Panel and EventHandler to serve as my wrapper. The only problem I see with it is drawText which doesn't seem to work right. It will only draw in my first Panel, and the placement of the Panel (the further it is away from 0,0) seems to distort the coordinates of the text (which may be the reason why it only shows up in my first panel). I have small rects drawn in each corner of my panels and they seem to hold there relative placement properly, so it makes me wonder if there is something specific about drawText.

I also tried creating an object that inherits from EventHandler and uses a Panel as a member, but I could not get it to work. (likely a mistake on my part I guess).

Anyway, thanks again for all your help.

billston
Hi billston,

First of all, and I want to say I'm sorry if I said something that seemed simple, or even obvious to you. My intention was not offending you, it's just that I didn't know your skills level and I was trying to help.
I think I understand your approach now, and let me say it looks very, very interesting.
About drawText method on panel, we'll check it out right now, to see if we have a bug there.

I'll get back to you when we release the new Panel features (and correct the drawText method, if necessary)

Again, thank you for your feedback,
Sergio.
Hi Sergio. I absolutely was not offended by anything you said :) It is hard to know a person's development experience and it's different for everyone.

for the record:
My expertise is in J2EE as that has been my day job for the past 5 years. I have ventured into other areas surrounding java from opengl to j2me to a simple reflective persistence framework. I've worked with c# using managed directx and for the origins of this program I'm working on now. I've also dabbled in the odd web scripting here and there. However, I'm very new to c++. I never learned it in school, and up until recently, never had a need to (although I always wanted to). So I'm very eager to learn and I expect there to be different ways of doing things given the flexibility of c++.

Anyway, whatever the case is, I will never claim to know everything. We all have the opportunity to learn from others and it's difficult to communicate most effectively through forum posts. So please know that I do appreciate your efforts to ensure that we understand each other and that I always enjoy discussing strategies. :)

billston
Hi billston,

I'm writting just to let you know that we've released a new version of GledPlay, adding the functionality for changing Panel's size and position.
Also, the bug on drawText method, in Panel class was fixed.

Thank you for your feedback!

Sergio.