godot_open_save_delegate.mm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**************************************************************************/
  2. /* godot_open_save_delegate.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "godot_open_save_delegate.h"
  31. @implementation GodotOpenSaveDelegate
  32. - (instancetype)init {
  33. self = [super init];
  34. if ((self = [super init])) {
  35. dialog = nullptr;
  36. cur_index = 0;
  37. ctr_id = 1;
  38. allowed_types = nullptr;
  39. root = String();
  40. }
  41. return self;
  42. }
  43. - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &)p_filters options:(const TypedArray<Dictionary> &)p_options {
  44. dialog = p_panel;
  45. NSMutableArray *constraints = [NSMutableArray array];
  46. NSView *base_view = [[NSView alloc] initWithFrame:NSZeroRect];
  47. base_view.translatesAutoresizingMaskIntoConstraints = NO;
  48. NSGridView *view = [NSGridView gridViewWithNumberOfColumns:2 rows:0];
  49. view.translatesAutoresizingMaskIntoConstraints = NO;
  50. view.columnSpacing = 10;
  51. view.rowSpacing = 10;
  52. view.rowAlignment = NSGridRowAlignmentLastBaseline;
  53. int option_count = 0;
  54. for (int i = 0; i < p_options.size(); i++) {
  55. const Dictionary &item = p_options[i];
  56. if (!item.has("name") || !item.has("values") || !item.has("default")) {
  57. continue;
  58. }
  59. const String &name = item["name"];
  60. const Vector<String> &values = item["values"];
  61. int default_idx = item["default"];
  62. NSTextField *label = [NSTextField labelWithString:[NSString stringWithUTF8String:name.utf8().get_data()]];
  63. if (@available(macOS 10.14, *)) {
  64. label.textColor = NSColor.secondaryLabelColor;
  65. }
  66. if (@available(macOS 11.10, *)) {
  67. label.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
  68. }
  69. NSView *popup = nullptr;
  70. if (values.is_empty()) {
  71. NSButton *popup_check = [NSButton checkboxWithTitle:@"" target:self action:@selector(popupCheckAction:)];
  72. int tag = [self setDefaultBool:name value:(bool)default_idx];
  73. popup_check.state = (default_idx) ? NSControlStateValueOn : NSControlStateValueOff;
  74. popup_check.tag = tag;
  75. popup = popup_check;
  76. } else {
  77. NSPopUpButton *popup_list = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
  78. for (int i = 0; i < values.size(); i++) {
  79. [popup_list addItemWithTitle:[NSString stringWithUTF8String:values[i].utf8().get_data()]];
  80. }
  81. int tag = [self setDefaultInt:name value:default_idx];
  82. [popup_list selectItemAtIndex:default_idx];
  83. popup_list.tag = tag;
  84. popup_list.target = self;
  85. popup_list.action = @selector(popupOptionAction:);
  86. popup = popup_list;
  87. }
  88. [view addRowWithViews:[NSArray arrayWithObjects:label, popup, nil]];
  89. option_count++;
  90. }
  91. NSMutableArray *new_allowed_types = [[NSMutableArray alloc] init];
  92. bool has_type_popup = false;
  93. {
  94. NSTextField *label = [NSTextField labelWithString:[NSString stringWithUTF8String:RTR("Format").utf8().get_data()]];
  95. if (@available(macOS 10.14, *)) {
  96. label.textColor = NSColor.secondaryLabelColor;
  97. }
  98. if (@available(macOS 11.10, *)) {
  99. label.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
  100. }
  101. if (p_filters.size() > 1) {
  102. NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
  103. for (int i = 0; i < p_filters.size(); i++) {
  104. Vector<String> tokens = p_filters[i].split(";");
  105. if (tokens.size() >= 1) {
  106. String flt = tokens[0].strip_edges();
  107. int filter_slice_count = flt.get_slice_count(",");
  108. NSMutableArray *type_filters = [[NSMutableArray alloc] init];
  109. for (int j = 0; j < filter_slice_count; j++) {
  110. String str = (flt.get_slice(",", j).strip_edges());
  111. if (!str.is_empty()) {
  112. [type_filters addObject:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
  113. }
  114. }
  115. if ([type_filters count] > 0) {
  116. NSString *name_str = [NSString stringWithUTF8String:((tokens.size() == 1) ? tokens[0] : tokens[1].strip_edges()).utf8().get_data()];
  117. [new_allowed_types addObject:type_filters];
  118. [popup addItemWithTitle:name_str];
  119. }
  120. }
  121. }
  122. if (popup.numberOfItems > 1) {
  123. has_type_popup = true;
  124. popup.target = self;
  125. popup.action = @selector(popupFileAction:);
  126. [view addRowWithViews:[NSArray arrayWithObjects:label, popup, nil]];
  127. }
  128. } else if (p_filters.size() == 1) {
  129. Vector<String> tokens = p_filters[0].split(";");
  130. if (tokens.size() >= 1) {
  131. String flt = tokens[0].strip_edges();
  132. int filter_slice_count = flt.get_slice_count(",");
  133. NSMutableArray *type_filters = [[NSMutableArray alloc] init];
  134. for (int j = 0; j < filter_slice_count; j++) {
  135. String str = (flt.get_slice(",", j).strip_edges());
  136. if (!str.is_empty()) {
  137. [type_filters addObject:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
  138. }
  139. }
  140. if ([type_filters count] > 0) {
  141. [new_allowed_types addObject:type_filters];
  142. }
  143. }
  144. }
  145. [self setFileTypes:new_allowed_types];
  146. }
  147. [base_view addSubview:view];
  148. [constraints addObject:[view.topAnchor constraintEqualToAnchor:base_view.topAnchor constant:10]];
  149. [constraints addObject:[base_view.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:10]];
  150. [constraints addObject:[base_view.centerXAnchor constraintEqualToAnchor:view.centerXAnchor constant:10]];
  151. [NSLayoutConstraint activateConstraints:constraints];
  152. if (option_count > 0 || has_type_popup) {
  153. [p_panel setAccessoryView:base_view];
  154. }
  155. if ([new_allowed_types count] > 0) {
  156. NSMutableArray *type_filters = [new_allowed_types objectAtIndex:0];
  157. if (type_filters && [type_filters count] == 1 && [[type_filters objectAtIndex:0] isEqualToString:@"*"]) {
  158. [p_panel setAllowedFileTypes:nil];
  159. [p_panel setAllowsOtherFileTypes:true];
  160. } else {
  161. [p_panel setAllowsOtherFileTypes:false];
  162. [p_panel setAllowedFileTypes:type_filters];
  163. }
  164. } else {
  165. [p_panel setAllowedFileTypes:nil];
  166. [p_panel setAllowsOtherFileTypes:true];
  167. }
  168. }
  169. - (int)getIndex {
  170. return cur_index;
  171. }
  172. - (Dictionary)getSelection {
  173. return options;
  174. }
  175. - (int)setDefaultInt:(const String &)p_name value:(int)p_value {
  176. int cid = ctr_id++;
  177. options[p_name] = p_value;
  178. ctr_ids[cid] = p_name;
  179. return cid;
  180. }
  181. - (int)setDefaultBool:(const String &)p_name value:(bool)p_value {
  182. int cid = ctr_id++;
  183. options[p_name] = p_value;
  184. ctr_ids[cid] = p_name;
  185. return cid;
  186. }
  187. - (void)setFileTypes:(NSMutableArray *)p_allowed_types {
  188. allowed_types = p_allowed_types;
  189. }
  190. - (instancetype)initWithDialog:(NSSavePanel *)p_dialog {
  191. if ((self = [super init])) {
  192. dialog = p_dialog;
  193. cur_index = 0;
  194. ctr_id = 1;
  195. allowed_types = nullptr;
  196. }
  197. return self;
  198. }
  199. - (void)popupCheckAction:(id)p_sender {
  200. NSButton *btn = p_sender;
  201. if (btn && ctr_ids.has(btn.tag)) {
  202. options[ctr_ids[btn.tag]] = ([btn state] == NSControlStateValueOn);
  203. }
  204. }
  205. - (void)popupOptionAction:(id)p_sender {
  206. NSPopUpButton *btn = p_sender;
  207. if (btn && ctr_ids.has(btn.tag)) {
  208. options[ctr_ids[btn.tag]] = (int)[btn indexOfSelectedItem];
  209. }
  210. }
  211. - (void)popupFileAction:(id)p_sender {
  212. NSPopUpButton *btn = p_sender;
  213. if (btn) {
  214. NSUInteger index = [btn indexOfSelectedItem];
  215. if (allowed_types && index < [allowed_types count]) {
  216. NSMutableArray *type_filters = [allowed_types objectAtIndex:index];
  217. if (type_filters && [type_filters count] == 1 && [[type_filters objectAtIndex:0] isEqualToString:@"*"]) {
  218. [dialog setAllowedFileTypes:nil];
  219. [dialog setAllowsOtherFileTypes:true];
  220. } else {
  221. [dialog setAllowsOtherFileTypes:false];
  222. [dialog setAllowedFileTypes:type_filters];
  223. }
  224. cur_index = index;
  225. } else {
  226. [dialog setAllowedFileTypes:nil];
  227. [dialog setAllowsOtherFileTypes:true];
  228. cur_index = -1;
  229. }
  230. }
  231. }
  232. - (void)setRootPath:(const String &)p_root_path {
  233. root = p_root_path;
  234. }
  235. - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError *_Nullable *)outError {
  236. if (root.is_empty()) {
  237. return YES;
  238. }
  239. NSString *ns_path = url.URLByStandardizingPath.URLByResolvingSymlinksInPath.path;
  240. String path = String::utf8([ns_path UTF8String]).simplify_path();
  241. if (!path.begins_with(root.simplify_path())) {
  242. return NO;
  243. }
  244. return YES;
  245. }
  246. @end