BDFaceAdjustParamsController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // BDFaceAdjustParamsController.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by Zhang,Jian(MBD) on 2020/12/1.
  6. // Copyright © 2020 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceAdjustParamsController.h"
  9. #import "BDFaceAdjustParamsCell.h"
  10. #import "BDFaceAdjustParamsItem.h"
  11. #import "BDFaceAdjustParamsModel.h"
  12. #import "BDFaceAdjustParams.h"
  13. #import "UIColor+BDFaceColorUtils.h"
  14. #import "BDFaceAdjustParamsConstants.h"
  15. #import "BDFaceAdjustParamsFileManager.h"
  16. #import "BDFaceAdjustParamsRootCell.h"
  17. #import "BDFaceAlertController.h"
  18. #import "BDFaceAdjustParamsTool.h"
  19. static NSString *const BDFaceAdjustParamsControllerTitle = @"%@参数配置";
  20. static NSString *const BDFaceAdjustParamsRecoverText = @"恢复为%@参数配置";
  21. static float const BDFaceAdjustParamsSaveButtonWidth = 80.0f;
  22. static float const BDFaceAdjustParamsSaveButtonHeight = 40.0f;
  23. static float const BDFaceAdjustParamsSaveButtonRightMargin = 10.0f;
  24. static NSString *const BDFaceAdjustParamsSaveButtonText = @"保存";
  25. static NSString *const BDFaceAdjustParamsAlertTitle = @"是否保存修改";
  26. static NSString *const BDFaceAdjustParamsAlertContent = @"参数配置已修改,是否保存后离开";
  27. static NSString *const BDFaceAdjustParamsAlertJustLeave = @"直接离开";
  28. static NSString *const BDFaceAdjustParamsAlertSaveAndLeave = @"保存并离开";
  29. static NSString *const BDFaceAdjustParamsAlertCancel = @"取消";
  30. static NSString *const BDFaceAdjustParamsAlertSaveCustomConfig = @"保存自定义";
  31. static NSString *const BDFaceAdjustParamsAlertRecoverToDefaultTip = @"参数配置已修改,是否恢复默认";
  32. static NSString *const BDFaceAdjustParamsAlertRecoverToDefault = @"恢复默认";
  33. static float const BBDFaceAjustConfigTableViewOriginY = 10.0f;
  34. static float const BDFaceSelectConfigTableMargin = 20.0f;
  35. static float const BDFaceSelectConfigTableViewHeight = 24.0f;
  36. static NSString *const BDFaceAdjustParamsSection1Tip = @"数值越小越严格";
  37. static NSString *const BDFaceAdjustParamsSection2Tip = @"姿态阀值(绝对值,单位:度)";
  38. @interface BDFaceAdjustParamsController ()
  39. @property(nonatomic, strong) BDFaceAdjustParams *configInital;
  40. @property(nonatomic, strong) BDFaceAdjustParams *currentConfig;
  41. @property(nonatomic, strong) NSMutableArray *allData;
  42. @property(nonatomic, assign) BDFaceSelectType selectType;
  43. @property(nonatomic, assign) BDFaceSelectType initialSelect;
  44. @property(nonatomic, strong) UIButton *saveButton;
  45. @property(nonatomic, copy) NSString *selectTypeString;
  46. @property(nonatomic, copy) NSString *recoverText;
  47. @property(nonatomic, assign) BOOL isSameConfig;
  48. @property(nonatomic, weak) BDFaceAdjustParamsCell *recoverCell;
  49. @end
  50. @implementation BDFaceAdjustParamsController
  51. - (instancetype)initWithConfig:(BDFaceAdjustParams *)config {
  52. self = [super init];
  53. if (self) {
  54. self.configInital = [config copy];
  55. self.currentConfig = [config copy];
  56. self.selectType = [BDFaceAdjustParamsFileManager sharedInstance].selectType;
  57. self.initialSelect = self.selectType;
  58. self.selectTypeString = [BDFaceAdjustParamsModel getSelectTypeString:self.selectType];
  59. self.recoverText = [NSString stringWithFormat:BDFaceAdjustParamsRecoverText, self.selectTypeString];
  60. self.isSameConfig = YES;
  61. }
  62. return self;
  63. }
  64. - (void)viewDidLoad {
  65. [super viewDidLoad];
  66. // Do any additional setup after loading the view.
  67. self.allData = [BDFaceAdjustParamsModel loadItemsArray:self.currentConfig recorverText:self.recoverText selectType:self.selectType];
  68. [self loadTableWithCellClass:[BDFaceAdjustParamsCell class] reuseLabel:NSStringFromClass([BDFaceAdjustParamsCell class]) dataSourceArray:self.allData];
  69. CGRect tableRect = self.tableView.frame;
  70. tableRect.origin.x = BDFaceAdjustConfigTableMargin;
  71. tableRect.size.width = CGRectGetWidth(self.view.frame) - BDFaceAdjustConfigTableMargin * 2.0f;
  72. self.tableView.frame = tableRect;
  73. self.tableView.showsVerticalScrollIndicator = NO;
  74. self.titleLabel.text = [NSString stringWithFormat:BDFaceAdjustParamsControllerTitle, self.selectTypeString];
  75. [self loadSaveButton];
  76. }
  77. - (void)loadSaveButton {
  78. self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
  79. self.saveButton.frame = CGRectMake(CGRectGetWidth(self.titleView.frame) - BDFaceAdjustParamsSaveButtonRightMargin - BDFaceAdjustParamsSaveButtonWidth, (CGRectGetHeight(self.titleView.frame) - BDFaceAdjustParamsSaveButtonHeight) / 2.0f, BDFaceAdjustParamsSaveButtonWidth, BDFaceAdjustParamsSaveButtonHeight);
  80. [self.titleView addSubview:self.saveButton];
  81. [self.saveButton setTitle:BDFaceAdjustParamsSaveButtonText forState:UIControlStateNormal];
  82. [self updateSaveButtonTextColor];
  83. [self.saveButton addTarget:self action:@selector(userClickSave) forControlEvents:UIControlEventTouchUpInside];
  84. }
  85. /// 用户点击保存按钮
  86. - (void)userClickSave {
  87. if (self.isSameConfig) {
  88. [self.navigationController popViewControllerAnimated:YES];
  89. } else {
  90. __weak typeof(self) this = self;
  91. BDFaceAlertController *alert = [BDFaceAlertController alertControllerWithTitle:BDFaceAdjustParamsAlertTitle message:BDFaceAdjustParamsAlertContent preferredStyle:UIAlertControllerStyleAlert];
  92. UIAlertAction *saveAction = [UIAlertAction actionWithTitle:BDFaceAdjustParamsAlertSaveCustomConfig style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  93. [this saveConfig];
  94. }];
  95. [alert addAction:saveAction];
  96. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BDFaceAdjustParamsAlertCancel style:UIAlertActionStyleCancel handler:nil];
  97. [alert changeTextToBold:BDFaceAdjustParamsAlertSaveCustomConfig];
  98. [alert changeTextToLight:@[BDFaceAdjustParamsAlertCancel]];
  99. [alert addAction:cancelAction];
  100. [self presentViewController:alert animated:YES completion:nil];
  101. }
  102. }
  103. - (void)saveConfig {
  104. [BDFaceAdjustParamsFileManager sharedInstance].selectType = BDFaceSelectTypeCustom;
  105. [BDFaceAdjustParamsTool changeConfig:self.currentConfig];
  106. [[BDFaceAdjustParamsFileManager sharedInstance] saveToCustomConfigFile:self.currentConfig];
  107. [[NSNotificationCenter defaultCenter] postNotificationName:BDFaceAdjustParamsUserDidFinishSavedConfigNotification object:nil];
  108. [self.navigationController popViewControllerAnimated:YES];
  109. }
  110. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  111. BDFaceAdjustParamsCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
  112. if ([theCell isKindOfClass:[BDFaceAdjustParamsCell class]]) {
  113. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)theCell.data;
  114. if (item.contentType == BDFaceAdjustParamsContentTypeRecoverToNormal) {
  115. if (self.isSameConfig) {
  116. // do nothing
  117. } else {
  118. __weak typeof(self) this = self;
  119. BDFaceAlertController *alert = [BDFaceAlertController alertControllerWithTitle:[NSString stringWithFormat:@"是否%@", item.itemTitle] message:BDFaceAdjustParamsAlertRecoverToDefaultTip preferredStyle:UIAlertControllerStyleAlert];
  120. UIAlertAction *saveAction = [UIAlertAction actionWithTitle:BDFaceAdjustParamsAlertRecoverToDefault style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  121. [this recoverToInital];
  122. }];
  123. [alert addAction:saveAction];
  124. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BDFaceAdjustParamsAlertCancel style:UIAlertActionStyleCancel handler:nil];
  125. [alert changeTextToBold:BDFaceAdjustParamsAlertRecoverToDefault];
  126. [alert changeTextToLight:@[BDFaceAdjustParamsAlertCancel]];
  127. [alert addAction:cancelAction];
  128. [self presentViewController:alert animated:YES completion:nil];
  129. }
  130. }
  131. }
  132. }
  133. /// 恢复为默认值
  134. - (void)recoverToInital {
  135. self.selectType = self.initialSelect;
  136. BDFaceAdjustParams *config = [[BDFaceAdjustParamsFileManager sharedInstance] configBySelection:self.selectType];
  137. self.configInital = [config copy];
  138. self.currentConfig = [config copy];
  139. self.isSameConfig = YES;
  140. [self updateSaveButtonTextColor];
  141. [self.allData removeAllObjects];
  142. NSMutableArray *array = [BDFaceAdjustParamsModel loadItemsArray:self.currentConfig recorverText:self.recoverText selectType:self.selectType];
  143. [self.allData addObjectsFromArray:array];
  144. [self.tableView reloadData];
  145. dispatch_after(0.5, dispatch_get_main_queue(), ^{
  146. [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
  147. });
  148. }
  149. - (UITableViewStyle)customTableViewStyle {
  150. return UITableViewStyleGrouped;
  151. }
  152. /// 点击返回按钮
  153. - (void)goBack {
  154. if (self.isSameConfig) {
  155. [self.navigationController popViewControllerAnimated:YES];
  156. } else {
  157. __weak typeof(self) this = self;
  158. BDFaceAlertController *alert = [BDFaceAlertController alertControllerWithTitle:BDFaceAdjustParamsAlertTitle message:BDFaceAdjustParamsAlertContent preferredStyle:UIAlertControllerStyleAlert];
  159. UIAlertAction *saveAction = [UIAlertAction actionWithTitle:BDFaceAdjustParamsAlertSaveAndLeave style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  160. [this saveConfig];
  161. }];
  162. [alert addAction:saveAction];
  163. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BDFaceAdjustParamsAlertJustLeave style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  164. [alert dismissViewControllerAnimated:NO completion:nil];
  165. [this.navigationController popViewControllerAnimated:YES];
  166. }];
  167. [alert changeTextToBold:BDFaceAdjustParamsAlertSaveAndLeave];
  168. [alert changeTextToLight:@[BDFaceAdjustParamsAlertJustLeave]];
  169. [alert addAction:cancelAction];
  170. [self presentViewController:alert animated:YES completion:nil];
  171. }
  172. }
  173. - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  174. UIView *des = nil;
  175. switch (section) {
  176. case 0:
  177. {
  178. CGRect rect = CGRectMake(BDFaceSelectConfigTableMargin, 0, CGRectGetWidth(self.view.frame) - BDFaceSelectConfigTableMargin * 2.0f, BDFaceSelectConfigTableViewHeight);
  179. UIView *view = [[UIView alloc] initWithFrame:rect];
  180. CGRect label1Rect = view.bounds;
  181. label1Rect.origin.y = BBDFaceAjustConfigTableViewOriginY;
  182. label1Rect.size.height = BDFaceSelectConfigTableViewHeight;
  183. UILabel *label1 = [[UILabel alloc] initWithFrame:label1Rect];
  184. [view addSubview:label1];
  185. label1.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustConfigTipTextColor];
  186. label1.text = BDFaceAdjustParamsSection1Tip;
  187. label1.font = [UIFont systemFontOfSize:14.0f];
  188. des = view;
  189. }
  190. break;
  191. case 1:{
  192. CGRect rect = CGRectMake(BDFaceSelectConfigTableMargin, 0, CGRectGetWidth(self.view.frame) - BDFaceSelectConfigTableMargin * 2.0f, BDFaceSelectConfigTableViewHeight);
  193. UIView *view = [[UIView alloc] initWithFrame:rect];
  194. CGRect label1Rect = view.bounds;
  195. label1Rect.origin.y = BBDFaceAjustConfigTableViewOriginY;
  196. label1Rect.size.height = BDFaceSelectConfigTableViewHeight;
  197. UILabel *label1 = [[UILabel alloc] initWithFrame:label1Rect];
  198. [view addSubview:label1];
  199. label1.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustConfigTipTextColor];
  200. label1.text = BDFaceAdjustParamsSection2Tip;
  201. label1.font = [UIFont systemFontOfSize:14.0f];
  202. des = view;
  203. }
  204. break;;
  205. default:
  206. break;
  207. }
  208. return des;
  209. }
  210. - (void)updateCellContent:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  211. BDFaceAdjustParamsCell *theCell = (BDFaceAdjustParamsCell *)cell;
  212. theCell.isSameConfig = self.isSameConfig;
  213. __weak typeof(self) this = self;
  214. if ([theCell isKindOfClass:[BDFaceAdjustParamsCell class]]) {
  215. theCell.didFinishAdjustParams = ^(BDFaceAdjustParamsItemType type, float value) {
  216. [this changCurrentValue:type value:value];
  217. [this compareValues];
  218. };
  219. }
  220. }
  221. - (void)updateSaveButtonTextColor {
  222. if (self.isSameConfig) {
  223. [self.saveButton setTitleColor:[UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverUnactiveTextColor] forState:UIControlStateNormal];
  224. } else {
  225. [self.saveButton setTitleColor:[UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverActiveTextColor] forState:UIControlStateNormal];
  226. }
  227. }
  228. - (void)compareValues {
  229. BOOL temp = self.isSameConfig;
  230. if (![self.currentConfig compareToParams:self.configInital]) {
  231. self.isSameConfig = NO;
  232. } else {
  233. self.isSameConfig = YES;
  234. }
  235. if (temp != self.isSameConfig) {
  236. NSDictionary *dic = @{BDFaceAdjustParamsControllerConfigIsSameKey : @(self.isSameConfig)};
  237. [[NSNotificationCenter defaultCenter] postNotificationName:BDFaceAdjustParamsControllerConfigDidChangeNotification object:dic];
  238. [self updateSaveButtonTextColor];
  239. }
  240. }
  241. - (void)changCurrentValue:(BDFaceAdjustParamsItemType)type value:(float) value {
  242. switch (type) {
  243. /// 光照
  244. case BDFaceAdjustParamsTypeMinLightIntensity:
  245. {
  246. self.currentConfig.minLightIntensity = value;
  247. }
  248. break;
  249. case BDFaceAdjustParamsTypeMaxLightIntensity:
  250. {
  251. self.currentConfig.maxLightIntensity = value;
  252. }
  253. break;
  254. case BDFaceAdjustParamsTypeAmbiguity:
  255. {
  256. self.currentConfig.ambiguity = value;
  257. }
  258. break;
  259. /// 遮挡
  260. case BDFaceAdjustParamsTypeLeftEyeOcclusion:
  261. {
  262. self.currentConfig.leftEyeOcclusion = value;
  263. }
  264. break;
  265. case BDFaceAdjustParamsTypeRightEyeOcclusion:
  266. {
  267. self.currentConfig.rightEyeOcclusion = value;
  268. }
  269. break;
  270. case BDFaceAdjustParamsTypeNoseOcclusion:
  271. {
  272. self.currentConfig.noseOcclusion = value;
  273. }
  274. break;
  275. case BDFaceAdjustParamsTypeMouthOcclusion:
  276. {
  277. self.currentConfig.mouthOcclusion = value;
  278. }
  279. break;
  280. case BDFaceAdjustParamsTypeLeftFaceOcclusion:
  281. {
  282. self.currentConfig.leftFaceOcclusion = value;
  283. }
  284. break;
  285. case BDFaceAdjustParamsTypeRightFaceOcclusion:
  286. {
  287. self.currentConfig.rightFaceOcclusion = value;
  288. }
  289. break;
  290. case BDFaceAdjustParamsTypeLowerJawOcclusion:
  291. {
  292. self.currentConfig.lowerJawOcclusion = value;
  293. }
  294. break;
  295. /// 姿势
  296. case BDFaceAdjustParamsTypeUpAndDownAngle:
  297. {
  298. self.currentConfig.upAndDownAngle = value;
  299. }
  300. break;
  301. case BDFaceAdjustParamsTypeLeftAndRightAngle:
  302. {
  303. self.currentConfig.leftAndRightAngle = value;
  304. }
  305. break;
  306. case BDFaceAdjustParamsTypeSpinAngle:
  307. {
  308. self.currentConfig.spinAngle = value;
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. @end