From 8714e0f875788299939edcb00c5bd715a8c2b748 Mon Sep 17 00:00:00 2001 From: Vankata453 <78196474+Vankata453@users.noreply.github.com> Date: Sat, 3 Aug 2024 01:43:52 +0300 Subject: [PATCH] `Screen`: Virtual methods for SDL and window resize events The `Screen` abstract class now provides virtual methods for SDL and window resize events. The `Editor` and `PartcleEditor` classes now `override` the `event(const SDL_Event& ev)` method and `Editor` `override`s `on_window_resize()`. Resize events in `ScreenManager` are now handled by all `Screen`s in the stack. SDL events are only handled by the current (top) screen. Additionally, `OptionsMenu` now calls `ScreenManager::on_window_resize()` on video setting changes, instead of just calling `MenuManager::on_window_resize()`. This fixes a bug where the editor crashes on changing the "Video Resolution" and "Magnification" settings from the in-editor "Options" menu. --- src/editor/button_widget.cpp | 2 +- src/editor/button_widget.hpp | 2 +- src/editor/editor.cpp | 4 ++-- src/editor/editor.hpp | 4 ++-- src/editor/layers_widget.cpp | 4 ++-- src/editor/layers_widget.hpp | 2 +- src/editor/overlay_widget.cpp | 2 +- src/editor/overlay_widget.hpp | 2 +- src/editor/particle_editor.hpp | 2 +- src/editor/tilebox.cpp | 6 +++--- src/editor/tilebox.hpp | 2 +- src/editor/toolbox_widget.cpp | 6 +++--- src/editor/toolbox_widget.hpp | 2 +- src/editor/widget.cpp | 6 ------ src/editor/widget.hpp | 2 +- src/supertux/menu/options_menu.cpp | 12 ++++++------ src/supertux/screen.hpp | 11 +++++++++++ src/supertux/screen_manager.cpp | 25 ++++++++++++------------- src/supertux/screen_manager.hpp | 2 ++ 19 files changed, 52 insertions(+), 46 deletions(-) diff --git a/src/editor/button_widget.cpp b/src/editor/button_widget.cpp index b742197d85c..2fd570fe1f9 100644 --- a/src/editor/button_widget.cpp +++ b/src/editor/button_widget.cpp @@ -62,7 +62,7 @@ ButtonWidget::setup() } void -ButtonWidget::resize() +ButtonWidget::on_window_resize() { } diff --git a/src/editor/button_widget.hpp b/src/editor/button_widget.hpp index 363bf4c01df..747d8cbbcb3 100644 --- a/src/editor/button_widget.hpp +++ b/src/editor/button_widget.hpp @@ -39,7 +39,7 @@ class ButtonWidget : public Widget virtual void update(float dt_sec) override; virtual void setup() override; - virtual void resize() override; + virtual void on_window_resize() override; virtual bool on_mouse_button_up(const SDL_MouseButtonEvent& button) override; virtual bool on_mouse_button_down(const SDL_MouseButtonEvent& button) override; diff --git a/src/editor/editor.cpp b/src/editor/editor.cpp index 47219e2df2d..4c9b0571c42 100644 --- a/src/editor/editor.cpp +++ b/src/editor/editor.cpp @@ -840,11 +840,11 @@ Editor::setup() } void -Editor::resize() +Editor::on_window_resize() { for(const auto& widget: m_widgets) { - widget->resize(); + widget->on_window_resize(); } } diff --git a/src/editor/editor.hpp b/src/editor/editor.hpp index 1b47d688aec..d31a5b80cae 100644 --- a/src/editor/editor.hpp +++ b/src/editor/editor.hpp @@ -82,8 +82,8 @@ class Editor final : public Screen, virtual IntegrationStatus get_status() const override; - void event(const SDL_Event& ev); - void resize(); + void event(const SDL_Event& ev) override; + void on_window_resize() override; void disable_keyboard() { m_enabled = false; } diff --git a/src/editor/layers_widget.cpp b/src/editor/layers_widget.cpp index 5b7067a58df..61d9bf78f1d 100644 --- a/src/editor/layers_widget.cpp +++ b/src/editor/layers_widget.cpp @@ -350,7 +350,7 @@ EditorLayersWidget::has_mouse_focus() const } void -EditorLayersWidget::resize() +EditorLayersWidget::on_window_resize() { m_Ypos = SCREEN_HEIGHT - 32; m_Width = SCREEN_WIDTH - 128; @@ -359,7 +359,7 @@ EditorLayersWidget::resize() void EditorLayersWidget::setup() { - resize(); + on_window_resize(); } void diff --git a/src/editor/layers_widget.hpp b/src/editor/layers_widget.hpp index 79551c014bc..e974fa91e26 100644 --- a/src/editor/layers_widget.hpp +++ b/src/editor/layers_widget.hpp @@ -59,7 +59,7 @@ class EditorLayersWidget final : public Widget virtual bool on_mouse_wheel(const SDL_MouseWheelEvent& wheel) override; virtual void setup() override; - virtual void resize() override; + virtual void on_window_resize() override; void refresh(); diff --git a/src/editor/overlay_widget.cpp b/src/editor/overlay_widget.cpp index 6e33179d161..6bf19cdd0fd 100644 --- a/src/editor/overlay_widget.cpp +++ b/src/editor/overlay_widget.cpp @@ -1240,7 +1240,7 @@ EditorOverlayWidget::on_key_down(const SDL_KeyboardEvent& key) } void -EditorOverlayWidget::resize() +EditorOverlayWidget::on_window_resize() { update_pos(); } diff --git a/src/editor/overlay_widget.hpp b/src/editor/overlay_widget.hpp index 3da98b8a8fc..89b6b9388f6 100644 --- a/src/editor/overlay_widget.hpp +++ b/src/editor/overlay_widget.hpp @@ -61,7 +61,7 @@ class EditorOverlayWidget final : public Widget virtual bool on_mouse_motion(const SDL_MouseMotionEvent& motion) override; virtual bool on_key_up(const SDL_KeyboardEvent& key) override; virtual bool on_key_down(const SDL_KeyboardEvent& key) override; - virtual void resize() override; + virtual void on_window_resize() override; void update_pos(); void delete_markers(); diff --git a/src/editor/particle_editor.hpp b/src/editor/particle_editor.hpp index 166881244cb..0be83856831 100644 --- a/src/editor/particle_editor.hpp +++ b/src/editor/particle_editor.hpp @@ -59,7 +59,7 @@ class ParticleEditor final : public Screen, virtual IntegrationStatus get_status() const override; - void event(const SDL_Event& ev); + void event(const SDL_Event& ev) override; void update_keyboard(const Controller& controller); void check_unsaved_changes(const std::function& action); void quit_editor(); diff --git a/src/editor/tilebox.cpp b/src/editor/tilebox.cpp index fc63e1ba612..bc082898499 100644 --- a/src/editor/tilebox.cpp +++ b/src/editor/tilebox.cpp @@ -310,7 +310,7 @@ EditorTilebox::update_hovered_tile() } void -EditorTilebox::resize() +EditorTilebox::on_window_resize() { m_scrollbar->set_covered_region(m_rect.get_height()); m_scrollbar->set_total_region(get_tiles_height()); @@ -320,7 +320,7 @@ EditorTilebox::resize() void EditorTilebox::setup() { - resize(); + on_window_resize(); m_tiles->set_tile(0); } @@ -328,7 +328,7 @@ void EditorTilebox::set_rect(const Rectf& rect) { m_rect = rect; - resize(); + on_window_resize(); m_hovered_item = HoveredItem::NONE; m_hovered_tile = -1; diff --git a/src/editor/tilebox.hpp b/src/editor/tilebox.hpp index 331c5cc91a7..c76392d18f1 100644 --- a/src/editor/tilebox.hpp +++ b/src/editor/tilebox.hpp @@ -67,7 +67,7 @@ class EditorTilebox final : public Widget virtual bool on_mouse_wheel(const SDL_MouseWheelEvent& wheel) override; virtual void setup() override; - virtual void resize() override; + virtual void on_window_resize() override; void set_rect(const Rectf& rect); Rectf get_rect() const { return m_rect; } diff --git a/src/editor/toolbox_widget.cpp b/src/editor/toolbox_widget.cpp index 3a794e739a6..00fc2b1f3d4 100644 --- a/src/editor/toolbox_widget.cpp +++ b/src/editor/toolbox_widget.cpp @@ -252,7 +252,7 @@ EditorToolboxWidget::on_mouse_wheel(const SDL_MouseWheelEvent& wheel) } void -EditorToolboxWidget::resize() +EditorToolboxWidget::on_window_resize() { m_pos_x = static_cast(SCREEN_WIDTH - 128); m_tilebox->set_rect(Rectf(Vector(m_pos_x, 96.f), @@ -264,7 +264,7 @@ EditorToolboxWidget::resize() m_move_mode->m_pos = Vector(m_pos_x + 64.0f, 64.0f); m_undo_mode->m_pos = Vector(m_pos_x + 96.0f, 64.0f); - m_tilebox->resize(); + m_tilebox->on_window_resize(); } void @@ -272,7 +272,7 @@ EditorToolboxWidget::setup() { m_tilebox->setup(); - resize(); + on_window_resize(); m_tilebox->get_tiles()->set_tile(0); } diff --git a/src/editor/toolbox_widget.hpp b/src/editor/toolbox_widget.hpp index a9f2eb3ed70..521a0c7dbfb 100644 --- a/src/editor/toolbox_widget.hpp +++ b/src/editor/toolbox_widget.hpp @@ -51,7 +51,7 @@ class EditorToolboxWidget final : public Widget virtual bool on_mouse_wheel(const SDL_MouseWheelEvent& wheel) override; virtual void setup() override; - virtual void resize() override; + virtual void on_window_resize() override; void select_tilegroup(int id); void select_objectgroup(int id); diff --git a/src/editor/widget.cpp b/src/editor/widget.cpp index d5b2e0d1ef7..7aabeef207d 100644 --- a/src/editor/widget.cpp +++ b/src/editor/widget.cpp @@ -39,12 +39,6 @@ Widget::event(const SDL_Event& ev) case SDL_KEYUP: return on_key_up(ev.key); - case SDL_WINDOWEVENT: - if (ev.window.event == SDL_WINDOWEVENT_RESIZED) { - resize(); - } - return false; - default: return false; } diff --git a/src/editor/widget.hpp b/src/editor/widget.hpp index 6c6f27ca64f..bef38e4b763 100644 --- a/src/editor/widget.hpp +++ b/src/editor/widget.hpp @@ -33,7 +33,7 @@ class Widget virtual void update(float dt_sec) {} virtual void setup() {} - virtual void resize() {} + virtual void on_window_resize() {} virtual bool on_mouse_button_up(const SDL_MouseButtonEvent& button) { return false; } virtual bool on_mouse_button_down(const SDL_MouseButtonEvent& button) { return false; } diff --git a/src/supertux/menu/options_menu.cpp b/src/supertux/menu/options_menu.cpp index c06f2eeef1c..9d120d7bc57 100644 --- a/src/supertux/menu/options_menu.cpp +++ b/src/supertux/menu/options_menu.cpp @@ -24,11 +24,11 @@ #include "gui/item_stringselect.hpp" #include "gui/item_toggle.hpp" #include "gui/menu_item.hpp" -#include "gui/menu_manager.hpp" #include "supertux/gameconfig.hpp" #include "supertux/game_session.hpp" #include "supertux/globals.hpp" #include "supertux/menu/menu_storage.hpp" +#include "supertux/screen_manager.hpp" #include "supertux/title_screen.hpp" #include "util/gettext.hpp" #include "util/log.hpp" @@ -579,13 +579,13 @@ OptionsMenu::menu_action(MenuItem& item) { g_config->aspect_size = Size(0, 0); // Magic values VideoSystem::current()->apply_config(); - MenuManager::instance().on_window_resize(); + ScreenManager::current()->on_window_resize(); } else if (sscanf(m_aspect_ratios.list[m_aspect_ratios.next].c_str(), "%d:%d", &g_config->aspect_size.width, &g_config->aspect_size.height) == 2) { VideoSystem::current()->apply_config(); - MenuManager::instance().on_window_resize(); + ScreenManager::current()->on_window_resize(); } else { @@ -605,7 +605,7 @@ OptionsMenu::menu_action(MenuItem& item) g_config->magnification /= 100.0f; } VideoSystem::current()->apply_config(); - MenuManager::instance().on_window_resize(); + ScreenManager::current()->on_window_resize(); break; case MNID_WINDOW_RESIZABLE: @@ -626,7 +626,7 @@ OptionsMenu::menu_action(MenuItem& item) { g_config->window_size = Size(width, height); VideoSystem::current()->apply_config(); - MenuManager::instance().on_window_resize(); + ScreenManager::current()->on_window_resize(); } } break; @@ -711,7 +711,7 @@ OptionsMenu::menu_action(MenuItem& item) case MNID_FULLSCREEN: VideoSystem::current()->apply_config(); - MenuManager::instance().on_window_resize(); + ScreenManager::current()->on_window_resize(); g_config->save(); break; diff --git a/src/supertux/screen.hpp b/src/supertux/screen.hpp index b94826e07ed..0b65704c818 100644 --- a/src/supertux/screen.hpp +++ b/src/supertux/screen.hpp @@ -21,6 +21,7 @@ class Compositor; class Controller; +union SDL_Event; /** * Abstract base class for code the MainLoop runs exclusively and full-screen. @@ -56,6 +57,16 @@ class Screen */ virtual void update(float dt_sec, const Controller& controller) = 0; + /** + * gets called whenever an SDL event occurs + */ + virtual void event(const SDL_Event& ev) {} + + /** + * gets called whenever the game window is resized or resolution settings are changed + */ + virtual void on_window_resize() {} + /** * Gives details about what the user is doing right now. * diff --git a/src/supertux/screen_manager.cpp b/src/supertux/screen_manager.cpp index 3f3bdd269cc..1343af44ace 100644 --- a/src/supertux/screen_manager.cpp +++ b/src/supertux/screen_manager.cpp @@ -20,8 +20,7 @@ #include "addon/addon_manager.hpp" #include "audio/sound_manager.hpp" -#include "editor/editor.hpp" -#include "editor/particle_editor.hpp" +#include "control/input_manager.hpp" #include "gui/dialog.hpp" #include "gui/menu_manager.hpp" #include "gui/mousecursor.hpp" @@ -205,6 +204,15 @@ ScreenManager::get_speed() const return m_speed; } +void +ScreenManager::on_window_resize() +{ + m_menu_manager->on_window_resize(); + + for (const auto& screen : m_screen_stack) + screen->on_window_resize(); +} + void ScreenManager::draw_fps(DrawingContext& context, FPS_Stats& fps_statistics) { @@ -395,13 +403,7 @@ ScreenManager::process_events() m_menu_manager->event(event); - if (Editor::is_active()) { - Editor::current()->event(event); - } - - if (ParticleEditor::is_active()) { - ParticleEditor::current()->event(event); - } + m_screen_stack.back()->event(event); switch (event.type) { @@ -414,10 +416,7 @@ ScreenManager::process_events() { case SDL_WINDOWEVENT_RESIZED: m_video_system.on_resize(event.window.data1, event.window.data2); - m_menu_manager->on_window_resize(); - if (Editor::is_active()) { - Editor::current()->resize(); - } + on_window_resize(); break; case SDL_WINDOWEVENT_HIDDEN: diff --git a/src/supertux/screen_manager.hpp b/src/supertux/screen_manager.hpp index 91c7eba7e91..b2e7ab46fad 100644 --- a/src/supertux/screen_manager.hpp +++ b/src/supertux/screen_manager.hpp @@ -52,6 +52,8 @@ class ScreenManager final : public Currenton float get_speed() const; bool has_pending_fadeout() const; + void on_window_resize(); + // push new screen on screen_stack void push_screen(std::unique_ptr screen, std::unique_ptr fade = {}); void pop_screen(std::unique_ptr fade = {});