diff --git a/src/ui/items.cpp b/src/ui/items.cpp index ecf4490..d6f097b 100644 --- a/src/ui/items.cpp +++ b/src/ui/items.cpp @@ -236,23 +236,8 @@ void events_update(Panel& panel) actions.add( ACTION_RENAME, [selection]() { return selection.size() == 1; }, [&state, selection]() { state.newId = *selection.begin(); }); - actions.add( - ACTION_ADD, {}, - [&panel]() { - auto& state = panel.state; - command_push(panel, [&state](Document& document) { - int id = 0; - document.edit_apply(EDIT_ADD_EVENT, [&](Model& model) { - auto& events = model.content.events; - id = item_next_id_get(events); - events.push_back({.id = id, .name = localize.get(TEXT_NEW_EVENT)}); - }); - document.selected_ids_set(SelectionKind::EVENTS, {id}); - document.focused_id_set(SelectionKind::EVENTS, id); - state.newId = id; - }); - }, - TOOLTIP_ADD_EVENT); + named_item_add_action_add(actions, panel, SelectionKind::EVENTS, &Content::events, EDIT_ADD_EVENT, + TEXT_NEW_EVENT, TOOLTIP_ADD_EVENT); content_remove_action_add(actions, panel, ElementType::EVENT_ELEMENT, SelectionKind::EVENTS); items_clipboard_actions_add(actions, panel, SelectionKind::EVENTS, EDIT_PASTE_EVENTS, TOAST_DESERIALIZE_EVENTS_FAILED, &Content::events); diff --git a/src/ui/panel.h b/src/ui/panel.h index e3258b1..640bdaa 100644 --- a/src/ui/panel.h +++ b/src/ui/panel.h @@ -227,6 +227,29 @@ void items_paste(const Panel& panel, SelectionKind kind, StringType edit, String }); } +// An Add action making a named item (an event or a shader), selected and ready to rename. +template +void named_item_add_action_add(Actions& actions, Panel& panel, SelectionKind kind, ContentList list, + StringType edit, StringType name, StringType tooltip) +{ + actions.add( + ACTION_ADD, {}, + [&panel, kind, list, edit, name]() { + command_push(panel, [&state = panel.state, kind, list, edit, name](Document& document) { + int id = 0; + document.edit_apply(edit, [&](Model& model) { + auto& items = model.content.*list; + id = item_next_id_get(items); + items.push_back({.id = id, .name = localize.get(name)}); + }); + document.selected_ids_set(kind, {id}); + document.focused_id_set(kind, id); + state.newId = id; + }); + }, + tooltip); +} + // Copy and paste for a content list, after a separator. template void items_clipboard_actions_add(Actions& actions, Panel& panel, SelectionKind kind, StringType pasteEdit, diff --git a/src/ui/shaders.cpp b/src/ui/shaders.cpp index 6b3e854..7d34985 100644 --- a/src/ui/shaders.cpp +++ b/src/ui/shaders.cpp @@ -307,22 +307,8 @@ void shaders_update(Panel& panel, ShadersPanel& shaders) }; Actions actions = {}; - actions.add( - ACTION_ADD, {}, - [&]() { - command_push(panel, [&state](Document& document) { - int id = 0; - document.edit_apply(EDIT_ADD_SHADER, [&](Model& model) { - auto& shaders = model.content.shaders; - id = item_next_id_get(shaders); - shaders.push_back({.id = id, .name = localize.get(TEXT_NEW_SHADER)}); - }); - document.selected_ids_set(SelectionKind::SHADERS, {id}); - document.focused_id_set(SelectionKind::SHADERS, id); - state.newId = id; - }); - }, - TOOLTIP_ADD_SHADER); + named_item_add_action_add(actions, panel, SelectionKind::SHADERS, &Content::shaders, EDIT_ADD_SHADER, + TEXT_NEW_SHADER, TOOLTIP_ADD_SHADER); content_remove_action_add(actions, panel, ElementType::SHADER, SelectionKind::SHADERS); actions.add( ACTION_RELOAD, [selection]() { return !selection.empty(); },