- Modification -
ポリゴンツールのドローオブジェクトの保存と読み込み

■ ポリゴンツールのドローオブジェクトの保存と読み込みに必要なもの
さて、ポリゴンツール用の、保存読み込みのメソッドなんだけど。まず、保存に必要なインスタンス変数を選びだそう。それは、2 つあることが分かる。アンカーポイントの配列である _anchorPoints と、パスを閉じるかどうかを表す _isPathClosed だ。
|
Sketch/DocumentModel.subproj/SKTPolygon.h (created by mkino)
|
@interface SKTPolygon : SKTGraphic {
@private
NSMutableArray* _anchorPoints;
BOOL _isPathClosed;
...
}
|
SKTPolygon には、アフィン変換を表す、NSAffineTransform 型の _transform もあるんだけど、これは applyTransform で現在のアンカーポイントに適用してしまえば、保存する必要はなくなる。
■ ポリゴンツールのドローオブジェクトの保存と読み込みの実装
実装では、propertyListRepresentaion と loadPropertyListRepresentation を、オーバーライドするんだ。
◆ キーワード
まずは、キーワード。アンカーポイントと、パスの閉鎖性のフラグのためのキーワードだ。
|
Sketch/DocumentModel.subproj/SKTPolygon.m (created by mkino)
|
NSString* SKTPolygonAnchorPointsKey = @"PolyAnchorPoints";
NSString* SKTPolygonIsPathClosedKey = @"PolyIsPathClosed";
|
この 2 つのキーワードを使う。
◆ 書き込み(propertyListRepresentation)
書き込み用の propertyListRepresentation メソッドだ。
|
Sketch/DocumentModel.subproj/SKTPolygon.m (created by mkino)
|
- (NSMutableDictionary *)propertyListRepresentation {
NSMutableDictionary* dict = [super propertyListRepresentation];
NSMutableArray* array =
[NSMutableArray arrayWithCapacity:[_anchorPoints count]];
int i;
// Apply transform at first
[self applyTransform];
// Make an anchor points array represented in NSString
for (i = 0; i < [_anchorPoints count]; i++) {
[array addObject:NSStringFromPoint([self pointAtIndex:i])];
}
[dict setObject:array forKey:SKTPolygonAnchorPointsKey];
// For _isPathClosed
[dict setObject:[NSNumber numberWithBool:_isPathClosed]
forKey:SKTPolygonIsPathClosedKey];
return dict;
}
|
保存する前に、applyTransform を呼んで、アフィン変換を適用させておいてやる。
その後、辞書につっこむんだけど、アンカーポイントの配列はちょっと注意が必要。この配列には、NSPoint をラップした NSValue オブジェクトが入っているんだけど、これはこのままでは、うまく propertyList が得られらない。そこで、NSPoint を、NSStringFromPoint() を使って NSString 型に変更してやるんだ。そうやって作った新しい配列を、保存のための辞書に突っ込んでやるんだ。
◆ 読み出し(flipHorizontally, flipVertically)
続いて読み出し。
|
Sketch/DocumentModel.subproj/SKTPolygon.m (created by mkino)
|
- (void)loadPropertyListRepresentation:(NSDictionary *)dict {
id obj;
int i;
[super loadPropertyListRepresentation:dict];
obj = [dict objectForKey:SKTPolygonAnchorPointsKey];
if (obj) {
NSMutableArray* array = [NSMutableArray arrayWithCapacity:[obj count]];
for (i = 0; i < [obj count]; i++) {
[array addObject:[NSValue valueWithPoint:
NSPointFromString([obj objectAtIndex:i])]];
}
[self setAnchorPoints:array];
}
obj = [dict objectForKey:SKTPolygonIsPathClosedKey];
if (obj) {
[self setPathClosed:[obj intValue]];
}
}
|
注意するところとしては、さっきの書き込みのところで、アンカーポイントの NSPoint を NSString に変換したので、ここでは NSPointFromString() を使って、NSPoint に戻してやるところ、かな。
実際に、ドローオブジェクトの情報が書き出されたファイルは、こんな感じ。
{
DrawDocumentVersion = 1;
GraphicsList = (
{
Bounds = "{{173, 198}, {125, 155}}";
Class = Polygon;
DrawsFill = NO;
DrawsStroke = YES;
FillColor = <040b7479 70656473 74726561 6d8103e8
84014084 8484074e 53436f6c 6f720084
84084e53 4f626a65 63740085 84016303
84026666 010186>;
PolyAnchorPoints = ("{211, 198}", "{173, 353}", "{298, 309}");
PolyIsPathClosed = 1;
StrokeColor = <040b7479 70656473 74726561 6d8103e8
84014084 8484074e 53436f6c 6f720084
84084e53 4f626a65 63740085 84016303
84026666 000186>;
StrokeLineWidth = 1.00;
}
);
|
'PolyAnchorPoints' と 'PolyIsPathClosed' の 2 つの項目が増えているよな。
|