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