Stage 8a: panels as plain functions; content selection on the uid Selection

- New panel kit (imgui/window/panel.*): list/card drawing with multi-select,
  rename, arrow keys, drag hooks and tooltips; a list panel helper for the
  context menu, shortcuts and footer; panels build their Actions inline.
- Layers, nulls, events, sounds, spritesheets, regions, shaders and
  animations are plain functions over a small persistent state struct.
- Content items are selected by uid in the shared Selection (one kind per
  content type, plus a focus per kind); regions are those of the focused
  spritesheet. Selection survives undo, paste and removal like the timeline's.
- Combo labels are computed when drawn (Document::choices_get).
- Deletes the Window callback framework, WINDOW_FLAGS, Storage and the
  per-window selection snapshot fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 17:47:21 -04:00
1 parent e50d76c784
commit d08f59d7ba
37 files changed
+2355 -2578

No files matched your search

+332 -302
View File
@@ -1,11 +1,10 @@
#include "window.hpp"
#include "panel.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"
@@ -45,42 +44,40 @@ namespace anm2ed::imgui
if (region.origin == Origin::CENTER) region.pivot = region.size * 0.5f;
}
std::vector<model::Region>* regions_get(Document& document, model::Model& model)
{
auto spritesheet = model::item_get(model.content.spritesheets, document.spritesheet.reference);
return spritesheet ? &spritesheet->regions : nullptr;
}
const model::Spritesheet* region_spritesheet_get(Document& document)
{
return model::item_get(document.model.content.spritesheets, document.spritesheet.reference);
}
const model::Region* region_get(Document& document, int id)
{
auto spritesheet = region_spritesheet_get(document);
return spritesheet ? model::item_get(spritesheet->regions, id) : nullptr;
return model::item_get(document.model.content.spritesheets, document.focused_id_get(SelectionKind::SPRITESHEETS));
}
// New regions go after the last selected (or focused) one.
int region_insert_index_get(Document& document)
{
auto spritesheet = region_spritesheet_get(document);
auto& storage = document.region;
int index = spritesheet ? (int)spritesheet->regions.size() : 0;
for (int i = 0; spritesheet && i < (int)spritesheet->regions.size(); i++)
if (!spritesheet) return 0;
auto selection = document.selected_ids_get(SelectionKind::REGIONS);
auto focus = document.focused_id_get(SelectionKind::REGIONS);
int index = (int)spritesheet->regions.size();
for (int i = 0; i < (int)spritesheet->regions.size(); i++)
{
auto id = spritesheet->regions[i].id;
if (storage.selection.contains(id) || (storage.selection.empty() && storage.reference == id)) index = i + 1;
if (selection.contains(id) || (selection.empty() && focus == id)) index = i + 1;
}
return index;
}
WindowCardImage region_image_get(Document& document, Resources& resources, int id)
void region_select(Document& document, int id)
{
auto& region = *region_get(document, id);
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};
document.editTarget = Document::EditTarget::REGION;
document.selected_ids_set(SelectionKind::REGIONS, {id});
document.focused_id_set(SelectionKind::REGIONS, id);
document.reference_set({document.reference_get().animationIndex});
document.frame_references_clear();
}
CardImage region_image_get(Document& document, Resources& resources, const model::Region& region)
{
auto texture = document.texture_get(document.focused_id_get(SelectionKind::SPRITESHEETS));
if (!texture || !texture->is_valid()) return {.texture = &resources.icons[icon::NONE], .size = region.size};
return {.texture = texture,
.size = region.size,
.uvMin = region.crop / vec2(texture->size),
@@ -131,7 +128,7 @@ namespace anm2ed::imgui
auto outputPath = options.path;
if (!outputPath.empty())
{
outputPath = window_asset_path_get(document, outputPath.replace_extension(PNG_EXTENSION));
outputPath = 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());
@@ -146,6 +143,14 @@ namespace anm2ed::imgui
{
document.edit_begin(EDIT_EXPORT_REGION);
if (options.isRemoveCurrent)
{
if (auto source = model::item_get(document.model.content.spritesheets, options.spritesheetId))
std::erase_if(source->regions, [&](const model::Region& region) { return region.id == options.regionId; });
edit::region_frames_sync(document.model);
document.selected_clear(SelectionKind::REGIONS);
}
if (options.isMakeSpritesheet)
{
auto& spritesheets = document.model.content.spritesheets;
@@ -162,27 +167,15 @@ namespace anm2ed::imgui
document.texture_set(exported.id, Image(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};
document.selected_ids_set(SelectionKind::SPRITESHEETS, {exported.id});
document.focused_id_set(SelectionKind::SPRITESHEETS, exported.id);
document.selected_ids_set(SelectionKind::REGIONS, {exportedRegion.id});
document.focused_id_set(SelectionKind::REGIONS, exportedRegion.id);
if (!outputPath.empty())
document.spritesheet_hash_set_saved(exported.id);
else
document.spritesheetSaveHashes[exported.id] = 0;
}
if (options.isRemoveCurrent)
{
if (auto source = model::item_get(document.model.content.spritesheets, options.spritesheetId))
std::erase_if(source->regions, [&](const model::Region& region) { return region.id == options.regionId; });
edit::region_frames_sync(document.model);
if (!options.isMakeSpritesheet)
{
document.region.reference = -1;
document.region.selection.clear();
}
}
document.change();
}
@@ -190,15 +183,15 @@ namespace anm2ed::imgui
return true;
}
bool region_drag_drop_update(Window& window, Manager& manager, Document& document, int id, int index)
bool region_drag_drop_update(Panel& panel, int id, int index)
{
auto& selection = document.region.selection;
auto& state = panel.state;
auto selection = panel.document.selected_ids_get(SelectionKind::REGIONS);
if (ImGui::BeginDragDropSource(DRAG_DROP_SOURCE_FLAGS))
{
window.dragSelection =
state.dragIds =
selection.contains(id) ? std::vector<int>(selection.begin(), selection.end()) : std::vector<int>{id};
ImGui::SetDragDropPayload(REGION_DRAG_DROP, window.dragSelection.data(),
window.dragSelection.size() * sizeof(int));
ImGui::SetDragDropPayload(REGION_DRAG_DROP, state.dragIds.data(), state.dragIds.size() * sizeof(int));
ImGui::EndDragDropSource();
}
@@ -215,26 +208,25 @@ namespace anm2ed::imgui
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 (auto keyIndex = vector::find_index(state.keys, payloadIds[i]); keyIndex != -1) indices.push_back(keyIndex);
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
{
if (!model::item_get(document.model.content.spritesheets, spritesheetId)) return;
document.edit_apply(EDIT_MOVE_REGIONS,
[&](model::Model& model)
{
auto spritesheet = model::item_get(model.content.spritesheets, spritesheetId);
vector::move_indices_to_position(spritesheet->regions, indices, targetIndex);
document.region.selection = std::set<int>(movedIds.begin(), movedIds.end());
});
}});
command_push(panel,
[indices, movedIds = state.dragIds, targetIndex = index + (isAfter ? 1 : 0),
spritesheetId = panel.document.focused_id_get(SelectionKind::SPRITESHEETS)](Document& document)
{
if (!model::item_get(document.model.content.spritesheets, spritesheetId)) return;
document.edit_apply(
EDIT_MOVE_REGIONS,
[&](model::Model& model)
{
auto spritesheet = model::item_get(model.content.spritesheets, spritesheetId);
vector::move_indices_to_position(spritesheet->regions, indices, targetIndex);
});
document.selected_ids_set(SelectionKind::REGIONS, {movedIds.begin(), movedIds.end()});
});
isMoved = true;
}
}
@@ -242,281 +234,319 @@ namespace anm2ed::imgui
return isMoved;
}
void region_drag_tooltip_update(Window& window, Document& document)
void region_drag_tooltip_update(Panel& panel)
{
auto& state = panel.state;
auto payload = ImGui::GetDragDropPayload();
auto spritesheet = region_spritesheet_get(document);
auto spritesheet = region_spritesheet_get(panel.document);
if (!spritesheet || !payload || !payload->IsDataType(REGION_DRAG_DROP) || payload->IsDelivery() ||
window.dragSelection.empty())
state.dragIds.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::SetDragDropPayload(REGION_DRAG_DROP, state.dragIds.data(), state.dragIds.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)
for (auto regionId : state.dragIds)
if (auto region = model::item_get(spritesheet->regions, 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 ? model::item_get(spritesheet->regions, 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.editRegion = target ? *target : model::Region{};
if (window.popup.isJustOpened) window.isPreserveEditElementOnOpen = false;
auto& region = window.editRegion;
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_item_apply_push<model::Region>(window, manager, region, reference, regions_get,
region_insert_index_get(document));
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)
void region_properties_open(RegionsPanel& regions, Document& document, int id)
{
auto spritesheet = region_spritesheet_get(document);
auto regionId = window.editId != -1 ? window.editId : document.region.reference;
auto region = spritesheet && regionId != -1 ? model::item_get(spritesheet->regions, 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.icon_id_get(icon::FOLDER), 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();
auto region = spritesheet ? model::item_get(spritesheet->regions, id) : nullptr;
if (id != -1 && !region) return;
regions.editId = id;
regions.editRegion = region ? *region : model::Region{};
regions.propertiesPopup.open();
}
Window regions_window_register()
void region_properties_popup_update(Panel& panel, RegionsPanel& regions)
{
Window window{};
window.title = LABEL_REGIONS_WINDOW;
window.isOpen = &Settings::windowIsRegions;
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.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_items_bind<model::Region>(window, regions_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)
auto& popup = regions.propertiesPopup;
popup.trigger();
if (!ImGui::BeginPopupModal(popup.label(), &popup.isOpen, ImGuiWindowFlags_NoResize)) return popup.end();
auto& region = regions.editRegion;
if (ImGui::BeginChild("##Child", child_size_get(REGION_POPUP_ROWS), ImGuiChildFlags_Borders))
{
document.editTarget = Document::EditTarget::REGION;
document.reference_set({document.reference_get().animationIndex});
document.frame_references_clear();
};
window.tooltip_draw = [](Document& document, Resources& resources, int id)
if (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 = popup_buttons_draw(panel.manager, localize.get(regions.editId == -1 ? BASIC_ADD : BASIC_CONFIRM));
if (result == PopupButton::CONFIRM)
{
auto& region = *region_get(document, id);
window_tooltip_image_draw(
region_image_get(document, resources, id),
[&]()
{
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&)
auto& state = panel.state;
command_push(panel,
[&state, edited = region, id = regions.editId, insertIndex = region_insert_index_get(panel.document),
spritesheetId = panel.document.focused_id_get(SelectionKind::SPRITESHEETS)](Document& document)
{
auto changedId = id;
document.edit_apply(id == -1 ? EDIT_ADD_REGION : EDIT_SET_REGION_PROPERTIES,
[&](model::Model& model)
{
auto spritesheet =
model::item_get(model.content.spritesheets, spritesheetId);
if (!spritesheet) return;
auto& items = spritesheet->regions;
if (auto target = model::item_get(items, id))
{
auto uid = target->uid;
*target = edited;
target->uid = uid;
target->id = id;
return;
}
if (id != -1) return;
changedId = model::item_next_id_get(items);
auto& added = *items.insert(
items.begin() + std::min(insertIndex, (int)items.size()), edited);
added.uid = util::uid_next();
added.id = changedId;
state.newId = changedId;
});
if (changedId != -1) region_select(document, changedId);
});
}
if (result != PopupButton::NONE) popup.close();
ImGui::EndPopup();
popup.end();
}
void region_export_popup_update(Panel& panel, RegionsPanel& regions)
{
auto& [manager, settings, resources, dialog, clipboard, document, state] = panel;
auto spritesheet = region_spritesheet_get(document);
auto region = spritesheet ? model::item_get(spritesheet->regions, regions.exportId) : nullptr;
auto& popup = regions.exportPopup;
popup.trigger();
if (!ImGui::BeginPopupModal(popup.label(), &popup.isOpen, ImGuiWindowFlags_NoResize)) return popup.end();
if (!region) popup.close();
if (region && popup.isJustOpened)
{
document.region.reference = -1;
window.editRegion = {};
window.popup.open();
};
window.properties = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
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 (dialog.is_selected(Dialog::REGION_EXPORT_PATH_SET))
{
auto& selection = document.region.selection;
if (selection.size() != 1 || !region_get(document, *selection.begin())) return;
document.region.reference = *selection.begin();
window.popup.open();
};
window.export_open = [](Window& window, Manager&, Settings&, Document& document, Clipboard&)
settings.exportRegionPath = dialog.path;
dialog.reset();
}
if (ImGui::BeginChild("##Export Region Child", child_size_get(REGION_POPUP_ROWS), ImGuiChildFlags_Borders))
{
window.editId = *document.region.selection.begin();
window.popup2.open();
};
window.remove_unused = [](Window&, Manager&, Settings&, Document& document, Clipboard&)
if (ImGui::ImageButton("##Export Region Path Set", resources.icon_id_get(icon::FOLDER), icon_size_get()))
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 spritesheetId = document.focused_id_get(SelectionKind::SPRITESHEETS);
auto texture = document.texture_get(spritesheetId);
auto isValid = region && texture && texture->is_valid() && !texture->pixels.empty() &&
(settings.isExportRegionMakeSpritesheet || !settings.exportRegionPath.empty());
auto result = popup_buttons_draw(manager, localize.get(BASIC_CONFIRM), isValid);
if (result == PopupButton::CONFIRM)
{
document.edit_apply(EDIT_REMOVE_UNUSED_REGIONS, [&](model::Model& model)
{ return edit::regions_remove_unused(model, document.spritesheet.reference); });
};
window.trim = [](Window&, Manager&, Settings&, Document& document, Clipboard&)
if (!settings.exportRegionPath.empty()) settings.exportRegionPath.replace_extension(PNG_EXTENSION);
RegionExportOptions options{.spritesheetId = spritesheetId,
.regionId = regions.exportId,
.path = settings.exportRegionPath,
.isMakeSpritesheet = settings.isExportRegionMakeSpritesheet,
.isRemoveCurrent = settings.isExportRegionRemoveCurrent};
command_push(panel,
[&state, options](Document& document)
{
if (region_export(document, options) && options.isMakeSpritesheet)
state.newId = document.focused_id_get(SelectionKind::REGIONS);
});
}
if (result != PopupButton::NONE) popup.close();
ImGui::EndPopup();
popup.end();
}
void regions_update(Panel& panel, RegionsPanel& regions)
{
auto& [manager, settings, resources, dialog, clipboard, document, state] = panel;
if (manager.isMakeRegionRequested)
{
auto& region = document.region;
document.edit_apply(EDIT_TRIM_REGIONS,
[&](model::Model&)
{
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_set({document.reference_get().animationIndex});
document.frame_references_clear();
});
};
window.paste = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard)
document.focused_id_set(SelectionKind::SPRITESHEETS, manager.makeRegionSpritesheetId);
regions.editId = -1;
regions.editRegion = manager.makeRegion;
regions.propertiesPopup.open();
manager.isMakeRegionRequested = false;
}
if (ImGui::Begin(localize.get(LABEL_REGIONS_WINDOW), &settings.windowIsRegions))
{
auto spritesheet = region_spritesheet_get(document);
if (!spritesheet || clipboard.is_empty()) return;
auto maxIdBefore = model::item_next_id_get(spritesheet->regions) - 1;
std::string errorString{};
document.edit_apply(EDIT_PASTE_REGIONS,
[&](model::Model& model)
{
edit::regions_paste(model, document.spritesheet.reference, clipboard.get(),
region_insert_index_get(document), &errorString);
auto target = region_spritesheet_get(document);
auto maxIdAfter = target ? model::item_next_id_get(target->regions) - 1 : -1;
if (maxIdAfter <= maxIdBefore) return;
window.newElementId = maxIdAfter;
document.region.selection = {maxIdAfter};
document.region.reference = maxIdAfter;
});
if (!errorString.empty()) toast_log(Level::ERROR, TOAST_DESERIALIZE_REGIONS_FAILED, errorString);
};
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_drag_tooltip_update(window, document);
};
window.popup_update =
[](Window& window, Manager& manager, Settings& settings, Resources& resources, Clipboard&, Document& document)
{
if (manager.isMakeRegionRequested)
if (!spritesheet)
ImGui::TextUnformatted(localize.get(TEXT_SELECT_SPRITESHEET));
else
{
document.spritesheet.reference = manager.makeRegionSpritesheetId;
document.region.reference = -1;
window.editRegion = manager.makeRegion;
window.isPreserveEditElementOnOpen = true;
window.popup.open();
manager.isMakeRegionRequested = false;
auto spritesheetId = spritesheet->id;
auto selection = document.selected_ids_get(SelectionKind::REGIONS);
auto isOne = [selection]() { return selection.size() == 1; };
Actions actions{};
actions.add(ACTION_PROPERTIES, isOne, [&]() { region_properties_open(regions, document, *selection.begin()); });
actions.add(ACTION_ADD, {}, [&]() { region_properties_open(regions, document, -1); }, TOOLTIP_ADD_REGION);
actions.add(
ACTION_REMOVE_UNUSED, {},
[&]()
{
command_push(panel,
[spritesheetId](Document& document)
{
document.edit_apply(EDIT_REMOVE_UNUSED_REGIONS, [&](model::Model& model)
{ return edit::regions_remove_unused(model, spritesheetId); });
});
},
TOOLTIP_REMOVE_UNUSED_REGIONS);
actions.add(
ACTION_TRIM, [selection]() { return !selection.empty(); },
[&]()
{
command_push(panel,
[spritesheetId, selection](Document& document)
{
document.edit_apply(EDIT_TRIM_REGIONS,
[&](model::Model&)
{
if (!document.regions_trim(spritesheetId, selection)) return;
document.reference_set({document.reference_get().animationIndex});
document.frame_references_clear();
});
});
},
TOOLTIP_TRIM_REGIONS);
actions.add(ACTION_EXPORT, isOne,
[&]()
{
regions.exportId = *selection.begin();
regions.exportPopup.open();
});
actions.separator();
actions.add(
ACTION_COPY, [selection]() { return !selection.empty(); },
[&]() { items_copy(clipboard, spritesheet->regions, selection); });
actions.add(
ACTION_PASTE, [&]() { return !clipboard.is_empty(); },
[&]()
{
command_push(
panel,
[&state, spritesheetId, text = clipboard.get(),
insertIndex = region_insert_index_get(document)](Document& document)
{
auto spritesheet_regions_get = [&]() -> const std::vector<model::Region>*
{
auto spritesheet = model::item_get(document.model.content.spritesheets, spritesheetId);
return spritesheet ? &spritesheet->regions : nullptr;
};
if (!spritesheet_regions_get()) return;
auto nextId = model::item_next_id_get(*spritesheet_regions_get());
std::string errorString{};
document.edit_apply(
EDIT_PASTE_REGIONS, [&](model::Model& model)
{ return edit::regions_paste(model, spritesheetId, text, insertIndex, &errorString); });
if (!errorString.empty()) toast_log(Level::ERROR, TOAST_DESERIALIZE_REGIONS_FAILED, errorString);
auto lastId = model::item_next_id_get(*spritesheet_regions_get()) - 1;
if (lastId < nextId) return;
state.newId = lastId;
region_select(document, lastId);
});
});
ListRows rows{.label_get = [&](int id) { return model::item_get(spritesheet->regions, id)->name; },
.tooltip_draw =
[&](int id)
{
auto& region = *model::item_get(spritesheet->regions, id);
tooltip_image_draw(
region_image_get(document, resources, region),
[&]()
{
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());
});
},
.image_get = [&](int id)
{ return region_image_get(document, resources, *model::item_get(spritesheet->regions, id)); },
.select =
[&](int)
{
document.editTarget = Document::EditTarget::REGION;
document.reference_set({document.reference_get().animationIndex});
document.frame_references_clear();
},
.activate = [&](int id) { region_properties_open(regions, document, id); },
.drag_drop_update = [&](int id, int index) { return region_drag_drop_update(panel, id, index); },
.cardLines = REGION_CARD_LINES};
list_panel_draw(panel, actions, {{ACTION_ADD, ACTION_EXPORT, ACTION_REMOVE_UNUSED}},
[&]()
{
content_list_draw(panel, SelectionKind::REGIONS, item_ids_get(spritesheet->regions), rows);
region_drag_tooltip_update(panel);
});
}
region_properties_popup_update(window, manager, document);
region_export_popup_update(window, manager, settings, resources, document);
};
return window;
}
ImGui::End();
region_properties_popup_update(panel, regions);
region_export_popup_update(panel, regions);
}
}