-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDebugMapExample.m
275 lines (198 loc) · 10.7 KB
/
DebugMapExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#import "DebugMapExample.h"
#import <MapboxMaps/MapboxMaps.h>
#import <MapboxCoreMaps/MapboxCoreMaps.h>
#import <MapboxMapObjC/MapboxMapObjC.h>
#import "MapboxMaps-Swift.h"
#import "UIColor+AdditionalColors.h"
@class SettingsViewController;
@protocol DebugOptionSettingsDelegate <NSObject>
- (void) debugOptionSettingsDidChange: (SettingsViewController *) vc;
@end
@interface DebugMapExample () <DebugOptionSettingsDelegate>
@property (strong) MapView* mapView;
@end
@interface SettingsViewController : UIViewController <UITableViewDataSource>
@property (strong, readonly) NSMutableArray* enabledDebugOptions;
@property (weak) id<DebugOptionSettingsDelegate> delegate;
- (instancetype) initWithDebugOptions: (NSArray *) debugOptions;
@end
@interface MapDebugOptionSetting : NSObject
@property (readonly) MBMMapDebugOptions debugOptions;
@property (readonly) NSString* displayTitle;
- (instancetype) initWithDebugOptions: (MBMMapDebugOptions) debugOptions displayTitle: (NSString *) displayTitle;
+ (MapDebugOptionSetting *) newWithDebugOptions: (MBMMapDebugOptions) debugOptions displayTitle: (NSString *) displayTitle;
@end
@implementation MapDebugOptionSetting
- (instancetype)initWithDebugOptions:(MBMMapDebugOptions)debugOptions
displayTitle:(NSString *)displayTitle {
self = [super init];
_debugOptions = debugOptions;
_displayTitle = displayTitle;
return self;
}
+ (MapDebugOptionSetting *)newWithDebugOptions:(MBMMapDebugOptions)debugOptions
displayTitle:(NSString *)displayTitle {
return [[MapDebugOptionSetting alloc] initWithDebugOptions:debugOptions
displayTitle:displayTitle];
}
@end
@interface DebugOptionCell : UITableViewCell
- (void) configureWithSetting: (MapDebugOptionSetting *) setting enabled: (BOOL) enabled;
- (void) onToggled: (void (^)(MapDebugOptionSetting*, bool))handler;
@end
@implementation DebugMapExample
- (void)viewDidLoad {
[super viewDidLoad];
MapView* mapView = [MapViewFactory createWithFrame:self.view.bounds options:nil];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
TMBOrnamentOptions* ornamentOptions = [[mapView ornaments] options];
[[ornamentOptions scaleBar] setVisibility:TMBOrnamentVisibilityVisible];
if (@available(iOS 15.0, *)) {
float maxFPS = UIScreen.mainScreen.maximumFramesPerSecond;
CAFrameRateRange frameRateRange = CAFrameRateRangeMake(1, maxFPS, maxFPS);
[mapView preferredFrameRateRange:frameRateRange];
}
self.mapView = mapView;
[self.view addSubview:mapView];
self.view.backgroundColor = [UIColor skyBlue];
mapView.translatesAutoresizingMaskIntoConstraints = false;
[NSLayoutConstraint activateConstraints:@[
[mapView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
[mapView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[mapView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
[mapView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
]];
UIBarButtonItem* editBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(openDebugOptionsMenu:)];
self.navigationItem.rightBarButtonItem = editBarButton;
}
- (void) openDebugOptionsMenu: (UIBarButtonItem *) sender {
NSArray* debugOptions = [self.mapView mapboxMapDebugOptions];
SettingsViewController* settingsViewController = [[SettingsViewController alloc] initWithDebugOptions: debugOptions];
settingsViewController.delegate = self;
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController: settingsViewController];
navigationController.modalPresentationStyle = UIModalPresentationPopover;
navigationController.popoverPresentationController.barButtonItem = sender;
[self presentViewController:navigationController animated:true completion:nil];
}
- (void)debugOptionSettingsDidChange:(SettingsViewController *)vc {
[vc dismissViewControllerAnimated:true completion:nil];
[self.mapView mapboxMapDebugOptions: vc.enabledDebugOptions];
}
@end
@interface SettingsViewController ()
@property (strong) UITableView* tableView;
@property (strong, readonly) NSArray<MapDebugOptionSetting *> * allSettings;
@end
@implementation SettingsViewController
- (instancetype)initWithDebugOptions:(NSArray *)debugOptions {
_enabledDebugOptions = [debugOptions mutableCopy];
_allSettings = [SettingsViewController availableSettings];
self = [super initWithNibName:nil bundle:nil];
return self;
}
+ (NSArray<MapDebugOptionSetting *> *) availableSettings {
return @[
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsCollision displayTitle: @"Debug collision"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsDepthBuffer displayTitle: @"Show depth buffer"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsOverdraw displayTitle: @"Debug overdraw"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsParseStatus displayTitle: @"Show tile coordinate"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsStencilClip displayTitle: @"Show stencil buffer"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsTileBorders displayTitle: @"Debug tile clipping"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsTimestamps displayTitle: @"Show tile loaded time"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsModelBounds displayTitle: @"Show 3D model bounding boxes"],
[MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsLight displayTitle: @"Show light conditions"],
// [MapDebugOptionSetting newWithDebugOptions: MBMMapDebugOptionsC displayTitle: @"Show tile loaded time"]
];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Debug options";
self.tableView = [[UITableView alloc] init];
self.tableView.dataSource = self;
[self.tableView registerClass:DebugOptionCell.class
forCellReuseIdentifier:NSStringFromClass(DebugOptionCell.class)];
[self.view addSubview:self.tableView];
self.tableView.translatesAutoresizingMaskIntoConstraints = false;
[NSLayoutConstraint activateConstraints:@[
[self.tableView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[self.tableView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
[self.tableView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
]];
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:@selector(saveSettings:)];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.preferredContentSize = self.tableView.contentSize;
}
- (void) saveSettings: (UIBarButtonItem *) button {
if (self.delegate) {
[self.delegate debugOptionSettingsDidChange:self];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allSettings.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* cellID = NSStringFromClass(DebugOptionCell.class);
DebugOptionCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
MapDebugOptionSetting* setting = self.allSettings[indexPath.row];
NSNumber *value = [NSNumber numberWithInt:setting.debugOptions];
BOOL enabled = [self.enabledDebugOptions containsObject: value];
[cell configureWithSetting: setting enabled: enabled];
[cell onToggled:^(MapDebugOptionSetting* setting, bool enabled) {
NSNumber *option = [NSNumber numberWithInt:setting.debugOptions];
if (enabled) {
[self.enabledDebugOptions addObject:option];
} else {
[self.enabledDebugOptions removeObject:option];
}
}];
return cell;
}
@end
typedef void (^OnToggleHandler)(MapDebugOptionSetting*, bool) ;
@interface DebugOptionCell()
@property UILabel* titleLabel;
@property UISwitch* toggle;
@property OnToggleHandler toggleHandler;
@property MapDebugOptionSetting* setting;
@end
@implementation DebugOptionCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.titleLabel = [[UILabel alloc] init];
self.toggle = [[UISwitch alloc] init];
[self.toggle addTarget:self action:@selector(didToggle:) forControlEvents:UIControlEventValueChanged];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.toggle];
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false;
self.toggle.translatesAutoresizingMaskIntoConstraints = false;
[NSLayoutConstraint activateConstraints:@[
[self.titleLabel.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant: 16],
[self.titleLabel.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor],
[self.titleLabel.topAnchor constraintGreaterThanOrEqualToAnchor: self.contentView.topAnchor constant: 8],
[self.toggle.leftAnchor constraintGreaterThanOrEqualToAnchor: self.titleLabel.rightAnchor constant: 16],
[self.toggle.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant: -16],
[self.toggle.centerYAnchor constraintEqualToAnchor:self.titleLabel.centerYAnchor],
[self.toggle.topAnchor constraintGreaterThanOrEqualToAnchor: self.contentView.topAnchor constant: 8],
]];
return self;
}
- (void) didToggle: (UISwitch *) sender {
self.toggleHandler(self.setting, sender.isOn);
}
- (void)configureWithSetting:(MapDebugOptionSetting *)setting enabled:(BOOL)enabled {
self.titleLabel.text = setting.displayTitle;
[self.toggle setOn: enabled];
self.setting = setting;
}
- (void)onToggled:(OnToggleHandler) handler {
self.toggleHandler = handler;
}
@end