Files
anm2ed/src/ui/spritesheet_editor.cpp
T
shweet 8d3ebf2329 Add tooltip_set and PopupHelper::begin, and use localize_format
149 localized tooltips, 38 localized formats and 27 modal popups lose their boilerplate.
2026-11-22 18:47:33 -05:00

550 lines
26 KiB
C++

#include "ui/spritesheet_editor.h"
#include <cmath>
#include <format>
#include <set>
#include <utility>
#include "vec.h"
#include "ui/actions.h"
#include "imgui_internal.h"
#include "math_util.h"
#include "model/frames.h"
#include "localization.h"
#include "tool.h"
#include "types.h"
#include "ui/draw.h"
#include "ui/input.h"
#include "ui/layout.h"
#include "ui/shortcut.h"
void SpritesheetEditor::update(Manager& manager, Settings& settings, Resources& resources)
{
isFocused = false;
auto& document = *manager.get();
auto& model = document.model;
auto reference = document.reference_get();
auto referenceSpritesheet = document.focused_id_get(SelectionKind::SPRITESHEETS);
auto& pan = document.editorPan;
auto& zoom = document.editorZoom;
auto& backgroundColor = settings.editorBackgroundColor;
auto& gridColor = settings.editorGridColor;
auto& gridSize = settings.editorGridSize;
auto& gridOffset = settings.editorGridOffset;
auto& toolColor = settings.toolColor;
auto& isGrid = settings.editorIsGrid;
auto& isGridSnap = settings.editorIsGridSnap;
auto& isBorder = settings.editorIsBorder;
auto& isTransparent = settings.editorIsTransparent;
auto spritesheet = item_get(model.content.spritesheets, referenceSpritesheet);
auto baseTexture = document.texture_get(referenceSpritesheet);
auto texture = baseTexture;
auto& tool = settings.tool;
auto& shaderGrid = resources.shaders[SHADER_GRID];
auto& shaderTexture = resources.shaders[SHADER_TEXTURE];
auto& shaderLine = resources.shaders[SHADER_LINE];
auto& dashedShader = resources.shaders[SHADER_DASHED];
auto regionReference = document.focused_id_get(SelectionKind::REGIONS);
auto regionSelection = document.selected_ids_get(SelectionKind::REGIONS);
auto center_view = [&]() {
pan = -size * 0.5f;
};
auto fit_view = [&]() {
auto fitTexture = baseTexture && baseTexture->is_valid() ? baseTexture : texture;
if (fitTexture && fitTexture->is_valid())
set_to_rect(zoom, pan, {0, 0, (float)fitTexture->size.x, (float)fitTexture->size.y});
};
auto region_get = [&](int id) {
return spritesheet ? item_get(spritesheet->regions, id) : nullptr;
};
if (ImGui::Begin(localize.get(LABEL_SPRITESHEET_EDITOR_WINDOW), &settings.windowIsSpritesheetEditor))
{
isFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
auto childSize = ImVec2(row_widget_width_get(3),
(ImGui::GetTextLineHeightWithSpacing() * 4) + (ImGui::GetStyle().WindowPadding.y * 2));
grid_child_draw(childSize, isGrid, gridColor, gridSize, gridOffset, &isGridSnap);
ImGui::SameLine();
if (ImGui::BeginChild("##View Child", childSize, true, ImGuiWindowFlags_HorizontalScrollbar))
{
ImGui::InputFloat(localize.get(BASIC_ZOOM), &zoom, 0.0f, 0.0f, "%.0f%%");
tooltip_set(TOOLTIP_EDITOR_ZOOM);
view_buttons_draw(manager, settings, center_view, fit_view);
auto mousePosInt = IVec2(mousePos);
ImGui::TextUnformatted(localize_format(FORMAT_POSITION_SPACED, mousePosInt.x, mousePosInt.y).c_str());
}
ImGui::EndChild();
ImGui::SameLine();
if (ImGui::BeginChild("##Background Child", childSize, true, ImGuiWindowFlags_HorizontalScrollbar))
{
auto subChildSize = ImVec2(row_widget_width_get(2), ImGui::GetContentRegionAvail().y);
if (ImGui::BeginChild("##Background Child 1", subChildSize))
{
ImGui::BeginDisabled(isTransparent);
{
ImGui::ColorEdit3(localize.get(LABEL_BACKGROUND_COLOR), values_get(backgroundColor),
ImGuiColorEditFlags_NoInputs);
tooltip_set(TOOLTIP_BACKGROUND_COLOR);
}
ImGui::EndDisabled();
ImGui::Checkbox(localize.get(LABEL_BORDER), &isBorder);
tooltip_set(TOOLTIP_SPRITESHEET_BORDER);
}
ImGui::EndChild();
ImGui::SameLine();
if (ImGui::BeginChild("##Background Child 2", subChildSize))
{
ImGui::Checkbox(localize.get(LABEL_TRANSPARENT), &isTransparent);
tooltip_set(TOOLTIP_TRANSPARENT);
}
ImGui::EndChild();
}
ImGui::EndChild();
auto drawList = ImGui::GetCurrentWindow()->DrawList;
size_set(to_vec2(ImGui::GetContentRegionAvail()));
auto cursorScreenPos = ImGui::GetCursorScreenPos();
auto min = ImGui::GetCursorScreenPos();
auto max = to_imvec2(to_vec2(min) + size);
hoveredRegionId = -1;
if (is_canvas_hovered(min, max))
{
auto input = canvas_input_get(manager, isFocused);
auto isMouseClicked = input.isLeftClicked || input.isRightClicked;
auto isMouseDown = input.isLeftDown || input.isRightDown;
auto isBegin = isMouseClicked || input.isArrowBegin;
auto isDuring = isMouseDown || input.isArrowDown;
auto isEnd = input.isLeftReleased || input.isRightReleased || input.isArrowEnd;
auto arrow = input.arrow * input.step;
auto gridArrow = isGridSnap ? arrow * Vec2(gridSize) : arrow;
auto isArrow = arrow != Vec2();
auto selectedFrameReferences = document.frame_references_get();
std::erase_if(selectedFrameReferences,
[](const Reference& frameReference) { return frameReference.itemType != LAYER; });
auto editReference = reference;
if (!selectedFrameReferences.contains(reference) && !selectedFrameReferences.empty())
editReference = *selectedFrameReferences.begin();
auto frame = model.frame_get(editReference);
auto layer = item_get(model.content.layers, editReference.itemID);
bool isReferenceLayerOnSpritesheet =
frame && editReference.itemID > -1 && layer && layer->spritesheetId == referenceSpritesheet;
if (selectedFrameReferences.empty() && isReferenceLayerOnSpritesheet)
selectedFrameReferences.insert(editReference);
previousMousePos = mousePos;
mousePos = position_translate(zoom, pan, to_ivec2(ImGui::GetMousePos()) - to_ivec2(cursorScreenPos));
auto pivot_snap = [&](Vec2 value, Vec2 current) {
return isGridSnap ? Vec2(IVec2(value)) : vec_floor(value) + (current - vec_floor(current));
};
// Region edits apply to the given regions of the edited spritesheet when the command runs.
auto regions_update = [&](std::set<int> ids, std::function<void(Region&)> update) {
manager.command_push(
{manager.selected, [=, spritesheetId = referenceSpritesheet](Manager&, Document& document) {
auto spritesheet = item_get(document.model.content.spritesheets, spritesheetId);
for (auto id : ids)
if (auto region = spritesheet ? item_get(spritesheet->regions, id) : nullptr)
update(*region);
}});
};
auto region_pivot_update = [&](std::function<Vec2(Vec2)> pivot_get) {
regions_update({regionReference}, [=](Region& region) {
region.origin = Origin::CUSTOM;
region.pivot = pivot_get(region.pivot);
});
};
auto useTool = (Tool)tool;
if (input.isMiddleDown)
useTool = TOOL_PAN;
if (tool == TOOL_MOVE && input.isRightDown)
useTool = TOOL_CROP;
if (tool == TOOL_CROP && input.isRightDown)
useTool = TOOL_MOVE;
if (tool == TOOL_DRAW && input.isRightDown)
useTool = TOOL_ERASE;
if (tool == TOOL_ERASE && input.isRightDown)
useTool = TOOL_DRAW;
if (useTool == TOOL_PAN && spritesheet && texture && texture->is_valid())
for (auto& region : spritesheet->regions)
{
auto minPoint = vec_min(region.crop, region.crop + region.size);
auto maxPoint = vec_max(region.crop, region.crop + region.size);
if (is_all_less_equal(minPoint, mousePos) && is_all_less_equal(mousePos, maxPoint))
{
hoveredRegionId = region.id;
break;
}
}
// Crop and pivot edit regions while a region is the edit target, or a frame's region is in use.
auto isRegionEditTarget = document.editTarget == Document::EditTarget::REGION;
auto toolFrames = isRegionEditTarget ? std::set<Reference>{} : selectedFrameReferences;
bool isRegionInUse = !isRegionEditTarget && toolFrames.size() <= 1 && frame && frame->regionId != -1 &&
(useTool == TOOL_CROP || useTool == TOOL_MOVE);
auto isRegionTool = isRegionEditTarget || isRegionInUse || toolFrames.empty();
auto isFrameTool = frame && !toolFrames.empty() && !isRegionInUse;
bool isFrameAvailable = isFrameTool || (useTool == TOOL_CROP && !regionSelection.empty()) ||
(useTool == TOOL_MOVE && regionReference != -1);
auto frames_change = [&](FrameChange change, ChangeType type = ChangeType::ADJUST) {
frames_change_push(manager, toolFrames, change, type);
};
tool_cursor_update(useTool, TOOL_AREA_SPRITESHEET_EDITOR, isFrameAvailable, texture && texture->is_valid(),
isMouseDown || input.isArrowDown,
isRegionInUse ? TEXT_REGION_IN_USE
: useTool == TOOL_CROP ? TEXT_SELECT_FRAME_OR_REGION
: TEXT_SELECT_FRAME);
if (useTool == TOOL_PAN)
{
if (input.isLeftClicked && hoveredRegionId != -1)
{
document.editTarget = Document::EditTarget::REGION;
regionReference = hoveredRegionId;
regionSelection = {hoveredRegionId};
document.selected_ids_set(SelectionKind::REGIONS, regionSelection);
document.focused_id_set(SelectionKind::REGIONS, hoveredRegionId);
if (auto region = region_get(hoveredRegionId); region && isReferenceLayerOnSpritesheet)
{
edit_begin_push(manager, EDIT_FRAME_REGION);
frames_change_push(manager, selectedFrameReferences,
{.regionId = hoveredRegionId,
.cropX = region->crop.x,
.cropY = region->crop.y,
.sizeX = region->size.x,
.sizeY = region->size.y,
.pivotX = region->pivot.x,
.pivotY = region->pivot.y});
document_change_push(manager);
}
}
if (isMouseDown || input.isMiddleDown)
pan += input.mouseDelta;
}
auto region = isRegionTool ? region_get(regionReference) : nullptr;
if (useTool == TOOL_MOVE && region)
{
if (isBegin)
edit_begin_push(manager, EDIT_REGION_MOVE);
if (isMouseDown)
region_pivot_update(
[pivot = pivot_snap(mousePos - region->crop, region->pivot)](Vec2) { return pivot; });
if (isArrow)
region_pivot_update(
[=, this](Vec2 pivot) { return isGridSnap ? Vec2(IVec2(pivot + arrow)) : pivot + arrow; });
if (isDuring)
tooltip_lines_draw({localize_format(FORMAT_PIVOT, region->pivot.x, region->pivot.y)});
if (isEnd)
document_change_push(manager);
}
else if (useTool == TOOL_MOVE && isFrameTool)
{
if (isBegin)
edit_begin_push(manager, EDIT_FRAME_PIVOT);
if (isMouseDown)
{
auto pivot = pivot_snap(mousePos - frame->crop, frame->pivot);
frames_change({.regionId = -1, .pivotX = pivot.x, .pivotY = pivot.y});
}
if (isArrow)
frames_change({.regionId = -1, .pivotX = arrow.x, .pivotY = arrow.y}, ChangeType::ADD);
if (isDuring)
tooltip_lines_draw({localize_format(FORMAT_PIVOT, frame->pivot.x, frame->pivot.y)});
if (isEnd)
document_change_push(manager);
}
auto isRegionCrop = isRegionTool && !regionSelection.empty() && spritesheet;
if (useTool == TOOL_CROP && (isRegionCrop || isFrameTool))
{
if (isBegin)
edit_begin_push(manager, isRegionCrop ? EDIT_REGION_CROP : EDIT_FRAME_CROP);
auto rect_set = [&](Vec2 crop, Vec2 cropSize) {
if (isRegionCrop)
regions_update(regionSelection, [=](Region& region) {
region.crop = crop;
region.size = cropSize;
});
else
frames_change({.regionId = -1,
.cropX = crop.x,
.cropY = crop.y,
.sizeX = cropSize.x,
.sizeY = cropSize.y});
};
if (isMouseClicked)
{
cropAnchor = mousePos;
auto crop = Vec2(IVec2(cropAnchor));
if (isRegionCrop)
rect_set(crop, Vec2());
else
frames_change({.regionId = -1, .cropX = crop.x, .cropY = crop.y});
}
if (isMouseDown)
{
auto [minPoint, maxPoint] = rect_snap(cropAnchor, mousePos, isGridSnap, gridSize, gridOffset);
rect_set(minPoint, maxPoint - minPoint);
}
if (isArrow && isRegionCrop)
regions_update(regionSelection, [=](Region& region) {
region.crop = Vec2(IVec2(region.crop + gridArrow));
region.size = Vec2(IVec2(region.size));
});
else if (isArrow)
frames_change({.regionId = -1, .cropX = gridArrow.x, .cropY = gridArrow.y}, ChangeType::ADD);
// Arrow nudges are snapped (and the rectangle normalized) when the command runs, after the nudge.
if (isDuring && !isMouseDown && isRegionCrop)
regions_update(regionSelection, [=, this](Region& region) {
auto [minPoint, maxPoint] =
rect_snap(region.crop, region.crop + region.size, isGridSnap, gridSize, gridOffset);
region.crop = minPoint;
region.size = maxPoint - minPoint;
});
else if (isDuring && !isMouseDown)
manager.command_push({manager.selected, [=, this](Manager&, Document& document) {
auto source = document.model.frame_get(editReference);
if (!source)
return;
auto [minPoint, maxPoint] =
rect_snap(source->crop, source->crop + source->size, isGridSnap,
gridSize, gridOffset);
frames_change_apply(document.model, {.references = toolFrames},
{.regionId = -1,
.cropX = minPoint.x,
.cropY = minPoint.y,
.sizeX = maxPoint.x - minPoint.x,
.sizeY = maxPoint.y - minPoint.y},
ChangeType::ADJUST);
}});
auto shown = isRegionCrop ? region_get(*regionSelection.begin()) : nullptr;
auto crop = shown ? shown->crop : frame ? frame->crop : Vec2();
auto cropSize = shown ? shown->size : frame ? frame->size : Vec2();
if (isDuring && (shown || !isRegionCrop))
tooltip_lines_draw({localize_format(FORMAT_CROP, crop.x, crop.y),
localize_format(FORMAT_SIZE, cropSize.x, cropSize.y)});
if (isEnd)
document_change_push(manager);
}
auto isDrawTool = useTool == TOOL_DRAW || useTool == TOOL_ERASE;
if (isDrawTool && texture && texture->is_vector())
{
ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
if (isMouseDown)
tooltip_lines_draw({localize.get(TEXT_SVG_NOT_EDITABLE)});
}
else if (isDrawTool && texture)
{
if (isMouseClicked)
edit_begin_push(manager, useTool == TOOL_DRAW ? EDIT_DRAW : EDIT_ERASE);
if (isMouseDown)
manager.command_push(
{manager.selected, [start = IVec2(previousMousePos), end = IVec2(mousePos),
color = useTool == TOOL_DRAW ? toolColor : Vec4(),
spritesheetId = referenceSpritesheet](Manager&, Document& document) {
if (auto texture = document.texture_edit(spritesheetId))
texture->pixel_line(start, end, color);
}});
if (input.isLeftReleased || input.isRightReleased)
manager.command_push(
{manager.selected, [spritesheetId = referenceSpritesheet](Manager&, Document& document) {
document.texture_change(spritesheetId);
}});
}
if (useTool == TOOL_COLOR_PICKER && texture && isDuring)
{
toolColor = texture->pixel_read(mousePos);
if (ImGui::BeginTooltip())
{
ImGui::ColorButton("##Color Picker Button", to_imvec4(toolColor));
ImGui::SameLine();
auto rgba8 = vec_clamp(IVec4(toolColor * 255.0f + 0.5f), IVec4(0), IVec4(255));
ImGui::TextUnformatted(
std::format("#{:02X}{:02X}{:02X}{:02X}", rgba8.r, rgba8.g, rgba8.b, rgba8.a).c_str());
ImGui::SameLine();
ImGui::Text("(%d, %d, %d, %d)", rgba8.r, rgba8.g, rgba8.b, rgba8.a);
ImGui::EndTooltip();
}
}
if (tool == TOOL_PAN && hoveredRegionId != -1 && spritesheet)
if (auto region = region_get(hoveredRegionId); region && ImGui::BeginTooltip())
{
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(region->name.c_str());
ImGui::PopFont();
auto originLabel = localize.get(region->origin == Origin::TOP_LEFT ? LABEL_REGION_ORIGIN_TOP_LEFT
: LABEL_REGION_ORIGIN_CENTER);
for (auto& line : {localize_format(FORMAT_ID, hoveredRegionId),
localize_format(FORMAT_CROP, region->crop.x, region->crop.y),
localize_format(FORMAT_SIZE, region->size.x, region->size.y),
region->origin == Origin::CUSTOM
? localize_format(FORMAT_PIVOT, region->pivot.x, region->pivot.y)
: localize_format(FORMAT_ORIGIN, originLabel)})
ImGui::TextUnformatted(line.c_str());
ImGui::EndTooltip();
}
if (input.wheel != 0 || input.isZoomIn || input.isZoomOut)
{
auto focus = input.wheel != 0 ? Vec2(mousePos) : texture ? Vec2(texture->size) / 2.0f : Vec2();
zoom_step(zoom, pan, focus, (input.wheel > 0 || input.isZoomIn) ? ZOOM_LEVEL_STEP : -ZOOM_LEVEL_STEP);
}
}
bind();
viewport_set();
clear(isTransparent ? Vec4(0) : Vec4(backgroundColor, 1.0f));
auto frame = model.frame_get(reference);
auto viewTexture = baseTexture && baseTexture->is_valid() ? baseTexture : texture;
if (spritesheet && viewTexture && viewTexture->is_valid())
{
auto transform = transform_get(zoom, pan);
auto spritesheetModel = quad_model_get(viewTexture->size);
auto spritesheetTransform = transform * spritesheetModel;
if (baseTexture && baseTexture->is_valid())
texture_render(shaderTexture,
texture_id_get(*document.vector_image_get(baseTexture, percent_to_unit(zoom))),
spritesheetTransform);
if (isGrid)
grid_render(shaderGrid, zoom, pan, gridSize, gridOffset, gridColor);
if (isBorder)
rect_render(dashedShader, spritesheetTransform, spritesheetModel, COLOR_WHITE, BORDER_DASH_LENGTH,
BORDER_DASH_GAP, BORDER_DASH_OFFSET);
if (hoveredRegionId != -1)
{
if (auto region = region_get(hoveredRegionId))
{
auto cropModel = quad_model_get(region->size, region->crop);
auto cropTransform = transform * cropModel;
rect_fill_render(shaderLine, cropTransform, cropModel, Vec4(1.0f, 1.0f, 1.0f, 0.5f));
}
}
auto layer = item_get(model.content.layers, reference.itemID);
bool isReferenceLayerOnSpritesheet =
frame && reference.itemID > -1 && layer && layer->spritesheetId == referenceSpritesheet;
int highlightedRegionId = -1;
if (document.editTarget == Document::EditTarget::REGION && regionReference != -1 &&
region_get(regionReference))
{
highlightedRegionId = regionReference;
}
else if (isReferenceLayerOnSpritesheet && frame->regionId != -1 && region_get(frame->regionId))
{
highlightedRegionId = frame->regionId;
}
else if (regionReference != -1 && region_get(regionReference))
{
highlightedRegionId = regionReference;
}
auto draw_region_rect = [&](const Region& region, Vec4 regionColor) {
auto cropModel = quad_model_get(region.size, region.crop);
auto cropTransform = transform * cropModel;
rect_render(dashedShader, cropTransform, cropModel, regionColor, BORDER_DASH_LENGTH, BORDER_DASH_GAP,
BORDER_DASH_OFFSET);
};
for (auto& region : spritesheet->regions)
{
auto id = region.id;
if (id == highlightedRegionId)
continue;
draw_region_rect(region, COLOR_WHITE);
auto pivotTransform =
transform * quad_model_get(PIVOT_SIZE, region.crop + region.pivot, PIVOT_SIZE * 0.5f);
texture_render(shaderTexture, resources.icon_id_get(ICON_PIVOT), pivotTransform, COLOR_WHITE);
}
if (highlightedRegionId != -1)
{
if (auto region = region_get(highlightedRegionId))
{
draw_region_rect(*region, COLOR_RED);
auto pivotTransform =
transform * quad_model_get(PIVOT_SIZE, region->crop + region->pivot, PIVOT_SIZE * 0.5f);
texture_render(shaderTexture, resources.icon_id_get(ICON_PIVOT), pivotTransform, PIVOT_COLOR);
}
}
bool isFrameOnSpritesheet = isReferenceLayerOnSpritesheet;
if (isFrameOnSpritesheet && frame->regionId == -1)
{
auto frameModel = quad_model_get(frame->size, frame->crop);
auto frameTransform = transform * frameModel;
rect_render(shaderLine, frameTransform, frameModel, COLOR_RED);
auto pivotTransform =
transform * quad_model_get(PIVOT_SIZE, frame->crop + frame->pivot, PIVOT_SIZE * 0.5f);
texture_render(shaderTexture, resources.icon_id_get(ICON_PIVOT), pivotTransform, PIVOT_COLOR);
}
}
unbind();
checker_pan_sync(zoom, pan);
if (isTransparent)
render_checker_background(drawList, min, max, -size * 0.5f - checkerPan, CHECKER_SIZE);
else
drawList->AddRectFilled(min, max, ImGui::GetColorU32(to_imvec4(Vec4(backgroundColor, 1.0f))));
image_premultiplied_draw(this->texture, to_imvec2(size));
}
if (tool == TOOL_PAN)
view_context_menu_draw("##Spritesheet Editor Context Menu", manager, settings, document, zoom, pan, center_view,
fit_view, (baseTexture && baseTexture->is_valid()) || (texture && texture->is_valid()));
if (!document.isSpritesheetEditorSet)
{
size = settings.editorSize;
zoom = settings.editorStartZoom;
set();
center_view();
checker_pan_reset(zoom, pan);
document.isSpritesheetEditorSet = true;
}
settings.editorSize = size;
settings.editorStartZoom = zoom;
ImGui::End();
}