-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separation of Event Management from Window Base to a custom type #256
Conversation
And why is this preferable? I feel like this is a very subjective change and don't really see why we have to facilitate this hook in SFML.Net. |
Consider a game with multiple scenes, and the game gets transitioned to each scene without any fixed order. Thus for each scene, the .NET events should be registered (and unregistered) during these transitioned. Of course it is possible, but if the game makes use of multiple events (say KeyPressed, KeyReleased, MouseButtonScrolled and MouseButtonReleased), it gets cumbersome to register and unregister the events for each scene. Alternatively, If a single large method is made for all the scenes, (which provides a switch for all states), this approach mandates updating this large method for any addition or removal of scenes in the game. Also, if I want my logic to consider multiple types of SFML events together in a single method, the NET event approach is not good for this, (unless I store the events in some buffer). The core issue, in my opinion here is that the event management is tightly coupled to the window in this case. The PR makes use of dependency injection to resolve this. There are additional benefits as well. I don't always want to use, say the
Yes I agree this is a bit subjective, but I honestly feel there are more gains than losses in this approach. The original approach is still available, and can be used for simple games, whereas the developers who are looking for their own custom approach can also do so without issues. |
The example build is failing for the single VB.Net project Sub App_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles window.Closed Here, |
I feel like there are a lot better patterns to address this, than changing how SFML handles events, especially in C#. You could for example build that dispatcher you integrated into SFML on top of SFML and then distribute events as needed. Additionally, your problem sounds to me like your scenes are too closely coupled to events. Why do scenes have to directly handle SFML events? Shouldn't they handle more generic events and then you have again one place to handle SFML events from where you dispatch input events?
You can certainly register the same method on two different events, not sure that's very advisable though. When you have type specific information and you make it type unspecific again, just to branch anew to be type specific.
But events are tightly couple to the window. Events without a window doesn't make sense. I'm still not convinced of this change:
|
Yes, I agree there are many ways to solve this. I would argue that all these ways involve adding to the layers of abstraction for solving the problem (of handling the events), in contrast to my approach to removing one layer, but I understand that this is opinionated.
I am not sure if I understand this part. The problem here is not on scenes being tightly coupled to the SFML events. The game window listens on the SFML events, exposes them to the scene on which scene must change state or transition to another scene. A scene can take the sequence of SFML events it wants to listen on, and sure, I could add layers of abstraction here between the SFML events and my custom game scenes, but this is forced on me because of how the window is dispatching the events. Again, I may have not understood your point.
Oh that's a good point I hadn't considered.
Yes, the window must expose the events, but the issue here is that the logic of dispatching of those events is tightly bound to the window. In the C++ library, this is not a problem, because the
For the second point, I'd say that I am not providing multiple ways to do the same thing. I am providing an option to replace the sealed way of doing the thing. |
Yes, I can absolutely see that and if it was all one code base, I could also see providing such hooks, but I don't see it justifying to modify SFML, especially for a relatively narrow use case.
It's a bit of a strong word go with "forced on me". Nobody forced you to uses scene objects, you could also just do it in one big loop 😉
If I can provide two answers to the question of "how can I handle events in SFML.Net", I'm providing multiple ways. Yes, one is a deep hook and the other one is using a "default implementation", but it's still multiple ways for which one needs to write/provide justifications for when to use what.
Thank you for bringing it up, having a discussion and also respecting the decision! 🙂 If you have an ideas on how to improve the design of SFML.Net, maybe you can leave some comments over on #262 |
Summary of PR
This PR introduces a new approach to SFML event management. Prior to this, the C# events within the
WindowBase
class are required to be subscribed to .NET events within theWindowBase
class. This PR makes the classWindowBase
to hold an object ofIEventMan
, and theWindowBase.DispatchEvents()
now passes all events to thisIEventMan
object. Thus, the developer can now define their own event management logic using Dependancy Injection of their own custom type which implements this interface.The interface
IEventMan
defines two methods.and the class
WindowBase
is modifed asThus, a custom logic for event handling can be provided as shown
The above example is also added in a new project in the
examples
folder.The original .NET event based implementation is separated to a class
SubscribeManager
. This is set to be the default object created forWindowBase
. This does make it to be a breaking change, and the examples are also modified to support them.Also, as a small bonus, the dummy values in the constructors are resolved. Since the constructors of base class
WindowBase
now also accept a type ofIEventMan
, all public and protected constructors can be differentiated by the order of the arguements.Details of commits
The first commit defines
IEventMan
, along with modifying theWindowBase
class. The original implementation of event handling is moved to the classSubscribeManager
.The second commit modifies the constructor to accept an argument of the type
IEventMan
, which would be used by theWindowBase
. This commit also resolves the public and protected constructors so that the dummy variables are not needed.The third commit modifies the examples to build sucessfully. Also, a new example
custom_eventman
is added. All examples are tested on linux, by running them innet6.0
framework.