| 1 | /**************************************************************************/ |
| 2 | /* editor_event_search_bar.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "editor_event_search_bar.h" |
| 32 | |
| 33 | #include "editor/settings/event_listener_line_edit.h" |
| 34 | #include "scene/gui/button.h" |
| 35 | #include "scene/gui/dialogs.h" |
| 36 | #include "scene/gui/line_edit.h" |
| 37 | |
| 38 | void EditorEventSearchBar::_on_event_changed(const Ref<InputEvent> &p_event) { |
| 39 | if (p_event.is_valid() && (!p_event->is_pressed() || p_event->is_echo())) { |
| 40 | return; |
| 41 | } |
| 42 | _value_changed(); |
| 43 | } |
| 44 | |
| 45 | void EditorEventSearchBar::_on_clear_all() { |
| 46 | search_by_name->set_block_signals(true); |
| 47 | search_by_name->clear(); |
| 48 | search_by_name->set_block_signals(false); |
| 49 | |
| 50 | search_by_event->set_block_signals(true); |
| 51 | search_by_event->clear_event(); |
| 52 | search_by_event->set_block_signals(false); |
| 53 | |
| 54 | _value_changed(); |
| 55 | } |
| 56 | |
| 57 | void EditorEventSearchBar::_value_changed() { |
| 58 | clear_all->set_disabled(!is_searching()); |
| 59 | emit_signal(SceneStringName(value_changed)); |
| 60 | } |
| 61 | |
| 62 | bool EditorEventSearchBar::is_searching() const { |
| 63 | return !get_name().is_empty() || get_event().is_valid(); |
| 64 | } |
| 65 | |
| 66 | String EditorEventSearchBar::get_name() const { |
| 67 | return search_by_name->get_text().strip_edges(); |
| 68 | } |
| 69 | |
| 70 | Ref<InputEvent> EditorEventSearchBar::get_event() const { |
| 71 | return search_by_event->get_event(); |
| 72 | } |
| 73 | |
| 74 | void EditorEventSearchBar::_notification(int p_what) { |
| 75 | switch (p_what) { |
| 76 | case NOTIFICATION_THEME_CHANGED: { |
| 77 | search_by_name->set_right_icon(get_editor_theme_icon(SNAME("Search"))); |
| 78 | } break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void EditorEventSearchBar::_bind_methods() { |
| 83 | ADD_SIGNAL(MethodInfo("value_changed")); |
| 84 | } |
| 85 | |
| 86 | EditorEventSearchBar::EditorEventSearchBar() { |
| 87 | set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 88 | |
| 89 | search_by_name = memnew(LineEdit); |
| 90 | search_by_name->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 91 | search_by_name->set_placeholder(TTRC("Filter by Name")); |
| 92 | search_by_name->set_accessibility_name(TTRC("Filter by Name")); |
| 93 | search_by_name->set_clear_button_enabled(true); |
| 94 | search_by_name->connect(SceneStringName(text_changed), callable_mp(this, &EditorEventSearchBar::_value_changed).unbind(1)); |
| 95 | add_child(search_by_name); |
| 96 | |
| 97 | search_by_event = memnew(EventListenerLineEdit); |
| 98 | search_by_event->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 99 | search_by_event->set_stretch_ratio(0.75); |
| 100 | search_by_event->set_accessibility_name(TTRC("Action Event")); |
| 101 | search_by_event->connect("event_changed", callable_mp(this, &EditorEventSearchBar::_on_event_changed)); |
| 102 | add_child(search_by_event); |
| 103 | |
| 104 | clear_all = memnew(Button(TTRC("Clear All"))); |
| 105 | clear_all->set_tooltip_text(TTRC("Clear all search filters.")); |
| 106 | clear_all->connect(SceneStringName(pressed), callable_mp(this, &EditorEventSearchBar::_on_clear_all)); |
| 107 | clear_all->set_disabled(true); |
| 108 | add_child(clear_all); |
| 109 | } |