| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/input/shortcut.h" |
| 34 |
#include "scene/gui/dialogs.h" |
| 35 |
#include "scene/gui/tree.h" |
| 36 |
|
| 37 |
class EditorCommandPalette : public ConfirmationDialog { |
| 38 |
GDCLASS(EditorCommandPalette, ConfirmationDialog); |
| 39 |
|
| 40 |
static EditorCommandPalette *singleton; |
| 41 |
LineEdit *command_search_box = nullptr; |
| 42 |
Tree *search_options = nullptr; |
| 43 |
|
| 44 |
struct Command { |
| 45 |
Callable callable; |
| 46 |
String name; |
| 47 |
Ref<Shortcut> shortcut; |
| 48 |
String shortcut_text; |
| 49 |
int last_used = 0; |
| 50 |
}; |
| 51 |
|
| 52 |
struct CommandEntry { |
| 53 |
String key_name; |
| 54 |
String display_name; |
| 55 |
String shortcut_text; |
| 56 |
int last_used = 0; |
| 57 |
float score = 0; |
| 58 |
}; |
| 59 |
|
| 60 |
struct CommandEntryComparator { |
| 61 |
_FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const { |
| 62 |
return A.score > B.score; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
struct CommandHistoryComparator { |
| 67 |
_FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const { |
| 68 |
if (A.last_used == B.last_used) { |
| 69 |
return A.display_name < B.display_name; |
| 70 |
} else { |
| 71 |
return A.last_used > B.last_used; |
| 72 |
} |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
HashMap<String, Command> commands; |
| 77 |
HashMap<String, Pair<String, Ref<Shortcut>>> unregistered_shortcuts; |
| 78 |
|
| 79 |
void _update_command_search(const String &search_text); |
| 80 |
float _score_path(const String &p_search, const String &p_path); |
| 81 |
void _sbox_input(const Ref<InputEvent> &p_event); |
| 82 |
void _confirmed(); |
| 83 |
void _add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text = "None"); |
| 84 |
void _save_history() const; |
| 85 |
|
| 86 |
EditorCommandPalette(); |
| 87 |
|
| 88 |
protected: |
| 89 |
static void _bind_methods(); |
| 90 |
void _notification(int p_what); |
| 91 |
|
| 92 |
public: |
| 93 |
void open_popup(); |
| 94 |
void get_actions_list(List<String> *p_list) const; |
| 95 |
void add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, const Ref<Shortcut> &p_shortcut); |
| 96 |
void execute_command(const String &p_command_name); |
| 97 |
void register_shortcuts_as_command(); |
| 98 |
Ref<Shortcut> add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut); |
| 99 |
void remove_command(String p_key_name); |
| 100 |
static EditorCommandPalette *get_singleton(); |
| 101 |
}; |
| 102 |
|
| 103 |
Ref<Shortcut> ED_SHORTCUT_AND_COMMAND(const String &p_path, const String &p_name, Key p_keycode = Key::NONE, String p_command = ""); |
| 104 |
Ref<Shortcut> ED_SHORTCUT_ARRAY_AND_COMMAND(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes, String p_command = ""); |