Best way to model something that's drawable (i.e. has a visual representation in the UI)
Best way to model something that's drawable (i.e. has a visual representation in the UI)
- Subject: Best way to model something that's drawable (i.e. has a visual representation in the UI)
- From: William Squires <email@hidden>
- Date: Tue, 12 Jul 2016 13:11:20 -0500
Normally, of course, models shouldn't know anything about UI, and vice versa, but what about when the models themselves represent something visual that the UI needs to draw (like in a drawing/painting program, or - in my case - GameObject instances that can be objects found in the "dungeon" that the player in the game can see and possibly interact with)?
In this case, it makes sense that the object with the information needed to draw, is the model object itself (since it has all the properties needed, such as visible, passable, floorImage, etc...), but the model objects have no graphics context to draw into (that's the responsibility of the drawRect: method of a UIView subclass). So, in my app, the view's drawRect: method will get a reference to it's view controller, and ask for all the model objects that are visible within its bounds rect (and considering the "camera" view rect which is managed by the view controller), and will then iterate over them, asking them to draw themselves, now that the graphics context is properly set up.
Otherwise, the UIView subclass' drawRect: method becomes unwieldy as it is then too coupled to the model objects (i.e. it would - for each model object - have to determine what class they are, then have custom drawing code to actually draw what the model represents) This is too brittle, and is one reason to use polymorphism via subclassing to define behaviors specific to a particular class of object.
The question is, if my GameObject (base class) defines a drawRect(bounds: CGRect) itself, and the code in the UIView subclass' drawRect: method passes a CGRect (which is within it's own bounds) to the model's drawRect:, will the code in the model's drawRect: "see" the graphics context of the UIView, so that you can set the stroke or fill color using the setStroke() and setFill() methods on UIColor, as well as anything else that may require that context, such as lower-level calls to CoreGraphics?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden