F
F
fedoraman2019-03-09 01:33:47
C++ / C#
fedoraman, 2019-03-09 01:33:47

How to correctly handle clicks in a self-written game engine?

In general, it was necessary to pervert and write an engine for a 2D toy in C ++. In the engine, you need to handle mouse clicks by determining which widget hitbox hit the click (a widget is anything that can be drawn: a button, a unit on the screen, etc.), and in some way calling the handler. Now the trick is to figure out how and in what form it is more correct to add handlers so that it looks glamorous and does not lead to unnecessary writing or refactoring when I wrote a lot of code and saw that everything was garbage and I needed it all over again.
There are two options running through my head right now:

  • Make in the base class pure virtual methods onClick, onMouseIn, etc. and implement them in derived classes. Disadvantage: for buttons, for example, you will have to inherit a separate class for each behavior. Is this really normal? I did this when I wrote two-button applications in Delphi in my dashing youth, but I'm not sure that this will not lead to sad consequences in the form of spaghetti code closer to the deadline.
  • Pass a handler to the widget when it is created. Disadvantage: it is not clear how to implement this, because you will either need to pass some class method, which is not particularly feasible, or a similar function that takes a pointer to the widget being processed, which seems not to be entirely OOP-shno.

I'm more inclined towards the first option, but for some reason I'm not sure that when I start implementing the main game logic (i.e., all kinds of little men running around the map), it will painlessly expand on them, because there, it seems, the number of different reactions to clicks is much more extensive than to the buttons in the main menu, and for each reaction, inherit a class ... brr.
In short, you need a pattern that is magical in its simplicity or just a pat on the shoulder from a person with experience explaining why I am wrong and writing one of these two options will be fine. Once again, I write in C++.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2019-03-09
@dollar

When you click on the game screen, it searches for all elements that can receive clicks. If the click hit something, then all child elements that can receive clicks are searched. And so on, until the click hits a specific button. This is the general scheme. How you do it is a matter of taste. What is important here is not so much OOP, that is, the inheritance of classes, as the implementation of the hierarchy of elements of the interface itself (GUI or HUD), that is, the inheritance of objects. The objects themselves need references to child and parent elements, much like in the browser DOM.
Further nuances. For example, an element can have its own handler and child elements at the same time. You can make it so that if there is an own handler, then the child ones are ignored. Or you can make it so that if the click did not hit the children, then its own handler is called. Everything has pros and cons. Again, do as you please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question