Task:
make buttons in a program that respond with mouse events(motion,press,release)
Approach [1] :
Button class have a method that handle events. So it checks if the the event is press or release or motion and call the right method, then I take every handle for each button created and loop it as long as the programe is running.
Approach [2] :
I already have a Mouse class handling all mouse inputs. So the second approach would work on mouse motion. I mean I won’t be looping a handler all the time, but when a mouse motion is detected I check if the motion was on a button (this check will be performed in the Mouse class that handles mouse inputs). If it was on a button then execute that button method that correspond to the event type.
So I was wondering which approach would be better?!
A button may not be exclusively tied to your mouse input, but can also be triggered by your keyboard inputs (for example).
Even if this program isn’t using keyboard inputs for buttons, it may be the case for your next program. If you go for Approach [1], you will be able to re-use your code in a wide range of situations.
On top of that, with Approach [2] your Mouse code will be filled with Buttons stuff which can pollute it.
That’s why I suggest you to go for the Approach [1]