Skip to content

Commit

Permalink
Toggle fullscreen mode when pressing Alt + Enter by default
Browse files Browse the repository at this point in the history
Alt + Enter is a widely followed convention to toggle fullscreen.
This adds support for it both in the editor and projects, as long
as the window is configured to be resizable.

The rationale for providing this feature in all projects is that
many developers don't bother about adding a fullscreen toggle,
especially for smaller/gamejam games. Yet, this convention is
followed by many popular games out there, including AAA games.

The shortcut can be modified or disabled by editing the Input Map
in the Project Settings.
  • Loading branch information
Calinou committed May 12, 2023
1 parent fd4a06c commit 24e441c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ ProjectSettings::ProjectSettings() {
GLOBAL_DEF("display/window/size/transparent", false);
GLOBAL_DEF("display/window/size/extend_to_title", false);
GLOBAL_DEF("display/window/size/no_focus", false);
GLOBAL_DEF("display/window/size/enable_toggle_fullscreen_shortcut", true);

GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/size/window_width_override", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"), 0); // 8K resolution
GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/size/window_height_override", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"), 0); // 8K resolution
Expand Down
6 changes: 6 additions & 0 deletions core/input/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
{ "ui_filedialog_refresh", TTRC("Refresh") },
{ "ui_filedialog_show_hidden", TTRC("Show Hidden") },
{ "ui_swap_input_direction ", TTRC("Swap Input Direction") },
{ "ui_toggle_fullscreen", TTRC("Toggle Fullscreen") },
{ "", ""}
/* clang-format on */
};
Expand Down Expand Up @@ -720,6 +721,11 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD_OR_CTRL));
default_builtin_cache.insert("ui_swap_input_direction", inputs);

// ///// Miscellaneous Shortcuts /////
inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::ALT));
default_builtin_cache.insert("ui_toggle_fullscreen", inputs);

return default_builtin_cache;
}

Expand Down
3 changes: 3 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,9 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}
}

if (!bool(GLOBAL_GET("display/window/size/enable_toggle_fullscreen_shortcut"))) {
window_flags |= DisplayServer::WINDOW_FLAG_FULLSCREEN_TOGGLE_ENABLED_BIT;
}
if (!bool(GLOBAL_GET("display/window/size/resizable"))) {
window_flags |= DisplayServer::WINDOW_FLAG_RESIZE_DISABLED_BIT;
}
Expand Down
14 changes: 14 additions & 0 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,18 @@ void Window::_window_input(const Ref<InputEvent> &p_ev) {
emit_signal(SceneStringNames::get_singleton()->window_input, p_ev);
}

print_line(flags[FLAG_FULLSCREEN_TOGGLE_ENABLED]);
if (flags[FLAG_FULLSCREEN_TOGGLE_ENABLED] && p_ev->is_action_pressed("ui_toggle_fullscreen")) {
if (mode == MODE_FULLSCREEN || mode == MODE_EXCLUSIVE_FULLSCREEN) {
print_line("Going to previous mode: ", itos(toggle_fullscreen_previous_mode));
set_mode(toggle_fullscreen_previous_mode);
} else {
print_line("Going fullscreen");
toggle_fullscreen_previous_mode = mode;
set_mode(MODE_FULLSCREEN);
}
}

if (is_inside_tree()) {
push_input(p_ev);
}
Expand Down Expand Up @@ -2456,6 +2468,7 @@ void Window::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "fullscreen_shortcut_enabled"), "set_flag", "get_flag", FLAG_FULLSCREEN_TOGGLE_ENABLED);

ADD_GROUP("Limits", "");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
Expand Down Expand Up @@ -2505,6 +2518,7 @@ void Window::_bind_methods() {
BIND_ENUM_CONSTANT(FLAG_POPUP);
BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
BIND_ENUM_CONSTANT(FLAG_MOUSE_PASSTHROUGH);
BIND_ENUM_CONSTANT(FLAG_FULLSCREEN_TOGGLE_ENABLED);
BIND_ENUM_CONSTANT(FLAG_MAX);

BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
Expand Down
3 changes: 3 additions & 0 deletions scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Window : public Viewport {
FLAG_POPUP = DisplayServer::WINDOW_FLAG_POPUP,
FLAG_EXTEND_TO_TITLE = DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE,
FLAG_MOUSE_PASSTHROUGH = DisplayServer::WINDOW_FLAG_MOUSE_PASSTHROUGH,
FLAG_FULLSCREEN_TOGGLE_ENABLED = DisplayServer::WINDOW_FLAG_FULLSCREEN_TOGGLE_ENABLED,
FLAG_MAX = DisplayServer::WINDOW_FLAG_MAX,
};

Expand Down Expand Up @@ -109,12 +110,14 @@ class Window : public Viewport {
mutable Size2i max_size;
mutable Vector<Vector2> mpath;
mutable Mode mode = MODE_WINDOWED;
mutable Mode toggle_fullscreen_previous_mode = mode;
mutable bool flags[FLAG_MAX] = {};
bool visible = true;
bool focused = false;
WindowInitialPosition initial_position = WINDOW_INITIAL_POSITION_ABSOLUTE;

bool use_font_oversampling = false;
bool fullscreen_shortcut_enabled = false;
bool transient = false;
bool exclusive = false;
bool wrap_controls = false;
Expand Down
1 change: 1 addition & 0 deletions servers/display_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ void DisplayServer::_bind_methods() {
BIND_ENUM_CONSTANT(WINDOW_FLAG_POPUP);
BIND_ENUM_CONSTANT(WINDOW_FLAG_EXTEND_TO_TITLE);
BIND_ENUM_CONSTANT(WINDOW_FLAG_MOUSE_PASSTHROUGH);
BIND_ENUM_CONSTANT(WINDOW_FLAG_FULLSCREEN_TOGGLE_ENABLED);
BIND_ENUM_CONSTANT(WINDOW_FLAG_MAX);

BIND_ENUM_CONSTANT(WINDOW_EVENT_MOUSE_ENTER);
Expand Down
2 changes: 2 additions & 0 deletions servers/display_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ class DisplayServer : public Object {
WINDOW_FLAG_POPUP,
WINDOW_FLAG_EXTEND_TO_TITLE,
WINDOW_FLAG_MOUSE_PASSTHROUGH,
WINDOW_FLAG_FULLSCREEN_TOGGLE_ENABLED,
WINDOW_FLAG_MAX,
};

Expand All @@ -351,6 +352,7 @@ class DisplayServer : public Object {
WINDOW_FLAG_POPUP_BIT = (1 << WINDOW_FLAG_POPUP),
WINDOW_FLAG_EXTEND_TO_TITLE_BIT = (1 << WINDOW_FLAG_EXTEND_TO_TITLE),
WINDOW_FLAG_MOUSE_PASSTHROUGH_BIT = (1 << WINDOW_FLAG_MOUSE_PASSTHROUGH),
WINDOW_FLAG_FULLSCREEN_TOGGLE_ENABLED_BIT = (1 << WINDOW_FLAG_FULLSCREEN_TOGGLE_ENABLED),
};

virtual WindowID create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i());
Expand Down

0 comments on commit 24e441c

Please sign in to comment.