Path: blob/master/editor/inspector/input_event_editor_plugin.cpp
9896 views
/**************************************************************************/1/* input_event_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "input_event_editor_plugin.h"3132#include "editor/editor_undo_redo_manager.h"33#include "editor/settings/event_listener_line_edit.h"34#include "editor/settings/input_event_configuration_dialog.h"3536void InputEventConfigContainer::_configure_pressed() {37config_dialog->popup_and_configure(input_event);38}3940void InputEventConfigContainer::_event_changed() {41input_event_text->set_text(input_event->as_text());42}4344void InputEventConfigContainer::_config_dialog_confirmed() {45Ref<InputEvent> ie = config_dialog->get_event();4647EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();48undo_redo->create_action(TTR("Event Configured"));4950// When command_or_control_autoremap is toggled to false, it should be set first;51// and when it is toggled to true, it should be set last.52bool will_toggle = false;53bool pending = false;54Ref<InputEventWithModifiers> iewm = input_event;55if (iewm.is_valid()) {56Variant new_value = ie->get("command_or_control_autoremap");57will_toggle = new_value != input_event->get("command_or_control_autoremap");58if (will_toggle) {59pending = new_value;60if (pending) {61undo_redo->add_undo_property(input_event.ptr(), "command_or_control_autoremap", !pending);62} else {63undo_redo->add_do_property(input_event.ptr(), "command_or_control_autoremap", pending);64}65}66}6768List<PropertyInfo> pi;69ie->get_property_list(&pi);70for (const PropertyInfo &E : pi) {71if (E.name == "resource_path") {72continue; // Do not change path.73}74if (E.name == "command_or_control_autoremap") {75continue; // Handle it separately.76}77Variant old_value = input_event->get(E.name);78Variant new_value = ie->get(E.name);79if (old_value == new_value) {80continue;81}82undo_redo->add_do_property(input_event.ptr(), E.name, new_value);83undo_redo->add_undo_property(input_event.ptr(), E.name, old_value);84}8586if (will_toggle) {87if (pending) {88undo_redo->add_do_property(input_event.ptr(), "command_or_control_autoremap", pending);89} else {90undo_redo->add_undo_property(input_event.ptr(), "command_or_control_autoremap", !pending);91}92}9394undo_redo->add_do_property(input_event_text, "text", ie->as_text());95undo_redo->add_undo_property(input_event_text, "text", input_event->as_text());96undo_redo->commit_action();97}9899void InputEventConfigContainer::set_event(const Ref<InputEvent> &p_event) {100Ref<InputEventKey> k = p_event;101Ref<InputEventMouseButton> m = p_event;102Ref<InputEventJoypadButton> jb = p_event;103Ref<InputEventJoypadMotion> jm = p_event;104105if (k.is_valid()) {106config_dialog->set_allowed_input_types(INPUT_KEY);107} else if (m.is_valid()) {108config_dialog->set_allowed_input_types(INPUT_MOUSE_BUTTON);109} else if (jb.is_valid()) {110config_dialog->set_allowed_input_types(INPUT_JOY_BUTTON);111} else if (jm.is_valid()) {112config_dialog->set_allowed_input_types(INPUT_JOY_MOTION);113}114115input_event = p_event;116_event_changed();117input_event->connect_changed(callable_mp(this, &InputEventConfigContainer::_event_changed));118}119120InputEventConfigContainer::InputEventConfigContainer() {121input_event_text = memnew(Label);122input_event_text->set_focus_mode(FOCUS_ACCESSIBILITY);123input_event_text->set_h_size_flags(SIZE_EXPAND_FILL);124input_event_text->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD_SMART);125input_event_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);126add_child(input_event_text);127128EditorInspectorActionButton *open_config_button = memnew(EditorInspectorActionButton(TTRC("Configure"), SNAME("Edit")));129open_config_button->connect(SceneStringName(pressed), callable_mp(this, &InputEventConfigContainer::_configure_pressed));130add_child(open_config_button);131132add_child(memnew(Control));133134config_dialog = memnew(InputEventConfigurationDialog);135config_dialog->connect(SceneStringName(confirmed), callable_mp(this, &InputEventConfigContainer::_config_dialog_confirmed));136add_child(config_dialog);137}138139///////////////////////140141bool EditorInspectorPluginInputEvent::can_handle(Object *p_object) {142Ref<InputEventKey> k = Ref<InputEventKey>(p_object);143Ref<InputEventMouseButton> m = Ref<InputEventMouseButton>(p_object);144Ref<InputEventJoypadButton> jb = Ref<InputEventJoypadButton>(p_object);145Ref<InputEventJoypadMotion> jm = Ref<InputEventJoypadMotion>(p_object);146147return k.is_valid() || m.is_valid() || jb.is_valid() || jm.is_valid();148}149150void EditorInspectorPluginInputEvent::parse_begin(Object *p_object) {151Ref<InputEvent> ie = Ref<InputEvent>(p_object);152153InputEventConfigContainer *picker_controls = memnew(InputEventConfigContainer);154picker_controls->set_event(ie);155add_custom_control(picker_controls);156}157158///////////////////////159160InputEventEditorPlugin::InputEventEditorPlugin() {161Ref<EditorInspectorPluginInputEvent> plugin;162plugin.instantiate();163add_inspector_plugin(plugin);164}165166167