mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:47:44 +00:00
Maps: Add toolbar and frame border to map widget
This commit is contained in:
parent
b5f91e3365
commit
048e8d7744
3 changed files with 54 additions and 26 deletions
|
@ -80,9 +80,8 @@ void MapWidget::mousedown_event(GUI::MouseEvent& event)
|
|||
|
||||
if (event.button() == GUI::MouseButton::Primary) {
|
||||
// Ignore attribution click
|
||||
if (m_attribution_enabled && static_cast<float>(event.x()) > width() - m_attribution_width && static_cast<float>(event.y()) > height() - m_attribution_height) {
|
||||
if (m_attribution_enabled && static_cast<float>(event.x()) > frame_inner_rect().right() - m_attribution_width && static_cast<float>(event.y()) > frame_inner_rect().bottom() - m_attribution_height)
|
||||
return;
|
||||
}
|
||||
|
||||
// Start map tiles dragging
|
||||
m_dragging = true;
|
||||
|
@ -110,7 +109,7 @@ void MapWidget::mousemove_event(GUI::MouseEvent& event)
|
|||
|
||||
// Handle attribution hover
|
||||
if (m_attribution_enabled) {
|
||||
if (static_cast<float>(event.x()) > width() - m_attribution_width && static_cast<float>(event.y()) > height() - m_attribution_height) {
|
||||
if (static_cast<float>(event.x()) > frame_inner_rect().right() - m_attribution_width && static_cast<float>(event.y()) > frame_inner_rect().bottom() - m_attribution_height) {
|
||||
set_override_cursor(Gfx::StandardCursor::Hand);
|
||||
} else {
|
||||
set_override_cursor(Gfx::StandardCursor::Arrow);
|
||||
|
@ -132,7 +131,7 @@ void MapWidget::mouseup_event(GUI::MouseEvent& event)
|
|||
|
||||
if (event.button() == GUI::MouseButton::Primary) {
|
||||
// Handle attribution click
|
||||
if (m_attribution_enabled && static_cast<float>(event.x()) > width() - m_attribution_width && static_cast<float>(event.y()) > height() - m_attribution_height) {
|
||||
if (m_attribution_enabled && static_cast<float>(event.x()) > frame_inner_rect().right() - m_attribution_width && static_cast<float>(event.y()) > frame_inner_rect().bottom() - m_attribution_height) {
|
||||
Desktop::Launcher::open(m_attribution_url);
|
||||
return;
|
||||
}
|
||||
|
@ -344,7 +343,7 @@ void MapWidget::paint_scale(GUI::Painter& painter)
|
|||
// Metric line
|
||||
double meters = nice_round_number(max_meters);
|
||||
float metric_width = m_scale_max_width * (meters / max_meters);
|
||||
Gfx::IntRect metric_rect = { margin_x, height() - margin_y - line_height * 2, metric_width, line_height };
|
||||
Gfx::IntRect metric_rect = { frame_inner_rect().x() + margin_x, frame_inner_rect().bottom() - margin_y - line_height * 2, metric_width, line_height };
|
||||
if (meters < 1000) {
|
||||
paint_scale_line(painter, MUST(String::formatted("{} m", meters)), metric_rect);
|
||||
} else {
|
||||
|
@ -357,7 +356,7 @@ void MapWidget::paint_scale(GUI::Painter& painter)
|
|||
double max_miles = max_feet / 5280;
|
||||
double miles = nice_round_number(max_miles);
|
||||
float imperial_width = m_scale_max_width * (feet < 5280 ? feet / max_feet : miles / max_miles);
|
||||
Gfx::IntRect imperial_rect = { margin_x, height() - margin_y - line_height, imperial_width, line_height };
|
||||
Gfx::IntRect imperial_rect = { frame_inner_rect().x() + margin_x, frame_inner_rect().bottom() - margin_y - line_height, imperial_width, line_height };
|
||||
if (feet < 5280) {
|
||||
paint_scale_line(painter, MUST(String::formatted("{} ft", feet)), imperial_rect);
|
||||
} else {
|
||||
|
@ -365,27 +364,29 @@ void MapWidget::paint_scale(GUI::Painter& painter)
|
|||
}
|
||||
|
||||
// Border between
|
||||
painter.fill_rect({ margin_x, height() - margin_y - line_height, max(metric_width, imperial_width), 1.0f }, panel_foreground_color);
|
||||
painter.fill_rect({ frame_inner_rect().x() + margin_x, frame_inner_rect().bottom() - margin_y - line_height, max(metric_width, imperial_width), 1.0f }, panel_foreground_color);
|
||||
}
|
||||
|
||||
void MapWidget::paint_attribution(GUI::Painter& painter)
|
||||
{
|
||||
m_attribution_width = PANEL_PADDING_X + painter.font().width(m_attribution_text) + PANEL_PADDING_X;
|
||||
m_attribution_height = PANEL_PADDING_Y + painter.font().pixel_size() + PANEL_PADDING_Y;
|
||||
painter.fill_rect({ width() - m_attribution_width, height() - m_attribution_height, m_attribution_width, m_attribution_height }, panel_background_color);
|
||||
Gfx::FloatRect attribution_text_rect { 0.0f, 0.0f, width() - PANEL_PADDING_X, height() - PANEL_PADDING_Y };
|
||||
painter.fill_rect({ frame_inner_rect().right() - m_attribution_width, frame_inner_rect().bottom() - m_attribution_height, m_attribution_width, m_attribution_height }, panel_background_color);
|
||||
Gfx::FloatRect attribution_text_rect { 0.0f, 0.0f, frame_inner_rect().right() - PANEL_PADDING_X, frame_inner_rect().bottom() - PANEL_PADDING_Y };
|
||||
painter.draw_text(attribution_text_rect, m_attribution_text, Gfx::TextAlignment::BottomRight, panel_foreground_color);
|
||||
}
|
||||
|
||||
void MapWidget::paint_event(GUI::PaintEvent&)
|
||||
void MapWidget::paint_event(GUI::PaintEvent& event)
|
||||
{
|
||||
GUI::Painter painter(*this);
|
||||
painter.fill_rect(rect(), map_background_color);
|
||||
Frame::paint_event(event);
|
||||
|
||||
if (m_connection_failed) {
|
||||
painter.draw_text(rect(), "Failed to fetch map tiles :^("sv, Gfx::TextAlignment::Center, panel_foreground_color);
|
||||
return;
|
||||
}
|
||||
GUI::Painter painter(*this);
|
||||
painter.add_clip_rect(event.rect());
|
||||
painter.add_clip_rect(frame_inner_rect());
|
||||
painter.fill_rect(frame_inner_rect(), map_background_color);
|
||||
|
||||
if (m_connection_failed)
|
||||
return painter.draw_text(frame_inner_rect(), "Failed to fetch map tiles :^("sv, Gfx::TextAlignment::Center, panel_foreground_color);
|
||||
|
||||
paint_tiles(painter);
|
||||
if (m_scale_enabled)
|
||||
|
|
|
@ -8,12 +8,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Queue.h>
|
||||
#include <LibGUI/Frame.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <LibProtocol/Request.h>
|
||||
#include <LibProtocol/RequestClient.h>
|
||||
|
||||
class MapWidget final : public GUI::Widget {
|
||||
class MapWidget : public GUI::Frame {
|
||||
C_OBJECT(MapWidget);
|
||||
|
||||
public:
|
||||
|
|
|
@ -9,10 +9,14 @@
|
|||
#include <LibCore/System.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/ToolbarContainer.h>
|
||||
#include <LibGUI/Window.h>
|
||||
|
||||
static int constexpr MAP_ZOOM_DEFAULT = 3;
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
|
||||
|
@ -31,29 +35,52 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->restore_size_and_position("Maps"sv, "Window"sv, { { 640, 480 } });
|
||||
window->save_size_and_position_on_close("Maps"sv, "Window"sv);
|
||||
|
||||
// Root widget
|
||||
auto root_widget = TRY(window->set_main_widget<GUI::Widget>());
|
||||
root_widget->set_fill_with_background_color(true);
|
||||
root_widget->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 2);
|
||||
|
||||
// Toolbar
|
||||
auto toolbar_container = TRY(root_widget->try_add<GUI::ToolbarContainer>());
|
||||
auto toolbar = TRY(toolbar_container->try_add<GUI::Toolbar>());
|
||||
|
||||
// Map widget
|
||||
MapWidget::Options options {};
|
||||
options.center.latitude = Config::read_string("Maps"sv, "MapView"sv, "CenterLatitude"sv, "30"sv).to_double().value_or(30.0);
|
||||
options.center.longitude = Config::read_string("Maps"sv, "MapView"sv, "CenterLongitude"sv, "0"sv).to_double().value_or(0.0);
|
||||
options.zoom = Config::read_i32("Maps"sv, "MapView"sv, "Zoom"sv, 3);
|
||||
auto maps = TRY(MapWidget::try_create(options));
|
||||
window->set_main_widget(maps);
|
||||
options.zoom = Config::read_i32("Maps"sv, "MapView"sv, "Zoom"sv, MAP_ZOOM_DEFAULT);
|
||||
auto maps = TRY(root_widget->try_add<MapWidget>(options));
|
||||
maps->set_frame_style(Gfx::FrameStyle::SunkenContainer);
|
||||
|
||||
// Main menu
|
||||
// Main menu actions
|
||||
auto file_menu = window->add_menu("&File"_string);
|
||||
file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) { GUI::Application::the()->quit(); }));
|
||||
|
||||
auto view_menu = window->add_menu("&View"_string);
|
||||
view_menu->add_action(GUI::CommonActions::make_zoom_in_action([maps](auto&) { maps->set_zoom(maps->zoom() + 1); }, window));
|
||||
view_menu->add_action(GUI::CommonActions::make_zoom_out_action([maps](auto&) { maps->set_zoom(maps->zoom() - 1); }, window));
|
||||
view_menu->add_action(GUI::CommonActions::make_reset_zoom_action([maps](auto&) { maps->set_zoom(3); }, window));
|
||||
auto zoom_in_action = GUI::CommonActions::make_zoom_in_action([maps](auto&) { maps->set_zoom(maps->zoom() + 1); }, window);
|
||||
auto zoom_out_action = GUI::CommonActions::make_zoom_out_action([maps](auto&) { maps->set_zoom(maps->zoom() - 1); }, window);
|
||||
auto reset_zoom_action = GUI::CommonActions::make_reset_zoom_action([maps](auto&) { maps->set_zoom(MAP_ZOOM_DEFAULT); }, window);
|
||||
auto fullscreen_action = GUI::CommonActions::make_fullscreen_action([window, toolbar_container, maps](auto&) {
|
||||
window->set_fullscreen(!window->is_fullscreen());
|
||||
toolbar_container->set_visible(!window->is_fullscreen());
|
||||
maps->set_frame_style(window->is_fullscreen() ? Gfx::FrameStyle::NoFrame : Gfx::FrameStyle::SunkenContainer);
|
||||
},
|
||||
window);
|
||||
view_menu->add_action(zoom_in_action);
|
||||
view_menu->add_action(zoom_out_action);
|
||||
view_menu->add_action(reset_zoom_action);
|
||||
view_menu->add_separator();
|
||||
view_menu->add_action(GUI::CommonActions::make_fullscreen_action([window](auto&) { window->set_fullscreen(!window->is_fullscreen()); }, window));
|
||||
view_menu->add_action(fullscreen_action);
|
||||
|
||||
auto help_menu = window->add_menu("&Help"_string);
|
||||
help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
|
||||
help_menu->add_action(GUI::CommonActions::make_about_action("Maps", app_icon, window));
|
||||
|
||||
// Main toolbar actions
|
||||
toolbar->add_action(zoom_in_action);
|
||||
toolbar->add_action(zoom_out_action);
|
||||
toolbar->add_action(reset_zoom_action);
|
||||
|
||||
window->show();
|
||||
|
||||
// Remember last map position
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue