Jebbs / DSFML

DSFML is a D binding of SFML
Other
95 stars 23 forks source link

Event Driven Events #96

Open Jebbs opened 10 years ago

Jebbs commented 10 years ago

A while ago, @Halsys had the terrific idea of having a way of handling events similar to the SFML.Net API(#44), which is an Event Driven system and not the current Polling system DSFML uses. It was never finished, though he had a lot done.

I want to revisit this idea and make it possible for people to use an Event Driven system, or a Polling system, or hell, even both at the same time if they want. Since Halsys already did a lot of work, it shouldn't be too bad to finish it up.

auRose94 commented 10 years ago

Well you are welcome, And you(or anyone else for that matter) are/is free to do what you like with what I had going and I would probably do it a bit differently if I had/wanted to finish it but I hit real world problems and had to drop D,DSFML/SFML for work on C#/Lua and Unity3D/Love... Sorry I never finished.

I actually want to start a pet project and see if D is as capable as C# and add some pretty neat stuff to DSFML like gameobject and components(For gameobject). I would kill to see D/DSFML be more or less like C#/Unity3D. I can tell you it won't be called (D)SFML when I'm done/started with everything. I would drown the thing in features like its nobody's business if I had some time, money, and bandwidth. Also I really dislike C++ Difficulty and I'm really not impressed with C# performance, even fully optimized and what not.... Hence the want to poke D again.

patrickjm commented 10 years ago

This could be done easily with the help of std.signals. I've been using the following method in a GUI engine (thanks to StackOverflow - it's a shame I can't find the original page):

class GuiEvent(T...){
    mixin Signal!(T);       

    void broadcast(T args){ 
        emit(args);
    }       
    void opAddAssign(slot_t slot){
        connect(slot);
    }
    void opSubAssign(slot_t slot) {
        disconnect(slot);
    }
}

For example, a button event:

auto onClick = new GuiEvent!(GuiButton, int int)();
onClick += &someEventFunction;
// calling all of the events:
onClick.broadcast(myButton, mouseX, mouseY);
// maybe someEventFunction should only be used once:
onClick -= &someEventFunction;

Only a suggestion, of course, but I've found it to be really useful. I think this could work wonderfully with DSFML's input system, and the event structs (KeyboardEvent, etc) could stay the same. Regardless, it's about as close to C# events as I have managed, and they work just as well.