LOTShapeGroup.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // LOTShape.m
  3. // LottieAnimator
  4. //
  5. // Created by Brandon Withrow on 12/14/15.
  6. // Copyright © 2015 Brandon Withrow. All rights reserved.
  7. //
  8. #import "LOTShapeGroup.h"
  9. #import "LOTShapeFill.h"
  10. #import "LOTShapePath.h"
  11. #import "LOTShapeCircle.h"
  12. #import "LOTShapeStroke.h"
  13. #import "LOTShapeTransform.h"
  14. #import "LOTShapeRectangle.h"
  15. #import "LOTShapeTrimPath.h"
  16. #import "LOTShapeGradientFill.h"
  17. #import "LOTShapeStar.h"
  18. #import "LOTShapeRepeater.h"
  19. @implementation LOTShapeGroup
  20. - (instancetype)initWithJSON:(NSDictionary *)jsonDictionary {
  21. self = [super init];
  22. if (self) {
  23. [self _mapFromJSON:jsonDictionary];
  24. }
  25. return self;
  26. }
  27. - (void)_mapFromJSON:(NSDictionary *)jsonDictionary {
  28. if (jsonDictionary[@"nm"] ) {
  29. _keyname = [jsonDictionary[@"nm"] copy];
  30. }
  31. NSArray *itemsJSON = jsonDictionary[@"it"];
  32. NSMutableArray *items = [NSMutableArray array];
  33. for (NSDictionary *itemJSON in itemsJSON) {
  34. id newItem = [LOTShapeGroup shapeItemWithJSON:itemJSON];
  35. if (newItem) {
  36. [items addObject:newItem];
  37. }
  38. }
  39. _items = items;
  40. }
  41. + (id)shapeItemWithJSON:(NSDictionary *)itemJSON {
  42. NSString *type = itemJSON[@"ty"];
  43. if ([type isEqualToString:@"gr"]) {
  44. LOTShapeGroup *group = [[LOTShapeGroup alloc] initWithJSON:itemJSON];
  45. return group;
  46. } else if ([type isEqualToString:@"st"]) {
  47. LOTShapeStroke *stroke = [[LOTShapeStroke alloc] initWithJSON:itemJSON];
  48. return stroke;
  49. } else if ([type isEqualToString:@"fl"]) {
  50. LOTShapeFill *fill = [[LOTShapeFill alloc] initWithJSON:itemJSON];
  51. return fill;
  52. } else if ([type isEqualToString:@"tr"]) {
  53. LOTShapeTransform *transform = [[LOTShapeTransform alloc] initWithJSON:itemJSON];
  54. return transform;
  55. } else if ([type isEqualToString:@"sh"]) {
  56. LOTShapePath *path = [[LOTShapePath alloc] initWithJSON:itemJSON];
  57. return path;
  58. } else if ([type isEqualToString:@"el"]) {
  59. LOTShapeCircle *circle = [[LOTShapeCircle alloc] initWithJSON:itemJSON];
  60. return circle;
  61. } else if ([type isEqualToString:@"rc"]) {
  62. LOTShapeRectangle *rectangle = [[LOTShapeRectangle alloc] initWithJSON:itemJSON];
  63. return rectangle;
  64. } else if ([type isEqualToString:@"tm"]) {
  65. LOTShapeTrimPath *trim = [[LOTShapeTrimPath alloc] initWithJSON:itemJSON];
  66. return trim;
  67. } else if ([type isEqualToString:@"gs"]) {
  68. NSLog(@"%s: Warning: gradient strokes are not supported", __PRETTY_FUNCTION__);
  69. } else if ([type isEqualToString:@"gf"]) {
  70. LOTShapeGradientFill *gradientFill = [[LOTShapeGradientFill alloc] initWithJSON:itemJSON];
  71. return gradientFill;
  72. } else if ([type isEqualToString:@"sr"]) {
  73. LOTShapeStar *star = [[LOTShapeStar alloc] initWithJSON:itemJSON];
  74. return star;
  75. } else if ([type isEqualToString:@"mm"]) {
  76. NSString *name = itemJSON[@"nm"];
  77. NSLog(@"%s: Warning: merge shape is not supported. name: %@", __PRETTY_FUNCTION__, name);
  78. } else if ([type isEqualToString:@"rp"]) {
  79. LOTShapeRepeater *repeater = [[LOTShapeRepeater alloc] initWithJSON:itemJSON];
  80. return repeater;
  81. } else {
  82. NSString *name = itemJSON[@"nm"];
  83. NSLog(@"%s: Unsupported shape: %@ name: %@", __PRETTY_FUNCTION__, type, name);
  84. }
  85. return nil;
  86. }
  87. - (NSString *)description {
  88. NSMutableString *text = [[super description] mutableCopy];
  89. [text appendFormat:@" items: %@", self.items];
  90. return text;
  91. }
  92. @end