/
DebugMapExample.swift
206 lines (165 loc) · 8.1 KB
/
DebugMapExample.swift
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
import UIKit
@_spi(Experimental) import MapboxMaps
private protocol DebugOptionSettingsDelegate: AnyObject {
func debugOptionSettingsDidChange(_ controller: SettingsViewController)
}
private struct MapDebugOptionSetting {
let debugOption: MapDebugOptions
let displayTitle: String
}
final class DebugMapExample: UIViewController, ExampleProtocol, DebugOptionSettingsDelegate {
private var mapView: MapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MapView(frame: view.bounds)
if #available(iOS 15.0, *) {
let maxFPS = Float(UIScreen.main.maximumFramesPerSecond)
mapView.preferredFrameRateRange = CAFrameRateRange(minimum: 1, maximum: maxFPS, preferred: maxFPS)
}
view.addSubview(mapView)
view.backgroundColor = .skyBlue
mapView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
mapView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
let debugOptionsBarItem = UIBarButtonItem(
barButtonSystemItem: .edit,
target: self,
action: #selector(openDebugOptionsMenu(_:)))
let tileCover = UIBarButtonItem(
title: "Tiles",
style: .plain,
target: self,
action: #selector(tileCover))
navigationItem.rightBarButtonItems = [debugOptionsBarItem, tileCover]
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// The below line is used for internal testing purposes only.
finish()
}
@objc private func openDebugOptionsMenu(_ sender: UIBarButtonItem) {
let settingsViewController = SettingsViewController(debugOptions: mapView.mapboxMap.debugOptions)
settingsViewController.delegate = self
let navigationController = UINavigationController(rootViewController: settingsViewController)
navigationController.modalPresentationStyle = .popover
navigationController.popoverPresentationController?.barButtonItem = sender
present(navigationController, animated: true, completion: nil)
}
@objc private func tileCover() {
let tileIds = mapView.mapboxMap.tileCover(for: TileCoverOptions(tileSize: 512, minZoom: 0, maxZoom: 22, roundZoom: false))
let message = tileIds.map { "\($0.z)/\($0.x)/\($0.y)" }.joined(separator: "\n")
showAlert(withTitle: "Displayed tiles", and: message)
}
fileprivate func debugOptionSettingsDidChange(_ controller: SettingsViewController) {
controller.dismiss(animated: true, completion: nil)
mapView.mapboxMap.debugOptions = Array(controller.enabledDebugOptions)
}
}
private final class SettingsViewController: UIViewController, UITableViewDataSource {
weak var delegate: DebugOptionSettingsDelegate?
private var listView: UITableView!
private(set) var enabledDebugOptions: Set<MapDebugOptions>
private let allSettings: [MapDebugOptionSetting] = [
MapDebugOptionSetting(debugOption: .collision, displayTitle: "Debug collision"),
MapDebugOptionSetting(debugOption: .depthBuffer, displayTitle: "Show depth buffer"),
MapDebugOptionSetting(debugOption: .overdraw, displayTitle: "Debug overdraw"),
MapDebugOptionSetting(debugOption: .parseStatus, displayTitle: "Show tile coordinate"),
MapDebugOptionSetting(debugOption: .renderCache, displayTitle: "Render Cache"),
MapDebugOptionSetting(debugOption: .stencilClip, displayTitle: "Show stencil buffer"),
MapDebugOptionSetting(debugOption: .tileBorders, displayTitle: "Debug tile clipping"),
MapDebugOptionSetting(debugOption: .timestamps, displayTitle: "Show tile loaded time"),
]
init(debugOptions: [MapDebugOptions]) {
enabledDebugOptions = Set(debugOptions)
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Debug options"
listView = UITableView()
listView.dataSource = self
listView.register(DebugOptionCell.self, forCellReuseIdentifier: String(describing: DebugOptionCell.self))
view.addSubview(listView)
listView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
listView.topAnchor.constraint(equalTo: view.topAnchor),
listView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
listView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
listView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
navigationItem.largeTitleDisplayMode = .never
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .save,
target: self,
action: #selector(saveSettings(_:)))
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
preferredContentSize = listView.contentSize
}
@objc private func saveSettings(_ sender: UIBarButtonItem) {
delegate?.debugOptionSettingsDidChange(self)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
allSettings.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellID = String(describing: DebugOptionCell.self)
// swiftlint:disable:next force_cast
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! DebugOptionCell
let setting = allSettings[indexPath.row]
cell.configure(with: setting, isOptionEnabled: enabledDebugOptions.contains(setting.debugOption))
cell.onToggled { [unowned self] isEnabled in
if isEnabled {
self.enabledDebugOptions.insert(setting.debugOption)
} else {
self.enabledDebugOptions.remove(setting.debugOption)
}
}
return cell
}
}
// MARK: Cell
private class DebugOptionCell: UITableViewCell {
private let titleLabel = UILabel()
private let toggle = UISwitch()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
toggle.addTarget(self, action: #selector(didToggle(_:)), for: .valueChanged)
contentView.addSubview(titleLabel)
contentView.addSubview(toggle)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
toggle.translatesAutoresizingMaskIntoConstraints = false
let constraints: [NSLayoutConstraint] = [
titleLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 16),
titleLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
titleLabel.topAnchor.constraint(greaterThanOrEqualTo: contentView.topAnchor, constant: 8),
toggle.leftAnchor.constraint(greaterThanOrEqualTo: titleLabel.rightAnchor, constant: 16),
toggle.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -16),
toggle.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor),
toggle.topAnchor.constraint(greaterThanOrEqualTo: contentView.topAnchor, constant: 8),
]
NSLayoutConstraint.activate(constraints)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(with setting: MapDebugOptionSetting, isOptionEnabled: Bool) {
titleLabel.text = setting.displayTitle
toggle.isOn = isOptionEnabled
}
private var onToggleHandler: ((Bool) -> Void)?
func onToggled(_ handler: @escaping (Bool) -> Void) {
onToggleHandler = handler
}
@objc private func didToggle(_ sender: UISwitch) {
onToggleHandler?(sender.isOn)
}
}