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

Action Event Layers, multifocus for split screen #20091

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions core/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ List<Ref<InputEvent> >::Element *InputMap::_find_event(Action &p_action, const R
//if (e.type != Ref<InputEvent>::KEY && e.device != p_event.device) -- unsure about the KEY comparison, why is this here?
// continue;

if (e->get_layer() != p_event->get_layer())
continue;

int device = e->get_device();
if (device == ALL_DEVICES || device == p_event->get_device()) {
if (e->action_match(p_event, p_pressed, p_strength, p_action.deadzone)) {
Expand Down
13 changes: 13 additions & 0 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ int InputEvent::get_device() const {
return device;
}

void InputEvent::set_layer(int p_layer) {
layer = p_layer;
}

int InputEvent::get_layer() const {
return layer;
}

bool InputEvent::is_action(const StringName &p_action) const {

return InputMap::get_singleton()->event_is_action(Ref<InputEvent>((InputEvent *)this), p_action);
Expand Down Expand Up @@ -108,6 +116,9 @@ void InputEvent::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_device", "device"), &InputEvent::set_device);
ClassDB::bind_method(D_METHOD("get_device"), &InputEvent::get_device);

ClassDB::bind_method(D_METHOD("set_layer", "layer"), &InputEvent::set_layer);
ClassDB::bind_method(D_METHOD("get_layer"), &InputEvent::get_layer);

ClassDB::bind_method(D_METHOD("is_action", "action"), &InputEvent::is_action);
ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &InputEvent::is_action_pressed);
ClassDB::bind_method(D_METHOD("is_action_released", "action"), &InputEvent::is_action_released);
Expand All @@ -125,11 +136,13 @@ void InputEvent::_bind_methods() {
ClassDB::bind_method(D_METHOD("xformed_by", "xform", "local_ofs"), &InputEvent::xformed_by, DEFVAL(Vector2()));

ADD_PROPERTY(PropertyInfo(Variant::INT, "device"), "set_device", "get_device");
ADD_PROPERTY(PropertyInfo(Variant::INT, "layer"), "set_layer", "get_layer");
}

InputEvent::InputEvent() {

device = 0;
layer = 0;
}

//////////////////
Expand Down
4 changes: 4 additions & 0 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class InputEvent : public Resource {
GDCLASS(InputEvent, Resource)

int device;
int layer;

protected:
static void _bind_methods();
Expand All @@ -157,6 +158,9 @@ class InputEvent : public Resource {
void set_device(int p_device);
int get_device() const;

void set_layer(int p_layer);
int get_layer() const;

bool is_action(const StringName &p_action) const;
bool is_action_pressed(const StringName &p_action) const;
bool is_action_released(const StringName &p_action) const;
Expand Down
46 changes: 39 additions & 7 deletions editor/project_settings_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,34 @@ void ProjectSettingsEditor::_action_edited() {

undo_redo->create_action(TTR("Change Action deadzone"));
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", name, new_action);
undo_redo->add_do_method(this, "_update_actions");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", name, old_action);
undo_redo->add_undo_method(this, "_update_actions");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
} else if (input_editor->get_selected_column() == 2) {

// Change action layer
String name = "input/" + ti->get_parent()->get_text(0);
Dictionary old_val = ProjectSettings::get_singleton()->get(name);
Dictionary action = old_val.duplicate();
int idx = ti->get_metadata(0);

Array events = action["events"];
ERR_FAIL_INDEX(idx, events.size());

Ref<InputEvent> ie = events[idx];
ERR_FAIL_COND(!ie.is_valid());

ie->set_layer(ti->get_range(2));

undo_redo->create_action(TTR("Change Action Event Layer"));
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", name, action);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", name, old_val);
undo_redo->add_undo_method(this, "_update_actions");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
}
}

Expand Down Expand Up @@ -684,10 +706,12 @@ void ProjectSettingsEditor::_update_actions() {
item->set_range(1, action["deadzone"]);
item->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));

item->add_button(2, get_icon("Add", "EditorIcons"), 1, false, TTR("Add Event"));
item->set_custom_bg_color(2, get_color("prop_subsection", "Editor"));

item->add_button(3, get_icon("Add", "EditorIcons"), 1, false, TTR("Add Event"));
if (!ProjectSettings::get_singleton()->get_input_presets().find(pi.name)) {
item->add_button(2, get_icon("Remove", "EditorIcons"), 2, false, TTR("Remove"));
item->set_editable(2, true);
item->add_button(3, get_icon("Remove", "EditorIcons"), 2, false, TTR("Remove"));
item->set_editable(3, true);
}

for (int i = 0; i < events.size(); i++) {
Expand Down Expand Up @@ -760,8 +784,13 @@ void ProjectSettingsEditor::_update_actions() {
action->set_metadata(0, i);
action->set_meta("__input", event);

action->add_button(2, get_icon("Edit", "EditorIcons"), 3, false, TTR("Edit"));
action->add_button(2, get_icon("Remove", "EditorIcons"), 2, false, TTR("Remove"));
action->set_editable(2, true);
action->set_cell_mode(2, TreeItem::CELL_MODE_RANGE);
action->set_range_config(2, 0.0, 20.0, 1.0);
action->set_range(2, event->get_layer());

action->add_button(3, get_icon("Edit", "EditorIcons"), 3, false, TTR("Edit"));
action->add_button(3, get_icon("Remove", "EditorIcons"), 2, false, TTR("Remove"));
}
}

Expand Down Expand Up @@ -1796,14 +1825,17 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
input_editor = memnew(Tree);
vbc->add_child(input_editor);
input_editor->set_v_size_flags(SIZE_EXPAND_FILL);
input_editor->set_columns(3);
input_editor->set_columns(4);
input_editor->set_column_titles_visible(true);
input_editor->set_column_title(0, TTR("Action"));
input_editor->set_column_title(1, TTR("Deadzone"));
input_editor->set_column_expand(1, false);
input_editor->set_column_min_width(1, 80);
input_editor->set_column_title(2, TTR("Layer"));
input_editor->set_column_expand(2, false);
input_editor->set_column_min_width(2, 50);
input_editor->set_column_expand(3, false);
input_editor->set_column_min_width(3, 50);
input_editor->connect("item_edited", this, "_action_edited");
input_editor->connect("item_activated", this, "_action_activated");
input_editor->connect("cell_selected", this, "_action_selected");
Expand Down
1 change: 1 addition & 0 deletions scene/gui/viewport_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ void ViewportContainer::_input(const Ref<InputEvent> &p_event) {
if (!c || c->is_input_disabled())
continue;

ev->set_layer(c->get_focus_layer());
c->input(ev);
}
}
Expand Down
30 changes: 27 additions & 3 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ void Viewport::_notification(int p_what) {
find_world_2d()->_register_viewport(this, Rect2());

add_to_group("_viewports");
add_to_group(focus_group);
if (get_tree()->is_debugging_collisions_hint()) {
//2D
Physics2DServer::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
Expand Down Expand Up @@ -351,7 +352,7 @@ void Viewport::_notification(int p_what) {
}

remove_from_group("_viewports");

remove_from_group(focus_group);
VS::get_singleton()->viewport_set_active(viewport, false);

} break;
Expand Down Expand Up @@ -1187,7 +1188,10 @@ Ref<InputEvent> Viewport::_make_input_local(const Ref<InputEvent> &ev) {
Vector2 vp_ofs = _get_window_offset();
Transform2D ai = get_final_transform().affine_inverse() * _get_input_pre_xform();

return ev->xformed_by(ai, -vp_ofs);
Ref<InputEvent> out = ev->xformed_by(ai, -vp_ofs);
out->set_layer(get_focus_layer());

return out;
}

void Viewport::_vp_input_text(const String &p_text) {
Expand Down Expand Up @@ -1272,6 +1276,21 @@ void Viewport::_gui_prepare_subwindows() {
_gui_sort_subwindows();
}

void Viewport::set_focus_layer(int p_focus_layer) {
StringName old = focus_group;
focus_layer = p_focus_layer;
focus_group = "_viewport_focus_" + itos(focus_layer);

if (is_inside_tree()) {
remove_from_group(old);
add_to_group(focus_group);
}
}

int Viewport::get_focus_layer() const {
return focus_layer;
}

void Viewport::_gui_sort_subwindows() {

if (!gui.subwindow_order_dirty)
Expand Down Expand Up @@ -2337,7 +2356,7 @@ void Viewport::_gui_control_grab_focus(Control *p_control) {
//no need for change
if (gui.key_focus && gui.key_focus == p_control)
return;
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "_gui_remove_focus");
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, focus_group, "_gui_remove_focus");
gui.key_focus = p_control;
p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
p_control->update();
Expand Down Expand Up @@ -2740,6 +2759,9 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
ClassDB::bind_method(D_METHOD("warp_mouse", "to_position"), &Viewport::warp_mouse);

ClassDB::bind_method(D_METHOD("set_focus_layer", "focus_layer"), &Viewport::set_focus_layer);
ClassDB::bind_method(D_METHOD("get_focus_layer"), &Viewport::get_focus_layer);

ClassDB::bind_method(D_METHOD("gui_has_modal_stack"), &Viewport::gui_has_modal_stack);
ClassDB::bind_method(D_METHOD("gui_get_drag_data"), &Viewport::gui_get_drag_data);

Expand Down Expand Up @@ -2791,6 +2813,7 @@ void Viewport::_bind_methods() {
ADD_GROUP("Physics", "physics_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking"), "set_physics_object_picking", "get_physics_object_picking");
ADD_GROUP("GUI", "gui_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "gui_focus_layer", PROPERTY_HINT_RANGE, "0,20"), "set_focus_layer", "get_focus_layer");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_disable_input"), "set_disable_input", "is_input_disabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_snap_controls_to_pixels"), "set_snap_controls_to_pixels", "is_snap_controls_to_pixels_enabled");
ADD_GROUP("Shadow Atlas", "shadow_atlas_");
Expand Down Expand Up @@ -2906,6 +2929,7 @@ Viewport::Viewport() {
unhandled_input_group = "_vp_unhandled_input" + id;
unhandled_key_input_group = "_vp_unhandled_key_input" + id;

set_focus_layer(0);
disable_input = false;
disable_3d = false;
keep_3d_linear = false;
Expand Down
5 changes: 5 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ class Viewport : public Node {
} gui;

bool disable_input;
int focus_layer;
StringName focus_group;

void _gui_call_input(Control *p_control, const Ref<InputEvent> &p_input);
void _gui_prepare_subwindows();
Expand Down Expand Up @@ -433,6 +435,9 @@ class Viewport : public Node {
void input(const Ref<InputEvent> &p_event);
void unhandled_input(const Ref<InputEvent> &p_event);

void set_focus_layer(int p_focus_layer);
int get_focus_layer() const;

void set_disable_input(bool p_disable);
bool is_input_disabled() const;

Expand Down