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/DoricViewNode.m

235 lines
8.0 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
//
// DoricViewNode.m
// Doric
//
// Created by pengfei.zhou on 2019/7/30.
//
#import "DoricViewNode.h"
#import "DoricUtil.h"
#import "DoricGroupNode.h"
2019-07-30 21:05:27 +08:00
#import "DoricRootNode.h"
#import "DoricConstant.h"
2019-11-15 14:57:41 +08:00
#import "DoricSuperNode.h"
#import "DoricExtensions.h"
2019-07-30 15:20:11 +08:00
2019-08-02 20:24:05 +08:00
void DoricAddEllipticArcPath(CGMutablePathRef path,
2019-10-12 14:48:19 +08:00
CGPoint origin,
CGFloat radius,
CGFloat startAngle,
CGFloat endAngle) {
2019-08-02 20:24:05 +08:00
CGAffineTransform t = CGAffineTransformMakeTranslation(origin.x, origin.y);
CGPathAddArc(path, &t, 0, 0, radius, startAngle, endAngle, NO);
}
CGPathRef DoricCreateRoundedRectPath(CGRect bounds,
2019-10-12 14:48:19 +08:00
CGFloat leftTop,
CGFloat rightTop,
CGFloat rightBottom,
CGFloat leftBottom) {
2019-08-02 20:24:05 +08:00
const CGFloat minX = CGRectGetMinX(bounds);
const CGFloat minY = CGRectGetMinY(bounds);
const CGFloat maxX = CGRectGetMaxX(bounds);
const CGFloat maxY = CGRectGetMaxY(bounds);
2019-10-12 14:48:19 +08:00
2019-08-02 20:24:05 +08:00
CGMutablePathRef path = CGPathCreateMutable();
2019-10-12 14:48:19 +08:00
DoricAddEllipticArcPath(path, (CGPoint) {
minX + leftTop, minY + leftTop
2019-08-02 20:24:05 +08:00
}, leftTop, M_PI, 3 * M_PI_2);
2019-10-12 14:48:19 +08:00
DoricAddEllipticArcPath(path, (CGPoint) {
maxX - rightTop, minY + rightTop
2019-08-02 20:24:05 +08:00
}, rightTop, 3 * M_PI_2, 0);
2019-10-12 14:48:19 +08:00
DoricAddEllipticArcPath(path, (CGPoint) {
maxX - rightBottom, maxY - rightBottom
2019-08-02 20:24:05 +08:00
}, rightBottom, 0, M_PI_2);
2019-10-12 14:48:19 +08:00
DoricAddEllipticArcPath(path, (CGPoint) {
minX + leftBottom, maxY - leftBottom
2019-08-02 20:24:05 +08:00
}, leftBottom, M_PI_2, M_PI);
CGPathCloseSubpath(path);
return path;
}
2019-10-12 14:48:19 +08:00
@interface DoricViewNode ()
@property(nonatomic, strong) NSMutableDictionary *callbackIds;
2019-07-31 19:22:00 +08:00
@end
2019-07-30 15:20:11 +08:00
@implementation DoricViewNode
2019-07-31 19:22:00 +08:00
- (instancetype)initWithContext:(DoricContext *)doricContext {
2019-10-12 14:48:19 +08:00
if (self = [super initWithContext:doricContext]) {
2019-07-31 19:22:00 +08:00
_callbackIds = [[NSMutableDictionary alloc] init];
}
return self;
}
2019-10-12 14:48:19 +08:00
2019-11-15 14:57:41 +08:00
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
2019-11-19 11:21:49 +08:00
if ([self isKindOfClass:[DoricSuperNode class]]) {
((DoricSuperNode *) self).reusable = superNode.reusable;
}
2019-11-15 14:57:41 +08:00
self.superNode = superNode;
self.view = [[self build] also:^(UIView *it) {
it.layoutConfig = [superNode generateDefaultLayoutParams];
}];
}
- (DoricLayoutConfig *)layoutConfig {
return self.view.layoutConfig;
}
- (UIView *)build {
2019-07-30 15:20:11 +08:00
return [[UIView alloc] init];
}
- (void)blend:(NSDictionary *)props {
self.view.layoutConfig = self.layoutConfig;
2019-07-30 15:20:11 +08:00
for (NSString *key in props) {
id value = props[key];
[self blendView:self.view forPropName:key propValue:value];
}
}
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
2019-10-12 14:48:19 +08:00
if ([name isEqualToString:@"width"]) {
NSNumber *width = (NSNumber *) prop;
if ([width floatValue] >= 0) {
2019-07-31 14:18:20 +08:00
view.width = [width floatValue];
}
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"height"]) {
NSNumber *height = (NSNumber *) prop;
if ([height floatValue] >= 0) {
2019-07-31 14:18:20 +08:00
view.height = [height floatValue];
}
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"x"]) {
view.x = [(NSNumber *) prop floatValue];
} else if ([name isEqualToString:@"y"]) {
view.y = [(NSNumber *) prop floatValue];
} else if ([name isEqualToString:@"bgColor"]) {
2019-07-30 15:20:11 +08:00
view.backgroundColor = DoricColor(prop);
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"layoutConfig"]) {
2019-11-15 14:57:41 +08:00
if (self.superNode && [prop isKindOfClass:[NSDictionary class]]) {
[self.superNode blendSubNode:self layoutConfig:prop];
2019-07-30 15:20:11 +08:00
}
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"onClick"]) {
self.callbackIds[@"onClick"] = prop;
2019-07-31 19:22:00 +08:00
view.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClick:)];
[view addGestureRecognizer:tapGestureRecognizer];
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"border"]) {
NSDictionary *dic = prop;
CGFloat width = [(NSNumber *) dic[@"width"] floatValue];
UIColor *color = DoricColor((NSNumber *) dic[@"color"]);
view.layer.borderWidth = width;
view.layer.borderColor = color.CGColor;
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"corners"]) {
if ([prop isKindOfClass:NSNumber.class]) {
view.layer.cornerRadius = [(NSNumber *) prop floatValue];
} else if ([prop isKindOfClass:NSDictionary.class]) {
2019-08-02 20:24:05 +08:00
NSDictionary *dic = prop;
CGFloat leftTop = [(NSNumber *) dic[@"leftTop"] floatValue];
CGFloat rightTop = [(NSNumber *) dic[@"rightTop"] floatValue];
CGFloat rightBottom = [(NSNumber *) dic[@"rightBottom"] floatValue];
CGFloat leftBottom = [(NSNumber *) dic[@"leftBottom"] floatValue];
2019-08-02 20:24:05 +08:00
CALayer *mask = nil;
2019-10-12 14:48:19 +08:00
if (ABS(leftTop - rightTop) > CGFLOAT_MIN
|| ABS(leftTop - rightBottom) > CGFLOAT_MIN
|| ABS(leftTop - leftBottom) > CGFLOAT_MIN) {
2019-08-02 20:24:05 +08:00
view.layer.cornerRadius = 0;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGPathRef path = DoricCreateRoundedRectPath(self.view.bounds, leftTop, rightTop, rightBottom, leftBottom);
shapeLayer.path = path;
CGPathRelease(path);
mask = shapeLayer;
} else {
view.layer.cornerRadius = leftTop;
}
view.layer.mask = mask;
}
2019-10-12 14:48:19 +08:00
} else if ([name isEqualToString:@"shadow"]) {
NSDictionary *dic = prop;
CGFloat opacity = [(NSNumber *) dic[@"opacity"] floatValue];
if (opacity > CGFLOAT_MIN) {
view.clipsToBounds = NO;
UIColor *color = DoricColor((NSNumber *) dic[@"color"]);
view.layer.shadowColor = color.CGColor;
view.layer.shadowRadius = [(NSNumber *) dic[@"radius"] floatValue];
view.layer.shadowOffset = CGSizeMake([(NSNumber *) dic[@"offsetX"] floatValue], [(NSNumber *) dic[@"offsetY"] floatValue]);
view.layer.shadowOpacity = (float) opacity;
} else {
view.clipsToBounds = YES;
}
} else {
2019-07-30 21:05:27 +08:00
DoricLog(@"Blend View error for View Type :%@, prop is %@", self.class, name);
2019-07-30 15:20:11 +08:00
}
}
2019-07-31 19:22:00 +08:00
- (void)onClick:(UIView *)view {
[self callJSResponse:self.callbackIds[@"onClick"], nil];
2019-07-31 14:18:20 +08:00
}
2019-07-30 21:05:27 +08:00
- (NSArray<NSString *> *)idList {
NSMutableArray *ret = [[NSMutableArray alloc] init];
DoricViewNode *node = self;
do {
[ret addObject:node.viewId];
2019-11-15 14:57:41 +08:00
node = node.superNode;
2019-07-30 21:05:27 +08:00
} while (node && ![node isKindOfClass:[DoricRootNode class]]);
2019-10-12 14:48:19 +08:00
2019-07-31 19:22:00 +08:00
return [[ret reverseObjectEnumerator] allObjects];
2019-07-30 21:05:27 +08:00
}
2019-11-15 19:30:07 +08:00
- (DoricAsyncResult *)callJSResponse:(NSString *)funcId, ... {
2019-07-30 21:05:27 +08:00
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:self.idList];
[array addObject:funcId];
va_list args;
va_start(args, funcId);
id arg;
while ((arg = va_arg(args, id)) != nil) {
[array addObject:arg];
}
2019-11-15 19:30:07 +08:00
DoricAsyncResult *ret = [self.doricContext callEntity:DORIC_ENTITY_RESPONSE withArgumentsArray:array];
2019-07-30 21:05:27 +08:00
va_end(args);
2019-11-15 19:30:07 +08:00
return ret;
2019-07-30 21:05:27 +08:00
}
2019-11-15 19:30:07 +08:00
+ (__kindof DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type {
2019-07-30 21:05:27 +08:00
DoricRegistry *registry = context.driver.registry;
2019-10-12 14:48:19 +08:00
Class clz = [registry acquireViewNode:type];
2019-11-16 13:23:11 +08:00
DoricViewNode *viewNode = [(DoricViewNode *) [clz alloc] initWithContext:context];
viewNode.type = type;
return viewNode;
2019-07-31 14:18:20 +08:00
}
2019-08-06 20:06:34 +08:00
- (void)requestLayout {
2019-11-15 14:57:41 +08:00
[self.superNode requestLayout];
2019-08-06 20:06:34 +08:00
}
2019-11-18 16:33:23 +08:00
- (NSNumber *)getWidth {
return @(self.view.width);
}
- (NSNumber *)getHeight {
return @(self.view.height);
}
2019-07-30 15:20:11 +08:00
@end