diff --git a/src/edit/edit.hpp b/src/edit/edit.hpp index ca989ea..ed80c0b 100644 --- a/src/edit/edit.hpp +++ b/src/edit/edit.hpp @@ -110,7 +110,7 @@ namespace anm2ed::edit Uids items_group(model::Model&, int, int, const std::set&, const std::string&); Uids items_move(model::Model&, int, int, const std::set&, const std::set&, RowTarget, bool, bool); Uids animation_length_fit(model::Model&, int); - int item_add(model::Model&, ItemType, int, int, const std::string&, int, bool, int, types::destination::Type); + int item_add(model::Model&, ItemType, int, int, model::Layer, model::Null, int, types::destination::Type); std::set animation_group_ids_get(const model::Model&); std::set animation_group_indices_get(const model::Model&, int); diff --git a/src/edit/tracks.cpp b/src/edit/tracks.cpp index bc583ee..2a3cd22 100644 --- a/src/edit/tracks.cpp +++ b/src/edit/tracks.cpp @@ -159,26 +159,23 @@ namespace anm2ed::edit } // Adds a layer or null (or updates the one with the item's id) and gives it a track in one or every animation. - int item_add(Model& model, ItemType type, int animationIndex, int id, const std::string& name, int spritesheetId, - bool isShowRect, int insertBeforeId, types::destination::Type destination) + // Adds track `id` (a new item, from `layer` or `null`, at -1) to an animation or all of them. + int item_add(Model& model, ItemType type, int animationIndex, int id, Layer layer, Null null, int insertBeforeId, + types::destination::Type destination) { auto& content = model.content; + auto item_insert = [&](auto& items, auto item) + { + if (id == -1) id = item_next_id_get(items); + if (item_get(items, id)) return; + item.id = id; + items.push_back(item); + }; + if (!item_get(content.spritesheets, layer.spritesheetId)) layer.spritesheetId = 0; if (type == ItemType::LAYER) - { - if (id == -1) id = item_next_id_get(content.layers); - auto layer = item_get(content.layers, id); - if (!layer) layer = &content.layers.emplace_back(Layer{.id = id}); - if (!name.empty()) layer->name = name; - layer->spritesheetId = item_get(content.spritesheets, spritesheetId) ? spritesheetId : 0; - } + item_insert(content.layers, layer); else if (type == ItemType::NULL_) - { - if (id == -1) id = item_next_id_get(content.nulls); - auto null = item_get(content.nulls, id); - if (!null) null = &content.nulls.emplace_back(Null{.id = id}); - if (!name.empty()) null->name = name; - null->isShowRect = isShowRect; - } + item_insert(content.nulls, null); else return -1; diff --git a/src/imgui/popup/item_properties.cpp b/src/imgui/popup/item_properties.cpp index 09f4c32..d645546 100644 --- a/src/imgui/popup/item_properties.cpp +++ b/src/imgui/popup/item_properties.cpp @@ -2,6 +2,7 @@ #include +#include "../window/panel.hpp" #include "util/imgui/input.hpp" #include "util/imgui/layout.hpp" #include "util/imgui/shortcut.hpp" @@ -13,12 +14,7 @@ namespace anm2ed::imgui::popup { constexpr int EXISTING_ITEMS_ROWS = 6; - void ItemProperties::reset() - { - addItemName.clear(); - addItemID = -1; - addItemSpritesheetID = {}; - } + void ItemProperties::reset() { edit = {}; } void ItemProperties::open() { @@ -114,18 +110,13 @@ namespace anm2ed::imgui::popup { ImGui::SeparatorText(localize.get(LABEL_OPTIONS)); - input_text_string(localize.get(BASIC_NAME), &addItemName); + input_text_string(localize.get(BASIC_NAME), &edit.name); ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_ITEM_NAME)); if (type == LAYER) - { - auto spritesheets = document.choices_get(SelectionKind::SPRITESHEETS); - combo_id_mapped(localize.get(LABEL_SPRITESHEET), &addItemSpritesheetID, spritesheets.ids, - spritesheets.labels); - ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_SPRITESHEET)); - } + layer_source_draw(document, edit); else if (type == NULL_) { - ImGui::Checkbox(localize.get(LABEL_RECT), &addItemIsShowRect); + ImGui::Checkbox(localize.get(LABEL_RECT), &edit.isShowRect); ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_NULL_RECT)); } } @@ -137,11 +128,11 @@ namespace anm2ed::imgui::popup ImVec2(0, ImGui::GetFrameHeightWithSpacing() * EXISTING_ITEMS_ROWS))) { auto unusedItems = unused_items_get(type); - if (addItemID != -1 && !unusedItems.contains(addItemID)) addItemID = -1; + if (edit.id != -1 && !unusedItems.contains(edit.id)) edit.id = -1; for (auto id : unusedItems) { - auto isSelected = addItemID == id; + auto isSelected = edit.id == id; ImGui::PushID(id); @@ -153,8 +144,7 @@ namespace anm2ed::imgui::popup std::make_format_args(id, layer->name, layer->spritesheetId)); if (ImGui::Selectable(label.c_str(), isSelected)) { - addItemID = id; - addItemSpritesheetID = layer->spritesheetId; + edit.id = id; } } } @@ -165,8 +155,7 @@ namespace anm2ed::imgui::popup auto label = std::vformat(localize.get(FORMAT_NULL), std::make_format_args(id, null->name)); if (ImGui::Selectable(label.c_str(), isSelected)) { - addItemID = id; - addItemIsShowRect = null->isShowRect; + edit.id = id; } } } @@ -181,7 +170,7 @@ namespace anm2ed::imgui::popup auto widgetSize = widget_size_with_row_get(2); - ImGui::BeginDisabled(source == source::EXISTING && addItemID == -1); + ImGui::BeginDisabled(source == source::EXISTING && edit.id == -1); shortcut(manager.chords[SHORTCUT_CONFIRM]); if (ImGui::Button(localize.get(BASIC_ADD), widgetSize)) { @@ -189,10 +178,9 @@ namespace anm2ed::imgui::popup auto queuedDestination = destination; auto queuedAnimationIndex = reference.animationIndex; auto queuedInsertBeforeID = reference.itemType == LAYER && type == LAYER ? reference.itemID : -1; - auto queuedAddItemID = addItemID; - auto queuedAddItemName = addItemName; - auto queuedAddItemSpritesheetID = addItemSpritesheetID; - auto queuedAddItemIsShowRect = addItemIsShowRect; + auto queuedAddItemID = source == source::NEW ? -1 : edit.id; + auto queuedLayer = item_edit_layer_get(edit); + auto queuedNull = item_edit_null_get(edit); manager.command_push({manager.selected, [=](Manager&, Document& document) { @@ -202,8 +190,7 @@ namespace anm2ed::imgui::popup { addId = edit::item_add( model, (ItemType)queuedType, queuedAnimationIndex, - queuedAddItemID, queuedAddItemName, - queuedAddItemSpritesheetID, queuedAddItemIsShowRect, + queuedAddItemID, queuedLayer, queuedNull, queuedInsertBeforeID, (destination::Type)queuedDestination); }); diff --git a/src/imgui/popup/item_properties.hpp b/src/imgui/popup/item_properties.hpp index 571bba8..518d344 100644 --- a/src/imgui/popup/item_properties.hpp +++ b/src/imgui/popup/item_properties.hpp @@ -12,10 +12,7 @@ namespace anm2ed::imgui::popup class ItemProperties { PopupHelper popup{PopupHelper(LABEL_TIMELINE_PROPERTIES_POPUP, POPUP_SMALL_NO_HEIGHT)}; - std::string addItemName{"New Item"}; - bool addItemIsShowRect{}; - int addItemID{-1}; - int addItemSpritesheetID{-1}; + Manager::ItemEdit edit{}; void reset(); diff --git a/src/imgui/window/items.cpp b/src/imgui/window/items.cpp index 62a42d4..c20656e 100644 --- a/src/imgui/window/items.cpp +++ b/src/imgui/window/items.cpp @@ -22,6 +22,50 @@ namespace anm2ed::imgui ImGui::TextUnformatted(std::vformat(localize.get(FORMAT_ID), std::make_format_args(id)).c_str()); } + model::Layer item_edit_layer_get(const Manager::ItemEdit& edit) + { + return {.name = edit.name, + .spritesheetId = edit.spritesheetId, + .libraryId = edit.isAnimation ? edit.libraryId : -1, + .animation = edit.isAnimation ? edit.animation : std::string{}}; + } + + model::Null item_edit_null_get(const Manager::ItemEdit& edit) + { + return {.name = edit.name, .isShowRect = edit.isShowRect}; + } + + // A layer's source: a spritesheet, or an animation of this file ("Self") or a library. + void layer_source_draw(Document& document, Manager::ItemEdit& layer) + { + ImGui::PushID("##Source"); + for (auto [label, isAnimation] : {std::pair{LABEL_SPRITESHEET, false}, std::pair{LABEL_ANIMATION, true}}) + { + if (isAnimation) ImGui::SameLine(); + if (ImGui::RadioButton(localize.get(label), layer.isAnimation == isAnimation)) layer.isAnimation = isAnimation; + ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_SOURCE)); + } + ImGui::PopID(); + ImGui::Separator(); + + if (!layer.isAnimation) + { + auto spritesheets = document.choices_get(SelectionKind::SPRITESHEETS); + combo_id_mapped(localize.get(LABEL_SPRITESHEET), &layer.spritesheetId, spritesheets.ids, spritesheets.labels); + ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_SPRITESHEET)); + return; + } + + auto libraries = document.choices_get(SelectionKind::LIBRARIES); + libraries.labels.front() = localize.get(TEXT_SELF); + combo_id_mapped(localize.get(LABEL_LIBRARY), &layer.libraryId, libraries.ids, libraries.labels); + ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_LIBRARY)); + auto names = document.animation_names_get(layer.libraryId); + if (layer.animation.empty() && !names.empty()) layer.animation = names.front(); + combo_string_mapped(localize.get(LABEL_ANIMATION), &layer.animation, names, names); + ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_ANIMATION)); + } + // The layer/null properties popup: adds a new item (id -1) or replaces the edited one, then selects it. template void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKind kind, StringType nameTooltip, @@ -47,15 +91,11 @@ namespace anm2ed::imgui auto result = popup_buttons_draw(manager, localize.get(edit.id == -1 ? BASIC_ADD : BASIC_CONFIRM)); if (result == PopupButton::CONFIRM) { - Item item{.name = edit.name}; - if constexpr (requires { item.spritesheetId; }) item.spritesheetId = edit.spritesheetId; - if constexpr (requires { item.isShowRect; }) item.isShowRect = edit.isShowRect; - if constexpr (requires { item.animation; }) - if (edit.isAnimation) - { - item.libraryId = edit.libraryId; - item.animation = edit.animation; - } + Item item{}; + if constexpr (std::is_same_v) + item = item_edit_layer_get(edit); + else + item = item_edit_null_get(edit); auto& state = panel.state; command_push(panel, [&state, item, id = edit.id, kind, addEdit, propertiesEdit, items_get](Document& document) @@ -142,41 +182,13 @@ namespace anm2ed::imgui } ImGui::End(); - item_properties_update( - panel, 0, LAYER_PROPERTIES_ROWS, SelectionKind::LAYERS, TOOLTIP_ITEM_NAME, EDIT_ADD_LAYER, - EDIT_SET_LAYER_PROPERTIES, layers_get, - [&](Manager::ItemEdit& layer) - { - ImGui::SeparatorText(localize.get(LABEL_LAYER_SOURCE)); - ImGui::PushID("##Source"); - for (auto [label, isAnimation] : {std::pair{LABEL_SPRITESHEET, false}, std::pair{LABEL_ANIMATION, true}}) - { - if (isAnimation) ImGui::SameLine(); - if (ImGui::RadioButton(localize.get(label), layer.isAnimation == isAnimation)) - layer.isAnimation = isAnimation; - ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_SOURCE)); - } - ImGui::PopID(); - ImGui::Separator(); - - if (!layer.isAnimation) - { - auto spritesheets = document.choices_get(SelectionKind::SPRITESHEETS); - combo_id_mapped(localize.get(LABEL_SPRITESHEET), &layer.spritesheetId, spritesheets.ids, - spritesheets.labels); - ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_SPRITESHEET)); - return; - } - - auto libraries = document.choices_get(SelectionKind::LIBRARIES); - libraries.labels.front() = localize.get(TEXT_SELF); - combo_id_mapped(localize.get(LABEL_LIBRARY), &layer.libraryId, libraries.ids, libraries.labels); - ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_LIBRARY)); - auto names = document.animation_names_get(layer.libraryId); - if (layer.animation.empty() && !names.empty()) layer.animation = names.front(); - combo_string_mapped(localize.get(LABEL_ANIMATION), &layer.animation, names, names); - ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_ANIMATION)); - }); + item_properties_update(panel, 0, LAYER_PROPERTIES_ROWS, SelectionKind::LAYERS, TOOLTIP_ITEM_NAME, + EDIT_ADD_LAYER, EDIT_SET_LAYER_PROPERTIES, layers_get, + [&](Manager::ItemEdit& layer) + { + ImGui::SeparatorText(localize.get(LABEL_LAYER_SOURCE)); + layer_source_draw(document, layer); + }); } void nulls_update(Panel& panel) diff --git a/src/imgui/window/panel.hpp b/src/imgui/window/panel.hpp index 77aa2e5..e5b1ba4 100644 --- a/src/imgui/window/panel.hpp +++ b/src/imgui/window/panel.hpp @@ -148,6 +148,9 @@ namespace anm2ed::imgui void nulls_update(Panel&); void events_update(Panel&); void sounds_update(Panel&); + model::Layer item_edit_layer_get(const Manager::ItemEdit&); + model::Null item_edit_null_get(const Manager::ItemEdit&); + void layer_source_draw(Document&, Manager::ItemEdit&); void libraries_update(Panel&); void spritesheets_update(Panel&, SpritesheetsPanel&); void regions_update(Panel&, RegionsPanel&); diff --git a/tests/operations.cpp b/tests/operations.cpp index 73d6a0c..de614d2 100644 --- a/tests/operations.cpp +++ b/tests/operations.cpp @@ -58,8 +58,9 @@ TEST_CASE("file_merge presets") TEST_CASE("item_add") { auto model = fixture_load("02_items.anm2"); - CHECK(edit::item_add(model, ItemType::LAYER, 0, -1, "added", 1, false, 1, types::destination::ALL) == 2); - CHECK(edit::item_add(model, ItemType::NULL_, 1, -1, "thisOnly", 0, false, -1, types::destination::THIS) == 2); + CHECK(edit::item_add(model, ItemType::LAYER, 0, -1, {.name = "added", .spritesheetId = 1}, {}, 1, + types::destination::ALL) == 2); + CHECK(edit::item_add(model, ItemType::NULL_, 1, -1, {}, {.name = "thisOnly"}, -1, types::destination::THIS) == 2); operation_check("item_add", model); }