Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made serialization of Command toggleable when saving InputEvents. #43662

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions core/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ int64_t InputEventFromWindow::get_window_id() const {

///////////////////////////////////

void InputEventWithModifiers::set_store_command(bool p_enabled) {
store_command = p_enabled;
}

bool InputEventWithModifiers::is_storing_command() const {
return store_command;
}

void InputEventWithModifiers::set_shift(bool p_enabled) {
shift = p_enabled;
}
Expand Down Expand Up @@ -186,6 +194,9 @@ void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModif
}

void InputEventWithModifiers::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_store_command", "enable"), &InputEventWithModifiers::set_store_command);
ClassDB::bind_method(D_METHOD("is_storing_command"), &InputEventWithModifiers::is_storing_command);

ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt);
ClassDB::bind_method(D_METHOD("get_alt"), &InputEventWithModifiers::get_alt);

Expand All @@ -201,13 +212,36 @@ void InputEventWithModifiers::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_command", "enable"), &InputEventWithModifiers::set_command);
ClassDB::bind_method(D_METHOD("get_command"), &InputEventWithModifiers::get_command);

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "store_command"), "set_store_command", "is_storing_command");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt"), "set_alt", "get_alt");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift"), "set_shift", "get_shift");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "control"), "set_control", "get_control");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta"), "set_metakey", "get_metakey");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command"), "set_command", "get_command");
}

void InputEventWithModifiers::_validate_property(PropertyInfo &property) const {
if (store_command) {
// If we only want to Store "Command".
#ifdef APPLE_STYLE_KEYS
// Don't store "Meta" on Mac.
if (property.name == "meta") {
property.usage ^= PROPERTY_USAGE_STORAGE;
}
#else
// Don't store "Control".
if (property.name == "control") {
property.usage ^= PROPERTY_USAGE_STORAGE;
}
#endif
} else {
// We don't want to store command, only control or meta (on mac).
if (property.name == "command") {
property.usage ^= PROPERTY_USAGE_STORAGE;
}
}
}

///////////////////////////////////

void InputEventKey::set_pressed(bool p_pressed) {
Expand Down
6 changes: 6 additions & 0 deletions core/input/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class InputEventFromWindow : public InputEvent {
class InputEventWithModifiers : public InputEventFromWindow {
GDCLASS(InputEventWithModifiers, InputEventFromWindow);

bool store_command = true;

bool shift = false;
bool alt = false;
#ifdef APPLE_STYLE_KEYS
Expand All @@ -228,8 +230,12 @@ class InputEventWithModifiers : public InputEventFromWindow {

protected:
static void _bind_methods();
virtual void _validate_property(PropertyInfo &property) const override;

public:
void set_store_command(bool p_enabled);
bool is_storing_command() const;

void set_shift(bool p_enabled);
bool get_shift() const;

Expand Down
4 changes: 4 additions & 0 deletions doc/classes/InputEventWithModifiers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<member name="shift" type="bool" setter="set_shift" getter="get_shift" default="false">
State of the [kbd]Shift[/kbd] modifier.
</member>
<member name="store_command" type="bool" setter="set_store_command" getter="is_storing_command" default="true">
If [code]true[/code], pressing [kbd]Cmd[/kbd] on macOS or [kbd]Ctrl[/kbd] on all other platforms will both be serialized as [member command]. If [code]false[/code], those same keys will be serialized as [member meta] on macOS and [member control] on all other platforms.
This aids with cross-platform compatibility when developing e.g. on Windows for macOS, or vice-versa.
</member>
</members>
<constants>
</constants>
Expand Down