Skip to content

Adding support for new hierarchies

chinmaygarde edited this page Feb 11, 2013 · 3 revisions

You need to to subclass HDDetective and add a category to the class that represents you view (UIView in UIKit, CALayer in Quartz and CCNode in Cocos2D).

The HDDetective Subclass

HDDetective is as an abstract class that handles network communication, application lifecycle, Bonjour registrations, etc.. Any subclass of HDDetective must override the following methods. Don't call super implementations from within the overridden methods.

  • -(void) processCommand:(HDMessage *) message withCompletionHandler:(HDCommandProcessCompletionHandler) handler;
  • -(NSString *) aspectName;

The messageType property on HDMessage contains the what you think it contains. You respond to a message by inspecting the contents of the requestArguments property, setting the appropriate values on the responseData property and invoking the completion handler with an optional NSError argument (pass nil if there was no error). It is always a good idea to invoke the completion handler since both the server and client hold on to some resources (till the connection is reset) for each message with no response.

The following protocol must be followed for each messageType:

  • HDMessageTypeUnknown: Call the completion handler with nil error.
  • HDMessageTypeRequestEntireHierarchy: The requestArguments property is nil. The response must be an array containing the roots nodes of the items in the hierarchy. Even if the hierarchy has one root node, it must be added to an array before the array is set as the responseData property of the message.
  • HDMessageTypeGetSufaceForNode: The requestArguments is an object of class HDArgument. The pointerValue property of the argument can be used to fetch the node in the hierarchy (I am sorry this is so ugly). Make sure you iterate down the hierarchy from the root to ensure that the pointer is a valid node before capturing the PNG image representation of just that node (and not its children) and returning this NSData as the responseData of the message.
  • HDMessageTypeGetFullSurfaceForNode: This is similar to HDMessageTypeGetSufaceForNode but you must return the PNG image representation of the node and its children.
  • HDMessageTypeApplyScriptAtNode: For future implementation. Invoke the completion handler with an appropriate NSError.
  • HDMessageTypeUpdatePropertyForNode: For future implementation. Invoke the completion handler with an appropriate NSError.

Override the -(NSString *) aspectName method to return the name of your view hierarchy (UIKit, Quartz, Cocos2D etc..)

The Category

Data Exchange

JSON is used for exchange of data between the server and the client. The serialization and deserialization is handled automatically for NSString, containers (NSArray and NSDictionary) and certain structs (CGRect, CGPoint, CGSize). Other objects that require serialization need to adopt the HDProperties protocol. A category that adopts this protocol will suffice. The one required method of this protocol is the -(NSArray *) serializableProperties;. This is an array of strings that are the names of the properties that need to serialized and deserialized.

What you need to do

Add a category on the class that represents your view/node that conforms to the HDProperties protocol. For the desktop application to accurately depict your view hierarchy, the minimum set of properties returned by -(NSArray *) serializableProperties; is @[@"pointerValue", @"bounds", @"frame", @"subviews", @"classHierarchy", @"viewControllerClass", @"pathToRoot"];. If there are no equivalents for the same in your view/node class, you will need to add the same as properties to the category. See the Cocos2D implementation for an example.

Coordinate Spaces

The desktop application assumes that views are in the window coordinate space with the top left as the origin. You will have to make the appropriate coordinate space conversions before passing the same over the wire. For example, the Cocos2D coordinate system necessitates a scale and translate on the affine transform representing the frame of the CCNode.