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:
1 parent
af60cdc11c
commit
6b6cd671f7
7 files changed
+91
-94
No files matched your search
+1
-1
@@ -110,7 +110,7 @@ namespace anm2ed::edit
|
||||
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 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_indices_get(const model::Model&, int);
|
||||
|
||||
+13
-16
@@ -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;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <format>
|
||||
|
||||
#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);
|
||||
});
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
+56
-44
@@ -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 <class Item, class ItemsGet, class FieldsDraw>
|
||||
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, model::Layer>)
|
||||
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<model::Layer>(
|
||||
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<model::Layer>(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)
|
||||
|
||||
@@ -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&);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user