Split window.cpp per window and drive menus from one flag table

- WINDOW_FLAGS table builds every context menu, footer and shortcut set
- Shared list-row and card-row renderers replace per-window copies
- Layers/nulls/events share one properties popup; Manager keeps one item popup API
- Regions, spritesheets and sounds drop hand-built menus, footers and shortcut blocks
- Animation remove and cut share one erase path that also clears group ids
- Index-mapped multi-select fixes shift-range over non-contiguous ids
- Fix Remove Unused tooltip showing the Add shortcut and the missing break in item properties

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 13:27:34 -04:00
1 parent 3f31cedeed
commit d926fc6677
13 files changed
+2797 -4452

No files matched your search

+3
View File
@@ -509,6 +509,9 @@ namespace anm2ed
bool element_child_id_erase(Element&, ElementType, int);
int track_frames_count_get(const Element&);
int animations_count_get(const Element&);
int animations_child_index_get(const Element&, int);
int animations_child_insert_index_get(const Element&, int);
Element root_animation_make();
float interpolation_factor(Interpolation, float);
void frame_mix(Element&, const Element&, float);
Element frame_generate(const Element&, float);
-3
View File
@@ -61,9 +61,6 @@ namespace anm2ed
ElementType group_child_type_get(ElementType);
Element* content_container_get(Element&, ElementType);
std::unordered_map<int, int> child_ids_compact(Element&, ElementType);
int animations_child_index_get(const Element&, int);
int animations_child_insert_index_get(const Element&, int);
Element root_animation_make();
void group_frames_bake(Element&);
void group_frames_restore(Element&);
void group_metadata_embed(Element&);
+954
View File
@@ -0,0 +1,954 @@
#include "window.hpp"
#include <algorithm>
#include <format>
#include <optional>
#include "toast.hpp"
#include "util/imgui/draw.hpp"
#include "util/imgui/layout.hpp"
#include "util/imgui/shortcut.hpp"
#include "util/imgui/tree.hpp"
#include "vector.hpp"
using namespace anm2ed::resource;
using namespace anm2ed::types;
using namespace anm2ed::util;
namespace anm2ed::imgui
{
constexpr const char* ANIMATION_DRAG_DROP = "Animation Drag Drop";
constexpr float DROP_ZONE_FRACTION = 1.0f / 3.0f;
constexpr int MERGE_OPTIONS_ROWS = 2;
constexpr float MERGE_OPTIONS_HALF = 0.5f;
constexpr int MERGE_POPUP_CHILD_COUNT = 3;
enum class AnimationDropZone
{
BEFORE,
INSIDE,
AFTER
};
enum class AnimationDragDropType
{
ANIMATION,
GROUP
};
struct AnimationDragDropItem
{
AnimationDragDropType type{AnimationDragDropType::ANIMATION};
int id{-1};
bool operator==(const AnimationDragDropItem&) const = default;
};
struct AnimationMergeOptions
{
std::set<int> selection{};
int reference{-1};
merge::Type type{};
bool isDeleteAnimationsAfter{};
};
std::set<int> animation_group_ids_get(const Element& animations)
{
std::set<int> result{};
for (const auto& item : animations.children)
if (item.type == ElementType::GROUP) result.insert(item.id);
return result;
}
std::set<int> animation_group_indices_get(const Element& animations, int groupId)
{
std::set<int> result{};
int animationIndex{};
for (const auto& animation : animations.children)
if (animation.type == ElementType::ANIMATION)
{
if (animation.groupId == groupId) result.insert(animationIndex);
++animationIndex;
}
return result;
}
int animation_index_from_child_index_get(const Element& animations, int childIndex)
{
int current{};
for (int i = 0; i < (int)animations.children.size(); ++i)
if (animations.children[i].type == ElementType::ANIMATION)
{
if (i == childIndex) return current;
++current;
}
return -1;
}
int animation_group_child_insert_index_get(const Element& animations, int groupId, AnimationDropZone dropZone)
{
auto group = std::ranges::find_if(animations.children, [&](const Element& item)
{ return item.type == ElementType::GROUP && item.id == groupId; });
if (group == animations.children.end()) return (int)animations.children.size();
auto groupChildIndex = (int)(group - animations.children.begin());
if (dropZone != AnimationDropZone::INSIDE) return groupChildIndex + (dropZone == AnimationDropZone::AFTER);
auto insertIndex = groupChildIndex + 1;
for (int i = 0; i < (int)animations.children.size(); ++i)
if (animations.children[i].type == ElementType::ANIMATION && animations.children[i].groupId == groupId)
insertIndex = i + 1;
return insertIndex;
}
bool is_animation_grouped(const std::set<int>& groupIds, const Element& animation)
{
return animation.groupId != -1 && groupIds.contains(animation.groupId);
}
std::set<int> animation_selected_indices_get(Document& document, const Window& window)
{
std::set<int> result(document.animation.selection.begin(), document.animation.selection.end());
if (auto animations = document.anm2.element_get(ElementType::ANIMATIONS))
for (auto groupId : window.selection)
result.merge(animation_group_indices_get(*animations, groupId));
return result;
}
std::vector<int> animation_groupable_indices_get(Document& document)
{
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
auto& selection = document.animation.selection;
if (!animations || selection.empty()) return {};
auto groupIds = animation_group_ids_get(*animations);
std::vector<int> result{};
int index{};
for (const auto& animation : animations->children)
if (animation.type == ElementType::ANIMATION)
{
if (selection.contains(index) && is_animation_grouped(groupIds, animation)) return {};
if (selection.contains(index)) result.push_back(index);
++index;
}
return result.size() == selection.size() ? result : std::vector<int>{};
}
std::set<int> animation_merge_indices_get(Document& document, const std::set<int>& keys, int reference)
{
std::set<int> result{};
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
for (auto key : keys)
if (!is_window_group_key(key))
result.insert(key);
else if (animations)
result.merge(animation_group_indices_get(*animations, window_group_id_from_key_get(key)));
result.erase(reference);
return result;
}
void animation_overlay_reset(Document& document, const std::set<int>& indices)
{
auto isOverlayDocument = document.overlayDocumentId == 0 || document.overlayDocumentId == document.tabId;
if (!isOverlayDocument || !indices.contains(document.overlayIndex)) return;
document.overlayIndex = -1;
document.overlayDocumentId = 0;
}
void animation_groups_remove(Element& animations, const std::set<int>& groupIds)
{
for (auto& item : animations.children)
if (item.type == ElementType::ANIMATION && groupIds.contains(item.groupId)) item.groupId = -1;
std::erase_if(animations.children,
[&](const Element& item) { return item.type == ElementType::GROUP && groupIds.contains(item.id); });
}
void animations_erase(Window& window, Document& document, const std::set<int>& indices, const std::set<int>& groupIds)
{
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
if (!animations) return;
animation_groups_remove(*animations, groupIds);
animation_overlay_reset(document, indices);
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
{
auto childIndex = animations_child_index_get(*animations, *it);
if (childIndex == -1) continue;
if (document.reference.animationIndex == *it) document.reference.animationIndex = -1;
animations->children.erase(animations->children.begin() + childIndex);
}
document.animation.selection.clear();
window.selection.clear();
}
std::string animation_clipboard_text_get(Document& document, const Window& window)
{
std::string clipboardText{};
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
if (!animations) return clipboardText;
int animationIndex{};
for (const auto& item : animations->children)
{
if (item.type == ElementType::GROUP && window.selection.contains(item.id))
clipboardText += element_to_string(item);
if (item.type != ElementType::ANIMATION) continue;
if (document.animation.selection.contains(animationIndex) || window.selection.contains(item.groupId))
clipboardText += element_to_string(item);
++animationIndex;
}
return clipboardText;
}
void animations_insert(Window& window, Document& document, const std::string& text, int start, int targetGroupId,
StringType edit)
{
std::set<int> indices{};
std::set<int> groupIds{};
std::string errorString{};
auto pasted = document.anm2;
if (!pasted.animations_deserialize(text, start, indices, &errorString, &groupIds))
{
toast_log(Level::ERROR, TOAST_DESERIALIZE_ANIMATIONS_FAILED, errorString);
return;
}
window_edit(document, window.changeType, localize.get(edit),
[&]()
{
document.anm2 = std::move(pasted);
if (targetGroupId != -1 && groupIds.empty())
for (auto index : indices)
if (auto animation = document.anm2.element_get(ElementType::ANIMATION, index))
animation->groupId = targetGroupId;
document.animation.selection = groupIds.empty() ? indices : std::set<int>{};
window.selection = groupIds;
document.reference = {};
window.newElementId = -1;
if (indices.empty()) return;
window.scrollQueued = *indices.rbegin();
if (!groupIds.empty()) return;
document.reference = {*indices.rbegin()};
if (indices.size() == 1) window.newElementId = *indices.rbegin();
});
}
int animations_merge(Document& document, const AnimationMergeOptions& options,
const std::set<int>* quickSelection = nullptr,
const std::set<int>* quickGroupSelection = nullptr)
{
auto& anm2 = document.anm2;
int merged{-1};
document.snapshots.anm2_push(localize.get(EDIT_MERGE_ANIMATIONS));
if (options.selection.empty())
{
auto selected = *quickSelection;
auto isQuickGroupMerge = quickGroupSelection && !quickGroupSelection->empty();
auto animations = anm2.element_get(ElementType::ANIMATIONS);
auto count = animations ? animations_count_get(*animations) : 0;
animation_overlay_reset(document, selected);
if (selected.size() == 1 && !isQuickGroupMerge && *selected.begin() != count - 1)
selected.insert(*selected.begin() + 1);
if (selected.size() > 1)
merged = anm2.animations_merge(*selected.begin(), selected);
else if (selected.size() == 1 && isQuickGroupMerge)
merged = *selected.begin();
if (merged != -1 && isQuickGroupMerge && animations) animation_groups_remove(*animations, *quickGroupSelection);
}
else
{
auto mergeSelection = animation_merge_indices_get(document, options.selection, options.reference);
if (mergeSelection.empty()) return -1;
animation_overlay_reset(document, mergeSelection);
merged = anm2.animations_merge(options.reference, mergeSelection, options.type, options.isDeleteAnimationsAfter);
}
if (merged == -1) return -1;
document.animation.selection = {merged};
document.reference = {merged};
document.change(Document::ANIMATIONS);
return merged;
}
Element animation_track_container_shell_copy(const Element* source, const TrackContainer& row)
{
auto destination = element_make(row.container);
if (!source) return destination;
auto nextGroupId = element_child_next_id_get(*source, ElementType::GROUP);
std::set<int> copiedTrackIds{};
auto track_push = [&](const Element& track, int groupId)
{
if (track.type != row.track || !copiedTrackIds.insert(track_id_get(track)).second) return;
auto item = element_make(row.track);
item.*(row.id) = track.*(row.id);
item.groupId = track.groupId != -1 ? track.groupId : groupId;
item.isVisible = track.isVisible;
destination.children.push_back(item);
};
for (const auto& sourceItem : source->children)
{
if (sourceItem.type != ElementType::GROUP)
{
track_push(sourceItem, -1);
continue;
}
auto group = element_make(ElementType::GROUP);
group.id = sourceItem.id >= 0 ? sourceItem.id : nextGroupId++;
group.name = sourceItem.name;
group.isExpanded = sourceItem.isExpanded;
group.isVisible = sourceItem.isVisible;
group.children.push_back(root_animation_make());
destination.children.push_back(group);
for (const auto& child : sourceItem.children)
track_push(child, group.id);
}
return destination;
}
AnimationDropZone animation_drop_zone_get(ImVec2 min, ImVec2 max)
{
auto zoneHeight = (max.y - min.y) * DROP_ZONE_FRACTION;
auto mouseY = ImGui::GetIO().MousePos.y;
if (mouseY < min.y + zoneHeight) return AnimationDropZone::BEFORE;
if (mouseY >= max.y - zoneHeight) return AnimationDropZone::AFTER;
return AnimationDropZone::INSIDE;
}
void animation_items_move(Window& window, Document& document, const std::vector<AnimationDragDropItem>& items,
int targetChildIndex, int targetGroupId)
{
auto animations = window_container_get(window, document);
if (!animations) return;
auto groupIds = animation_group_ids_get(*animations);
auto groupId = groupIds.contains(targetGroupId) ? targetGroupId : -1;
std::set<int> draggedAnimationIds{};
std::set<int> draggedGroupIds{};
for (auto item : items)
if (item.type == AnimationDragDropType::GROUP && groupIds.contains(item.id))
draggedGroupIds.insert(item.id);
else if (item.type == AnimationDragDropType::ANIMATION && animations_child_index_get(*animations, item.id) != -1)
draggedAnimationIds.insert(item.id);
if (draggedAnimationIds.empty() && draggedGroupIds.empty()) return;
std::vector<int> childIndices{};
std::set<int> directAnimationChildIndices{};
int animationIndex{};
for (int i = 0; i < (int)animations->children.size(); ++i)
{
auto& item = animations->children[i];
if (item.type == ElementType::GROUP && draggedGroupIds.contains(item.id)) childIndices.push_back(i);
if (item.type != ElementType::ANIMATION) continue;
auto isGroupDragged = draggedGroupIds.contains(item.groupId);
if (draggedAnimationIds.contains(animationIndex) && !isGroupDragged)
{
item.groupId = groupId;
directAnimationChildIndices.insert(i);
}
if (isGroupDragged || draggedAnimationIds.contains(animationIndex)) childIndices.push_back(i);
++animationIndex;
}
auto movedChildIndices = vector::move_indices_to_position(animations->children, childIndices, targetChildIndex);
if (movedChildIndices.empty()) return;
document.animation.selection.clear();
window.selection = draggedGroupIds;
auto moved = movedChildIndices.begin();
for (auto source = childIndices.begin(); source != childIndices.end() && moved != movedChildIndices.end();
++source, ++moved)
if (directAnimationChildIndices.contains(*source))
if (auto index = animation_index_from_child_index_get(*animations, *moved); index != -1)
document.animation.selection.insert(index);
}
void animation_move_command_push(Window& window, Manager& manager, std::vector<AnimationDragDropItem> items,
int targetGroupId, std::function<int(const Element&)> target_child_index_get)
{
if (items.empty()) return;
manager.command_push(
{manager.selected, [&window, items, targetGroupId, target_child_index_get](Manager&, Document& document)
{
auto animations = window_container_get(window, document);
if (!animations) return;
window_edit(
document, window.changeType, localize.get(EDIT_MOVE_ANIMATIONS), [&]()
{ animation_items_move(window, document, items, target_child_index_get(*animations), targetGroupId); });
}});
}
void animation_drag_drop_source_update(Document& document, const Window& window, AnimationDragDropItem fallback)
{
if (!ImGui::BeginDragDropSource(DRAG_DROP_SOURCE_FLAGS)) return;
static std::vector<AnimationDragDropItem> items{};
items.clear();
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
auto isFallbackSelected = fallback.type == AnimationDragDropType::GROUP
? window.selection.contains(fallback.id)
: document.animation.selection.contains(fallback.id);
if (animations && isFallbackSelected)
{
auto groupIds = animation_group_ids_get(*animations);
for (auto groupId : window.selection)
if (groupIds.contains(groupId)) items.push_back({AnimationDragDropType::GROUP, groupId});
for (auto animationIndex : document.animation.selection)
if (animations_child_index_get(*animations, animationIndex) != -1)
items.push_back({AnimationDragDropType::ANIMATION, animationIndex});
}
if (items.empty() && fallback.id >= 0) items.push_back(fallback);
ImGui::SetDragDropPayload(ANIMATION_DRAG_DROP, items.data(), items.size() * sizeof(AnimationDragDropItem));
ImGui::EndDragDropSource();
}
std::vector<AnimationDragDropItem> animation_drag_drop_target_items_get()
{
auto payload = ImGui::AcceptDragDropPayload(ANIMATION_DRAG_DROP, ImGuiDragDropFlags_AcceptBeforeDelivery |
ImGuiDragDropFlags_AcceptNoDrawDefaultRect);
if (!payload || payload->DataSize % (int)sizeof(AnimationDragDropItem) != 0) return {};
auto items = static_cast<const AnimationDragDropItem*>(payload->Data);
return {items, items + payload->DataSize / sizeof(AnimationDragDropItem)};
}
bool animation_group_drag_drop_update(Window& window, Manager& manager, int groupId, ImVec2 itemMin, ImVec2 itemMax)
{
if (!ImGui::BeginDragDropTarget()) return false;
auto items = animation_drag_drop_target_items_get();
auto isDelivered = false;
if (!items.empty())
{
auto dropZone = animation_drop_zone_get(itemMin, itemMax);
auto isGroupDragged = std::ranges::any_of(items, [](const AnimationDragDropItem& item)
{ return item.type == AnimationDragDropType::GROUP; });
auto isDropIntoGroup = dropZone == AnimationDropZone::INSIDE && !isGroupDragged;
if (isDropIntoGroup)
drop_box_draw(ImGui::GetWindowDrawList(), itemMin, itemMax);
else
drop_line_draw(ImGui::GetWindowDrawList(), itemMin, itemMax, dropZone != AnimationDropZone::BEFORE);
isDelivered = ImGui::GetDragDropPayload()->IsDelivery();
if (isDelivered)
{
auto targetDropZone =
dropZone == AnimationDropZone::INSIDE && !isDropIntoGroup ? AnimationDropZone::AFTER : dropZone;
animation_move_command_push(
window, manager, items, isDropIntoGroup ? groupId : -1, [groupId, targetDropZone](const Element& animations)
{ return animation_group_child_insert_index_get(animations, groupId, targetDropZone); });
}
}
ImGui::EndDragDropTarget();
return isDelivered;
}
bool animation_row_drag_drop_update(Window& window, Manager& manager, Document& document, const Element& animation,
int index)
{
animation_drag_drop_source_update(document, window, {AnimationDragDropType::ANIMATION, index});
if (!ImGui::BeginDragDropTarget()) return false;
auto items = animation_drag_drop_target_items_get();
auto isDelivered = false;
if (!items.empty())
{
auto itemMin = ImGui::GetItemRectMin();
auto itemMax = ImGui::GetItemRectMax();
auto isDropAfter = is_drop_after(itemMin, itemMax);
drop_line_draw(ImGui::GetWindowDrawList(), itemMin, itemMax, isDropAfter);
isDelivered = ImGui::GetDragDropPayload()->IsDelivery();
if (isDelivered)
animation_move_command_push(window, manager, items, animation.groupId,
[targetIndex = index + (isDropAfter ? 1 : 0)](const Element& animations)
{ return animations_child_insert_index_get(animations, targetIndex); });
}
ImGui::EndDragDropTarget();
return isDelivered;
}
void animation_group_set_push(Manager& manager, int groupId, StringType edit, std::optional<std::string> name,
std::optional<bool> isExpanded)
{
manager.command_push({manager.selected, [groupId, edit, name, isExpanded](Manager&, Document& document)
{
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
auto group = animations ? child_id_get(*animations, ElementType::GROUP, groupId) : nullptr;
if (!group || (name.value_or(group->name) == group->name &&
isExpanded.value_or(group->isExpanded) == group->isExpanded))
return;
window_edit(document, Document::ANIMATIONS, localize.get(edit),
[&]()
{
group->name = name.value_or(group->name);
group->isExpanded = isExpanded.value_or(group->isExpanded);
});
}});
}
void animation_group_draw(Window& window, Manager& manager, Resources& resources, Document& document, Element& group,
int arrowSelectionId, bool& isRowsDone)
{
auto& groupSelection = window.selection;
auto container = window_container_get(window, document);
auto renameKey = window_group_key_get(group.id);
auto isRenaming = window.renameId == renameKey;
if (window.renameQueued == renameKey)
{
window.renameState = RENAME_FORCE_EDIT;
window.renameQueued = -1;
}
ImGui::PushID("Animation Group");
ImGui::PushID(group.id);
ImGui::SetNextItemOpen(group.isExpanded, ImGuiCond_Always);
auto tree =
tree_node_input_text(group.name.empty() ? std::string(localize.get(TEXT_NEW_GROUP)) : group.name,
std::format("###Document #{} Animation Group #{}", manager.selected, group.id),
isRenaming ? window.renameText : group.name, groupSelection.contains(group.id),
ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth, window.renameState);
auto itemMin = ImGui::GetItemRectMin();
auto itemMax = ImGui::GetItemRectMax();
if (tree.isRenameStarted)
{
window.renameId = renameKey;
window.renameText = group.name;
}
else if (tree.isRenameFinished)
{
if (isRenaming) animation_group_set_push(manager, group.id, EDIT_RENAME_GROUP, window.renameText, std::nullopt);
window.renameId = -1;
window.renameText.clear();
}
if (tree.isOpen != group.isExpanded && !is_drag_drop_active() && !ImGui::IsMouseDragging(ImGuiMouseButton_Left))
animation_group_set_push(manager, group.id, EDIT_TOGGLE_GROUP_EXPANDED, std::nullopt, tree.isOpen);
if (tree.isClicked)
{
auto isCtrl = ImGui::GetIO().KeyCtrl;
auto& selection = document.animation.selection;
auto isDeselected = isCtrl && groupSelection.contains(group.id);
if (!isCtrl)
{
selection.clear();
groupSelection.clear();
}
if (isDeselected)
groupSelection.erase(group.id);
else
groupSelection.insert(group.id);
if (isDeselected ? selection.empty() && groupSelection.empty() : !isCtrl || selection.empty())
{
document.animation.reference = -1;
document.reference = {};
document.frames.clear();
}
}
animation_drag_drop_source_update(document, window, {AnimationDragDropType::GROUP, group.id});
isRowsDone = animation_group_drag_drop_update(window, manager, group.id, itemMin, itemMax);
if (tree.isOpen)
{
int animationIndex{};
for (auto& animation : container->children)
if (animation.type == ElementType::ANIMATION && !isRowsDone)
{
if (animation.groupId == group.id)
isRowsDone = window_row_draw(window, manager, resources, document, animation, animationIndex,
animationIndex, arrowSelectionId);
++animationIndex;
}
ImGui::TreePop();
}
ImGui::PopID();
ImGui::PopID();
}
Window animations_window_register()
{
Window window{};
window.title = LABEL_ANIMATIONS_WINDOW;
window.isOpen = &Settings::windowIsAnimations;
window.changeType = Document::ANIMATIONS;
window.containerType = ElementType::ANIMATIONS;
window.elementType = ElementType::ANIMATION;
window.childLabel = "##Animations Child";
window.renameEdit = SNAPSHOT_RENAME_ANIMATION;
window.flags = WINDOW_ADD | WINDOW_DUPLICATE | WINDOW_MERGE | WINDOW_GROUP | WINDOW_REMOVE | WINDOW_DEFAULT |
WINDOW_CUT | WINDOW_COPY | WINDOW_PASTE | WINDOW_RENAME;
window.footer = {{WINDOW_ADD, WINDOW_DUPLICATE, WINDOW_MERGE, WINDOW_REMOVE, WINDOW_DEFAULT}};
window.tooltips = {{WINDOW_ADD, TOOLTIP_ADD_ANIMATION},
{WINDOW_DUPLICATE, TOOLTIP_DUPLICATE_ANIMATION},
{WINDOW_MERGE, TOOLTIP_OPEN_MERGE_POPUP},
{WINDOW_REMOVE, TOOLTIP_REMOVE_ANIMATION},
{WINDOW_DEFAULT, TOOLTIP_SET_DEFAULT_ANIMATION}};
window.enabled = {
{WINDOW_MERGE,
[](Window& window, Document& document) { return !animation_selected_indices_get(document, window).empty(); }},
{WINDOW_GROUP, [](Window&, Document& document) { return !animation_groupable_indices_get(document).empty(); }}};
window.popup = PopupHelper(LABEL_ANIMATIONS_MERGE_POPUP);
window.storage_get = [](Document& document) -> Storage& { return document.animation; };
window.element_get = [](Anm2& anm2, int index) { return anm2.element_get(ElementType::ANIMATION, index); };
window.element_key_get = [](const Element&, int index) { return index; };
window.row_font_get = [](Document& document, const Element& animation, int index)
{
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
auto isDefault = animations && animations->defaultAnimation == animation.name;
auto isReferenced = document.reference.animationIndex == index;
return isDefault && isReferenced ? font::BOLD_ITALICS
: isDefault ? font::BOLD
: isReferenced ? font::ITALICS
: font::REGULAR;
};
window.row_select = [](Window& window, Document& document, int index)
{
auto& io = ImGui::GetIO();
if (!io.KeyCtrl && !io.KeyShift) window.selection.clear();
document.reference = {index};
document.frames.clear();
};
window.rename_finish = [](Document& document, Element& animation, int, int count)
{
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
if (animations && count == 1) animations->defaultAnimation = animation.name;
};
window.tooltip_draw = [](Document& document, Resources& resources, const Element& animation)
{
auto animations = document.anm2.element_get(ElementType::ANIMATIONS);
window_tooltip_name_draw(resources, animation.name);
if (animations && animations->defaultAnimation == animation.name)
{
ImGui::PushFont(resources.fonts[font::ITALICS].get(), font::SIZE);
ImGui::TextUnformatted(localize.get(BASIC_DEFAULT));
ImGui::PopFont();
}
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_LENGTH), std::make_format_args(animation.frameNum)).c_str());
auto loopLabel = localize.get(animation.isLoop ? BASIC_YES : BASIC_NO);
ImGui::TextUnformatted(std::vformat(localize.get(FORMAT_LOOP), std::make_format_args(loopLabel)).c_str());
};
window.row_drag_drop_update = animation_row_drag_drop_update;
window.rows_update = [](Window& window, Manager& manager, Settings& settings, Resources& resources,
Clipboard& clipboard, Document& document)
{
auto container = window_container_get(window, document);
if (!container) return;
auto groupIds = animation_group_ids_get(*container);
std::erase_if(window.selection, [&](int groupId) { return !groupIds.contains(groupId); });
std::vector<int> visibleIds{};
window.order.clear();
int animationIndex{};
for (auto& item : container->children)
{
if (item.type == ElementType::GROUP && item.isExpanded)
visibleIds.insert_range(visibleIds.end(), animation_group_indices_get(*container, item.id));
if (item.type != ElementType::ANIMATION) continue;
if (!is_animation_grouped(groupIds, item)) visibleIds.push_back(animationIndex);
window.order.push_back(animationIndex++);
}
auto arrowSelectionId = window_selection_start(window, document, visibleIds);
if (ImGui::Shortcut(ImGuiMod_Ctrl | ImGuiKey_A, ImGuiInputFlags_RouteFocused))
{
document.animation.selection = std::set<int>(window.order.begin(), window.order.end());
window.selection = groupIds;
}
auto isRowsDone = false;
animationIndex = 0;
for (auto& item : container->children)
{
if (isRowsDone) break;
if (item.type == ElementType::GROUP)
animation_group_draw(window, manager, resources, document, item, arrowSelectionId, isRowsDone);
if (item.type != ElementType::ANIMATION) continue;
if (!is_animation_grouped(groupIds, item))
isRowsDone = window_row_draw(window, manager, resources, document, item, animationIndex, animationIndex,
arrowSelectionId);
++animationIndex;
}
window_selection_finish(window, manager, settings, clipboard, document, arrowSelectionId);
};
window.add = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto& selection = document.animation.selection;
auto animations = window_container_get(window, document);
if (!animations) return;
window_edit(document, window.changeType, localize.get(EDIT_ADD_ANIMATION),
[&]()
{
auto groupIds = animation_group_ids_get(*animations);
auto targetGroupId = window.selection.size() == 1 && groupIds.contains(*window.selection.begin())
? *window.selection.begin()
: -1;
auto animation = element_make(ElementType::ANIMATION);
animation.name = localize.get(TEXT_NEW_ANIMATION);
animation.children.push_back(root_animation_make());
auto referenceAnimation =
document.anm2.element_get(ElementType::ANIMATION, document.reference.animationIndex);
for (const auto& row : TRACK_CONTAINERS)
animation.children.push_back(animation_track_container_shell_copy(
referenceAnimation ? child_first_get(*referenceAnimation, row.container) : nullptr, row));
animation.children.push_back(element_make(ElementType::TRIGGERS));
auto count = animations_count_get(*animations);
auto groupIndices =
targetGroupId != -1 ? animation_group_indices_get(*animations, targetGroupId) : std::set<int>{};
auto index = !selection.empty() ? std::min(*selection.rbegin() + 1, count)
: !groupIndices.empty() ? std::min(*groupIndices.rbegin() + 1, count)
: count;
auto selected = selection.empty()
? nullptr
: document.anm2.element_get(ElementType::ANIMATION, *selection.rbegin());
if (selected && groupIds.contains(selected->groupId)) animation.groupId = selected->groupId;
if (selection.empty()) animation.groupId = targetGroupId;
if (count == 0) animations->defaultAnimation = animation.name;
animations->children.insert(animations->children.begin() +
animations_child_insert_index_get(*animations, index),
animation);
selection = {index};
window.selection.clear();
document.reference = {index};
window.newElementId = index;
window.scrollQueued = index;
});
};
window.remove = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto selection = std::set<int>(document.animation.selection.begin(), document.animation.selection.end());
auto groupSelection = window.selection;
window_edit(document, window.changeType,
localize.get(groupSelection.empty() ? EDIT_REMOVE_ANIMATIONS : EDIT_REMOVE_GROUP),
[&]() { animations_erase(window, document, selection, groupSelection); });
};
window.cut = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard)
{
if (auto text = animation_clipboard_text_get(document, window); !text.empty()) clipboard.set(text);
auto selectedIndices = animation_selected_indices_get(document, window);
auto groupSelection = window.selection;
window_edit(document, window.changeType, localize.get(EDIT_CUT_ANIMATIONS),
[&]() { animations_erase(window, document, selectedIndices, groupSelection); });
};
window.copy = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard)
{
if (auto text = animation_clipboard_text_get(document, window); !text.empty()) clipboard.set(text);
};
window.duplicate = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto animations = window_container_get(window, document);
auto text = animation_clipboard_text_get(document, window);
if (!animations || text.empty()) return;
auto selectedIndices = animation_selected_indices_get(document, window);
auto count = animations_count_get(*animations);
animations_insert(window, document, text,
selectedIndices.empty() ? count : std::min(*selectedIndices.rbegin() + 1, count), -1,
EDIT_DUPLICATE_ANIMATIONS);
};
window.paste = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard)
{
auto animations = window_container_get(window, document);
if (clipboard.is_empty() || !animations) return;
auto& selection = document.animation.selection;
auto count = animations_count_get(*animations);
auto groupIds = animation_group_ids_get(*animations);
auto targetGroupId =
selection.empty() && window.selection.size() == 1 && groupIds.contains(*window.selection.begin())
? *window.selection.begin()
: -1;
auto groupIndices =
targetGroupId != -1 ? animation_group_indices_get(*animations, targetGroupId) : std::set<int>{};
auto start = !selection.empty() ? *selection.rbegin() + 1
: !groupIndices.empty() ? std::min(*groupIndices.rbegin() + 1, count)
: count;
animations_insert(window, document, clipboard.get(), start, targetGroupId, EDIT_PASTE_ANIMATIONS);
};
window.merge = [](Window& window, Manager&, Settings& settings, Document& document, Clipboard&)
{
auto& mergeSelection = document.merge.selection;
auto quickSelection = animation_selected_indices_get(document, window);
auto quickGroupSelection = window.selection;
auto merged = animations_merge(document,
{.selection = mergeSelection,
.reference = document.merge.reference,
.type = (merge::Type)settings.mergeType,
.isDeleteAnimationsAfter = settings.mergeIsDeleteAnimationsAfter},
&quickSelection, &quickGroupSelection);
if (merged != -1)
{
window.scrollQueued = merged;
window.selection.clear();
}
mergeSelection.clear();
};
window.merge_open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
if (document.animation.selection.empty()) return;
window.popup.open();
document.merge.selection.clear();
document.merge.reference = *document.animation.selection.begin();
};
window.group = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto targetIndices = animation_groupable_indices_get(document);
auto animations = window_container_get(window, document);
if (targetIndices.empty() || !animations) return;
window_edit(document, window.changeType, localize.get(EDIT_GROUP_ITEMS),
[&]()
{
std::set<int> targetSet(targetIndices.begin(), targetIndices.end());
auto group = element_make(ElementType::GROUP);
group.id = element_child_next_id_get(*animations, ElementType::GROUP);
group.name = localize.get(TEXT_NEW_GROUP);
for (auto index : targetSet)
document.anm2.element_get(ElementType::ANIMATION, index)->groupId = group.id;
animations->children.insert(animations->children.begin() +
animations_child_index_get(*animations, *targetSet.begin()),
group);
document.animation.selection = targetSet;
window.selection = {group.id};
document.reference = {*targetSet.begin()};
window.scrollQueued = *targetSet.begin();
});
};
window.default_set = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto animations = window_container_get(window, document);
auto animation = document.anm2.element_get(ElementType::ANIMATION, *document.animation.selection.begin());
if (!animations || !animation) return;
window_edit(document, window.changeType, localize.get(EDIT_DEFAULT_ANIMATION),
[&]() { animations->defaultAnimation = animation->name; });
};
window.body_update =
[](Window& window, Manager& manager, Settings& settings, Resources&, Clipboard&, Document& document)
{
auto& mergeSelection = document.merge.selection;
auto mergeReference = document.merge.reference;
window.popup.trigger();
if (!ImGui::BeginPopupModal(window.popup.label(), &window.popup.isOpen, ImGuiWindowFlags_NoResize)) return;
struct AnimationMergeCandidate
{
int key{};
std::string label{};
};
std::vector<AnimationMergeCandidate> candidates{};
auto animations = window_container_get(window, document);
auto groupIds = animations ? animation_group_ids_get(*animations) : std::set<int>{};
static const Element EMPTY{};
auto candidate_animation_push = [&](const Element& animation, int animationIndex, bool isGrouped)
{
if (animationIndex != mergeReference)
candidates.push_back({animationIndex, isGrouped ? std::format(" {}", animation.name) : animation.name});
};
int animationIndex{};
for (const auto& item : (animations ? *animations : EMPTY).children)
{
if (item.type == ElementType::GROUP)
{
auto groupIndices = animation_group_indices_get(*animations, item.id);
groupIndices.erase(mergeReference);
if (groupIndices.empty()) continue;
candidates.push_back({window_group_key_get(item.id),
item.name.empty() ? std::string(localize.get(TEXT_NEW_GROUP)) : item.name});
int groupAnimationIndex{};
for (const auto& animation : animations->children)
if (animation.type == ElementType::ANIMATION)
{
if (animation.groupId == item.id) candidate_animation_push(animation, groupAnimationIndex, true);
++groupAnimationIndex;
}
}
if (item.type != ElementType::ANIMATION) continue;
if (!is_animation_grouped(groupIds, item)) candidate_animation_push(item, animationIndex, false);
++animationIndex;
}
auto footerSize = footer_size_get();
auto optionsSize = child_size_get(MERGE_OPTIONS_ROWS);
auto deleteAfterSize = child_size_get();
auto animationsSize =
ImVec2(0, ImGui::GetContentRegionAvail().y - (optionsSize.y + deleteAfterSize.y + footerSize.y +
ImGui::GetStyle().ItemSpacing.y * MERGE_POPUP_CHILD_COUNT));
if (ImGui::BeginChild(localize.get(LABEL_ANIMATIONS_CHILD), animationsSize, ImGuiChildFlags_Borders))
{
std::vector<int> candidateKeys{};
for (const auto& candidate : candidates)
candidateKeys.push_back(candidate.key);
mergeSelection.set_index_map(&candidateKeys);
mergeSelection.start(candidates.size());
for (int i = 0; i < (int)candidates.size(); ++i)
{
ImGui::PushID(candidates[i].key);
ImGui::SetNextItemSelectionUserData(i);
ImGui::Selectable(candidates[i].label.c_str(), mergeSelection.contains(candidates[i].key));
ImGui::PopID();
}
mergeSelection.finish();
mergeSelection.set_index_map(nullptr);
}
ImGui::EndChild();
if (ImGui::BeginChild("##Merge Options", optionsSize, ImGuiChildFlags_Borders))
{
auto size = ImVec2(optionsSize.x * MERGE_OPTIONS_HALF, optionsSize.y - ImGui::GetStyle().WindowPadding.y * 2);
if (ImGui::BeginChild("##Merge Options 1", size))
{
ImGui::RadioButton(localize.get(LABEL_APPEND_FRAMES), &settings.mergeType, merge::APPEND);
ImGui::RadioButton(localize.get(LABEL_PREPEND_FRAMES), &settings.mergeType, merge::PREPEND);
}
ImGui::EndChild();
ImGui::SameLine();
if (ImGui::BeginChild("##Merge Options 2", size))
{
ImGui::RadioButton(localize.get(LABEL_REPLACE_FRAMES), &settings.mergeType, merge::REPLACE);
ImGui::RadioButton(localize.get(LABEL_IGNORE_FRAMES), &settings.mergeType, merge::IGNORE);
}
ImGui::EndChild();
}
ImGui::EndChild();
if (ImGui::BeginChild("##Merge Delete After", deleteAfterSize, ImGuiChildFlags_Borders))
ImGui::Checkbox(localize.get(LABEL_DELETE_ANIMATIONS_AFTER), &settings.mergeIsDeleteAnimationsAfter);
ImGui::EndChild();
auto result = window_popup_buttons_draw(
manager, localize.get(LABEL_MERGE),
!animation_merge_indices_get(document, mergeSelection, mergeReference).empty(), LABEL_CLOSE);
if (result == PopupButton::CONFIRM)
manager.command_push(
{manager.selected,
[options = AnimationMergeOptions{.selection = mergeSelection,
.reference = mergeReference,
.type = (merge::Type)settings.mergeType,
.isDeleteAnimationsAfter = settings.mergeIsDeleteAnimationsAfter}](
Manager&, Document& document) { animations_merge(document, options); }});
if (result != PopupButton::NONE)
{
mergeSelection.clear();
window.popup.close();
}
ImGui::EndPopup();
};
window.post_update = [](Window& window, Manager& manager, Settings&, Resources&, Clipboard&, Document& document)
{
auto isNext = shortcut(manager.chords[SHORTCUT_NEXT_ANIMATION], shortcut::GLOBAL);
auto isPrevious = shortcut(manager.chords[SHORTCUT_PREVIOUS_ANIMATION], shortcut::GLOBAL);
auto animations = window_container_get(window, document);
auto count = animations ? animations_count_get(*animations) : 0;
if ((!isPrevious && !isNext) || count <= 0) return;
auto& reference = document.reference;
reference.animationIndex = std::clamp(reference.animationIndex + (isNext ? 1 : -1), 0, count - 1);
document.animation.selection = {reference.animationIndex};
window.scrollQueued = reference.animationIndex;
};
return window;
}
}
+159
View File
@@ -0,0 +1,159 @@
#include "window.hpp"
#include <format>
#include "util/imgui/input.hpp"
#include "util/imgui/layout.hpp"
using namespace anm2ed::types;
namespace anm2ed::imgui
{
constexpr int ITEM_PROPERTIES_ROWS = 2;
void item_tooltip_draw(Resources& resources, const Element& element)
{
window_tooltip_name_draw(resources, element.name);
ImGui::TextUnformatted(std::vformat(localize.get(FORMAT_ID), std::make_format_args(element.id)).c_str());
}
void item_properties_popup_update(Window& window, Manager& manager, Document& document, StringType nameTooltip,
const std::function<void(Element&)>& fields_draw)
{
auto& popup = manager.itemPropertiesPopups[track_container_get(window.elementType) - TRACK_CONTAINERS];
auto reference = window.storage_get(document).reference;
popup.trigger();
if (ImGui::BeginPopupModal(popup.label(), &popup.isOpen, ImGuiWindowFlags_NoResize))
{
if (ImGui::BeginChild("##Child", child_size_get(ITEM_PROPERTIES_ROWS), ImGuiChildFlags_Borders))
{
if (popup.isJustOpened) ImGui::SetKeyboardFocusHere();
input_text_string(localize.get(BASIC_NAME), &manager.itemEdit.name);
ImGui::SetItemTooltip("%s", localize.get(nameTooltip));
fields_draw(manager.itemEdit);
}
ImGui::EndChild();
auto result = window_popup_buttons_draw(manager, localize.get(reference == -1 ? BASIC_ADD : BASIC_CONFIRM));
if (result == PopupButton::CONFIRM) window_element_apply_push(window, manager, manager.itemEdit, reference);
if (result != PopupButton::NONE) popup.close();
ImGui::EndPopup();
}
popup.end();
}
Window item_window_make(StringType title, bool Settings::* isOpen, Document::ChangeType changeType,
ElementType elementType, const char* childLabel, StringType addTooltip,
StringType removeUnusedTooltip)
{
Window window{};
window.title = title;
window.isOpen = isOpen;
window.changeType = changeType;
window.containerType = ELEMENT_CONTAINERS[(int)elementType];
window.elementType = elementType;
window.childLabel = childLabel;
window.tooltips = {{WINDOW_ADD, addTooltip}, {WINDOW_REMOVE_UNUSED, removeUnusedTooltip}};
window.footer = {{WINDOW_ADD, WINDOW_REMOVE_UNUSED}};
window.flags = WINDOW_ADD | WINDOW_REMOVE_UNUSED | WINDOW_COPY | WINDOW_PASTE;
window.tooltip_draw = [](Document&, Resources& resources, const Element& element)
{ item_tooltip_draw(resources, element); };
window.properties_open = [elementType](Manager& manager, int id) { manager.item_properties_open(elementType, id); };
return window;
}
Window layers_window_register()
{
auto window =
item_window_make(LABEL_LAYERS_WINDOW, &Settings::windowIsLayers, Document::LAYERS, ElementType::LAYER_ELEMENT,
"##Layers Child", TOOLTIP_ADD_LAYER, TOOLTIP_REMOVE_UNUSED_LAYERS);
window.addEdit = EDIT_ADD_LAYER;
window.propertiesEdit = EDIT_SET_LAYER_PROPERTIES;
window.pasteEdit = EDIT_PASTE_LAYERS;
window.removeUnusedEdit = EDIT_REMOVE_UNUSED_LAYERS;
window.deserializeFailedToast = TOAST_DESERIALIZE_LAYERS_FAILED;
window.flags |= WINDOW_PROPERTIES;
window.storage_get = [](Document& document) -> Storage& { return document.layer; };
window.row_label_get = [](Document&, const Element& layer)
{
return std::vformat(localize.get(FORMAT_LAYER), std::make_format_args(layer.id, layer.name, layer.spritesheetId));
};
window.tooltip_draw = [](Document&, Resources& resources, const Element& layer)
{
item_tooltip_draw(resources, layer);
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_SPRITESHEET_ID), std::make_format_args(layer.spritesheetId)).c_str());
};
window.popup_update = [](Window& window, Manager& manager, Settings&, Resources&, Clipboard&, Document& document)
{
item_properties_popup_update(window, manager, document, TOOLTIP_ITEM_NAME,
[&](Element& layer)
{
combo_id_mapped(localize.get(LABEL_SPRITESHEET), &layer.spritesheetId,
document.spritesheet.ids, document.spritesheet.labels);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_LAYER_SPRITESHEET));
});
};
return window;
}
Window nulls_window_register()
{
auto window =
item_window_make(LABEL_NULLS_WINDOW, &Settings::windowIsNulls, Document::NULLS, ElementType::NULL_ELEMENT,
"##Nulls Child", TOOLTIP_ADD_NULL, TOOLTIP_REMOVE_UNUSED_NULLS);
window.addEdit = EDIT_ADD_NULL;
window.propertiesEdit = EDIT_SET_NULL_PROPERTIES;
window.pasteEdit = EDIT_PASTE_NULLS;
window.removeUnusedEdit = EDIT_REMOVE_UNUSED_NULLS;
window.deserializeFailedToast = TOAST_DESERIALIZE_NULLS_FAILED;
window.flags |= WINDOW_PROPERTIES | WINDOW_REFERENCE_ITALIC;
window.storage_get = [](Document& document) -> Storage& { return document.null; };
window.row_label_get = [](Document&, const Element& null)
{ return std::vformat(localize.get(FORMAT_NULL), std::make_format_args(null.id, null.name)); };
window.popup_update = [](Window& window, Manager& manager, Settings&, Resources&, Clipboard&, Document& document)
{
item_properties_popup_update(window, manager, document, TOOLTIP_NULL_NAME,
[](Element& null)
{
ImGui::Checkbox(localize.get(LABEL_RECT), &null.isShowRect);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_NULL_RECT));
});
};
return window;
}
Window events_window_register()
{
auto window =
item_window_make(LABEL_EVENTS_WINDOW, &Settings::windowIsEvents, Document::EVENTS, ElementType::EVENT_ELEMENT,
"##Events Child", TOOLTIP_ADD_EVENT, TOOLTIP_REMOVE_UNUSED_EVENTS);
window.addEdit = EDIT_ADD_EVENT;
window.renameEdit = EDIT_RENAME_EVENT;
window.pasteEdit = EDIT_PASTE_EVENTS;
window.removeUnusedEdit = EDIT_REMOVE_UNUSED_EVENTS;
window.deserializeFailedToast = TOAST_DESERIALIZE_EVENTS_FAILED;
window.flags |= WINDOW_RENAME;
window.properties_open = {};
window.storage_get = [](Document& document) -> Storage& { return document.event; };
window.add = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto event = element_make(ElementType::EVENT_ELEMENT);
event.name = localize.get(TEXT_NEW_EVENT);
auto events = window_container_get(window, document);
if (!events) return;
window_edit(document, window.changeType, localize.get(window.addEdit),
[&]()
{
event.id = element_child_next_id_get(*events, ElementType::EVENT_ELEMENT);
events->children.push_back(event);
auto& storage = window.storage_get(document);
storage.selection = {event.id};
storage.reference = event.id;
window.newElementId = event.id;
});
};
return window;
}
}
+546
View File
@@ -0,0 +1,546 @@
#include "window.hpp"
#include <algorithm>
#include <format>
#include "math.hpp"
#include "path.hpp"
#include "toast.hpp"
#include "util/imgui/draw.hpp"
#include "util/imgui/input.hpp"
#include "util/imgui/layout.hpp"
#include "vector.hpp"
#include "working_directory.hpp"
using namespace anm2ed::resource;
using namespace anm2ed::types;
using namespace anm2ed::util;
using namespace glm;
namespace anm2ed::imgui
{
constexpr int REGION_CARD_LINES = 2;
constexpr int REGION_POPUP_ROWS = 5;
constexpr const char* REGION_DRAG_DROP = "Region Drag Drop";
constexpr const char* REGION_EXPORT_NAME_DEFAULT = "region";
constexpr const char* PNG_EXTENSION = ".png";
constexpr ImVec2 DRAG_TOOLTIP_OFFSET = ImVec2(16.0f, 16.0f);
constexpr StringType ORIGIN_LABELS[] = {LABEL_REGION_ORIGIN_CUSTOM, LABEL_REGION_ORIGIN_TOP_LEFT,
LABEL_REGION_ORIGIN_CENTER};
constexpr Origin ORIGIN_COMBO_ORDER[] = {Origin::TOP_LEFT, Origin::CENTER, Origin::CUSTOM};
struct RegionExportOptions
{
int spritesheetId{-1};
int regionId{-1};
std::filesystem::path path{};
bool isMakeSpritesheet{};
bool isRemoveCurrent{};
};
void region_pivot_apply(Element& region)
{
if (region.origin == Origin::TOP_LEFT) region.pivot = {};
if (region.origin == Origin::CENTER) region.pivot = region.size * 0.5f;
}
Element* region_spritesheet_get(Document& document)
{
return document.anm2.element_get(ElementType::SPRITESHEET, document.spritesheet.reference);
}
int region_insert_index_get(Document& document)
{
auto spritesheet = region_spritesheet_get(document);
auto& storage = document.region;
int index = spritesheet ? (int)spritesheet->children.size() : 0;
for (int i = 0; spritesheet && i < (int)spritesheet->children.size(); i++)
{
const auto& child = spritesheet->children[i];
if (child.type == ElementType::REGION &&
(storage.selection.contains(child.id) || (storage.selection.empty() && storage.reference == child.id)))
index = i + 1;
}
return index;
}
WindowCardImage region_image_get(Document& document, Resources& resources, const Element& region)
{
auto texture = document.texture_get(document.spritesheet.reference);
auto isValid = texture && texture->is_valid();
if (!isValid) return {.texture = &resources.icons[icon::NONE], .size = region.size};
return {.texture = texture,
.size = region.size,
.uvMin = region.crop / vec2(texture->size),
.uvMax = (region.crop + region.size) / vec2(texture->size),
.isValid = true};
}
bool region_pixels_get(const Element& region, const Texture& texture, std::vector<uint8_t>& pixels, ivec2& size)
{
auto minPoint = ivec2(glm::min(region.crop, region.crop + region.size));
size = ivec2(glm::max(region.crop, region.crop + region.size)) - minPoint;
if (size.x <= 0 || size.y <= 0 || texture.size.x <= 0 || texture.size.y <= 0 || texture.pixels.empty())
return false;
pixels.assign((size_t)size.x * size.y * texture::CHANNELS, 0);
for (int y = 0; y < size.y; y++)
for (int x = 0; x < size.x; x++)
{
auto source = minPoint + ivec2(x, y);
if (source.x < 0 || source.y < 0 || source.x >= texture.size.x || source.y >= texture.size.y) continue;
std::copy_n(texture.pixels.data() + ((size_t)source.y * texture.size.x + source.x) * texture::CHANNELS,
texture::CHANNELS, pixels.data() + ((size_t)y * size.x + x) * texture::CHANNELS);
}
return true;
}
bool region_export(Document& document, const RegionExportOptions& options)
{
auto pathString = options.path.empty() ? std::string("in memory") : path::to_utf8(options.path);
if (options.path.empty() && !options.isMakeSpritesheet)
{
toast_log(Level::ERROR, TOAST_EXPORT_REGION_PATH_EMPTY);
return false;
}
auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, options.spritesheetId);
auto region = spritesheet ? child_id_get(*spritesheet, ElementType::REGION, options.regionId) : nullptr;
auto texture = document.texture_get(options.spritesheetId);
std::vector<uint8_t> pixels{};
ivec2 exportSize{};
if (!region || !texture || !texture->is_valid() || !region_pixels_get(*region, *texture, pixels, exportSize))
{
toast_log(Level::ERROR, TOAST_EXPORT_REGION_FAILED, region ? region->name : std::string(), pathString);
return false;
}
auto sourceRegion = *region;
auto outputPath = options.path;
if (!outputPath.empty())
{
outputPath = window_asset_path_get(document, outputPath.replace_extension(PNG_EXTENSION));
pathString = path::to_utf8(outputPath);
WorkingDirectory workingDirectory(document.directory_get());
path::ensure_directory(outputPath.parent_path());
if (!Texture::write_pixels_png(outputPath, exportSize, pixels.data()))
{
toast_log(Level::ERROR, TOAST_EXPORT_REGION_FAILED, sourceRegion.name, pathString);
return false;
}
}
if (options.isMakeSpritesheet || options.isRemoveCurrent)
{
document.snapshots.anm2_textures_push(localize.get(EDIT_EXPORT_REGION));
if (options.isMakeSpritesheet)
{
auto spritesheets = document.anm2.element_get(ElementType::SPRITESHEETS);
auto exported = element_make(ElementType::SPRITESHEET);
exported.id = element_child_next_id_get(*spritesheets, ElementType::SPRITESHEET);
exported.path = outputPath;
auto exportedRegion = sourceRegion;
exportedRegion.id = 0;
exportedRegion.crop = {};
exportedRegion.size = vec2(exportSize);
region_pivot_apply(exportedRegion);
exported.children.push_back(exportedRegion);
spritesheets->children.push_back(exported);
document.textures[exported.id] = Texture(pixels.data(), exportSize);
document.texturePaths[exported.id] = exported.path;
document.spritesheet.reference = exported.id;
document.spritesheet.selection = {exported.id};
document.region.reference = exportedRegion.id;
document.region.selection = {exportedRegion.id};
if (!outputPath.empty())
document.spritesheet_hash_set_saved(exported.id);
else
document.spritesheetSaveHashes[exported.id] = 0;
}
if (options.isRemoveCurrent)
{
if (auto source = document.anm2.element_get(ElementType::SPRITESHEET, options.spritesheetId))
element_child_id_erase(*source, ElementType::REGION, options.regionId);
document.anm2.region_frames_sync(true);
if (!options.isMakeSpritesheet)
{
document.region.reference = -1;
document.region.selection.clear();
}
}
document.change(Document::SPRITESHEETS);
}
toast_log(Level::INFO, TOAST_EXPORT_REGION, sourceRegion.name, pathString);
return true;
}
bool region_drag_drop_update(Window& window, Manager& manager, Document& document, const Element& region, int index)
{
auto& selection = document.region.selection;
if (ImGui::BeginDragDropSource(DRAG_DROP_SOURCE_FLAGS))
{
window.dragSelection = selection.contains(region.id) ? std::vector<int>(selection.begin(), selection.end())
: std::vector<int>{region.id};
ImGui::SetDragDropPayload(REGION_DRAG_DROP, window.dragSelection.data(),
window.dragSelection.size() * sizeof(int));
ImGui::EndDragDropSource();
}
if (!ImGui::BeginDragDropTarget()) return false;
auto isMoved = false;
if (auto payload = ImGui::AcceptDragDropPayload(REGION_DRAG_DROP, ImGuiDragDropFlags_AcceptBeforeDelivery |
ImGuiDragDropFlags_AcceptNoDrawDefaultRect))
{
auto cardMin = ImGui::GetWindowPos();
auto cardMax = ImVec2(cardMin.x + ImGui::GetWindowSize().x, cardMin.y + ImGui::GetWindowSize().y);
auto isAfter = is_drop_after(cardMin, cardMax);
drop_line_draw(ImGui::GetForegroundDrawList(), cardMin, cardMax, isAfter);
std::vector<int> indices{};
auto payloadIds = (const int*)payload->Data;
for (int i = 0; i < payload->DataSize / (int)sizeof(int); i++)
if (auto orderIndex = vector::find_index(window.order, payloadIds[i]); orderIndex != -1)
indices.push_back(orderIndex);
if (!indices.empty() && payload->IsDelivery())
{
std::sort(indices.begin(), indices.end());
manager.command_push(
{manager.selected,
[&window, indices, movedIds = window.dragSelection, targetIndex = index + (isAfter ? 1 : 0),
spritesheetId = document.spritesheet.reference](Manager&, Document& document) mutable
{
auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
if (!spritesheet) return;
window_edit(document, window.changeType, localize.get(EDIT_MOVE_REGIONS),
[&]()
{
vector::move_indices_to_position(spritesheet->children, indices, targetIndex);
document.region.selection = std::set<int>(movedIds.begin(), movedIds.end());
});
}});
isMoved = true;
}
}
ImGui::EndDragDropTarget();
return isMoved;
}
void region_drag_tooltip_update(Window& window, Document& document)
{
auto payload = ImGui::GetDragDropPayload();
auto spritesheet = region_spritesheet_get(document);
if (!spritesheet || !payload || !payload->IsDataType(REGION_DRAG_DROP) || payload->IsDelivery() ||
window.dragSelection.empty())
return;
if (payload->DataFrameCount != ImGui::GetFrameCount() &&
ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceExtern | DRAG_DROP_SOURCE_FLAGS))
{
ImGui::SetDragDropPayload(REGION_DRAG_DROP, window.dragSelection.data(),
window.dragSelection.size() * sizeof(int));
ImGui::EndDragDropSource();
}
auto mousePos = ImGui::GetIO().MousePos;
ImGui::SetNextWindowPos(ImVec2(mousePos.x + DRAG_TOOLTIP_OFFSET.x, mousePos.y + DRAG_TOOLTIP_OFFSET.y));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, window.tooltipWindowPadding);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, window.tooltipItemSpacing);
if (ImGui::BeginTooltip())
{
for (auto regionId : window.dragSelection)
if (auto region = child_id_get(*spritesheet, ElementType::REGION, regionId))
ImGui::TextUnformatted(region->name.c_str());
ImGui::EndTooltip();
}
ImGui::PopStyleVar(2);
}
void region_properties_popup_update(Window& window, Manager& manager, Document& document)
{
auto& reference = document.region.reference;
auto spritesheet = region_spritesheet_get(document);
auto target = spritesheet && reference != -1 ? child_id_get(*spritesheet, ElementType::REGION, reference) : nullptr;
window.popup.trigger();
if (ImGui::BeginPopupModal(window.popup.label(), &window.popup.isOpen, ImGuiWindowFlags_NoResize))
{
if (!spritesheet || (reference != -1 && !target)) window.popup.close();
if (window.popup.isJustOpened && !window.isPreserveEditElementOnOpen)
window.editElement = target ? *target : element_make(ElementType::REGION);
if (window.popup.isJustOpened) window.isPreserveEditElementOnOpen = false;
auto& region = window.editElement;
if (ImGui::BeginChild("##Child", child_size_get(REGION_POPUP_ROWS), ImGuiChildFlags_Borders))
{
if (window.popup.isJustOpened) ImGui::SetKeyboardFocusHere();
input_text_string(localize.get(BASIC_NAME), &region.name);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_ITEM_NAME));
ImGui::DragFloat2(localize.get(BASIC_CROP), value_ptr(region.crop), DRAG_SPEED, 0.0f, 0.0f,
math::vec2_format_get(region.crop));
ImGui::DragFloat2(localize.get(BASIC_SIZE), value_ptr(region.size), DRAG_SPEED, 0.0f, 0.0f,
math::vec2_format_get(region.size));
ImGui::BeginDisabled(region.origin != Origin::CUSTOM);
ImGui::DragFloat2(localize.get(BASIC_PIVOT), value_ptr(region.pivot), DRAG_SPEED, 0.0f, 0.0f,
math::vec2_format_get(region.pivot));
ImGui::EndDisabled();
const char* originOptions[std::size(ORIGIN_COMBO_ORDER)]{};
for (int i = 0; i < (int)std::size(ORIGIN_COMBO_ORDER); ++i)
originOptions[i] = localize.get(ORIGIN_LABELS[(int)ORIGIN_COMBO_ORDER[i]]);
auto originIndex = (int)(std::ranges::find(ORIGIN_COMBO_ORDER, region.origin) - std::begin(ORIGIN_COMBO_ORDER));
if (ImGui::Combo(localize.get(LABEL_REGION_PROPERTIES_ORIGIN), &originIndex, originOptions,
(int)std::size(originOptions)))
region.origin = ORIGIN_COMBO_ORDER[originIndex];
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_REGION_PROPERTIES_ORIGIN));
region_pivot_apply(region);
}
ImGui::EndChild();
auto result = window_popup_buttons_draw(manager, localize.get(reference == -1 ? BASIC_ADD : BASIC_CONFIRM));
if (result == PopupButton::CONFIRM) window_element_apply_push(window, manager, region, reference);
if (result != PopupButton::NONE) window.popup.close();
ImGui::EndPopup();
}
window.popup.end();
}
void region_export_popup_update(Window& window, Manager& manager, Settings& settings, Resources& resources,
Document& document)
{
auto spritesheet = region_spritesheet_get(document);
auto regionId = window.editId != -1 ? window.editId : document.region.reference;
auto region = spritesheet && regionId != -1 ? child_id_get(*spritesheet, ElementType::REGION, regionId) : nullptr;
window.popup2.trigger();
if (ImGui::BeginPopupModal(window.popup2.label(), &window.popup2.isOpen, ImGuiWindowFlags_NoResize))
{
if (!region) window.popup2.close();
if (region && window.popup2.isJustOpened)
{
auto filename = path::from_utf8(region->name).filename();
settings.exportRegionPath =
settings.exportRegionPath.parent_path() /
(filename.empty() ? std::filesystem::path(REGION_EXPORT_NAME_DEFAULT) : filename).concat(PNG_EXTENSION);
}
if (window.dialog && window.dialog->is_selected(Dialog::REGION_EXPORT_PATH_SET))
{
settings.exportRegionPath = window.dialog->path;
window.dialog->reset();
}
if (ImGui::BeginChild("##Export Region Child", child_size_get(REGION_POPUP_ROWS), ImGuiChildFlags_Borders))
{
if (ImGui::ImageButton("##Export Region Path Set", resources.icons[icon::FOLDER].id, icon_size_get()) &&
window.dialog)
window.dialog->file_save(Dialog::REGION_EXPORT_PATH_SET, settings.exportRegionPath);
ImGui::SameLine();
input_text_path(localize.get(LABEL_OUTPUT_PATH), &settings.exportRegionPath);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_OUTPUT_PATH));
ImGui::Checkbox(localize.get(LABEL_EXPORT_MAKE_SPRITESHEET), &settings.isExportRegionMakeSpritesheet);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_EXPORT_MAKE_SPRITESHEET));
ImGui::Checkbox(localize.get(LABEL_EXPORT_REMOVE_CURRENT_REGION), &settings.isExportRegionRemoveCurrent);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_EXPORT_REMOVE_CURRENT_REGION));
}
ImGui::EndChild();
auto texture = document.texture_get(document.spritesheet.reference);
auto isValid = region && texture && texture->is_valid() && !texture->pixels.empty() &&
(settings.isExportRegionMakeSpritesheet || !settings.exportRegionPath.empty());
auto result = window_popup_buttons_draw(manager, localize.get(BASIC_CONFIRM), isValid);
if (result == PopupButton::CONFIRM)
{
if (!settings.exportRegionPath.empty()) settings.exportRegionPath.replace_extension(PNG_EXTENSION);
RegionExportOptions options{.spritesheetId = document.spritesheet.reference,
.regionId = regionId,
.path = settings.exportRegionPath,
.isMakeSpritesheet = settings.isExportRegionMakeSpritesheet,
.isRemoveCurrent = settings.isExportRegionRemoveCurrent};
manager.command_push({manager.selected, [&window, options](Manager&, Document& document)
{
if (region_export(document, options) && options.isMakeSpritesheet)
window.newElementId = document.region.reference;
}});
}
if (result != PopupButton::NONE)
{
window.editId = -1;
window.popup2.close();
}
ImGui::EndPopup();
}
window.popup2.end();
}
Window regions_window_register()
{
Window window{};
window.title = LABEL_REGIONS_WINDOW;
window.isOpen = &Settings::windowIsRegions;
window.changeType = Document::SPRITESHEETS;
window.elementType = ElementType::REGION;
window.childLabel = "##Regions Child";
window.cardLines = REGION_CARD_LINES;
window.unavailableText = TEXT_SELECT_SPRITESHEET;
window.addEdit = EDIT_ADD_REGION;
window.propertiesEdit = EDIT_SET_REGION_PROPERTIES;
window.flags = WINDOW_PROPERTIES | WINDOW_ADD | WINDOW_REMOVE_UNUSED | WINDOW_TRIM | WINDOW_EXPORT | WINDOW_COPY |
WINDOW_PASTE;
window.footer = {{WINDOW_ADD, WINDOW_EXPORT, WINDOW_REMOVE_UNUSED}};
window.tooltips = {{WINDOW_ADD, TOOLTIP_ADD_REGION},
{WINDOW_REMOVE_UNUSED, TOOLTIP_REMOVE_UNUSED_REGIONS},
{WINDOW_TRIM, TOOLTIP_TRIM_REGIONS}};
window.editElement = element_make(ElementType::REGION);
window.popup = PopupHelper(LABEL_REGION_PROPERTIES, POPUP_SMALL_NO_HEIGHT);
window.popup2 = PopupHelper(LABEL_EXPORT_REGION, POPUP_SMALL_NO_HEIGHT);
window.storage_get = [](Document& document) -> Storage& { return document.region; };
window.container_get = region_spritesheet_get;
window.insert_index_get = region_insert_index_get;
window.is_available = [](Window&, Document& document) { return region_spritesheet_get(document) != nullptr; };
window.card_image_get = region_image_get;
window.row_drag_drop_update = region_drag_drop_update;
window.row_select = [](Window&, Document& document, int)
{
document.editTarget = Document::EditTarget::REGION;
document.reference = {document.reference.animationIndex};
document.frames.reference = -1;
document.frames.selection.clear();
};
window.tooltip_draw = [](Document& document, Resources& resources, const Element& region)
{
window_tooltip_image_draw(
region_image_get(document, resources, region),
[&]()
{
window_tooltip_name_draw(resources, region.name);
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_CROP), std::make_format_args(region.crop.x, region.crop.y)).c_str());
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_SIZE), std::make_format_args(region.size.x, region.size.y)).c_str());
auto originLabel = localize.get(ORIGIN_LABELS[(int)region.origin]);
ImGui::TextUnformatted(
(region.origin == Origin::CUSTOM
? std::vformat(localize.get(FORMAT_PIVOT), std::make_format_args(region.pivot.x, region.pivot.y))
: std::vformat(localize.get(FORMAT_ORIGIN), std::make_format_args(originLabel)))
.c_str());
});
};
window.add = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
document.region.reference = -1;
window.editElement = element_make(ElementType::REGION);
window.popup.open();
};
window.properties = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto& selection = document.region.selection;
if (selection.size() != 1 || !window_element_get(window, document, *selection.begin())) return;
document.region.reference = *selection.begin();
window.popup.open();
};
window.export_open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
window.editId = *document.region.selection.begin();
window.popup2.open();
};
window.remove_unused = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto spritesheetId = document.spritesheet.reference;
auto unused = document.anm2.region_unused(spritesheetId);
auto spritesheet = region_spritesheet_get(document);
if (unused.empty() || !spritesheet) return;
window_edit(document, window.changeType, localize.get(EDIT_REMOVE_UNUSED_REGIONS),
[&]()
{
animations_tracks_each(document.anm2.root, ElementType::LAYER_ANIMATION,
[&](Element& track)
{
auto layer =
document.anm2.element_get(ElementType::LAYER_ELEMENT, track.layerId);
if (!layer || layer->spritesheetId != spritesheetId) return;
for (auto& frame : track.children)
if (frame.type == ElementType::FRAME && unused.contains(frame.regionId))
frame.regionId = -1;
});
for (auto id : unused)
element_child_id_erase(*spritesheet, ElementType::REGION, id);
});
};
window.trim = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto& region = document.region;
window_edit(document, window.changeType, localize.get(EDIT_TRIM_REGIONS),
[&]()
{
if (!document.regions_trim(document.spritesheet.reference, region.selection)) return;
if (region.reference != -1 && !region.selection.contains(region.reference))
region.reference = *region.selection.begin();
document.reference = {document.reference.animationIndex};
document.frames.reference = -1;
document.frames.selection.clear();
});
};
window.paste = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard)
{
auto spritesheetId = document.spritesheet.reference;
auto spritesheet = region_spritesheet_get(document);
if (!spritesheet || clipboard.is_empty()) return;
auto maxIdBefore = element_child_max_id_get(*spritesheet, ElementType::REGION);
auto insertIndex = region_insert_index_get(document);
auto pasted = document.anm2;
std::string errorString{};
if (!pasted.deserialize(ElementType::REGION, clipboard.get(), true, &errorString, {}, spritesheetId))
{
toast_log(Level::ERROR, TOAST_DESERIALIZE_REGIONS_FAILED, errorString);
return;
}
window_edit(document, window.changeType, localize.get(EDIT_PASTE_REGIONS),
[&]()
{
document.anm2 = std::move(pasted);
auto target = region_spritesheet_get(document);
auto maxIdAfter = element_child_max_id_get(*target, ElementType::REGION);
if (maxIdAfter <= maxIdBefore) return;
std::vector<int> pastedIndices{};
for (int i = 0; i < (int)target->children.size(); i++)
if (target->children[i].type == ElementType::REGION && target->children[i].id > maxIdBefore)
pastedIndices.push_back(i);
vector::move_indices_to_position(target->children, pastedIndices, insertIndex);
window.newElementId = maxIdAfter;
document.region.selection = {maxIdAfter};
document.region.reference = maxIdAfter;
});
};
window.rows_update = [](Window& window, Manager& manager, Settings& settings, Resources& resources,
Clipboard& clipboard, Document& document)
{
window_cards_draw(window, manager, settings, resources, clipboard, document, region_spritesheet_get(document));
region_drag_tooltip_update(window, document);
};
window.popup_update =
[](Window& window, Manager& manager, Settings& settings, Resources& resources, Clipboard&, Document& document)
{
if (manager.isMakeRegionRequested)
{
document.spritesheet.reference = manager.makeRegionSpritesheetId;
document.region.reference = -1;
window.editElement = manager.makeRegion;
window.isPreserveEditElementOnOpen = true;
window.popup.open();
manager.isMakeRegionRequested = false;
}
region_properties_popup_update(window, manager, document);
region_export_popup_update(window, manager, settings, resources, document);
};
return window;
}
}
+131
View File
@@ -0,0 +1,131 @@
#include "window.hpp"
#include <format>
#include "path.hpp"
#include "toast.hpp"
using namespace anm2ed::resource;
using namespace anm2ed::types;
using namespace anm2ed::util;
namespace anm2ed::imgui
{
constexpr int SOUND_CARD_LINES = 2;
Window sounds_window_register()
{
Window window{};
window.title = LABEL_SOUNDS_WINDOW;
window.isOpen = &Settings::windowIsSounds;
window.changeType = Document::SOUNDS;
window.containerType = ElementType::SOUNDS;
window.elementType = ElementType::SOUND_ELEMENT;
window.childLabel = "##Sounds Child";
window.cardLines = SOUND_CARD_LINES;
window.pasteEdit = TOAST_SOUNDS_PASTE;
window.removeUnusedEdit = EDIT_REMOVE_UNUSED_SOUNDS;
window.deserializeFailedToast = TOAST_SOUNDS_DESERIALIZE_ERROR;
window.flags = WINDOW_PLAY | WINDOW_OPEN_DIRECTORY | WINDOW_ADD | WINDOW_REMOVE_UNUSED | WINDOW_RELOAD |
WINDOW_REPLACE | WINDOW_COPY | WINDOW_PASTE;
window.footer = {{WINDOW_ADD, WINDOW_REMOVE_UNUSED, WINDOW_RELOAD, WINDOW_REPLACE}};
window.tooltips = {{WINDOW_ADD, TOOLTIP_SOUND_ADD},
{WINDOW_REMOVE_UNUSED, TOOLTIP_REMOVE_UNUSED_SOUNDS},
{WINDOW_RELOAD, TOOLTIP_RELOAD_SOUNDS},
{WINDOW_REPLACE, TOOLTIP_REPLACE_SOUND}};
window.storage_get = [](Document& document) -> Storage& { return document.sound; };
window.row_label_get = [](Document&, const Element& sound)
{
auto pathString = path::to_utf8(sound.path);
return std::vformat(localize.get(FORMAT_SOUND), std::make_format_args(sound.id, pathString));
};
window.row_select = [](Window&, Document& document, int id)
{
if (auto audio = document.sound_get(id); audio && ImGui::IsItemClicked(ImGuiMouseButton_Left)) audio->play();
};
window.card_image_get = [](Document& document, Resources& resources, const Element& sound)
{
auto audio = document.sound_get(sound.id);
auto isValid = audio && audio->is_valid();
auto& texture = resources.icons[isValid ? icon::SOUND : icon::NONE];
return WindowCardImage{.texture = &texture, .size = glm::vec2(texture.size), .isValid = isValid};
};
window.tooltip_draw = [](Document& document, Resources& resources, const Element& sound)
{
auto audio = document.sound_get(sound.id);
window_tooltip_name_draw(resources, path::to_utf8(sound.path));
ImGui::Text("%s: %d", localize.get(BASIC_ID), sound.id);
if (audio && audio->is_valid())
ImGui::TextUnformatted(localize.get(TEXT_OPEN_DIRECTORY));
else
{
ImGui::Spacing();
ImGui::TextWrapped("%s", localize.get(TOOLTIP_SOUND_INVALID));
}
};
window.add = [](Window& window, Manager&, Settings&, Document&, Clipboard&)
{
if (window.dialog) window.dialog->file_open(Dialog::SOUND_OPEN, true);
};
window.replace = [](Window& window, Manager&, Settings&, Document&, Clipboard&)
{
if (window.dialog) window.dialog->file_open(Dialog::SOUND_REPLACE);
};
window.play = [](Window&, Manager&, Settings&, Document& document, Clipboard&)
{
if (auto audio = document.sound_get(*document.sound.selection.begin())) audio->play();
};
window.open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto sound = window_element_get(window, document, document.sound.reference);
if (sound && window.dialog) window_directory_open(*window.dialog, document, sound->path);
};
window.reload = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
window_edit(document, window.changeType, localize.get(EDIT_RELOAD_SOUNDS),
[&]()
{
for (auto id : document.sound.selection)
if (auto sound = window_element_get(window, document, id))
{
document.sound_reload(id);
toast_log(Level::INFO, TOAST_RELOAD_SOUND, id, path::to_utf8(sound->path));
}
});
};
window.body_update = [](Window& window, Manager& manager, Settings&, Resources&, Clipboard&, Document& document)
{
if (!window.dialog) return;
if (window.dialog->is_selected(Dialog::SOUND_OPEN))
{
manager.command_push({manager.selected, [&window, paths = window.dialog->paths](Manager&, Document& document)
{
document.sounds_add(paths);
window.newElementId = document.sound.reference;
}});
window.dialog->reset();
}
if (window.dialog->is_selected(Dialog::SOUND_REPLACE))
{
if (document.sound.selection.size() == 1 && !window.dialog->path.empty())
manager.command_push({manager.selected, [&window, id = *document.sound.selection.begin(),
dialogPath = window.dialog->path](Manager&, Document& document)
{
auto sound = window_element_get(window, document, id);
if (!sound) return;
window_edit(document, window.changeType, localize.get(EDIT_REPLACE_SOUND),
[&]()
{
sound->path = window_asset_path_get(document, dialogPath);
document.sound_reload(id);
toast_log(Level::INFO, TOAST_REPLACE_SOUND, id,
path::to_utf8(sound->path));
});
}});
window.dialog->reset();
}
};
return window;
}
}
+368
View File
@@ -0,0 +1,368 @@
#include "window.hpp"
#include <format>
#include "path.hpp"
#include "toast.hpp"
#include "util/imgui/input.hpp"
#include "util/imgui/layout.hpp"
#include "working_directory.hpp"
using namespace anm2ed::resource;
using namespace anm2ed::types;
using namespace anm2ed::util;
namespace anm2ed::imgui
{
constexpr int SPRITESHEET_CARD_LINES = 4;
constexpr int MERGE_OPTIONS_ROWS = 6;
constexpr int PADDING_MAX = 100;
bool is_spritesheet_regions(Document& document, int id)
{
auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, id);
return spritesheet && child_first_get(*spritesheet, ElementType::REGION);
}
void spritesheets_save(Document& document, const std::set<int>& ids)
{
for (auto id : ids)
{
auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, id);
auto texture = document.texture_get(id);
if (!spritesheet || !texture) continue;
auto pathString = path::to_utf8(spritesheet->path);
WorkingDirectory workingDirectory(document.directory_get());
path::ensure_directory(spritesheet->path.parent_path());
auto isSaved = texture->write_png(spritesheet->path);
if (isSaved) document.spritesheet_hash_set_saved(id);
toast_log(isSaved ? Level::INFO : Level::ERROR, isSaved ? TOAST_SAVE_SPRITESHEET : TOAST_SAVE_SPRITESHEET_FAILED,
id, pathString);
}
}
void spritesheet_path_change(Window& window, Manager& manager, int id, const std::filesystem::path& dialogPath,
StringType edit, bool isWritten)
{
manager.command_push({manager.selected, [&window, id, dialogPath, edit, isWritten](Manager&, Document& document)
{
auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, id);
auto texture = document.texture_get(id);
if (!spritesheet || (isWritten && !texture)) return;
window_edit(document, window.changeType, localize.get(edit),
[&]()
{
auto newPath = window_asset_path_get(document, dialogPath);
auto pathString = path::to_utf8(newPath);
if (isWritten)
{
WorkingDirectory workingDirectory(document.directory_get());
path::ensure_directory(newPath.parent_path());
if (!texture->write_png(newPath))
{
toast_log(Level::ERROR, TOAST_SAVE_SPRITESHEET_FAILED, id, pathString);
return;
}
}
spritesheet->path = newPath;
if (isWritten)
document.texturePaths[id] = newPath;
else
document.texture_reload(id);
document.spritesheet_hash_set_saved(id);
toast_log(Level::INFO,
isWritten ? TOAST_SAVE_SPRITESHEET : TOAST_REPLACE_SPRITESHEET, id,
pathString);
});
}});
}
void spritesheet_textures_edit(Document& document, StringType edit, Document::ChangeType changeType,
const std::function<bool()>& behavior, StringType successToast, StringType failToast)
{
document.snapshots.anm2_textures_push(localize.get(edit));
auto isSuccess = behavior();
toast_log(isSuccess ? Level::INFO : Level::ERROR, isSuccess ? successToast : failToast);
document.change(changeType);
}
Window spritesheets_window_register()
{
Window window{};
window.title = LABEL_SPRITESHEETS_WINDOW;
window.isOpen = &Settings::windowIsSpritesheets;
window.changeType = Document::SPRITESHEETS;
window.containerType = ElementType::SPRITESHEETS;
window.elementType = ElementType::SPRITESHEET;
window.childLabel = "##Spritesheets Child";
window.cardLines = SPRITESHEET_CARD_LINES;
window.pasteEdit = EDIT_PASTE_SPRITESHEETS;
window.deserializeFailedToast = TOAST_DESERIALIZE_SPRITESHEETS_FAILED;
window.flags = WINDOW_OPEN_DIRECTORY | WINDOW_SET_FILE_PATH | WINDOW_ADD | WINDOW_REMOVE_UNUSED | WINDOW_RELOAD |
WINDOW_REPLACE | WINDOW_MERGE | WINDOW_PACK | WINDOW_SAVE | WINDOW_COPY | WINDOW_PASTE;
window.footer = {{WINDOW_ADD, WINDOW_RELOAD, WINDOW_REPLACE}, {WINDOW_REMOVE_UNUSED, WINDOW_SAVE}};
window.tooltips = {
{WINDOW_ADD, TOOLTIP_ADD_SPRITESHEET}, {WINDOW_RELOAD, TOOLTIP_RELOAD_SPRITESHEETS},
{WINDOW_REPLACE, TOOLTIP_REPLACE_SPRITESHEET}, {WINDOW_REMOVE_UNUSED, TOOLTIP_REMOVE_UNUSED_SPRITESHEETS},
{WINDOW_PACK, TOOLTIP_PACK_SPRITESHEET}, {WINDOW_SAVE, TOOLTIP_SAVE_SPRITESHEETS}};
window.enabled = {{WINDOW_PACK, [](Window&, Document& document)
{
auto& selection = document.spritesheet.selection;
return selection.size() == 1 && is_spritesheet_regions(document, *selection.begin());
}}};
window.popup = PopupHelper(LABEL_SPRITESHEETS_MERGE_POPUP, POPUP_SMALL_NO_HEIGHT);
window.popup2 = PopupHelper(LABEL_SPRITESHEETS_PACK_POPUP, POPUP_SMALL_NO_HEIGHT);
window.popup3 = PopupHelper(LABEL_TASKBAR_OVERWRITE_FILE, POPUP_SMALL_NO_HEIGHT);
window.storage_get = [](Document& document) -> Storage& { return document.spritesheet; };
window.row_label_get = [](Document& document, const Element& spritesheet)
{
auto pathString = path::to_utf8(spritesheet.path);
auto label = std::vformat(localize.get(FORMAT_SPRITESHEET), std::make_format_args(spritesheet.id, pathString));
return document.spritesheet_is_dirty(spritesheet.id)
? std::vformat(localize.get(FORMAT_SPRITESHEET_NOT_SAVED), std::make_format_args(label))
: label;
};
window.row_select = [](Window&, Document& document, int)
{
document.editTarget = Document::EditTarget::SPRITESHEET;
document.region.reference = -1;
document.region.selection.clear();
};
window.card_image_get = [](Document& document, Resources& resources, const Element& spritesheet)
{
auto texture = document.texture_get(spritesheet.id);
auto isValid = texture && texture->is_valid();
auto& image = isValid ? *texture : resources.icons[icon::NONE];
return WindowCardImage{.texture = &image, .size = glm::vec2(image.size), .isValid = isValid};
};
window.tooltip_draw = [](Document& document, Resources& resources, const Element& spritesheet)
{
auto texture = document.texture_get(spritesheet.id);
auto isValid = texture && texture->is_valid();
auto& image = isValid ? *texture : resources.icons[icon::NONE];
window_tooltip_image_draw(
{.texture = &image, .size = glm::vec2(image.size), .isValid = isValid},
[&]()
{
window_tooltip_name_draw(resources, path::to_utf8(spritesheet.path));
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_ID), std::make_format_args(spritesheet.id)).c_str());
if (isValid)
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_TEXTURE_SIZE), std::make_format_args(image.size.x, image.size.y))
.c_str());
else
ImGui::TextUnformatted(localize.get(TOOLTIP_SPRITESHEET_INVALID));
ImGui::TextUnformatted(localize.get(TEXT_OPEN_DIRECTORY));
});
};
window.add = [](Window& window, Manager&, Settings&, Document&, Clipboard&)
{
if (window.dialog) window.dialog->file_open(Dialog::SPRITESHEET_OPEN, true);
};
window.replace = [](Window& window, Manager&, Settings&, Document&, Clipboard&)
{
if (window.dialog) window.dialog->file_open(Dialog::SPRITESHEET_REPLACE);
};
window.path_set = [](Window& window, Manager&, Settings&, Document&, Clipboard&)
{
if (window.dialog) window.dialog->file_save(Dialog::SPRITESHEET_PATH_SET);
};
window.open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto spritesheet = window_element_get(window, document, document.spritesheet.reference);
if (spritesheet && window.dialog) window_directory_open(*window.dialog, document, spritesheet->path);
};
window.remove_unused = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto unused = document.anm2.element_unused(ElementType::SPRITESHEET);
auto spritesheets = window_container_get(window, document);
if (unused.empty() || !spritesheets) return;
window_edit(document, window.changeType, localize.get(EDIT_REMOVE_UNUSED_SPRITESHEETS),
[&]()
{
for (auto id : unused)
if (auto spritesheet = window_element_get(window, document, id))
{
toast_log(Level::INFO, TOAST_REMOVE_SPRITESHEET, id, path::to_utf8(spritesheet->path));
element_child_id_erase(*spritesheets, ElementType::SPRITESHEET, id);
}
});
};
window.reload = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
auto selected = document.spritesheet.selection;
window_edit(document, window.changeType, localize.get(EDIT_RELOAD_SPRITESHEETS),
[&]()
{
for (auto id : selected)
document.texture_reload(id);
});
for (auto id : selected)
if (auto spritesheet = window_element_get(window, document, id))
{
document.spritesheet_hash_set_saved(id);
toast_log(Level::INFO, TOAST_RELOAD_SPRITESHEET, id, path::to_utf8(spritesheet->path));
}
};
window.save = [](Window& window, Manager&, Settings& settings, Document& document, Clipboard&)
{
if (!settings.fileIsWarnOverwrite) return spritesheets_save(document, document.spritesheet.selection);
window.selection2 = document.spritesheet.selection;
window.popup3.open();
};
window.merge_open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
window.selection = document.spritesheet.selection;
window.popup.open();
};
window.pack_open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
{
window.editId = *document.spritesheet.selection.begin();
window.popup2.open();
};
window.body_update = [](Window& window, Manager& manager, Settings&, Resources&, Clipboard&, Document& document)
{
if (!window.dialog) return;
auto& selection = document.spritesheet.selection;
if (window.dialog->is_selected(Dialog::SPRITESHEET_OPEN))
{
manager.command_push({manager.selected, [&window, paths = window.dialog->paths](Manager&, Document& document)
{
document.spritesheets_add(paths);
document.region.reference = -1;
document.region.selection.clear();
window.newElementId = document.spritesheet.reference;
}});
window.dialog->reset();
}
for (auto [type, edit] : {std::pair{Dialog::SPRITESHEET_REPLACE, EDIT_REPLACE_SPRITESHEET},
std::pair{Dialog::SPRITESHEET_PATH_SET, EDIT_SET_SPRITESHEET_FILE_PATH}})
{
if (!window.dialog->is_selected(type)) continue;
if (selection.size() == 1 && !window.dialog->path.empty())
spritesheet_path_change(window, manager, *selection.begin(), window.dialog->path, edit,
type == Dialog::SPRITESHEET_PATH_SET);
window.dialog->reset();
}
};
window.popup_update =
[](Window& window, Manager& manager, Settings& settings, Resources&, Clipboard&, Document& document)
{
window.popup.trigger();
if (ImGui::BeginPopupModal(window.popup.label(), &window.popup.isOpen, ImGuiWindowFlags_NoResize))
{
settings.mergeSpritesheetsRegionOrigin =
glm::clamp(settings.mergeSpritesheetsRegionOrigin, (int)origin::TOP_LEFT, (int)origin::ORIGIN_CENTER);
if (ImGui::BeginChild("##Merge Spritesheets Options", child_size_get(MERGE_OPTIONS_ROWS),
ImGuiChildFlags_Borders))
{
ImGui::SeparatorText(localize.get(LABEL_REGION_PROPERTIES_ORIGIN));
ImGui::RadioButton(localize.get(LABEL_MERGE_SPRITESHEETS_APPEND_BOTTOM), &settings.mergeSpritesheetsOrigin,
APPEND_BOTTOM);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_MERGE_SPRITESHEETS_BOTTOM_LEFT));
ImGui::SameLine();
ImGui::RadioButton(localize.get(LABEL_MERGE_SPRITESHEETS_APPEND_RIGHT), &settings.mergeSpritesheetsOrigin,
APPEND_RIGHT);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_MERGE_SPRITESHEETS_TOP_RIGHT));
ImGui::SeparatorText(localize.get(LABEL_OPTIONS));
ImGui::Checkbox(localize.get(LABEL_MERGE_MAKE_SPRITESHEET_REGIONS), &settings.mergeSpritesheetsIsMakeRegions);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_MERGE_MAKE_SPRITESHEET_REGIONS));
ImGui::BeginDisabled(!settings.mergeSpritesheetsIsMakeRegions);
ImGui::Checkbox(localize.get(LABEL_MERGE_MAKE_PRIMARY_SPRITESHEET_REGION),
&settings.mergeSpritesheetsIsMakePrimaryRegion);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_MERGE_MAKE_PRIMARY_SPRITESHEET_REGION));
const char* regionOriginOptions[] = {localize.get(LABEL_REGION_ORIGIN_TOP_LEFT),
localize.get(LABEL_REGION_ORIGIN_CENTER)};
ImGui::Combo(localize.get(LABEL_REGION_PROPERTIES_ORIGIN), &settings.mergeSpritesheetsRegionOrigin,
regionOriginOptions, IM_ARRAYSIZE(regionOriginOptions));
ImGui::EndDisabled();
}
ImGui::EndChild();
auto result =
window_popup_buttons_draw(manager, localize.get(BASIC_MERGE), window.selection.size() > 1, BASIC_CANCEL);
if (result == PopupButton::CONFIRM)
manager.command_push(
{manager.selected,
[ids = window.selection, isAppendRight = settings.mergeSpritesheetsOrigin == APPEND_RIGHT,
isMakeRegions = settings.mergeSpritesheetsIsMakeRegions,
isMakePrimaryRegion = settings.mergeSpritesheetsIsMakePrimaryRegion,
regionOrigin = (origin::Type)settings.mergeSpritesheetsRegionOrigin](Manager&, Document& document)
{
spritesheet_textures_edit(
document, EDIT_MERGE_SPRITESHEETS, Document::ALL,
[&]()
{
if (!document.spritesheets_merge(ids, isAppendRight, isMakeRegions, isMakePrimaryRegion,
regionOrigin))
return false;
document.spritesheet.selection = {*ids.begin()};
document.spritesheet.reference = *ids.begin();
document.region.reference = -1;
document.region.selection.clear();
return true;
},
TOAST_MERGE_SPRITESHEETS, TOAST_MERGE_SPRITESHEETS_FAILED);
}});
if (result != PopupButton::NONE)
{
window.selection.clear();
window.popup.close();
}
ImGui::EndPopup();
}
window.popup.end();
window.popup2.trigger();
if (ImGui::BeginPopupModal(window.popup2.label(), &window.popup2.isOpen, ImGuiWindowFlags_NoResize))
{
settings.packPadding = std::max(0, settings.packPadding);
if (ImGui::BeginChild("##Pack Spritesheet Options", child_size_get(), ImGuiChildFlags_Borders))
ImGui::DragInt(localize.get(LABEL_PACK_PADDING), &settings.packPadding, DRAG_SPEED, 0, PADDING_MAX);
ImGui::EndChild();
auto result = window_popup_buttons_draw(manager, localize.get(BASIC_PACK),
is_spritesheet_regions(document, window.editId), BASIC_CANCEL);
if (result == PopupButton::CONFIRM)
manager.command_push({manager.selected,
[id = window.editId, padding = settings.packPadding](Manager&, Document& document)
{
spritesheet_textures_edit(
document, EDIT_PACK_SPRITESHEET, Document::SPRITESHEETS,
[&]() { return document.spritesheet_pack(id, padding); }, TOAST_PACK_SPRITESHEET,
TOAST_PACK_SPRITESHEET_FAILED);
}});
if (result != PopupButton::NONE)
{
window.editId = -1;
window.popup2.close();
}
ImGui::EndPopup();
}
window.popup2.end();
window.popup3.trigger();
if (ImGui::BeginPopupModal(window.popup3.label(), &window.popup3.isOpen, ImGuiWindowFlags_NoResize))
{
ImGui::TextUnformatted(localize.get(LABEL_OVERWRITE_CONFIRMATION));
auto result = window_popup_buttons_draw(manager, localize.get(BASIC_YES), true, BASIC_NO);
if (result == PopupButton::CONFIRM)
manager.command_push({manager.selected, [ids = window.selection2](Manager&, Document& document)
{ spritesheets_save(document, ids); }});
if (result != PopupButton::NONE)
{
window.selection2.clear();
window.popup3.close();
}
ImGui::EndPopup();
}
window.popup3.end();
};
return window;
}
}
+1 -10
View File
@@ -1880,16 +1880,7 @@ namespace anm2ed::imgui
item_base_properties_open = [&](int type, int id)
{
switch (type)
{
case LAYER:
manager.layer_properties_open(id);
break;
case NULL_:
manager.null_properties_open(id);
default:
break;
};
if (auto row = anm2ed::track_container_get((ItemType)type)) manager.item_properties_open(row->element, id);
};
group_properties_close = [&]()
+524 -4325
View File
File diff suppressed because it is too large. Load diff
+96 -45
View File
@@ -1,6 +1,7 @@
#pragma once
#include <functional>
#include <map>
#include <set>
#include <string>
#include <vector>
@@ -16,30 +17,69 @@
namespace anm2ed::imgui
{
#define WINDOW_FLAGS \
X(PLAY, ACTION_PLAY, play) \
X(RENAME, ACTION_RENAME, rename) \
X(PROPERTIES, ACTION_PROPERTIES, properties) \
X(OPEN_DIRECTORY, ACTION_OPEN_DIRECTORY, open) \
X(SET_FILE_PATH, ACTION_SET_FILE_PATH, path_set) \
X(ADD, ACTION_ADD, add) \
X(DUPLICATE, ACTION_DUPLICATE, duplicate) \
X(REMOVE_UNUSED, ACTION_REMOVE_UNUSED, remove_unused) \
X(RELOAD, ACTION_RELOAD, reload) \
X(REPLACE, ACTION_REPLACE, replace) \
X(MERGE, ACTION_MERGE, merge) \
X(GROUP, ACTION_GROUP, group) \
X(REMOVE, ACTION_REMOVE, remove) \
X(DEFAULT, ACTION_DEFAULT, default_set) \
X(TRIM, ACTION_TRIM, trim) \
X(EXPORT, ACTION_EXPORT, export_open) \
X(PACK, ACTION_PACK, pack_open) \
X(SAVE, ACTION_SAVE, save) \
X(CUT, ACTION_CUT, cut) \
X(COPY, ACTION_COPY, copy) \
X(PASTE, ACTION_PASTE, paste)
enum WindowFlagIndex
{
#define X(symbol, action, command) WINDOW_INDEX_##symbol,
WINDOW_FLAGS
#undef X
WINDOW_INDEX_COUNT
};
enum WindowFlag
{
WINDOW_ADD = 1 << 0,
WINDOW_REMOVE = 1 << 1,
WINDOW_REMOVE_UNUSED = 1 << 2,
WINDOW_DUPLICATE = 1 << 3,
WINDOW_MERGE = 1 << 4,
WINDOW_DEFAULT = 1 << 5,
WINDOW_CUT = 1 << 6,
WINDOW_COPY = 1 << 7,
WINDOW_PASTE = 1 << 8,
WINDOW_RENAME = 1 << 9,
WINDOW_PROPERTIES = 1 << 10,
WINDOW_REFERENCE_ITALIC = 1 << 11,
WINDOW_GROUP = 1 << 12
#define X(symbol, action, command) WINDOW_##symbol = 1 << WINDOW_INDEX_##symbol,
WINDOW_FLAGS
#undef X
WINDOW_REFERENCE_ITALIC = 1 << WINDOW_INDEX_COUNT
};
using WindowFlags = int;
inline constexpr int WINDOW_GROUP_KEY_OFFSET = 2;
constexpr int window_group_key_get(int groupId) { return -groupId - WINDOW_GROUP_KEY_OFFSET; }
constexpr int window_group_id_from_key_get(int key) { return -key - WINDOW_GROUP_KEY_OFFSET; }
constexpr bool is_window_group_key(int key) { return key <= -WINDOW_GROUP_KEY_OFFSET; }
struct WindowCardImage
{
resource::Texture* texture{};
glm::vec2 size{};
glm::vec2 uvMin{};
glm::vec2 uvMax{1.0f};
bool isValid{};
};
constexpr bool window_flag_has(WindowFlags flags, WindowFlag flag) { return (flags & flag) != 0; }
struct Window
{
using StorageGet = std::function<Storage&(Document&)>;
using ContainerGet = std::function<Element*(Document&)>;
using IndexGet = std::function<int(Document&)>;
using ElementGet = std::function<Element*(Anm2&, int)>;
using ElementKeyGet = std::function<int(const Element&, int)>;
using RowLabelGet = std::function<std::string(Document&, const Element&)>;
@@ -48,11 +88,11 @@ namespace anm2ed::imgui
using RenameFinish = std::function<void(Document&, Element&, int, int)>;
using TooltipDraw = std::function<void(Document&, Resources&, const Element&)>;
using RowDragDropUpdate = std::function<bool(Window&, Manager&, Document&, const Element&, int)>;
using CardImageGet = std::function<WindowCardImage(Document&, Resources&, const Element&)>;
using PropertiesOpen = std::function<void(Manager&, int)>;
using Command = std::function<void(Window&, Manager&, Settings&, Document&, Clipboard&)>;
using IsAvailable = std::function<bool(Document&)>;
using IsEnabled = std::function<bool(Window&, Document&)>;
using Update = std::function<void(Window&, Manager&, Settings&, Resources&, Clipboard&, Document&)>;
using RowsUpdate = std::function<void(Window&, Manager&, Settings&, Resources&, Clipboard&, Document&, ImVec2)>;
StringType title{};
bool Settings::* isOpen{};
@@ -60,24 +100,21 @@ namespace anm2ed::imgui
ElementType containerType{ElementType::UNKNOWN};
ElementType elementType{ElementType::UNKNOWN};
const char* childLabel{"##Window Child"};
StringType addTooltip{};
StringType duplicateTooltip{};
StringType mergeTooltip{};
StringType removeTooltip{};
StringType removeUnusedTooltip{};
StringType defaultTooltip{};
std::map<WindowFlag, StringType> tooltips{};
std::vector<std::vector<WindowFlag>> footer{};
StringType addEdit{};
StringType propertiesEdit{};
StringType renameEdit{};
StringType pasteEdit{};
StringType removeUnusedEdit{};
StringType deserializeFailedToast{};
StringType unavailableText{STRING_UNDEFINED};
int cardLines{};
int newElementId{-1};
int scrollQueued{-1};
int renameQueued{-1};
int renameId{-1};
int editId{-1};
int footerRows{-1};
RenameState renameState{RENAME_SELECTABLE};
std::string renameText{};
PopupHelper popup{STRING_UNDEFINED};
@@ -90,11 +127,12 @@ namespace anm2ed::imgui
Element editElement{};
Dialog* dialog{};
WindowFlags flags{WINDOW_COPY | WINDOW_PASTE};
bool isChildPaddingZero{};
bool isPreserveEditElementOnOpen{};
ImVec2 tooltipWindowPadding{};
ImVec2 tooltipItemSpacing{};
StorageGet storage_get{};
ContainerGet container_get{};
IndexGet insert_index_get{};
ElementGet element_get{};
ElementKeyGet element_key_get{};
RowLabelGet row_label_get{};
@@ -103,30 +141,15 @@ namespace anm2ed::imgui
RenameFinish rename_finish{};
TooltipDraw tooltip_draw{};
RowDragDropUpdate row_drag_drop_update{};
CardImageGet card_image_get{};
PropertiesOpen properties_open{};
Command add{};
Command remove{};
Command duplicate{};
Command merge{};
#define X(symbol, action, command) Command command{};
WINDOW_FLAGS
#undef X
Command merge_open{};
Command group{};
Command default_set{};
Command cut{};
Command copy{};
Command paste{};
Command reload{};
Command replace{};
Command save{};
Command pack{};
Command trim{};
Command properties{};
Command open{};
Command path_set{};
IsAvailable is_available{};
Update begin_update{};
RowsUpdate rows_update{};
Update context_update{};
Update footer_update{};
std::map<WindowFlag, IsEnabled> enabled{};
IsEnabled is_available{};
Update rows_update{};
Update body_update{};
Update popup_update{};
Update post_update{};
@@ -140,4 +163,32 @@ namespace anm2ed::imgui
Window nulls_window_register();
Window events_window_register();
void window_update(Window&, Manager&, Settings&, Resources&, Dialog&, Clipboard&);
enum class PopupButton
{
NONE,
CONFIRM,
CANCEL
};
inline constexpr auto DRAG_DROP_SOURCE_FLAGS =
ImGuiDragDropFlags_SourceNoPreviewTooltip | ImGuiDragDropFlags_SourceNoHoldToOpenOthers;
inline constexpr ImVec4 CARD_TINT_INVALID = ImVec4(1.0f, 0.25f, 0.25f, 1.0f);
inline constexpr ImVec4 CARD_TINT_VALID = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
bool is_drag_drop_active();
Element* window_container_get(const Window&, Document&);
Element* window_element_get(const Window&, Document&, int);
void window_element_apply_push(Window&, Manager&, const Element&, int);
void window_edit(Document&, Document::ChangeType, const std::string&, const std::function<void()>&);
void window_command_push(Window&, Manager&, Settings&, Clipboard&, const Window::Command&);
int window_selection_start(Window&, Document&, const std::vector<int>&);
void window_selection_finish(Window&, Manager&, Settings&, Clipboard&, Document&, int);
bool window_row_draw(Window&, Manager&, Resources&, Document&, Element&, int, int, int);
void window_cards_draw(Window&, Manager&, Settings&, Resources&, Clipboard&, Document&, Element*);
void window_tooltip_name_draw(Resources&, const std::string&);
void window_tooltip_image_draw(const WindowCardImage&, const std::function<void()>&);
void window_directory_open(Dialog&, Document&, const std::filesystem::path&);
std::filesystem::path window_asset_path_get(Document&, const std::filesystem::path&);
PopupButton window_popup_buttons_draw(Manager&, const char*, bool = true, StringType = BASIC_CANCEL);
}
+8 -52
View File
@@ -224,59 +224,15 @@ namespace anm2ed
if (auto document = get()) document->change(Document::ALL);
}
void Manager::layer_properties_open(int id)
void Manager::item_properties_open(ElementType type, int id)
{
if (auto document = get(); document)
{
if (id == -1)
editLayer = element_make(ElementType::LAYER_ELEMENT);
else if (auto layer = document->anm2.element_get(ElementType::LAYER_ELEMENT, id); layer)
editLayer = *layer;
else
return;
document->layer.reference = id;
layerPropertiesPopup.open();
}
}
void Manager::layer_properties_trigger() { layerPropertiesPopup.trigger(); }
void Manager::layer_properties_end() { layerPropertiesPopup.end(); }
void Manager::layer_properties_close()
{
editLayer = element_make(ElementType::LAYER_ELEMENT);
layerPropertiesPopup.close();
}
void Manager::null_properties_open(int id)
{
if (auto document = get(); document)
{
auto nulls = document->anm2.element_get(ElementType::NULLS);
if (id == -1)
editNull = element_make(ElementType::NULL_ELEMENT);
else if (auto null = nulls ? child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; null)
editNull = *null;
else
return;
document->null.reference = id;
nullPropertiesPopup.open();
}
}
void Manager::null_properties_trigger() { nullPropertiesPopup.trigger(); }
void Manager::null_properties_end() { nullPropertiesPopup.end(); }
void Manager::null_properties_close()
{
editNull = element_make(ElementType::NULL_ELEMENT);
nullPropertiesPopup.close();
auto document = get();
auto row = track_container_get(type);
auto element = id == -1 || !document ? nullptr : document->anm2.element_get(type, id);
if (!document || !row || (id != -1 && !element)) return;
itemEdit = element ? *element : element_make(type);
(type == ElementType::LAYER_ELEMENT ? document->layer : document->null).reference = id;
itemPropertiesPopups[row - TRACK_CONTAINERS].open();
}
void Manager::recent_files_trim()
+4 -14
View File
@@ -62,12 +62,9 @@ namespace anm2ed
std::filesystem::path spritesheetDragDropPath{};
bool isSpritesheetDragDrop{};
Element editLayer{element_make(ElementType::LAYER_ELEMENT)};
imgui::PopupHelper layerPropertiesPopup{
imgui::PopupHelper(LABEL_MANAGER_LAYER_PROPERTIES, imgui::POPUP_SMALL_NO_HEIGHT)};
Element editNull{element_make(ElementType::NULL_ELEMENT)};
imgui::PopupHelper nullPropertiesPopup{
Element itemEdit{};
imgui::PopupHelper itemPropertiesPopups[std::size(TRACK_CONTAINERS)]{
imgui::PopupHelper(LABEL_MANAGER_LAYER_PROPERTIES, imgui::POPUP_SMALL_NO_HEIGHT),
imgui::PopupHelper(LABEL_MANAGER_NULL_PROPERTIES, imgui::POPUP_SMALL_NO_HEIGHT)};
Element makeRegion{element_make(ElementType::REGION)};
@@ -93,14 +90,7 @@ namespace anm2ed
void autosave_file_clear(int);
void set(int);
void close(int);
void layer_properties_open(int = -1);
void layer_properties_trigger();
void layer_properties_end();
void layer_properties_close();
void null_properties_open(int = -1);
void null_properties_trigger();
void null_properties_end();
void null_properties_close();
void item_properties_open(ElementType, int = -1);
void recent_files_load();
void recent_files_write();
+3 -3
View File
@@ -12,7 +12,7 @@ namespace anm2ed::util::vector
return index >= 0 && index < (int)v.size() ? &v[index] : nullptr;
}
template <typename T> int find_index(std::vector<T>& v, T& value)
template <typename T> int find_index(const std::vector<T>& v, const T& value)
{
auto it = std::find(v.begin(), v.end(), value);
if (it == v.end()) return -1;
@@ -86,8 +86,8 @@ namespace anm2ed::util::vector
return moveIndices;
}
template <typename T> std::set<int> move_indices_to_position(std::vector<T>& v, const std::vector<int>& indices,
int insertPos)
template <typename T>
std::set<int> move_indices_to_position(std::vector<T>& v, const std::vector<int>& indices, int insertPos)
{
if (indices.empty()) return {};