Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is this the best way to use events for control classes like a button?  (Read 3704 times)

0 Members and 2 Guests are viewing this topic.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
I have an interface that holds the methods needed to invoke the events and the events itself. Is this how you generally use events?

interface MouseInput
    {

        void HandleMousePressed(object sender, MouseButtonEventArgs e);

        void HandleMouseReleased(object sender, MouseButtonEventArgs e);

        void HandleMouseMoved(object sender, MouseMoveEventArgs e);

        event EventHandler OnPressed;
        event EventHandler OnReleased;
        event EventHandler OnHoveredEntered;
        event EventHandler OnHoveredExit;

    }
 

I then have a button class which derives from MouseInput.

Button.cs:

http://pastebin.com/QnyqxwPS

It works with no [known] bugs but it forces you to rewrite a lot of code like how the events are called. This gives flexibility on how you can use the interface but most classes deriving from the interface will want the same behavior (that I wrote in the Button class). And I really don't want to make the interface a class - most classes that would use this functionality will already being deriving from another class (like an Entity class) and I can't derive from more than two classes.
« Last Edit: March 21, 2016, 07:53:35 am by asusralis »

Dejan Geci

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Is this the best way to use events for control classes like a button?
« Reply #1 on: March 23, 2016, 03:11:11 pm »
I suggest reading about Component pattern - http://gameprogrammingpatterns.com/component.html. Great articles on that site. Also read about "composition over inheritance" principle, it should make things clearer.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: Is this the best way to use events for control classes like a button?
« Reply #2 on: March 24, 2016, 07:22:08 am »
How do you know when to choose from inheritance from a class, inheritance from an interface, and components?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is this the best way to use events for control classes like a button?
« Reply #3 on: March 24, 2016, 07:45:28 am »
Don't forget that this forum is about SFML, I feel like we're already quite far from it in this thread ;)
Laurent Gomila - SFML developer

Dejan Geci

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Is this the best way to use events for control classes like a button?
« Reply #4 on: March 24, 2016, 04:02:46 pm »
Yes, this is a general software question. A quick google query with the "How do you know when to choose from inheritance from a class, inheritance from an interface, and components?" yielded a bunch of results ;)

 

anything