-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (121 loc) · 5.39 KB
/
Copy pathmain.cpp
File metadata and controls
139 lines (121 loc) · 5.39 KB
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
#include <iostream>
#include <memory>
#include "map_window.h"
#include "slint_maplibre_headless.hpp"
int main(int argc, char** argv) {
std::cout << "[main] Starting application" << std::endl;
auto main_window = MapWindow::create();
auto slint_map = std::make_shared<SlintMapLibre>();
auto initialized = std::make_shared<bool>(false);
// Render: read frame from MapLibre and push to MMapAdapter
auto render_function = [=]() {
auto image = slint_map->render_map();
main_window->global<MMapAdapter>().set_frame(image);
// Update reactive camera state
if (auto* m = slint_map->get_map()) {
const auto cam = m->getCameraOptions();
if (cam.center) {
main_window->global<MMapAdapter>().set_current_lat(
static_cast<float>(cam.center->latitude()));
main_window->global<MMapAdapter>().set_current_lon(
static_cast<float>(cam.center->longitude()));
}
if (cam.zoom)
main_window->global<MMapAdapter>().set_current_zoom(
static_cast<float>(*cam.zoom));
if (cam.bearing)
main_window->global<MMapAdapter>().set_current_bearing(
static_cast<float>(*cam.bearing));
if (cam.pitch)
main_window->global<MMapAdapter>().set_current_pitch(
static_cast<float>(*cam.pitch));
}
};
slint_map->setRenderCallback(render_function);
// Render loop tick
main_window->global<MMapAdapter>().on_tick([=]() {
slint_map->run_map_loop();
if (slint_map->take_repaint_request() ||
slint_map->consume_forced_repaint()) {
render_function();
}
});
// User interactions
main_window->global<MMapAdapter>().on_mouse_pressed(
[=](float x, float y) { slint_map->handle_mouse_press(x, y); });
main_window->global<MMapAdapter>().on_mouse_released(
[=](float x, float y) { slint_map->handle_mouse_release(x, y); });
main_window->global<MMapAdapter>().on_mouse_moved(
[=](float x, float y) { slint_map->handle_mouse_move(x, y, true); });
main_window->global<MMapAdapter>().on_double_clicked(
[=](float x, float y, bool shift) {
slint_map->handle_double_click(x, y, shift);
});
main_window->global<MMapAdapter>().on_wheel_zoomed(
[=](float x, float y, float dy) {
slint_map->handle_wheel_zoom(x, y, dy);
});
// Commands
main_window->global<MMapAdapter>().on_request_style_change(
[=](const slint::SharedString& url) {
slint_map->setStyleUrl(std::string(url.data(), url.size()));
});
main_window->global<MMapAdapter>().on_request_fly_to(
[=](float lat, float lon, float zoom) {
slint_map->fly_to(static_cast<double>(lat),
static_cast<double>(lon),
static_cast<double>(zoom));
});
main_window->global<MMapAdapter>().on_request_pitch_change(
[=](float pitch) {
slint_map->set_pitch(static_cast<int>(pitch / 60.0f * 100.0f));
});
main_window->global<MMapAdapter>().on_request_bearing_change(
[=](float bearing) {
slint_map->set_bearing(bearing / 360.0f * 100.0f);
});
// Initialize/resize when map area size changes
main_window->on_map_size_changed([=]() {
const auto s = main_window->get_map_size();
const int w = static_cast<int>(s.width);
const int h = static_cast<int>(s.height);
if (w > 0 && h > 0) {
if (!*initialized) {
slint_map->initialize(w, h);
// Apply the map's declared initial style/camera (published by
// MMapView.init) now that the backend map exists, so a map
// declared with a style-url/center/zoom opens there instead of
// silently keeping the backend's built-in default.
auto& adapter = main_window->global<MMapAdapter>();
if (adapter.get_initial_config_set()) {
const auto url = adapter.get_initial_style_url();
if (url.size() > 0) {
slint_map->setStyleUrl(
std::string(url.data(), url.size()));
}
if (auto* m = slint_map->get_map()) {
mbgl::CameraOptions cam;
cam
.withCenter(mbgl::LatLng{
static_cast<double>(adapter.get_initial_lat()),
static_cast<double>(adapter.get_initial_lon())})
.withZoom(
static_cast<double>(adapter.get_initial_zoom()))
.withBearing(static_cast<double>(
adapter.get_initial_bearing()))
.withPitch(static_cast<double>(
adapter.get_initial_pitch()));
m->jumpTo(cam);
m->triggerRepaint();
}
}
*initialized = true;
} else {
slint_map->resize(w, h);
}
}
});
std::cout << "[main] Entering UI event loop" << std::endl;
main_window->run();
return 0;
}