Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C# - xna event listeners

Author
Message
flashing snall
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Boston
Posted: 14th Jul 2010 21:59
Hey. I have a simple question, but Im totally lost on it. I have two classes with one firing events, and I want the other one to be able to pick them up. I cant figure out how to span the classes though. I have no source, because I have no clue (even after lots of google)

Any advice?

RalphY
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: 404 (UK)
Posted: 15th Jul 2010 00:01
Simple example:



Is this what your asking?

Go banana! | Super Nintendo Chalmers! | When I grow up I'm going to Bovine University!
flashing snall
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Boston
Posted: 15th Jul 2010 00:34
I think so? So lets say Im using just two classes, the xna framework and the Windows.Form class. From another site http://royalexander.wordpress.com/2008/10/09/xna-30-and-winforms-the-easy-way/
you can use winForms with xna just by drawing to a picture frame within the winForms instead of the default window that xna makes.

The Form class works entirely off of events it seems, so what Im looking to do is to be able to listen for the events taking place in the Form class in the the Game class.

I couldnt figure it out, so I kinda cheated and just put the Windows Form in the constructor of the Game and then got access to all the Windows Forms fields by making them public and such. But I feel like thats not a smart path to go down

I attached my project folder to the post. Hopefully it will make more sense when the source can be seen.

Thanks for the help so far! That looks like it should make sense, but Im not sure how to apply it to the situation Im in.

Attachments

Login to view attachments
RalphY
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: 404 (UK)
Posted: 15th Jul 2010 02:16 Edited at: 15th Jul 2010 02:21
There's a lot of factors here that could effect what the best approach would be. Personally I would think about exactly what sort of control scheme I want from the form and design an interface for it, so for example:



This will allow me to handle key, and mouse input, and in this case console input (a simple text box on the form with a button that lets the user submit messages). I'm just using the KeyPress and MouseMove/MouseClick events already present on the Form class for simplicity. You could define your own specific events if you want, e.g. a MoveForward event.

The support class that will let me pass the console message to the game class:



For separation of concerns I'm also going to define a second interface called IRenderTarget, for rendering related features of the form:



Now in our form we just need to implement these interfaces. The mouse and keyboard related events in IControlSource are already implemented by Form so lets implement the console event and IRenderTarget:



Now we can use dependency injection and pass these interfaces to the game class constructor, this will allow us to mock the form class should the need arise:



Finally the game class:



There's other ways you could approach this, such as reversing it and having the game class forward request to the form. You could take it further and add a state object shared between the form and game class via an interface. You could then handle all events within the form recording the input state to your state object. Within the game class you can then just query the state object (I believe this is closer to how input in XNA is normally handled?), allowing you to effectively remove these cross class events.

To be honest, I'm not overly fond of the approach to XNA in WinForms used in that blog post you linked - I think it may be prone to inducing messy code. Have you looked at the example at http://creators.xna.com/en-GB/sample/winforms_series1?

Has this been any help or have I gone of on a tangent?

Go banana! | Super Nintendo Chalmers! | When I grow up I'm going to Bovine University!
flashing snall
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Boston
Posted: 15th Jul 2010 20:28
I did download and run through the source of microsoft example, but I found it to be really confusing. I wasnt sure where to put my game logic

Putting xna in the forms does seem like Im leaving myself open for some hairy scenarios, but I figure for a level editor thats only going to be used by me and my dev team, it should be fine.

Thanks for the help, I just read over it and it seems very intimidating and I dont have too much time right now to try and understand it. But it looks like good info, thank you very much!

Login to post a reply

Server time is: 2025-06-02 13:33:21
Your offset time is: 2025-06-02 13:33:21