Skip to content

Commit

Permalink
Screen: Virtual methods for SDL and window resize events (#3032)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Vankata453 authored Aug 7, 2024
1 parent 0eef4fb commit 36837d0
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/editor/button_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ButtonWidget::setup()
}

void
ButtonWidget::resize()
ButtonWidget::on_window_resize()
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/editor/button_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
4 changes: 2 additions & 2 deletions src/editor/layers_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -359,7 +359,7 @@ EditorLayersWidget::resize()
void
EditorLayersWidget::setup()
{
resize();
on_window_resize();
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/editor/layers_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/editor/overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ EditorOverlayWidget::on_key_down(const SDL_KeyboardEvent& key)
}

void
EditorOverlayWidget::resize()
EditorOverlayWidget::on_window_resize()
{
update_pos();
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/overlay_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/editor/particle_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void ()>& action);
void quit_editor();
Expand Down
6 changes: 3 additions & 3 deletions src/editor/tilebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -320,15 +320,15 @@ EditorTilebox::resize()
void
EditorTilebox::setup()
{
resize();
on_window_resize();
m_tiles->set_tile(0);
}

void
EditorTilebox::set_rect(const Rectf& rect)
{
m_rect = rect;
resize();
on_window_resize();

m_hovered_item = HoveredItem::NONE;
m_hovered_tile = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/editor/tilebox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
6 changes: 3 additions & 3 deletions src/editor/toolbox_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ EditorToolboxWidget::on_mouse_wheel(const SDL_MouseWheelEvent& wheel)
}

void
EditorToolboxWidget::resize()
EditorToolboxWidget::on_window_resize()
{
m_pos_x = static_cast<float>(SCREEN_WIDTH - 128);
m_tilebox->set_rect(Rectf(Vector(m_pos_x, 96.f),
Expand All @@ -264,15 +264,15 @@ 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
EditorToolboxWidget::setup()
{
m_tilebox->setup();

resize();
on_window_resize();
m_tilebox->get_tiles()->set_tile(0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/editor/toolbox_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/editor/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
12 changes: 6 additions & 6 deletions src/supertux/menu/options_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
{
Expand All @@ -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:
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions src/supertux/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

class Compositor;
class Controller;
union SDL_Event;

/**
* Abstract base class for code the MainLoop runs exclusively and full-screen.
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 12 additions & 13 deletions src/supertux/screen_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/supertux/screen_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ScreenManager final : public Currenton<ScreenManager>
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> screen, std::unique_ptr<ScreenFade> fade = {});
void pop_screen(std::unique_ptr<ScreenFade> fade = {});
Expand Down

0 comments on commit 36837d0

Please sign in to comment.