It seems like a form should be stand-alone, but here's my dilemma:
I have an object called GameMap
. It exists on a MapForm
which is all fine and good. It can edit itself, etc. However, I also have a TilesetForm
and LayerForm
I was passing in the GameMap
to these objects - but then I found out I also needed to pass the CurrentLayer between the layer form and map editor so they could edit and be selected on the other form. Next thing I know - dependencies between the two are pretty much tied together. The tileset lives its sole purpose to serve the MapForm
, so does it map sense to just couple them together?
How can I make this explicit - I can't use the constructor because the form might not be active - right now I just called SwitchContext
when a new map form is focused.
You handle this through using events.
Rather than passing an instance of the map to the tileset and having the tile set directly manipulating the map when something happens, define an event in tileset whenever important things happen. The map can then subscribe to those events and perform some action. The tileset then doesn't need to know anything at all about the map.
If it doesn't make sense for the map to logically "own" the tileset, then it wouldn't even be subscribing to the events directly. It might make sense to have a 3rd party (some higher level concept) that "knows about" both the tile set and the map; it would then subscribe an event handler to the tile set, and call some appropriate method on the map in the event handler. (This method is so that even this high level concept doesn't know about the specific UI implementations of the map.)