2019-07-30 15:20:11 +08:00
|
|
|
//
|
|
|
|
// DoricHLayoutNode.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricHLayoutNode.h"
|
2019-07-31 17:43:26 +08:00
|
|
|
#import "DoricUtil.h"
|
2019-07-30 15:20:11 +08:00
|
|
|
|
|
|
|
@implementation DoricHLayoutNode
|
2019-07-31 14:18:20 +08:00
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_space = 0;
|
|
|
|
_gravity = 0;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-07-31 17:43:26 +08:00
|
|
|
- (void)blendView:(id)view forPropName:(NSString *)name propValue:(id)prop {
|
|
|
|
if ([name isEqualToString:@"gravity"]) {
|
|
|
|
self.gravity = [(NSNumber *)prop integerValue];
|
|
|
|
} else if ([name isEqualToString:@"space"]) {
|
|
|
|
self.space = [(NSNumber *)prop floatValue];
|
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
|
|
|
[super blendChild:child layoutConfig:layoutconfig];
|
|
|
|
if (![child.layoutParams isKindOfClass:VHLayoutParams.class]) {
|
|
|
|
DoricLog(@"blend VLayout child error,layout params not match");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VHLayoutParams *params = (VHLayoutParams *)child.layoutParams;
|
|
|
|
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
|
|
|
if (margin) {
|
|
|
|
params.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
|
|
|
params.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
|
|
|
params.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
|
|
|
params.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
|
|
|
}
|
|
|
|
NSNumber *alignment = [layoutconfig objectForKey:@"alignment"];
|
|
|
|
if (alignment) {
|
|
|
|
params.alignment = [alignment integerValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (VHLayoutParams *)generateDefaultLayoutParams {
|
|
|
|
return [[VHLayoutParams alloc] init];
|
|
|
|
}
|
|
|
|
|
2019-07-31 14:18:20 +08:00
|
|
|
- (void)measureByParent:(DoricGroupNode *)parent {
|
|
|
|
DoricLayoutDesc widthSpec = self.layoutParams.width;
|
|
|
|
DoricLayoutDesc heightSpec = self.layoutParams.height;
|
|
|
|
CGFloat maxWidth = 0,maxHeight = 0;
|
|
|
|
for (DoricViewNode *child in self.indexedChildren) {
|
|
|
|
[child measureByParent:self];
|
2019-07-31 17:43:26 +08:00
|
|
|
CGFloat placeWidth = child.measuredWidth;
|
|
|
|
CGFloat placeHeight = child.measuredHeight;
|
2019-07-31 14:18:20 +08:00
|
|
|
maxWidth += placeWidth + self.space;
|
|
|
|
maxHeight = MAX(maxHeight, placeHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
maxWidth -= self.space;
|
|
|
|
|
|
|
|
self.desiredWidth = maxWidth;
|
|
|
|
self.desiredHeight = maxHeight;
|
|
|
|
|
|
|
|
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
self.width = maxWidth;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
self.height = maxHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutByParent:(DoricGroupNode *)parent {
|
|
|
|
if (self.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
|
|
|
self.width = parent.width;
|
|
|
|
}
|
|
|
|
if (self.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
|
|
|
self.height = parent.height;
|
|
|
|
}
|
|
|
|
// layotu child
|
|
|
|
CGFloat xStart = 0, yStart = 0;
|
|
|
|
if ((self.gravity & LEFT) == LEFT) {
|
|
|
|
xStart = 0;
|
|
|
|
} else if ((self.gravity & RIGHT) == RIGHT) {
|
|
|
|
xStart = self.width - self.desiredWidth;
|
|
|
|
} else if ((self.gravity & CENTER_X) == CENTER_X) {
|
|
|
|
xStart = (self.width -self.desiredWidth)/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((self.gravity & TOP) == TOP) {
|
|
|
|
yStart = 0;
|
|
|
|
} else if ((self.gravity & BOTTOM) == BOTTOM) {
|
|
|
|
yStart = self.height - self.desiredHeight;
|
|
|
|
} else if ((self.gravity & CENTER_Y) == CENTER_Y) {
|
|
|
|
yStart = (self.height -self.desiredHeight)/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DoricViewNode *child in self.indexedChildren) {
|
|
|
|
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
|
|
|
child.width = self.width;
|
|
|
|
}
|
|
|
|
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
|
|
|
child.height = self.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([child.layoutParams isKindOfClass:VHLayoutParams.class]) {
|
|
|
|
VHLayoutParams *layoutParams = (VHLayoutParams *)child.layoutParams;
|
|
|
|
DoricGravity gravity = layoutParams.alignment;
|
|
|
|
if ((gravity & TOP) == TOP) {
|
|
|
|
child.top = yStart;
|
|
|
|
} else if ((gravity & BOTTOM) == BOTTOM) {
|
|
|
|
child.bottom = yStart + self.desiredHeight;
|
|
|
|
} else if ((gravity & CENTER_Y) == CENTER_Y) {
|
|
|
|
child.centerY = yStart + self.desiredHeight/2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
child.left = xStart;
|
|
|
|
xStart = child.right + self.space;
|
|
|
|
[child layoutByParent:self];
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-30 15:20:11 +08:00
|
|
|
|
|
|
|
@end
|