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/DoricStackNode.m
2019-10-21 09:59:22 +08:00

118 lines
3.8 KiB
Objective-C

/*
* 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.
*/
//
// DoricStackNode.m
// Doric
//
// Created by pengfei.zhou on 2019/7/30.
//
#import "DoricStackNode.h"
#import "DoricUtil.h"
@implementation DoricStackNode
- (instancetype)init {
if (self = [super init]) {
_gravity = 0;
}
return self;
}
- (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];
CGFloat placeWidth = child.measuredWidth;
CGFloat placeHeight = child.measuredHeight;
maxWidth = MAX(maxWidth, placeWidth);
maxHeight = MAX(maxHeight, placeHeight);
}
self.desiredWidth = maxWidth;
self.desiredHeight = maxHeight;
if (widthSpec == LAYOUT_WRAP_CONTENT) {
self.width = maxWidth;
}
if (heightSpec == LAYOUT_WRAP_CONTENT) {
self.height = maxHeight;
}
}
- (LayoutParams *)generateDefaultLayoutParams {
return [[StackLayoutParams alloc] init];
}
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
[super blendChild:child layoutConfig:layoutconfig];
if (![child.layoutParams isKindOfClass:StackLayoutParams.class]) {
DoricLog(@"blend Stack child error,layout params not match");
return;
}
StackLayoutParams *params = (StackLayoutParams *) 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[@"alignment"];
if (alignment) {
params.alignment = [alignment integerValue];
}
}
- (void)layoutByParent:(DoricGroupNode *)parent {
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;
}
DoricGravity gravity = self.gravity;
if ([child.layoutParams isKindOfClass:StackLayoutParams.class]) {
StackLayoutParams *layoutParams = (StackLayoutParams *) child.layoutParams;
gravity |= layoutParams.alignment;
}
if ((gravity & LEFT) == LEFT) {
child.left = self.left;
}
if ((gravity & RIGHT) == RIGHT) {
child.right = self.right;
}
if ((gravity & TOP) == TOP) {
child.top = self.top;
}
if ((gravity & BOTTOM) == BOTTOM) {
child.bottom = self.bottom;
}
if ((gravity & CENTER_X) == CENTER_X) {
child.centerX = self.centerX;
}
if ((gravity & CENTER_Y) == CENTER_Y) {
child.centerY = self.centerY;
}
[child layoutByParent:self];
}
}
@end