BeautySettingPanel.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // BeautySettingPanel.m
  3. // RTMPiOSDemo
  4. //
  5. // Created by rushanting on 2017/5/5.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "BeautySettingPanel.h"
  9. #import "PituMotionAddress.h"
  10. #import "TextCell.h"
  11. #import "AFNetworking.h"
  12. #if POD_PITU
  13. #import <ZipArchive/ZipArchive.h>
  14. #endif
  15. #import "ColorMacro.h"
  16. #define BeautyViewMargin 8
  17. #define BeautyViewSliderHeight 30
  18. #define BeautyViewCollectionHeight 50
  19. #define BeautyViewTitleWidth 40
  20. #define L(x) NSLocalizedString((x), nil)
  21. @interface BeautySettingPanel() <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  22. {
  23. NSArray<NSArray *> *_optionsContainer;
  24. NSMutableDictionary<NSNumber*, NSIndexPath*> *_selectedIndexMap;
  25. NSDictionary *_motionNameMap;
  26. }
  27. @property (nonatomic, assign) PannelMenuIndex currentMenuIndex;
  28. @property (nonatomic, strong) UICollectionView *menuCollectionView;
  29. @property (nonatomic, strong) UICollectionView *optionsCollectionView;
  30. @property (nonatomic, strong) NSMutableDictionary *beautyValueMap;
  31. @property (nonatomic, strong) UILabel *filterLabel;
  32. @property (nonatomic, strong) UISlider *filterSlider;
  33. @property (nonatomic, strong) UILabel *beautyLabel;
  34. @property (nonatomic, strong) UISlider *beautySlider;
  35. @property (nonatomic, strong) NSArray *menuArray;
  36. @property (nonatomic, strong) NSDictionary *motionAddressDic;
  37. @property (nonatomic, strong) NSDictionary *koubeiAddressDic;
  38. @property (nonatomic, strong) NSURLSessionDownloadTask *operation;
  39. @property (nonatomic, assign) CGFloat beautyLevel;
  40. @property (nonatomic, assign) CGFloat whiteLevel;
  41. @property (nonatomic, assign) CGFloat ruddyLevel;
  42. @property (nonatomic, assign) TXVideoBeautyStyle beautyStyle;
  43. @property (nonatomic, strong) NSMutableDictionary* filterMap;
  44. @end
  45. @implementation BeautySettingPanel
  46. - (id)initWithFrame:(CGRect)frame
  47. {
  48. self = [super initWithFrame:frame];
  49. if(self){
  50. [self commonInit];
  51. }
  52. return self;
  53. }
  54. - (void)commonInit
  55. {
  56. self.beautySlider.frame = CGRectMake(BeautyViewMargin * 4, BeautyViewMargin, self.frame.size.width - 10 * BeautyViewMargin - BeautyViewSliderHeight, BeautyViewSliderHeight);
  57. [self addSubview:self.beautySlider];
  58. self.beautySlider.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  59. self.beautyLabel.frame = CGRectMake(self.beautySlider.frame.size.width + self.beautySlider.frame.origin.x + BeautyViewMargin, BeautyViewMargin, BeautyViewSliderHeight, BeautyViewSliderHeight);
  60. self.beautyLabel.layer.cornerRadius = self.beautyLabel.frame.size.width / 2;
  61. self.beautyLabel.layer.masksToBounds = YES;
  62. [self addSubview:self.beautyLabel];
  63. self.beautyLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  64. self.filterSlider.frame = CGRectMake(BeautyViewMargin * 4, BeautyViewMargin, self.frame.size.width - 10 * BeautyViewMargin - BeautyViewSliderHeight, BeautyViewSliderHeight);
  65. self.filterSlider.hidden = YES;
  66. [self addSubview:self.filterSlider];
  67. self.filterSlider.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  68. self.filterLabel.frame = CGRectMake(self.filterSlider.frame.size.width + self.filterSlider.frame.origin.x + BeautyViewMargin, BeautyViewMargin, BeautyViewSliderHeight, BeautyViewSliderHeight);
  69. self.filterLabel.layer.cornerRadius = self.filterLabel.frame.size.width / 2;
  70. self.filterLabel.layer.masksToBounds = YES;
  71. self.filterLabel.hidden = YES;
  72. [self addSubview:self.filterLabel];
  73. self.filterLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  74. _menuArray = @[/*L(@"BeautySettingPanel.FunctionArray1"),*/
  75. /*L(@"BeautySettingPanel.FunctionArray2"),*/
  76. L(@"BeautySettingPanel.FunctionArray3"),
  77. L(@"BeautySettingPanel.FunctionArray4"),
  78. L(@"BeautySettingPanel.FunctionArray5"),
  79. L(@"BeautySettingPanel.FunctionArray6"),
  80. L(@"BeautySettingPanel.FunctionArray7")];
  81. NSArray *effectArray = @[L(@"BeautySettingPanel.EffectArray1"),
  82. L(@"BeautySettingPanel.EffectArray2"),
  83. L(@"BeautySettingPanel.EffectArray3"),
  84. L(@"BeautySettingPanel.EffectArray4"),
  85. L(@"BeautySettingPanel.EffectArray5"),
  86. L(@"BeautySettingPanel.EffectArray6"),
  87. L(@"BeautySettingPanel.EffectArray7"),
  88. L(@"BeautySettingPanel.EffectArray8"),
  89. L(@"BeautySettingPanel.EffectArray9"),
  90. L(@"BeautySettingPanel.EffectArray10"),
  91. L(@"BeautySettingPanel.EffectArray11"),
  92. L(@"BeautySettingPanel.EffectArray12"),
  93. L(@"BeautySettingPanel.EffectArray13"),
  94. L(@"BeautySettingPanel.EffectArray14"),
  95. L(@"BeautySettingPanel.EffectArray15"),
  96. L(@"BeautySettingPanel.EffectArray16"),
  97. L(@"BeautySettingPanel.EffectArray17"),
  98. L(@"BeautySettingPanel.EffectArray18")];
  99. NSArray *beautyArray = @[L(@"BeautySettingPanel.BeautyArray1"),
  100. L(@"BeautySettingPanel.Beauty-Natural"),
  101. L(@"BeautySettingPanel.Beauty-P"),
  102. L(@"BeautySettingPanel.BeautyArray2"),
  103. L(@"BeautySettingPanel.BeautyArray3"),
  104. L(@"BeautySettingPanel.BeautyArray4"),
  105. L(@"BeautySettingPanel.BeautyArray5"),
  106. L(@"BeautySettingPanel.BeautyArray6"),
  107. L(@"BeautySettingPanel.BeautyArray7"),
  108. L(@"BeautySettingPanel.BeautyArray8"),
  109. L(@"BeautySettingPanel.BeautyArray9")];
  110. // NSArray *beautyStyleArray = @[L(@"BeautySettingPanel.BeautyTypeArray1"),
  111. // L(@"BeautySettingPanel.BeautyTypeArray2"),
  112. // L(@"BeautySettingPanel.BeautyTypeArray3")];
  113. NSArray *motionArray = @[L(@"BeautySettingPanel.None"), @"video_boom" , @"video_nihongshu", @"video_3DFace_dogglasses2",
  114. @"video_fengkuangdacall", @"video_Qxingzuo_iOS", @"video_caidai_iOS",
  115. @"video_liuhaifadai", @"video_rainbow", @"video_purplecat",
  116. @"video_huaxianzi", @"video_baby_agetest"];
  117. _motionAddressDic = NSDictionaryOfVariableBindings(video_3DFace_dogglasses2, video_baby_agetest, video_caidai_iOS, video_huaxianzi,
  118. video_liuhaifadai, video_nihongshu, video_rainbow, video_boom, video_fengkuangdacall,
  119. video_purplecat, video_Qxingzuo_iOS);
  120. NSArray *koubeiArray = @[L(@"BeautySettingPanel.None"), @"video_xiaofu"];
  121. _koubeiAddressDic = NSDictionaryOfVariableBindings(video_xiaofu);
  122. NSArray *greenArray = @[L(@"BeautySettingPanel.EffectArray1"), @"goodluck"];
  123. _optionsContainer = @[ beautyArray, effectArray, motionArray, koubeiArray, greenArray];
  124. _selectedIndexMap = [NSMutableDictionary dictionaryWithCapacity:_optionsContainer.count];
  125. self.optionsCollectionView.frame = CGRectMake(0, self.beautySlider.frame.size.height + self.beautySlider.frame.origin.y + BeautyViewMargin, self.frame.size.width, BeautyViewSliderHeight * 2 + 2 * BeautyViewMargin);
  126. self.optionsCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  127. [self addSubview:self.optionsCollectionView];
  128. self.menuCollectionView.frame = CGRectMake(0, self.optionsCollectionView.frame.size.height + self.optionsCollectionView.frame.origin.y, self.frame.size.width, BeautyViewCollectionHeight);
  129. self.menuCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  130. [self addSubview:self.menuCollectionView];
  131. }
  132. #pragma mark - collection
  133. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  134. {
  135. return 1;
  136. }
  137. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  138. {
  139. if (collectionView == self.menuCollectionView) {
  140. return self.menuArray.count;
  141. }
  142. return [_optionsContainer[_currentMenuIndex] count];
  143. }
  144. - (NSIndexPath *)selectedIndexPath {
  145. return [self selectedIndexPathForMenu:_currentMenuIndex];
  146. }
  147. - (NSIndexPath *)selectedIndexPathForMenu:(PannelMenuIndex)index {
  148. return _selectedIndexMap[@(index)] ?: [NSIndexPath indexPathForItem:0 inSection:0];
  149. }
  150. - (void)setSelectedIndexPath:(NSIndexPath *)indexPath {
  151. [self setSelectedIndexPath:indexPath forMenu:_currentMenuIndex];
  152. }
  153. - (void)setSelectedIndexPath:(NSIndexPath *)indexPath forMenu:(PannelMenuIndex)menuIndex {
  154. _selectedIndexMap[@(menuIndex)] = indexPath;
  155. }
  156. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  157. {
  158. if(collectionView == _menuCollectionView){
  159. TextCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[TextCell reuseIdentifier] forIndexPath:indexPath];
  160. cell.label.font = [UIFont systemFontOfSize: [UIFont buttonFontSize]];
  161. cell.label.text = self.menuArray[indexPath.row];
  162. cell.selected = indexPath.row == _currentMenuIndex;
  163. return cell;
  164. } else {
  165. TextCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[TextCell reuseIdentifier] forIndexPath:indexPath];
  166. cell.label.font = [UIFont systemFontOfSize: [UIFont buttonFontSize]];
  167. NSString *text = [self textAtIndex:indexPath.row inMenu:_currentMenuIndex];
  168. cell.label.text = text;
  169. cell.selected = [indexPath isEqual: [self selectedIndexPath]];
  170. return cell;
  171. }
  172. }
  173. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  174. {
  175. TextCell *cell = (TextCell *)[collectionView cellForItemAtIndexPath:indexPath];
  176. [cell setSelected:YES];
  177. if(collectionView == _menuCollectionView){
  178. if(indexPath.row != _currentMenuIndex){
  179. [self changeFunction:indexPath.row];
  180. // if([self.delegate respondsToSelector:@selector(reset:)]){
  181. // [self.delegate reset:(indexPath.row == 0? YES : NO)];
  182. // }
  183. }
  184. } else {
  185. // select options
  186. NSIndexPath *prevSelectedIndexPath = [self selectedIndexPath];
  187. [collectionView cellForItemAtIndexPath:prevSelectedIndexPath].selected = NO;
  188. if([indexPath isEqual:prevSelectedIndexPath]){
  189. // 和上次选的一样
  190. return;
  191. }
  192. [self setSelectedIndexPath:indexPath];
  193. switch (_currentMenuIndex) {
  194. case PannelMenuIndexBeauty: {
  195. float value = [[self.beautyValueMap objectForKey:[NSNumber numberWithInteger:indexPath.row]] floatValue];
  196. if (indexPath.row < 3) {
  197. self.beautyStyle = indexPath.item;
  198. _beautyLevel = value;
  199. }
  200. if(indexPath.row == 8){
  201. //下巴
  202. self.beautySlider.minimumValue = -10;
  203. self.beautySlider.maximumValue = 10;
  204. } else {
  205. self.beautySlider.minimumValue = 0;
  206. self.beautySlider.maximumValue = 10;
  207. }
  208. self.beautyLabel.text = [NSString stringWithFormat:@"%d",(int)value];
  209. [self.beautySlider setValue:value];
  210. [self _applyBeautySettings];
  211. } break;
  212. case PannelMenuIndexEffect: {
  213. [self onSetEffectWithIndex:indexPath.row];
  214. NSNumber* value = [self.filterMap objectForKey:@(indexPath.row)];
  215. [self.filterSlider setValue:value.floatValue];
  216. [self onValueChanged:self.filterSlider];
  217. }
  218. break;
  219. case PannelMenuIndexMotion:
  220. [self onSetMotionWithIndex:indexPath.row];
  221. break;
  222. case PannelMenuIndexKoubei:
  223. [self onSetKoubeiWithIndex:indexPath.row];
  224. break;
  225. case PannelMenuIndexGreen:
  226. [self onSetGreenWithIndex:indexPath.row];
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. }
  233. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  234. {
  235. NSString *text = nil;
  236. if(collectionView == _menuCollectionView){
  237. text = self.menuArray[indexPath.row];
  238. } else {
  239. text = [self textAtIndex:indexPath.row inMenu:_currentMenuIndex];;
  240. }
  241. UIFont *font = [UIFont systemFontOfSize: [UIFont buttonFontSize]];
  242. NSDictionary *attrs = @{NSFontAttributeName : font};
  243. CGSize size=[text sizeWithAttributes:attrs];
  244. return CGSizeMake(size.width + 2 * BeautyViewMargin, collectionView.frame.size.height);
  245. }
  246. #pragma mark - layout
  247. - (void)changeFunction:(PannelMenuIndex)index
  248. {
  249. self.beautyLabel.hidden = index != PannelMenuIndexBeauty;
  250. self.beautySlider.hidden = self.beautyLabel.hidden;
  251. self.filterLabel.hidden = index != PannelMenuIndexEffect;
  252. self.filterSlider.hidden = self.filterLabel.hidden;
  253. NSAssert(index < _optionsContainer.count, @"index out of range");
  254. [self.menuCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_currentMenuIndex inSection:0]].selected = NO;
  255. _currentMenuIndex = index;
  256. [self.optionsCollectionView reloadData];
  257. }
  258. - (void)_applyBeautySettings {
  259. if([self.delegate respondsToSelector:@selector(onSetBeautyStyle:beautyLevel:whitenessLevel:ruddinessLevel:)]){
  260. [self.delegate onSetBeautyStyle:self.beautyStyle beautyLevel:_beautyLevel whitenessLevel:_whiteLevel ruddinessLevel:_ruddyLevel];
  261. }
  262. }
  263. #pragma mark - value changed
  264. - (void)onValueChanged:(id)sender
  265. {
  266. UISlider *slider = (UISlider *)sender;
  267. float value = slider.value;
  268. if(slider == self.filterSlider){
  269. [self.filterMap setObject:[NSNumber numberWithFloat:self.filterSlider.value] forKey:[NSNumber numberWithInteger:[self selectedIndexPath].row]];
  270. self.filterLabel.text = [NSString stringWithFormat:@"%d",(int)self.filterSlider.value];
  271. if([self.delegate respondsToSelector:@selector(onSetMixLevel:)]){
  272. [self.delegate onSetMixLevel:self.filterSlider.value];
  273. }
  274. } else {
  275. // 判断选择了哪个二级菜单
  276. int beautyIndex = (int)[self selectedIndexPathForMenu:PannelMenuIndexBeauty].row;
  277. [self.beautyValueMap setObject:[NSNumber numberWithFloat:self.beautySlider.value] forKey:@(beautyIndex)];
  278. self.beautyLabel.text = [NSString stringWithFormat:@"%d",(int)self.beautySlider.value];
  279. if(beautyIndex < 5) {
  280. if (beautyIndex < 3) {
  281. _beautyLevel = slider.value;
  282. } else if (beautyIndex == 3) {
  283. _whiteLevel = value;
  284. } else if (beautyIndex == 4) {
  285. _ruddyLevel = value;
  286. }
  287. [self _applyBeautySettings];
  288. }
  289. if(beautyIndex == 5) {
  290. if([self.delegate respondsToSelector:@selector(onSetEyeScaleLevel:)]){
  291. [self.delegate onSetEyeScaleLevel:value];
  292. }
  293. }
  294. else if(beautyIndex == 6){
  295. if([self.delegate respondsToSelector:@selector(onSetFaceScaleLevel:)]){
  296. [self.delegate onSetFaceScaleLevel:value];
  297. }
  298. }
  299. // else if(beautyIndex == 5){
  300. // if([self.delegate respondsToSelector:@selector(onSetFaceBeautyLevel:)]){
  301. // [self.delegate onSetFaceBeautyLevel:self.beautySlider.value];
  302. // }
  303. // }
  304. else if(beautyIndex == 7){
  305. if([self.delegate respondsToSelector:@selector(onSetFaceVLevel:)]){
  306. [self.delegate onSetFaceVLevel:value];
  307. }
  308. }
  309. else if(beautyIndex == 8){
  310. if([self.delegate respondsToSelector:@selector(onSetChinLevel:)]){
  311. [self.delegate onSetChinLevel:value];
  312. }
  313. }
  314. else if(beautyIndex == 9){
  315. if([self.delegate respondsToSelector:@selector(onSetFaceShortLevel:)]){
  316. [self.delegate onSetFaceShortLevel:value];
  317. }
  318. }
  319. else if(beautyIndex == 10){
  320. if([self.delegate respondsToSelector:@selector(onSetNoseSlimLevel:)]){
  321. [self.delegate onSetNoseSlimLevel:value];
  322. }
  323. }
  324. }
  325. }
  326. - (void)onSetEffectWithIndex:(NSInteger)index
  327. {
  328. if ([self.delegate respondsToSelector:@selector(onSetFilter:)]) {
  329. UIImage* image = [self filterImageByIndex:index];
  330. [self.delegate onSetFilter:image];
  331. }
  332. }
  333. - (UIImage*)filterImageByIndex:(NSInteger)index
  334. {
  335. NSString* lookupFileName = @"";
  336. if (index < 0)
  337. index = _optionsContainer[1].count - 1;
  338. if (index > _optionsContainer[1].count - 1)
  339. index = 0;
  340. switch (index) {
  341. case 0:
  342. break;
  343. case 1:
  344. lookupFileName = @"biaozhun.png";
  345. break;
  346. case 2:
  347. lookupFileName = @"yinghong.png";
  348. break;
  349. case 3:
  350. lookupFileName = @"yunshang.png";
  351. break;
  352. case 4:
  353. lookupFileName = @"chunzhen.png";
  354. break;
  355. case 5:
  356. lookupFileName = @"bailan.png";
  357. break;
  358. case 6:
  359. lookupFileName = @"yuanqi.png";
  360. break;
  361. case 7:
  362. lookupFileName = @"chaotuo.png";
  363. break;
  364. case 8:
  365. lookupFileName = @"xiangfen.png";
  366. break;
  367. case 9:
  368. lookupFileName = @"white.png";
  369. break;
  370. case 10:
  371. lookupFileName = @"langman.png";
  372. break;
  373. case 11:
  374. lookupFileName = @"qingxin.png";
  375. break;
  376. case 12:
  377. lookupFileName = @"weimei.png";
  378. break;
  379. case 13:
  380. lookupFileName = @"fennen.png";
  381. break;
  382. case 14:
  383. lookupFileName = @"huaijiu.png";
  384. break;
  385. case 15:
  386. lookupFileName = @"landiao.png";
  387. break;
  388. case 16:
  389. lookupFileName = @"qingliang.png";
  390. break;
  391. case 17:
  392. lookupFileName = @"rixi.png";
  393. break;
  394. default:
  395. break;
  396. }
  397. NSString * path = [[NSBundle mainBundle] pathForResource:@"FilterResource" ofType:@"bundle"];
  398. if (path != nil && index != FilterType_None) {
  399. path = [path stringByAppendingPathComponent:lookupFileName];
  400. return [UIImage imageWithContentsOfFile:path];
  401. }
  402. return nil;
  403. }
  404. - (NSInteger)currentFilterIndex
  405. {
  406. return _selectedIndexMap[@(1)].row;
  407. }
  408. - (NSString*)currentFilterName
  409. {
  410. NSInteger index = self.currentFilterIndex;
  411. return _optionsContainer[1][index];
  412. }
  413. - (void)setCurrentFilterIndex:(NSInteger)currentFilterIndex
  414. {
  415. if (currentFilterIndex < 0)
  416. currentFilterIndex = _optionsContainer[1].count - 1;
  417. if (currentFilterIndex >= _optionsContainer[1].count)
  418. currentFilterIndex = 0;
  419. NSIndexPath* indexPath = [NSIndexPath indexPathForRow:currentFilterIndex inSection:0];
  420. _selectedIndexMap[@(1)] = indexPath;
  421. // self.selectEffectIndexPath = indexPath;
  422. // [self.effectCollectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
  423. // [self onValueChanged:self.filterSlider];
  424. // [self collectionView:self.effectCollectionView didSelectItemAtIndexPath:indexPath];
  425. [self changeFunction:PannelMenuIndexEffect];
  426. }
  427. -(float)filterMixLevelByIndex:(NSInteger)index
  428. {
  429. if (index < 0)
  430. index = self.filterMap.count - 1;
  431. if (index > self.filterMap.count - 1)
  432. index = 0;
  433. return ((NSNumber*)[self.filterMap objectForKey:@(index)]).floatValue;
  434. }
  435. - (void)onSetGreenWithIndex:(NSInteger)index
  436. {
  437. if ([self.delegate respondsToSelector:@selector(onSetGreenScreenFile:)]) {
  438. if (index == 0) {
  439. [self.delegate onSetGreenScreenFile:nil];
  440. }
  441. if (index == 1) {
  442. [self.delegate onSetGreenScreenFile:[[NSBundle mainBundle] URLForResource:@"goodluck" withExtension:@"mp4"]];
  443. }
  444. }
  445. }
  446. - (void)onSetMotionWithIndex:(NSInteger)index
  447. {
  448. if ([self.delegate respondsToSelector:@selector(onSelectMotionTmpl:inDir:)]) {
  449. NSString *localPackageDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/packages"];
  450. if (![[NSFileManager defaultManager] fileExistsAtPath:localPackageDir]) {
  451. [[NSFileManager defaultManager] createDirectoryAtPath:localPackageDir withIntermediateDirectories:NO attributes:nil error:nil];
  452. }
  453. if (index == 0){
  454. [self.delegate onSelectMotionTmpl:nil inDir:localPackageDir];
  455. }
  456. else{
  457. NSArray *motionAray = _optionsContainer[PannelMenuIndexMotion];
  458. NSString *tmp = [motionAray objectAtIndex:index];
  459. NSString *pituPath = [NSString stringWithFormat:@"%@/%@", localPackageDir, tmp];
  460. if ([[NSFileManager defaultManager] fileExistsAtPath:pituPath]) {
  461. [self.delegate onSelectMotionTmpl:tmp inDir:localPackageDir];
  462. }else{
  463. [self startLoadPitu:localPackageDir pituName:tmp packageURL:[NSURL URLWithString:[_motionAddressDic objectForKey:tmp]]];
  464. }
  465. }
  466. }
  467. }
  468. - (void)onSetKoubeiWithIndex:(NSInteger)index
  469. {
  470. if ([self.delegate respondsToSelector:@selector(onSelectMotionTmpl:inDir:)]) {
  471. NSString *localPackageDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/packages"];
  472. if (![[NSFileManager defaultManager] fileExistsAtPath:localPackageDir]) {
  473. [[NSFileManager defaultManager] createDirectoryAtPath:localPackageDir withIntermediateDirectories:NO attributes:nil error:nil];
  474. }
  475. if (index == 0){
  476. [self.delegate onSelectMotionTmpl:nil inDir:localPackageDir];
  477. }
  478. else{
  479. NSArray *koubeiArray = _optionsContainer[PannelMenuIndexKoubei];
  480. NSString *tmp = [koubeiArray objectAtIndex:index];
  481. NSString *pituPath = [NSString stringWithFormat:@"%@/%@", localPackageDir, tmp];
  482. if ([[NSFileManager defaultManager] fileExistsAtPath:pituPath]) {
  483. [self.delegate onSelectMotionTmpl:tmp inDir:localPackageDir];
  484. }else{
  485. [self startLoadPitu:localPackageDir pituName:tmp packageURL:[NSURL URLWithString:[_koubeiAddressDic objectForKey:tmp]]];
  486. }
  487. }
  488. }
  489. }
  490. - (void)startLoadPitu:(NSString *)pituDir pituName:(NSString *)pituName packageURL:(NSURL *)packageURL{
  491. #if POD_PITU
  492. if (self.operation) {
  493. if (self.operation.state != NSURLSessionTaskStateRunning) {
  494. [self.operation resume];
  495. }
  496. }
  497. NSString *targetPath = [NSString stringWithFormat:@"%@/%@.zip", pituDir, pituName];
  498. if ([[NSFileManager defaultManager] fileExistsAtPath:targetPath]) {
  499. [[NSFileManager defaultManager] removeItemAtPath:targetPath error:nil];
  500. }
  501. __weak __typeof(self) weakSelf = self;
  502. NSURLRequest *downloadReq = [NSURLRequest requestWithURL:packageURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.f];
  503. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  504. __weak AFHTTPSessionManager *weakManager = manager;
  505. [self.pituDelegate onLoadPituStart];
  506. self.operation = [manager downloadTaskWithRequest:downloadReq progress:^(NSProgress * _Nonnull downloadProgress) {
  507. if (weakSelf.pituDelegate) {
  508. CGFloat progress = (float)downloadProgress.completedUnitCount / (float)downloadProgress.totalUnitCount;
  509. [weakSelf.pituDelegate onLoadPituProgress:progress];
  510. }
  511. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath_, NSURLResponse * _Nonnull response) {
  512. return [NSURL fileURLWithPath:targetPath];
  513. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  514. [weakManager invalidateSessionCancelingTasks:YES];
  515. if (error) {
  516. [weakSelf.pituDelegate onLoadPituFailed];
  517. return;
  518. }
  519. // 解压
  520. BOOL unzipSuccess = NO;
  521. ZipArchive *zipArchive = [[ZipArchive alloc] init];
  522. if ([zipArchive UnzipOpenFile:targetPath]) {
  523. unzipSuccess = [zipArchive UnzipFileTo:pituDir overWrite:YES];
  524. [zipArchive UnzipCloseFile];
  525. // 删除zip文件
  526. [[NSFileManager defaultManager] removeItemAtPath:targetPath error:&error];
  527. }
  528. if (unzipSuccess) {
  529. [weakSelf.pituDelegate onLoadPituFinished];
  530. [weakSelf.delegate onSelectMotionTmpl:pituName inDir:pituDir];
  531. } else {
  532. [weakSelf.pituDelegate onLoadPituFailed];
  533. }
  534. }];
  535. [self.operation resume];
  536. #endif
  537. }
  538. #pragma mark - height
  539. + (NSUInteger)getHeight
  540. {
  541. return BeautyViewMargin * 4 + 3 * BeautyViewSliderHeight + BeautyViewCollectionHeight;
  542. }
  543. #pragma mark - Translator
  544. - (NSString *)textAtIndex:(NSInteger)index inMenu:(PannelMenuIndex)menuIndex {
  545. NSString *text = _optionsContainer[menuIndex][index];
  546. if (menuIndex == PannelMenuIndexMotion || menuIndex == PannelMenuIndexKoubei) {
  547. text = [self getMotionName:text];
  548. }
  549. return text;
  550. }
  551. - (NSString *)getMotionName:(NSString *)motion
  552. {
  553. if (_motionNameMap == nil) {
  554. _motionNameMap = @{
  555. @"video_boom": L(@"BeautySettingPanel.MotionName1"),
  556. @"video_nihongshu": L(@"BeautySettingPanel.MotionName2"),
  557. @"video_3DFace_dogglasses2": L(@"BeautySettingPanel.MotionName3"),
  558. @"video_fengkuangdacall": L(@"BeautySettingPanel.MotionName4"),
  559. @"video_Qxingzuo_iOS" : L(@"BeautySettingPanel.MotionName5"),
  560. @"video_caidai_iOS" : L(@"BeautySettingPanel.MotionName6"),
  561. @"video_liuhaifadai" : L(@"BeautySettingPanel.MotionName7"),
  562. @"video_rainbow": L(@"BeautySettingPanel.MotionName8"),
  563. @"video_purplecat": L(@"BeautySettingPanel.MotionName9"),
  564. @"video_huaxianzi": L(@"BeautySettingPanel.MotionName10"),
  565. @"video_baby_agetest": L(@"BeautySettingPanel.MotionName11"),
  566. @"video_xiaofu": L(@"BeautySettingPanel.MotionName12")
  567. };
  568. }
  569. return _motionNameMap[motion] ?: motion;
  570. }
  571. - (NSMutableDictionary *)beautyValueMap
  572. {
  573. if(!_beautyValueMap){
  574. _beautyValueMap = [[NSMutableDictionary alloc] init];
  575. }
  576. return _beautyValueMap;
  577. }
  578. - (UICollectionView *)optionsCollectionView {
  579. if (_optionsCollectionView == nil) {
  580. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  581. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  582. _optionsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  583. _optionsCollectionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
  584. _optionsCollectionView.showsHorizontalScrollIndicator = NO;
  585. _optionsCollectionView.delegate = self;
  586. _optionsCollectionView.dataSource = self;
  587. [_optionsCollectionView registerClass:[TextCell class] forCellWithReuseIdentifier:[TextCell reuseIdentifier]];
  588. }
  589. return _optionsCollectionView;
  590. }
  591. - (UICollectionView *)menuCollectionView
  592. {
  593. if(!_menuCollectionView){
  594. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  595. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  596. // layout.itemSize = CGSizeMake(100, 40);
  597. _menuCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  598. _menuCollectionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
  599. _menuCollectionView.showsHorizontalScrollIndicator = NO;
  600. _menuCollectionView.delegate = self;
  601. _menuCollectionView.dataSource = self;
  602. [_menuCollectionView registerClass:[TextCell class] forCellWithReuseIdentifier:[TextCell reuseIdentifier]];
  603. }
  604. return _menuCollectionView;
  605. }
  606. - (UISlider *)beautySlider
  607. {
  608. if(!_beautySlider){
  609. _beautySlider = [[UISlider alloc] init];
  610. _beautySlider.minimumValue = 0;
  611. _beautySlider.maximumValue = 10;
  612. [_beautySlider setMinimumTrackTintColor:RGB(235, 100, 86)];
  613. [_beautySlider setMaximumTrackTintColor:RGB(166, 166, 165)];
  614. [_beautySlider setThumbImage:[UIImage imageNamed:@"beauty_slider"] forState:UIControlStateNormal];
  615. [_beautySlider addTarget:self action:@selector(onValueChanged:) forControlEvents:UIControlEventValueChanged];
  616. }
  617. return _beautySlider;
  618. }
  619. - (UILabel *)beautyLabel
  620. {
  621. if(!_beautyLabel){
  622. _beautyLabel = [[UILabel alloc] init];
  623. _beautyLabel.backgroundColor = [UIColor whiteColor];
  624. _beautyLabel.textAlignment = NSTextAlignmentCenter;
  625. _beautyLabel.text = @"0";
  626. [_beautyLabel setTextColor:UIColorFromRGB(0xFF584C)];
  627. }
  628. return _beautyLabel;
  629. }
  630. - (UISlider *)filterSlider
  631. {
  632. if(!_filterSlider){
  633. _filterSlider = [[UISlider alloc] init];
  634. _filterSlider.minimumValue = 0;
  635. _filterSlider.maximumValue = 10;
  636. [_filterSlider setMinimumTrackTintColor:RGB(235, 100, 86)];
  637. [_filterSlider setMaximumTrackTintColor:RGB(166, 166, 165)];
  638. [_filterSlider setThumbImage:[UIImage imageNamed:@"beauty_slider"] forState:UIControlStateNormal];
  639. [_filterSlider addTarget:self action:@selector(onValueChanged:) forControlEvents:UIControlEventValueChanged];
  640. }
  641. return _filterSlider;
  642. }
  643. - (UILabel *)filterLabel
  644. {
  645. if(!_filterLabel){
  646. _filterLabel = [[UILabel alloc] init];
  647. _filterLabel.backgroundColor = [UIColor whiteColor];
  648. _filterLabel.textAlignment = NSTextAlignmentCenter;
  649. _filterLabel.text = @"0";
  650. [_filterLabel setTextColor:UIColorFromRGB(0xFF584C)];
  651. }
  652. return _filterLabel;
  653. }
  654. - (void)resetValues
  655. {
  656. self.beautySlider.hidden = NO;
  657. self.beautyLabel.hidden = NO;
  658. self.filterSlider.hidden = YES;
  659. self.filterLabel.hidden = YES;
  660. self.menuCollectionView.hidden = NO;
  661. [_selectedIndexMap removeAllObjects];
  662. [self onSetMotionWithIndex:0];
  663. [self onSetKoubeiWithIndex:0];
  664. [self onSetGreenWithIndex:0];
  665. [self.beautyValueMap removeAllObjects];
  666. [self.beautyValueMap setObject:@(6) forKey:@(1)]; //美颜默认值(自然)
  667. [self.beautyValueMap setObject:@(5) forKey:@(2)]; //美颜默认值(天天PITU)
  668. [self.beautyValueMap setObject:@(1) forKey:@(3)]; //美白默认值
  669. self.filterMap = [NSMutableDictionary new];
  670. [self.filterMap setObject:@(0) forKey:@(0)];
  671. [self.filterMap setObject:@(5) forKey:@(1)];
  672. [self.filterMap setObject:@(8) forKey:@(2)];
  673. [self.filterMap setObject:@(8) forKey:@(3)];
  674. [self.filterMap setObject:@(7) forKey:@(4)];
  675. [self.filterMap setObject:@(10) forKey:@(5)];
  676. [self.filterMap setObject:@(8) forKey:@(6)];
  677. [self.filterMap setObject:@(10) forKey:@(7)];
  678. [self.filterMap setObject:@(5) forKey:@(8)];
  679. [self.filterMap setObject:@(3) forKey:@(9)];
  680. [self.filterMap setObject:@(3) forKey:@(10)];
  681. [self.filterMap setObject:@(3) forKey:@(11)];
  682. [self.filterMap setObject:@(3) forKey:@(12)];
  683. [self.filterMap setObject:@(3) forKey:@(13)];
  684. [self.filterMap setObject:@(3) forKey:@(14)];
  685. [self.filterMap setObject:@(3) forKey:@(15)];
  686. [self.filterMap setObject:@(3) forKey:@(16)];
  687. [self.filterMap setObject:@(3) forKey:@(17)];
  688. _whiteLevel = 1;
  689. _beautyLevel = 6;
  690. _ruddyLevel = 0;
  691. self.beautySlider.minimumValue = 0;
  692. self.beautySlider.maximumValue = 10;
  693. float value = 6.3;;
  694. self.beautyLabel.text = [NSString stringWithFormat:@"%d",(int)value];
  695. self.beautySlider.value = value;
  696. self.filterSlider.value = 5;
  697. self.currentMenuIndex = PannelMenuIndexBeauty;
  698. [self onValueChanged:self.beautySlider];
  699. // [self onValueChanged:self.filterSlider];
  700. [self onSetEffectWithIndex:1];
  701. _selectedIndexMap[@(1)] = [NSIndexPath indexPathForRow:1 inSection:0];
  702. }
  703. @end