This seems to happen every time I create any sort of GUI. I have trouble figuring out how child classes should communicate to their siblings.
It's a general problem, but it's probably easier to use a concrete example (hopefully that's still okay for this forum? I know it's different from StackOverflow): I have a window that contains 4 panels. Right now, I simply have the children as member objects of the parent.
Keypresses change all 4 panels, so I have the child objects throw the event back to their parent, which does the logic, then makes the appropriate calls for each panel.
However, mouse events are different. They change the position of a camera (owned by the parent), which every panel uses to render itself. So I must recompute a few matrices, in order to render each panel. The matrices are different for each panel. The problem I'm having is: when do I give the children the matrices?
I think I have three possibilities:
- When reading a mouse event, compute the matrices and call setter methods for each panel. However, sometimes the matrix will be replaced before the window renders.
- Give the child panel a reference to the parent, and during the render call, request the matrix from the parent (either by method or member variable).
- Modify child.render() so that it accepts two matrix parameters, and make parent.render() call them appropriately.
#2 seems good, because I'm not unnecessarily setting the matrix in the child window. But it also requires me to mess with forward declarations for member objects, which seems like bad practice. And #3 I'm not quite sure how to implement, because render is called by many things other than my code (un-minimizing, for example), and I can't touch them. So I suppose it is #1?
Is there a design pattern people usually follow for this? Am I just overthinking, and #1 is actually completely OK?