Skip to content

Commit c7066d6

Browse files
authored
Merge pull request #44 from antkeeper/gh-42-fullscreen-option-outdated
GH-42: Update fullscreen option in graphics menu when fullscreen state is changed.
2 parents 59e3a3c + c33dcae commit c7066d6

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

src/engine/app/sdl/sdl-window.cpp

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -257,45 +257,47 @@ void sdl_window::set_maximized(bool maximized)
257257

258258
void sdl_window::set_fullscreen(bool fullscreen)
259259
{
260-
if (fullscreen != m_fullscreen)
260+
if (m_fullscreen == fullscreen)
261261
{
262-
if (fullscreen)
263-
{
264-
const SDL_DisplayMode* sdl_display_mode = nullptr;
265-
266-
if (const auto sdl_window_display_id = SDL_GetDisplayForWindow(m_internal_window); sdl_window_display_id)
267-
{
268-
sdl_display_mode = SDL_GetDesktopDisplayMode(sdl_window_display_id);
269-
}
262+
return;
263+
}
270264

271-
SDL_SetWindowFullscreenMode(m_internal_window, sdl_display_mode);
272-
}
265+
if (fullscreen)
266+
{
267+
const SDL_DisplayMode* sdl_display_mode = nullptr;
273268

274-
// Hide cursor if visible
275-
const bool is_cursor_visible = SDL_CursorVisible();
276-
if (is_cursor_visible)
269+
if (const auto sdl_window_display_id = SDL_GetDisplayForWindow(m_internal_window); sdl_window_display_id)
277270
{
278-
SDL_HideCursor();
271+
sdl_display_mode = SDL_GetDesktopDisplayMode(sdl_window_display_id);
279272
}
280273

281-
// Save global mouse position
282-
math::fvec2 mouse_position = {};
283-
SDL_GetGlobalMouseState(&mouse_position.x(), &mouse_position.y());
274+
SDL_SetWindowFullscreenMode(m_internal_window, sdl_display_mode);
275+
}
276+
277+
// Hide cursor if visible
278+
const bool is_cursor_visible = SDL_CursorVisible();
279+
if (is_cursor_visible)
280+
{
281+
SDL_HideCursor();
282+
}
284283

285-
// Change fullscreen state
286-
SDL_SetWindowFullscreen(m_internal_window, fullscreen);
284+
// Save global mouse position
285+
math::fvec2 mouse_position = {};
286+
SDL_GetGlobalMouseState(&mouse_position.x(), &mouse_position.y());
287287

288-
// Restore global mouse position
289-
SDL_WarpMouseGlobal(mouse_position.x(), mouse_position.y());
288+
// Change fullscreen state
289+
SDL_SetWindowFullscreen(m_internal_window, fullscreen);
290290

291-
// Restore cursor visibility
292-
if (is_cursor_visible)
293-
{
294-
SDL_ShowCursor();
295-
}
296-
297-
m_fullscreen = fullscreen;
291+
// Restore global mouse position
292+
SDL_WarpMouseGlobal(mouse_position.x(), mouse_position.y());
293+
294+
// Restore cursor visibility
295+
if (is_cursor_visible)
296+
{
297+
SDL_ShowCursor();
298298
}
299+
300+
m_fullscreen = fullscreen;
299301
}
300302

301303
void sdl_window::set_v_sync(bool v_sync)

src/game/game.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include <engine/hash/fnv.hpp>
7373
#include <engine/utility/paths.hpp>
7474
#include <engine/ui/label.hpp>
75+
#include <engine/ui/range.hpp>
7576
#include <entt/entt.hpp>
7677
#include <filesystem>
7778
#include <functional>
@@ -915,6 +916,33 @@ void game::setup_ui()
915916

916917
// Re-align debug text
917918
frame_time_text->set_translation({std::round(0.0f), std::round(viewport_size.y() - debug_font->get_metrics().size), 99.0f});
919+
920+
if (m_graphics_menu_container)
921+
{
922+
// HACK: Find the fullscreen button based on the string of its label and click it
923+
const auto fullscreen_string = get_string(*this, "graphics_menu_fullscreen");
924+
925+
std::shared_ptr<ui::element> fullscreen_button;
926+
m_graphics_menu_container->visit_descendants
927+
(
928+
[&](auto& descendant)
929+
{
930+
if (descendant.get_type() == ui::element_type::label)
931+
{
932+
auto& label = static_cast<ui::label&>(descendant);
933+
if (label.get_text() == fullscreen_string)
934+
{
935+
fullscreen_button = label.get_focus_right().lock();
936+
}
937+
}
938+
}
939+
);
940+
941+
if (fullscreen_button && fullscreen_button->get_type() == ui::element_type::range)
942+
{
943+
static_cast<ui::range&>(*fullscreen_button).set_value(window->is_fullscreen());
944+
}
945+
}
918946
}
919947
);
920948

src/game/menu.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,11 @@ namespace
13691369
(
13701370
[&ctx](const auto& event)
13711371
{
1372+
if (ctx.window->is_fullscreen() == static_cast<bool>(event.value))
1373+
{
1374+
return true;
1375+
}
1376+
13721377
ctx.window->set_fullscreen(static_cast<bool>(event.value));
13731378
return ctx.window->is_fullscreen() == static_cast<bool>(event.value);
13741379
}

0 commit comments

Comments
 (0)