This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/iOS/Pod/Classes/Shader/DoricVLayoutNode.m

136 lines
4.4 KiB
Mathematica
Raw Normal View History

2019-10-21 09:59:22 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2019-07-30 15:20:11 +08:00
//
// DoricVLayoutNode.m
// Doric
//
// Created by pengfei.zhou on 2019/7/30.
//
#import "DoricVLayoutNode.h"
2019-07-31 17:43:26 +08:00
#import "DoricUtil.h"
2019-07-30 15:20:11 +08:00
@implementation DoricVLayoutNode
2019-07-31 14:18:20 +08:00
- (instancetype)init {
if (self = [super init]) {
_space = 0;
_gravity = 0;
}
return self;
}
2019-07-30 15:20:11 +08:00
2019-07-31 17:43:26 +08:00
- (void)blendView:(id)view forPropName:(NSString *)name propValue:(id)prop {
if ([name isEqualToString:@"gravity"]) {
2019-10-12 14:48:19 +08:00
self.gravity = [(NSNumber *) prop integerValue];
2019-07-31 17:43:26 +08:00
} else if ([name isEqualToString:@"space"]) {
2019-10-12 14:48:19 +08:00
self.space = [(NSNumber *) prop floatValue];
2019-07-31 17:43:26 +08:00
} 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;
}
2019-10-12 14:48:19 +08:00
VHLayoutParams *params = (VHLayoutParams *) child.layoutParams;
NSDictionary *margin = layoutconfig[@"margin"];
2019-07-31 17:43:26 +08:00
if (margin) {
2019-10-12 14:48:19 +08:00
params.margin.top = [(NSNumber *) margin[@"top"] floatValue];
params.margin.left = [(NSNumber *) margin[@"left"] floatValue];
params.margin.right = [(NSNumber *) margin[@"right"] floatValue];
params.margin.bottom = [(NSNumber *) margin[@"bottom"] floatValue];
2019-07-31 17:43:26 +08:00
}
2019-10-12 14:48:19 +08:00
NSNumber *alignment = layoutconfig[@"alignment"];
2019-07-31 17:43:26 +08:00
if (alignment) {
params.alignment = [alignment integerValue];
}
}
- (LayoutParams *)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;
2019-10-12 14:48:19 +08:00
CGFloat maxWidth = 0, maxHeight = 0;
2019-07-31 14:18:20 +08:00
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 = MAX(maxWidth, placeWidth);
maxHeight += placeHeight + self.space;
}
maxHeight -= self.space;
2019-10-12 14:48:19 +08:00
2019-07-31 14:18:20 +08:00
self.desiredWidth = maxWidth;
self.desiredHeight = maxHeight;
2019-10-12 14:48:19 +08:00
2019-07-31 14:18:20 +08:00
if (widthSpec == LAYOUT_WRAP_CONTENT) {
2019-07-31 17:43:26 +08:00
self.width = maxWidth;
2019-07-31 14:18:20 +08:00
}
2019-10-12 14:48:19 +08:00
2019-07-31 14:18:20 +08:00
if (heightSpec == LAYOUT_WRAP_CONTENT) {
2019-07-31 17:43:26 +08:00
self.height = maxHeight;
2019-07-31 14:18:20 +08:00
}
}
- (void)layoutByParent:(DoricGroupNode *)parent {
if (self.layoutParams.width == LAYOUT_MATCH_PARENT) {
2019-07-31 17:43:26 +08:00
self.width = parent.width;
2019-07-31 14:18:20 +08:00
}
if (self.layoutParams.height == LAYOUT_MATCH_PARENT) {
2019-07-31 17:43:26 +08:00
self.height = parent.height;
}
2019-07-31 18:13:22 +08:00
// layout child
CGFloat yStart = 0;
2019-07-31 17:43:26 +08:00
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) {
2019-10-12 14:48:19 +08:00
yStart = (self.height - self.desiredHeight) / 2;
2019-07-31 17:43:26 +08:00
}
2019-10-12 14:48:19 +08:00
2019-07-31 14:18:20 +08:00
for (DoricViewNode *child in self.indexedChildren) {
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
2019-07-31 17:43:26 +08:00
child.width = self.width;
2019-07-31 14:18:20 +08:00
}
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
2019-07-31 17:43:26 +08:00
child.height = self.height;
2019-07-31 14:18:20 +08:00
}
2019-07-31 17:43:26 +08:00
if ([child.layoutParams isKindOfClass:VHLayoutParams.class]) {
2019-10-12 14:48:19 +08:00
VHLayoutParams *layoutParams = (VHLayoutParams *) child.layoutParams;
2019-07-31 18:13:22 +08:00
DoricGravity gravity = layoutParams.alignment | self.gravity;
2019-07-31 17:43:26 +08:00
if ((gravity & LEFT) == LEFT) {
2019-07-31 18:13:22 +08:00
child.left = 0;
2019-07-31 17:43:26 +08:00
} else if ((gravity & RIGHT) == RIGHT) {
2019-07-31 18:13:22 +08:00
child.right = self.width;
2019-07-31 17:43:26 +08:00
} else if ((gravity & CENTER_X) == CENTER_X) {
2019-10-12 14:48:19 +08:00
child.centerX = self.width / 2;
2019-07-31 17:43:26 +08:00
}
2019-10-12 14:48:19 +08:00
}
2019-07-31 17:43:26 +08:00
child.top = yStart;
yStart = child.bottom + self.space;
[child layoutByParent:self];
2019-07-31 14:18:20 +08:00
}
}
2019-07-30 15:20:11 +08:00
@end