Skip to content

Commit fdb9039

Browse files
authored
feat: add pitch and bearing camera controls (#41)
This adds horizontal sliders to control map pitch (tilt) and bearing (rotation). The sliders are positioned in the top toolbar alongside the existing buttons. Changes: - Add set_pitch() and set_bearing() methods to SlintMapLibre - Add camera.hpp include for CameraOptions - Wire up callbacks in main.cpp for pitch/bearing controls - Add pitch and bearing sliders to top toolbar in UI - Use MapLibre terminology (pitch/bearing) for consistency with API - Keep original VerticalBox layout structure
1 parent 124763b commit fdb9039

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

examples/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ int main(int argc, char** argv) {
6767
slint_map_libre->handle_wheel_zoom(x, y, dy);
6868
});
6969

70+
// Pitch and bearing controls
71+
main_window->global<MapAdapter>().on_pitch_changed(
72+
[=](int pitch_value) { slint_map_libre->set_pitch(pitch_value); });
73+
74+
main_window->global<MapAdapter>().on_bearing_changed(
75+
[=](float bearing_value) {
76+
slint_map_libre->set_bearing(bearing_value);
77+
});
78+
7079
main_window->global<MapAdapter>().on_fly_to(
7180
[=](const slint::SharedString& location) {
7281
slint_map_libre->fly_to(

examples/map_window.slint

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Button, VerticalBox, ComboBox, HorizontalBox } from "std-widgets.slint";
1+
import { Button, VerticalBox, ComboBox, HorizontalBox, Slider } from "std-widgets.slint";
22

33
// Define a struct to hold window size
44
export struct Size {
@@ -16,6 +16,8 @@ export global MapAdapter {
1616
callback double_click_with_shift(float, float, bool);
1717
callback wheel_zoom(float, float, float);
1818
callback fly_to(string);
19+
callback pitch_changed(int);
20+
callback bearing_changed(float);
1921
}
2022

2123
export component MapWindow inherits Window {
@@ -76,6 +78,30 @@ export component MapWindow inherits Window {
7678
MapAdapter.fly_to("tokyo");
7779
}
7880
}
81+
Text {
82+
text: "Pitch:";
83+
vertical-alignment: center;
84+
}
85+
Slider {
86+
minimum: 0;
87+
maximum: 100;
88+
value: 0;
89+
changed value => {
90+
MapAdapter.pitch_changed(self.value);
91+
}
92+
}
93+
Text {
94+
text: "Bearing:";
95+
vertical-alignment: center;
96+
}
97+
Slider {
98+
minimum: 0;
99+
maximum: 100;
100+
value: 0;
101+
changed value => {
102+
MapAdapter.bearing_changed(self.value);
103+
}
104+
}
79105
}
80106
map := Image {
81107
source: MapAdapter.map_texture;

src/slint_maplibre_headless.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,42 @@ void SlintMapLibre::handle_wheel_zoom(float x, float y, float dy) {
323323
map->triggerRepaint();
324324
}
325325

326+
void SlintMapLibre::set_pitch(int pitch_value) {
327+
if (!map)
328+
return;
329+
// Convert slider value (0-100) to pitch in degrees (0-60)
330+
double pitch = (pitch_value / 100.0) * 60.0;
331+
332+
// Get current camera state and update pitch
333+
const auto cam = map->getCameraOptions();
334+
mbgl::CameraOptions next;
335+
next.withCenter(cam.center)
336+
.withZoom(cam.zoom)
337+
.withBearing(cam.bearing)
338+
.withPitch(std::optional<double>(pitch));
339+
340+
map->jumpTo(next);
341+
map->triggerRepaint();
342+
}
343+
344+
void SlintMapLibre::set_bearing(float bearing_value) {
345+
if (!map)
346+
return;
347+
// Convert slider value (0-100) to bearing in degrees (0-360)
348+
double bearing = (bearing_value / 100.0) * 360.0;
349+
350+
// Get current camera state and update bearing
351+
const auto cam = map->getCameraOptions();
352+
mbgl::CameraOptions next;
353+
next.withCenter(cam.center)
354+
.withZoom(cam.zoom)
355+
.withPitch(cam.pitch)
356+
.withBearing(std::optional<double>(bearing));
357+
358+
map->jumpTo(next);
359+
map->triggerRepaint();
360+
}
361+
326362
void SlintMapLibre::run_map_loop() {
327363
if (run_loop) {
328364
run_loop->runOnce();

src/slint_maplibre_headless.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class SlintMapLibre : public mbgl::MapObserver {
6868
void handle_mouse_move(float x, float y, bool pressed);
6969
void handle_double_click(float x, float y, bool shift);
7070
void handle_wheel_zoom(float x, float y, float dy);
71+
void set_pitch(int pitch_value);
72+
void set_bearing(float bearing_value);
7173
void setStyleUrl(const std::string& url);
7274
void fly_to(const std::string& location);
7375

0 commit comments

Comments
 (0)