Item Properties shares Layer Properties' source fields

New layers added from the timeline can play an animation too: both popups
draw the source with layer_source_draw and build the item from the same
ItemEdit, and edit::item_add takes the new layer or null whole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-08 17:02:28 -04:00
1 parent af60cdc11c
commit 6b6cd671f7
7 files changed
+87 -90

No files matched your search

+1 -1
View File
@@ -110,7 +110,7 @@ namespace anm2ed::edit
Uids items_group(model::Model&, int, int, const std::set<int>&, const std::string&); Uids items_group(model::Model&, int, int, const std::set<int>&, const std::string&);
Uids items_move(model::Model&, int, int, const std::set<int>&, const std::set<int>&, RowTarget, bool, bool); Uids items_move(model::Model&, int, int, const std::set<int>&, const std::set<int>&, RowTarget, bool, bool);
Uids animation_length_fit(model::Model&, int); 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<int> animation_group_ids_get(const model::Model&); std::set<int> animation_group_ids_get(const model::Model&);
std::set<int> animation_group_indices_get(const model::Model&, int); std::set<int> animation_group_indices_get(const model::Model&, int);
+13 -16
View File
@@ -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. // 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, // Adds track `id` (a new item, from `layer` or `null`, at -1) to an animation or all of them.
bool isShowRect, int insertBeforeId, types::destination::Type destination) 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& 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 (type == ItemType::LAYER)
{ item_insert(content.layers, 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;
}
else if (type == ItemType::NULL_) else if (type == ItemType::NULL_)
{ item_insert(content.nulls, 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;
}
else else
return -1; return -1;
+14 -27
View File
@@ -2,6 +2,7 @@
#include <format> #include <format>
#include "../window/panel.hpp"
#include "util/imgui/input.hpp" #include "util/imgui/input.hpp"
#include "util/imgui/layout.hpp" #include "util/imgui/layout.hpp"
#include "util/imgui/shortcut.hpp" #include "util/imgui/shortcut.hpp"
@@ -13,12 +14,7 @@ namespace anm2ed::imgui::popup
{ {
constexpr int EXISTING_ITEMS_ROWS = 6; constexpr int EXISTING_ITEMS_ROWS = 6;
void ItemProperties::reset() void ItemProperties::reset() { edit = {}; }
{
addItemName.clear();
addItemID = -1;
addItemSpritesheetID = {};
}
void ItemProperties::open() void ItemProperties::open()
{ {
@@ -114,18 +110,13 @@ namespace anm2ed::imgui::popup
{ {
ImGui::SeparatorText(localize.get(LABEL_OPTIONS)); 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)); ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_ITEM_NAME));
if (type == LAYER) if (type == LAYER)
{ layer_source_draw(document, edit);
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));
}
else if (type == NULL_) 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)); ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_NULL_RECT));
} }
} }
@@ -137,11 +128,11 @@ namespace anm2ed::imgui::popup
ImVec2(0, ImGui::GetFrameHeightWithSpacing() * EXISTING_ITEMS_ROWS))) ImVec2(0, ImGui::GetFrameHeightWithSpacing() * EXISTING_ITEMS_ROWS)))
{ {
auto unusedItems = unused_items_get(type); 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) for (auto id : unusedItems)
{ {
auto isSelected = addItemID == id; auto isSelected = edit.id == id;
ImGui::PushID(id); ImGui::PushID(id);
@@ -153,8 +144,7 @@ namespace anm2ed::imgui::popup
std::make_format_args(id, layer->name, layer->spritesheetId)); std::make_format_args(id, layer->name, layer->spritesheetId));
if (ImGui::Selectable(label.c_str(), isSelected)) if (ImGui::Selectable(label.c_str(), isSelected))
{ {
addItemID = id; edit.id = id;
addItemSpritesheetID = layer->spritesheetId;
} }
} }
} }
@@ -165,8 +155,7 @@ namespace anm2ed::imgui::popup
auto label = std::vformat(localize.get(FORMAT_NULL), std::make_format_args(id, null->name)); auto label = std::vformat(localize.get(FORMAT_NULL), std::make_format_args(id, null->name));
if (ImGui::Selectable(label.c_str(), isSelected)) if (ImGui::Selectable(label.c_str(), isSelected))
{ {
addItemID = id; edit.id = id;
addItemIsShowRect = null->isShowRect;
} }
} }
} }
@@ -181,7 +170,7 @@ namespace anm2ed::imgui::popup
auto widgetSize = widget_size_with_row_get(2); 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]); shortcut(manager.chords[SHORTCUT_CONFIRM]);
if (ImGui::Button(localize.get(BASIC_ADD), widgetSize)) if (ImGui::Button(localize.get(BASIC_ADD), widgetSize))
{ {
@@ -189,10 +178,9 @@ namespace anm2ed::imgui::popup
auto queuedDestination = destination; auto queuedDestination = destination;
auto queuedAnimationIndex = reference.animationIndex; auto queuedAnimationIndex = reference.animationIndex;
auto queuedInsertBeforeID = reference.itemType == LAYER && type == LAYER ? reference.itemID : -1; auto queuedInsertBeforeID = reference.itemType == LAYER && type == LAYER ? reference.itemID : -1;
auto queuedAddItemID = addItemID; auto queuedAddItemID = source == source::NEW ? -1 : edit.id;
auto queuedAddItemName = addItemName; auto queuedLayer = item_edit_layer_get(edit);
auto queuedAddItemSpritesheetID = addItemSpritesheetID; auto queuedNull = item_edit_null_get(edit);
auto queuedAddItemIsShowRect = addItemIsShowRect;
manager.command_push({manager.selected, [=](Manager&, Document& document) manager.command_push({manager.selected, [=](Manager&, Document& document)
{ {
@@ -202,8 +190,7 @@ namespace anm2ed::imgui::popup
{ {
addId = edit::item_add( addId = edit::item_add(
model, (ItemType)queuedType, queuedAnimationIndex, model, (ItemType)queuedType, queuedAnimationIndex,
queuedAddItemID, queuedAddItemName, queuedAddItemID, queuedLayer, queuedNull,
queuedAddItemSpritesheetID, queuedAddItemIsShowRect,
queuedInsertBeforeID, (destination::Type)queuedDestination); queuedInsertBeforeID, (destination::Type)queuedDestination);
}); });
+1 -4
View File
@@ -12,10 +12,7 @@ namespace anm2ed::imgui::popup
class ItemProperties class ItemProperties
{ {
PopupHelper popup{PopupHelper(LABEL_TIMELINE_PROPERTIES_POPUP, POPUP_SMALL_NO_HEIGHT)}; PopupHelper popup{PopupHelper(LABEL_TIMELINE_PROPERTIES_POPUP, POPUP_SMALL_NO_HEIGHT)};
std::string addItemName{"New Item"}; Manager::ItemEdit edit{};
bool addItemIsShowRect{};
int addItemID{-1};
int addItemSpritesheetID{-1};
void reset(); void reset();
+52 -40
View File
@@ -22,6 +22,50 @@ namespace anm2ed::imgui
ImGui::TextUnformatted(std::vformat(localize.get(FORMAT_ID), std::make_format_args(id)).c_str()); 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. // The layer/null properties popup: adds a new item (id -1) or replaces the edited one, then selects it.
template <class Item, class ItemsGet, class FieldsDraw> template <class Item, class ItemsGet, class FieldsDraw>
void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKind kind, StringType nameTooltip, 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)); auto result = popup_buttons_draw(manager, localize.get(edit.id == -1 ? BASIC_ADD : BASIC_CONFIRM));
if (result == PopupButton::CONFIRM) if (result == PopupButton::CONFIRM)
{ {
Item item{.name = edit.name}; Item item{};
if constexpr (requires { item.spritesheetId; }) item.spritesheetId = edit.spritesheetId; if constexpr (std::is_same_v<Item, model::Layer>)
if constexpr (requires { item.isShowRect; }) item.isShowRect = edit.isShowRect; item = item_edit_layer_get(edit);
if constexpr (requires { item.animation; }) else
if (edit.isAnimation) item = item_edit_null_get(edit);
{
item.libraryId = edit.libraryId;
item.animation = edit.animation;
}
auto& state = panel.state; auto& state = panel.state;
command_push(panel, command_push(panel,
[&state, item, id = edit.id, kind, addEdit, propertiesEdit, items_get](Document& document) [&state, item, id = edit.id, kind, addEdit, propertiesEdit, items_get](Document& document)
@@ -142,40 +182,12 @@ namespace anm2ed::imgui
} }
ImGui::End(); ImGui::End();
item_properties_update<model::Layer>( item_properties_update<model::Layer>(panel, 0, LAYER_PROPERTIES_ROWS, SelectionKind::LAYERS, TOOLTIP_ITEM_NAME,
panel, 0, LAYER_PROPERTIES_ROWS, SelectionKind::LAYERS, TOOLTIP_ITEM_NAME, EDIT_ADD_LAYER, EDIT_ADD_LAYER, EDIT_SET_LAYER_PROPERTIES, layers_get,
EDIT_SET_LAYER_PROPERTIES, layers_get,
[&](Manager::ItemEdit& layer) [&](Manager::ItemEdit& layer)
{ {
ImGui::SeparatorText(localize.get(LABEL_LAYER_SOURCE)); ImGui::SeparatorText(localize.get(LABEL_LAYER_SOURCE));
ImGui::PushID("##Source"); layer_source_draw(document, layer);
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));
}); });
} }
+3
View File
@@ -148,6 +148,9 @@ namespace anm2ed::imgui
void nulls_update(Panel&); void nulls_update(Panel&);
void events_update(Panel&); void events_update(Panel&);
void sounds_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 libraries_update(Panel&);
void spritesheets_update(Panel&, SpritesheetsPanel&); void spritesheets_update(Panel&, SpritesheetsPanel&);
void regions_update(Panel&, RegionsPanel&); void regions_update(Panel&, RegionsPanel&);
+3 -2
View File
@@ -58,8 +58,9 @@ TEST_CASE("file_merge presets")
TEST_CASE("item_add") TEST_CASE("item_add")
{ {
auto model = fixture_load("02_items.anm2"); 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::LAYER, 0, -1, {.name = "added", .spritesheetId = 1}, {}, 1,
CHECK(edit::item_add(model, ItemType::NULL_, 1, -1, "thisOnly", 0, false, -1, types::destination::THIS) == 2); 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); operation_check("item_add", model);
} }