Path: blob/master/editor/settings/editor_command_palette.h
9896 views
/**************************************************************************/1/* editor_command_palette.h */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#pragma once3132#include "core/input/shortcut.h"33#include "scene/gui/dialogs.h"34#include "scene/gui/tree.h"3536class EditorCommandPalette : public ConfirmationDialog {37GDCLASS(EditorCommandPalette, ConfirmationDialog);3839static EditorCommandPalette *singleton;40LineEdit *command_search_box = nullptr;41Tree *search_options = nullptr;4243struct Command {44Callable callable;45String name;46Ref<Shortcut> shortcut;47String shortcut_text;48int last_used = 0; // Store time as int, because doubles have problems with text serialization.49};5051struct CommandEntry {52String key_name;53String display_name;54String shortcut_text;55int last_used = 0;56float score = 0;57};5859struct CommandEntryComparator {60_FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const {61return A.score > B.score;62}63};6465struct CommandHistoryComparator {66_FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const {67if (A.last_used == B.last_used) {68return A.display_name < B.display_name;69} else {70return A.last_used > B.last_used;71}72}73};7475HashMap<String, Command> commands;76HashMap<String, Pair<String, Ref<Shortcut>>> unregistered_shortcuts;7778void _update_command_search(const String &search_text);79float _score_path(const String &p_search, const String &p_path);80void _sbox_input(const Ref<InputEvent> &p_event);81void _confirmed();82void _add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text = "None");83void _save_history() const;8485EditorCommandPalette();8687protected:88static void _bind_methods();89void _notification(int p_what);9091public:92void open_popup();93void get_actions_list(List<String> *p_list) const;94void add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, const Ref<Shortcut> &p_shortcut);95void execute_command(const String &p_command_name);96void register_shortcuts_as_command();97Ref<Shortcut> add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut);98void remove_command(String p_key_name);99static EditorCommandPalette *get_singleton();100};101102Ref<Shortcut> ED_SHORTCUT_AND_COMMAND(const String &p_path, const String &p_name, Key p_keycode = Key::NONE, String p_command = "");103Ref<Shortcut> ED_SHORTCUT_ARRAY_AND_COMMAND(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes, String p_command = "");104105106