action_map_editor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**************************************************************************/
  2. /* action_map_editor.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. #include "action_map_editor.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/settings/editor_event_search_bar.h"
  33. #include "editor/settings/editor_settings.h"
  34. #include "editor/settings/event_listener_line_edit.h"
  35. #include "editor/settings/input_event_configuration_dialog.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "scene/gui/check_button.h"
  38. #include "scene/gui/margin_container.h"
  39. #include "scene/gui/separator.h"
  40. #include "scene/gui/tree.h"
  41. static bool _is_action_name_valid(const String &p_name) {
  42. const char32_t *cstr = p_name.get_data();
  43. for (int i = 0; cstr[i]; i++) {
  44. if (cstr[i] == '/' || cstr[i] == ':' || cstr[i] == '"' ||
  45. cstr[i] == '=' || cstr[i] == '\\' || cstr[i] < 32) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. void ActionMapEditor::_event_config_confirmed() {
  52. Ref<InputEvent> ev = event_config_dialog->get_event();
  53. Dictionary new_action = current_action.duplicate();
  54. Array events = new_action["events"].duplicate();
  55. if (current_action_event_index == -1) {
  56. // Add new event
  57. events.push_back(ev);
  58. } else {
  59. // Edit existing event
  60. events[current_action_event_index] = ev;
  61. }
  62. new_action["events"] = events;
  63. emit_signal(SNAME("action_edited"), current_action_name, new_action);
  64. }
  65. void ActionMapEditor::_add_action_pressed() {
  66. _add_action(add_edit->get_text());
  67. }
  68. String ActionMapEditor::_check_new_action_name(const String &p_name) {
  69. if (p_name.is_empty() || !_is_action_name_valid(p_name)) {
  70. return TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'");
  71. }
  72. if (_has_action(p_name)) {
  73. return vformat(TTR("An action with the name '%s' already exists."), p_name);
  74. }
  75. return "";
  76. }
  77. void ActionMapEditor::_add_edit_text_changed(const String &p_name) {
  78. const String error = _check_new_action_name(p_name);
  79. add_button->set_tooltip_text(error);
  80. add_button->set_disabled(!error.is_empty());
  81. }
  82. bool ActionMapEditor::_has_action(const String &p_name) const {
  83. for (const ActionInfo &action_info : actions_cache) {
  84. if (p_name == action_info.name) {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. void ActionMapEditor::_add_action(const String &p_name) {
  91. String error = _check_new_action_name(p_name);
  92. if (!error.is_empty()) {
  93. show_message(error);
  94. return;
  95. }
  96. add_edit->clear();
  97. emit_signal(SNAME("action_added"), p_name);
  98. }
  99. void ActionMapEditor::_action_edited() {
  100. TreeItem *ti = action_tree->get_edited();
  101. if (!ti) {
  102. return;
  103. }
  104. if (action_tree->get_selected_column() == 0) {
  105. // Name Edited
  106. String new_name = ti->get_text(0);
  107. String old_name = ti->get_meta("__name");
  108. if (new_name == old_name) {
  109. return;
  110. }
  111. if (new_name.is_empty() || !_is_action_name_valid(new_name)) {
  112. ti->set_text(0, old_name);
  113. show_message(TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'"));
  114. return;
  115. }
  116. if (_has_action(new_name)) {
  117. ti->set_text(0, old_name);
  118. show_message(vformat(TTR("An action with the name '%s' already exists."), new_name));
  119. return;
  120. }
  121. emit_signal(SNAME("action_renamed"), old_name, new_name);
  122. } else if (action_tree->get_selected_column() == 1) {
  123. // Deadzone Edited
  124. String name = ti->get_meta("__name");
  125. Dictionary old_action = ti->get_meta("__action");
  126. Dictionary new_action = old_action.duplicate();
  127. new_action["deadzone"] = ti->get_range(1);
  128. // Call deferred so that input can finish propagating through tree, allowing re-making of tree to occur.
  129. call_deferred(SNAME("emit_signal"), "action_edited", name, new_action);
  130. }
  131. }
  132. void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  133. if (p_button != MouseButton::LEFT) {
  134. return;
  135. }
  136. ItemButton option = (ItemButton)p_id;
  137. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  138. if (!item) {
  139. return;
  140. }
  141. switch (option) {
  142. case ActionMapEditor::BUTTON_ADD_EVENT: {
  143. current_action = item->get_meta("__action");
  144. current_action_name = item->get_meta("__name");
  145. current_action_event_index = -1;
  146. event_config_dialog->popup_and_configure(Ref<InputEvent>(), current_action_name);
  147. } break;
  148. case ActionMapEditor::BUTTON_EDIT_EVENT: {
  149. // Action and Action name is located on the parent of the event.
  150. current_action = item->get_parent()->get_meta("__action");
  151. current_action_name = item->get_parent()->get_meta("__name");
  152. current_action_event_index = item->get_meta("__index");
  153. Ref<InputEvent> ie = item->get_meta("__event");
  154. if (ie.is_valid()) {
  155. event_config_dialog->popup_and_configure(ie, current_action_name);
  156. }
  157. } break;
  158. case ActionMapEditor::BUTTON_REMOVE_ACTION: {
  159. // Send removed action name
  160. String name = item->get_meta("__name");
  161. emit_signal(SNAME("action_removed"), name);
  162. } break;
  163. case ActionMapEditor::BUTTON_REMOVE_EVENT: {
  164. // Remove event and send updated action
  165. Dictionary action = item->get_parent()->get_meta("__action").duplicate();
  166. String action_name = item->get_parent()->get_meta("__name");
  167. int event_index = item->get_meta("__index");
  168. Array events = action["events"].duplicate();
  169. events.remove_at(event_index);
  170. action["events"] = events;
  171. emit_signal(SNAME("action_edited"), action_name, action);
  172. } break;
  173. case ActionMapEditor::BUTTON_REVERT_ACTION: {
  174. ERR_FAIL_COND_MSG(!item->has_meta("__action_initial"), "Tree Item for action which can be reverted is expected to have meta value with initial value of action.");
  175. Dictionary action = item->get_meta("__action_initial").duplicate();
  176. String action_name = item->get_meta("__name");
  177. emit_signal(SNAME("action_edited"), action_name, action);
  178. } break;
  179. default:
  180. break;
  181. }
  182. }
  183. void ActionMapEditor::_tree_item_activated() {
  184. TreeItem *item = action_tree->get_selected();
  185. if (!item || !item->has_meta("__event")) {
  186. return;
  187. }
  188. _tree_button_pressed(item, 2, BUTTON_EDIT_EVENT, MouseButton::LEFT);
  189. }
  190. void ActionMapEditor::_set_show_builtin_actions(bool p_show) {
  191. show_builtin_actions = p_show;
  192. EditorSettings::get_singleton()->set_project_metadata("project_settings", "show_builtin_actions", show_builtin_actions);
  193. // Prevent unnecessary updates of action list when cache is empty.
  194. if (!actions_cache.is_empty()) {
  195. update_action_list();
  196. }
  197. }
  198. void ActionMapEditor::_on_search_bar_value_changed() {
  199. if (action_list_search_bar->is_searching()) {
  200. show_builtin_actions_checkbutton->set_pressed_no_signal(true);
  201. show_builtin_actions_checkbutton->set_disabled(true);
  202. show_builtin_actions_checkbutton->set_tooltip_text(TTRC("Built-in actions are always shown when searching."));
  203. } else {
  204. show_builtin_actions_checkbutton->set_pressed_no_signal(show_builtin_actions);
  205. show_builtin_actions_checkbutton->set_disabled(false);
  206. show_builtin_actions_checkbutton->set_tooltip_text(String());
  207. }
  208. update_action_list();
  209. }
  210. Variant ActionMapEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  211. TreeItem *selected = action_tree->get_selected();
  212. if (!selected) {
  213. return Variant();
  214. }
  215. String name = selected->get_text(0);
  216. Label *label = memnew(Label(name));
  217. label->set_theme_type_variation("HeaderSmall");
  218. label->set_modulate(Color(1, 1, 1, 1.0f));
  219. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  220. action_tree->set_drag_preview(label);
  221. get_viewport()->gui_set_drag_description(vformat(RTR("Action %s"), name));
  222. Dictionary drag_data;
  223. if (selected->has_meta("__action")) {
  224. drag_data["input_type"] = "action";
  225. }
  226. if (selected->has_meta("__event")) {
  227. drag_data["input_type"] = "event";
  228. }
  229. drag_data["source"] = selected->get_instance_id();
  230. action_tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  231. return drag_data;
  232. }
  233. bool ActionMapEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  234. Dictionary d = p_data;
  235. if (!d.has("input_type")) {
  236. return false;
  237. }
  238. TreeItem *source = Object::cast_to<TreeItem>(ObjectDB::get_instance(d["source"].operator ObjectID()));
  239. TreeItem *selected = action_tree->get_selected();
  240. TreeItem *item = (p_point == Vector2(Math::INF, Math::INF)) ? selected : action_tree->get_item_at_position(p_point);
  241. if (!selected || !item || item == source) {
  242. return false;
  243. }
  244. // Don't allow moving an action in-between events.
  245. if (d["input_type"] == "action" && item->has_meta("__event")) {
  246. return false;
  247. }
  248. // Don't allow moving an event to a different action.
  249. if (d["input_type"] == "event" && item->get_parent() != selected->get_parent()) {
  250. return false;
  251. }
  252. return true;
  253. }
  254. void ActionMapEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  255. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  256. return;
  257. }
  258. TreeItem *selected = action_tree->get_selected();
  259. TreeItem *target = (p_point == Vector2(Math::INF, Math::INF)) ? selected : action_tree->get_item_at_position(p_point);
  260. if (!target) {
  261. return;
  262. }
  263. bool drop_above = ((p_point == Vector2(Math::INF, Math::INF)) ? action_tree->get_drop_section_at_position(action_tree->get_item_rect(target).position) : action_tree->get_drop_section_at_position(p_point)) == -1;
  264. Dictionary d = p_data;
  265. if (d["input_type"] == "action") {
  266. // Change action order.
  267. String relative_to = target->get_meta("__name");
  268. String action_name = selected->get_meta("__name");
  269. emit_signal(SNAME("action_reordered"), action_name, relative_to, drop_above);
  270. } else if (d["input_type"] == "event") {
  271. // Change event order
  272. int current_index = selected->get_meta("__index");
  273. int target_index = target->get_meta("__index");
  274. // Construct new events array.
  275. Dictionary new_action = selected->get_parent()->get_meta("__action");
  276. Array events = new_action["events"];
  277. Array new_events;
  278. // The following method was used to perform the array changes since `remove` followed by `insert` was not working properly at time of writing.
  279. // Loop thought existing events
  280. for (int i = 0; i < events.size(); i++) {
  281. // If you come across the current index, just skip it, as it has been moved.
  282. if (i == current_index) {
  283. continue;
  284. } else if (i == target_index) {
  285. // We are at the target index. If drop above, add selected event there first, then target, so moved event goes on top.
  286. if (drop_above) {
  287. new_events.push_back(events[current_index]);
  288. new_events.push_back(events[target_index]);
  289. } else {
  290. new_events.push_back(events[target_index]);
  291. new_events.push_back(events[current_index]);
  292. }
  293. } else {
  294. new_events.push_back(events[i]);
  295. }
  296. }
  297. new_action["events"] = new_events;
  298. emit_signal(SNAME("action_edited"), selected->get_parent()->get_meta("__name"), new_action);
  299. }
  300. }
  301. void ActionMapEditor::_notification(int p_what) {
  302. switch (p_what) {
  303. case NOTIFICATION_TRANSLATION_CHANGED: {
  304. if (!actions_cache.is_empty()) {
  305. update_action_list();
  306. }
  307. if (!add_button->get_tooltip_text().is_empty()) {
  308. _add_edit_text_changed(add_edit->get_text());
  309. }
  310. } break;
  311. case NOTIFICATION_THEME_CHANGED: {
  312. add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  313. if (!actions_cache.is_empty()) {
  314. update_action_list();
  315. }
  316. } break;
  317. }
  318. }
  319. void ActionMapEditor::_bind_methods() {
  320. ADD_SIGNAL(MethodInfo("action_added", PropertyInfo(Variant::STRING, "name")));
  321. ADD_SIGNAL(MethodInfo("action_edited", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::DICTIONARY, "new_action")));
  322. ADD_SIGNAL(MethodInfo("action_removed", PropertyInfo(Variant::STRING, "name")));
  323. ADD_SIGNAL(MethodInfo("action_renamed", PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name")));
  324. ADD_SIGNAL(MethodInfo("action_reordered", PropertyInfo(Variant::STRING, "action_name"), PropertyInfo(Variant::STRING, "relative_to"), PropertyInfo(Variant::BOOL, "before")));
  325. }
  326. LineEdit *ActionMapEditor::get_search_box() const {
  327. return action_list_search_bar->get_name_search_box();
  328. }
  329. LineEdit *ActionMapEditor::get_path_box() const {
  330. return add_edit;
  331. }
  332. InputEventConfigurationDialog *ActionMapEditor::get_configuration_dialog() {
  333. return event_config_dialog;
  334. }
  335. bool ActionMapEditor::_should_display_action(const String &p_name, const Array &p_events) const {
  336. const Ref<InputEvent> search_ev = action_list_search_bar->get_event();
  337. bool event_match = true;
  338. if (search_ev.is_valid()) {
  339. event_match = false;
  340. for (int i = 0; i < p_events.size(); ++i) {
  341. const Ref<InputEvent> ev = p_events[i];
  342. if (ev.is_valid() && ev->is_match(search_ev, true)) {
  343. event_match = true;
  344. }
  345. }
  346. }
  347. return event_match && action_list_search_bar->get_name().is_subsequence_ofn(p_name);
  348. }
  349. void ActionMapEditor::update_action_list(const Vector<ActionInfo> &p_action_infos) {
  350. if (!p_action_infos.is_empty()) {
  351. actions_cache = p_action_infos;
  352. }
  353. Pair<String, int> selected_item;
  354. TreeItem *ti = action_tree->get_selected();
  355. if (ti) {
  356. selected_item.first = ti->get_text(0);
  357. selected_item.second = action_tree->get_selected_column();
  358. }
  359. HashSet<String> collapsed_actions;
  360. TreeItem *root = action_tree->get_root();
  361. if (root) {
  362. for (TreeItem *child = root->get_first_child(); child; child = child->get_next()) {
  363. if (child->is_collapsed()) {
  364. collapsed_actions.insert(child->get_meta("__name"));
  365. }
  366. }
  367. }
  368. action_tree->clear();
  369. root = action_tree->create_item();
  370. for (const ActionInfo &action_info : actions_cache) {
  371. const Array events = action_info.action["events"];
  372. if (!_should_display_action(action_info.name, events)) {
  373. continue;
  374. }
  375. if (!action_info.editable && !action_list_search_bar->is_searching() && !show_builtin_actions) {
  376. continue;
  377. }
  378. const Variant deadzone = action_info.action["deadzone"];
  379. // Update Tree...
  380. TreeItem *action_item = action_tree->create_item(root);
  381. ERR_FAIL_NULL(action_item);
  382. action_item->set_meta("__action", action_info.action);
  383. action_item->set_meta("__name", action_info.name);
  384. action_item->set_collapsed(collapsed_actions.has(action_info.name));
  385. // First Column - Action Name
  386. action_item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);
  387. action_item->set_cell_mode(0, TreeItem::CELL_MODE_STRING);
  388. action_item->set_text(0, action_info.name);
  389. action_item->set_editable(0, action_info.editable);
  390. action_item->set_icon(0, action_info.icon);
  391. // Second Column - Deadzone
  392. action_item->set_editable(1, true);
  393. action_item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  394. action_item->set_range_config(1, 0.0, 1.0, 0.01);
  395. action_item->set_range(1, deadzone);
  396. // Third column - buttons
  397. if (action_info.has_initial) {
  398. bool deadzone_eq = action_info.action_initial["deadzone"] == action_info.action["deadzone"];
  399. bool events_eq = Shortcut::is_event_array_equal(action_info.action_initial["events"], action_info.action["events"]);
  400. bool action_eq = deadzone_eq && events_eq;
  401. action_item->set_meta("__action_initial", action_info.action_initial);
  402. action_item->add_button(2, get_editor_theme_icon(SNAME("ReloadSmall")), BUTTON_REVERT_ACTION, action_eq, action_eq ? TTRC("Cannot Revert - Action is same as initial") : TTRC("Revert Action"));
  403. }
  404. action_item->add_button(2, get_editor_theme_icon(SNAME("Add")), BUTTON_ADD_EVENT, false, TTRC("Add Event"));
  405. action_item->add_button(2, get_editor_theme_icon(SNAME("Remove")), BUTTON_REMOVE_ACTION, !action_info.editable, action_info.editable ? TTRC("Remove Action") : TTRC("Cannot Remove Action"));
  406. action_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  407. action_item->set_custom_bg_color(1, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  408. action_item->set_custom_stylebox(0, get_theme_stylebox(SNAME("prop_subsection_stylebox_left"), EditorStringName(Editor)));
  409. action_item->set_custom_stylebox(1, get_theme_stylebox(SNAME("prop_subsection_stylebox_right"), EditorStringName(Editor)));
  410. if (selected_item.first == action_info.name) {
  411. action_item->select(selected_item.second);
  412. }
  413. for (int evnt_idx = 0; evnt_idx < events.size(); evnt_idx++) {
  414. Ref<InputEvent> event = events[evnt_idx];
  415. if (event.is_null()) {
  416. continue;
  417. }
  418. TreeItem *event_item = action_tree->create_item(action_item);
  419. // First Column - Text
  420. event_item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);
  421. event_item->set_text(0, EventListenerLineEdit::get_event_text(event, true));
  422. event_item->set_meta("__event", event);
  423. event_item->set_meta("__index", evnt_idx);
  424. // First Column - Icon
  425. Ref<InputEventKey> k = event;
  426. if (k.is_valid()) {
  427. if (k->get_physical_keycode() == Key::NONE && k->get_keycode() == Key::NONE && k->get_key_label() != Key::NONE) {
  428. event_item->set_icon(0, get_editor_theme_icon(SNAME("KeyboardLabel")));
  429. } else if (k->get_keycode() != Key::NONE) {
  430. event_item->set_icon(0, get_editor_theme_icon(SNAME("Keyboard")));
  431. } else if (k->get_physical_keycode() != Key::NONE) {
  432. event_item->set_icon(0, get_editor_theme_icon(SNAME("KeyboardPhysical")));
  433. } else {
  434. event_item->set_icon(0, get_editor_theme_icon(SNAME("KeyboardError")));
  435. }
  436. }
  437. Ref<InputEventMouseButton> mb = event;
  438. if (mb.is_valid()) {
  439. event_item->set_icon(0, get_editor_theme_icon(SNAME("Mouse")));
  440. }
  441. Ref<InputEventJoypadButton> jb = event;
  442. if (jb.is_valid()) {
  443. event_item->set_icon(0, get_editor_theme_icon(SNAME("JoyButton")));
  444. }
  445. Ref<InputEventJoypadMotion> jm = event;
  446. if (jm.is_valid()) {
  447. event_item->set_icon(0, get_editor_theme_icon(SNAME("JoyAxis")));
  448. }
  449. // Third Column - Buttons
  450. event_item->add_button(2, get_editor_theme_icon(SNAME("Edit")), BUTTON_EDIT_EVENT, false, TTRC("Edit Event"), TTRC("Edit Event"));
  451. event_item->add_button(2, get_editor_theme_icon(SNAME("Remove")), BUTTON_REMOVE_EVENT, false, TTRC("Remove Event"), TTRC("Remove Event"));
  452. event_item->set_button_color(2, 0, Color(1, 1, 1, 0.75));
  453. event_item->set_button_color(2, 1, Color(1, 1, 1, 0.75));
  454. if (selected_item.first == event_item->get_text(0)) {
  455. event_item->select(selected_item.second);
  456. }
  457. }
  458. }
  459. }
  460. void ActionMapEditor::show_message(const String &p_message) {
  461. message->set_text(p_message);
  462. message->popup_centered();
  463. }
  464. ActionMapEditor::ActionMapEditor() {
  465. // Main Vbox Container
  466. VBoxContainer *main_vbox = memnew(VBoxContainer);
  467. main_vbox->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  468. add_child(main_vbox);
  469. action_list_search_bar = memnew(EditorEventSearchBar);
  470. action_list_search_bar->connect(SceneStringName(value_changed), callable_mp(this, &ActionMapEditor::_on_search_bar_value_changed));
  471. main_vbox->add_child(action_list_search_bar);
  472. // Adding Action line edit + button
  473. add_hbox = memnew(HBoxContainer);
  474. add_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  475. add_edit = memnew(LineEdit);
  476. add_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  477. add_edit->set_placeholder(TTRC("Add New Action"));
  478. add_edit->set_accessibility_name(TTRC("Add New Action"));
  479. add_edit->set_clear_button_enabled(true);
  480. add_edit->set_keep_editing_on_text_submit(true);
  481. add_edit->connect(SceneStringName(text_changed), callable_mp(this, &ActionMapEditor::_add_edit_text_changed));
  482. add_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ActionMapEditor::_add_action));
  483. add_hbox->add_child(add_edit);
  484. add_button = memnew(Button);
  485. add_button->set_text(TTRC("Add"));
  486. add_button->connect(SceneStringName(pressed), callable_mp(this, &ActionMapEditor::_add_action_pressed));
  487. add_hbox->add_child(add_button);
  488. // Disable the button and set its tooltip.
  489. _add_edit_text_changed(add_edit->get_text());
  490. add_hbox->add_child(memnew(VSeparator));
  491. show_builtin_actions_checkbutton = memnew(CheckButton);
  492. show_builtin_actions_checkbutton->set_text(TTRC("Show Built-in Actions"));
  493. show_builtin_actions_checkbutton->connect(SceneStringName(toggled), callable_mp(this, &ActionMapEditor::_set_show_builtin_actions));
  494. add_hbox->add_child(show_builtin_actions_checkbutton);
  495. show_builtin_actions = EditorSettings::get_singleton()->get_project_metadata("project_settings", "show_builtin_actions", false);
  496. show_builtin_actions_checkbutton->set_pressed_no_signal(show_builtin_actions);
  497. main_vbox->add_child(add_hbox);
  498. MarginContainer *mc = memnew(MarginContainer);
  499. mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  500. mc->set_theme_type_variation("NoBorderHorizontalBottom");
  501. main_vbox->add_child(mc);
  502. // Action Editor Tree
  503. action_tree = memnew(Tree);
  504. action_tree->set_accessibility_name(TTRC("Action Map"));
  505. action_tree->set_theme_type_variation("TreeTable");
  506. action_tree->set_columns(3);
  507. action_tree->set_hide_root(true);
  508. action_tree->set_column_titles_visible(true);
  509. action_tree->set_column_title(0, TTRC("Action"));
  510. action_tree->set_column_clip_content(0, true);
  511. action_tree->set_column_title(1, TTRC("Deadzone"));
  512. action_tree->set_column_expand(1, false);
  513. action_tree->set_column_custom_minimum_width(1, 80 * EDSCALE);
  514. action_tree->set_column_expand(2, false);
  515. action_tree->set_column_custom_minimum_width(2, 50 * EDSCALE);
  516. action_tree->connect("item_edited", callable_mp(this, &ActionMapEditor::_action_edited), CONNECT_DEFERRED);
  517. action_tree->connect("item_activated", callable_mp(this, &ActionMapEditor::_tree_item_activated));
  518. action_tree->connect("button_clicked", callable_mp(this, &ActionMapEditor::_tree_button_pressed));
  519. mc->add_child(action_tree);
  520. SET_DRAG_FORWARDING_GCD(action_tree, ActionMapEditor);
  521. // Adding event dialog
  522. event_config_dialog = memnew(InputEventConfigurationDialog);
  523. event_config_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ActionMapEditor::_event_config_confirmed));
  524. add_child(event_config_dialog);
  525. message = memnew(AcceptDialog);
  526. add_child(message);
  527. }