From 595658581f0b86042a95238ad051fdb12f6e31c7 Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 3 Oct 2026 18:08:57 -0400 Subject: [PATCH] Stage 9a: shared canvas input, edit helpers and tool requirements - CanvasInput gathers mouse, arrow keys (as a step vector), zoom and modifier state once; arrow nudges are one change per tool instead of four. - frames_change_push/edit_begin_push/document_change_push replace each editor's own grouped frame-change command; rect_snap replaces three copies of grid-snap code. - tool::INFO carries whether a tool needs a frame or a spritesheet; tool_cursor_update sets the cursor and the "why not" hint for both canvases. - localize_format for localized format strings. Co-Authored-By: Claude Opus 5.5 --- src/imgui/window/animation_preview.cpp | 414 +++--------- src/imgui/window/canvas_view.cpp | 95 +++ src/imgui/window/canvas_view.hpp | 35 + src/imgui/window/spritesheet_editor.cpp | 814 +++++++----------------- src/resource/strings.hpp | 12 + src/tool.hpp | 25 +- 6 files changed, 479 insertions(+), 916 deletions(-) diff --git a/src/imgui/window/animation_preview.cpp b/src/imgui/window/animation_preview.cpp index de90a84..1d23410 100644 --- a/src/imgui/window/animation_preview.cpp +++ b/src/imgui/window/animation_preview.cpp @@ -1182,344 +1182,138 @@ namespace anm2ed::imgui if (isPreviewHovered) { - auto isMouseClicked = ImGui::IsMouseClicked(ImGuiMouseButton_Left); - auto isMouseReleased = ImGui::IsMouseReleased(ImGuiMouseButton_Left); - auto isMouseLeftDown = ImGui::IsMouseDown(ImGuiMouseButton_Left); - auto isMouseMiddleDown = ImGui::IsMouseDown(ImGuiMouseButton_Middle); - auto isMouseRightDown = ImGui::IsMouseDown(ImGuiMouseButton_Right); - auto isMouseRightClicked = ImGui::IsMouseClicked(ImGuiMouseButton_Right); - auto isMouseRightReleased = ImGui::IsMouseReleased(ImGuiMouseButton_Right); - auto isMouseDown = isMouseLeftDown || isMouseMiddleDown || isMouseRightDown; - auto mouseDelta = to_ivec2(ImGui::GetIO().MouseDelta); - auto mouseWheel = ImGui::GetIO().MouseWheel; - - auto isLeftJustPressed = ImGui::IsKeyPressed(ImGuiKey_LeftArrow, false); - auto isRightJustPressed = ImGui::IsKeyPressed(ImGuiKey_RightArrow, false); - auto isUpJustPressed = ImGui::IsKeyPressed(ImGuiKey_UpArrow, false); - auto isDownJustPressed = ImGui::IsKeyPressed(ImGuiKey_DownArrow, false); - auto isLeftPressed = ImGui::IsKeyPressed(ImGuiKey_LeftArrow); - auto isRightPressed = ImGui::IsKeyPressed(ImGuiKey_RightArrow); - auto isUpPressed = ImGui::IsKeyPressed(ImGuiKey_UpArrow); - auto isDownPressed = ImGui::IsKeyPressed(ImGuiKey_DownArrow); - auto isLeftDown = ImGui::IsKeyDown(ImGuiKey_LeftArrow); - auto isRightDown = ImGui::IsKeyDown(ImGuiKey_RightArrow); - auto isUpDown = ImGui::IsKeyDown(ImGuiKey_UpArrow); - auto isDownDown = ImGui::IsKeyDown(ImGuiKey_DownArrow); - auto isLeftReleased = ImGui::IsKeyReleased(ImGuiKey_LeftArrow); - auto isRightReleased = ImGui::IsKeyReleased(ImGuiKey_RightArrow); - auto isUpReleased = ImGui::IsKeyReleased(ImGuiKey_UpArrow); - auto isDownReleased = ImGui::IsKeyReleased(ImGuiKey_DownArrow); - auto isKeyJustPressed = isLeftJustPressed || isRightJustPressed || isUpJustPressed || isDownJustPressed; - auto isKeyDown = isLeftDown || isRightDown || isUpDown || isDownDown; - auto isKeyReleased = isLeftReleased || isRightReleased || isUpReleased || isDownReleased; - - auto isZoomIn = isFocused && shortcut(manager.chords[SHORTCUT_ZOOM_IN], shortcut::GLOBAL); - auto isZoomOut = isFocused && shortcut(manager.chords[SHORTCUT_ZOOM_OUT], shortcut::GLOBAL); - - auto isMod = ImGui::IsKeyDown(ImGuiMod_Shift); - auto useTool = tool; - auto step = (float)(isMod ? STEP_FAST : STEP); + auto input = canvas_input_get(manager, isFocused); mousePos = position_translate(zoom, pan, to_vec2(ImGui::GetMousePos()) - to_vec2(cursorScreenPos)); auto selectedFrameReferences = document.frame_references_get(); std::erase_if(selectedFrameReferences, [](const Reference& frameReference) { return frameReference.itemType == NONE || frameReference.itemType == TRIGGER; }); auto editReference = reference; - auto editItemType = referenceItemType; if ((referenceItemType == ItemType::TRIGGER || !selectedFrameReferences.contains(reference)) && !selectedFrameReferences.empty()) - { editReference = *selectedFrameReferences.begin(); - editItemType = static_cast(editReference.itemType); - } - auto frame = model.frame_get(editReference); - auto item = model.track_get(editReference); + auto frame = selectedFrameReferences.empty() ? nullptr : model.frame_get(editReference); auto selectedNull = - editItemType == ItemType::NULL_ ? model::item_get(model.content.nulls, editReference.itemID) : nullptr; + editReference.itemType == NULL_ ? model::item_get(model.content.nulls, editReference.itemID) : nullptr; bool isSelectedNullRect = selectedNull && selectedNull->isShowRect; auto null_rect_top_left = [](const model::Frame& frame) { return frame.position - (frame.scale * 0.5f); }; - if (isMouseMiddleDown) useTool = tool::PAN; - if (tool == tool::MOVE && isMouseRightDown) useTool = tool::SCALE; - if (tool == tool::SCALE && isMouseRightDown) useTool = tool::MOVE; + auto useTool = (tool::Type)tool; + if (input.isMiddleDown) useTool = tool::PAN; + if (tool == tool::MOVE && input.isRightDown) useTool = tool::SCALE; + if (tool == tool::SCALE && input.isRightDown) useTool = tool::MOVE; - bool isToolMouseClicked = isMouseClicked; - bool isToolMouseReleased = isMouseReleased; - bool isToolMouseDown = isMouseLeftDown; + // A tool swapped in by the right button is driven by the right button. + auto isSwapped = useTool != tool && useTool != tool::PAN; + auto isToolClicked = isSwapped ? input.isRightClicked : input.isLeftClicked; + auto isToolDown = isSwapped ? input.isRightDown : input.isLeftDown; + auto isToolReleased = isSwapped ? input.isRightReleased : input.isLeftReleased; + auto isBegin = isToolClicked || input.isArrowBegin; + auto isDuring = isToolDown || input.isArrowDown; + auto isEnd = isToolReleased || input.isArrowEnd; + auto arrow = input.arrow * input.step; + auto isArrow = arrow != vec2(); - if ((tool == tool::MOVE && useTool == tool::SCALE) || (tool == tool::SCALE && useTool == tool::MOVE)) + // Changes apply to every selected frame; absolute values are applied as the difference from the edited frame. + auto frames_change = [&](FrameChange change, ChangeType type = ChangeType::ADD) + { frames_change_push(manager, selectedFrameReferences, change, type); }; + auto null_rect_set = [&](vec2 minPoint, vec2 maxPoint) { - isToolMouseClicked = isMouseRightClicked; - isToolMouseReleased = isMouseRightReleased; - isToolMouseDown = isMouseRightDown; - } - - auto isToolBegin = isToolMouseClicked || isKeyJustPressed; - auto isToolDuring = isToolMouseDown || isKeyDown; - auto isToolEnd = isToolMouseReleased || isKeyReleased; - - auto frame_snapshot = [&](StringType label) - { - manager.command_push( - {manager.selected, [label](Manager&, Document& document) { document.edit_begin(label); }}); - }; - auto frame_change_apply = [&](FrameChange frameChange, ChangeType changeType = ChangeType::ADJUST) - { - auto queuedFrameReferences = selectedFrameReferences; - manager.command_push({manager.selected, [=](Manager&, Document& document) - { - std::map> groupedFrames{}; - for (auto frameReference : queuedFrameReferences) - { - auto itemReference = frameReference; - itemReference.frameIndex = -1; - groupedFrames[itemReference].insert(frameReference.frameIndex); - } - - for (auto& [itemReference, itemFrames] : groupedFrames) - { - auto itemType = static_cast(itemReference.itemType); - if (auto item = document.model.track_edit(itemReference)) - model::frames_change(*item, frameChange, itemType, changeType, itemFrames); - } - }}); - }; - auto frame_position_apply = [&](vec2 position) - { - if (!frame) return; - frame_change_apply({.positionX = position.x - frame->position.x, .positionY = position.y - frame->position.y}, - ChangeType::ADD); - }; - auto frame_scale_apply = [&](vec2 scale) - { - if (!frame) return; - frame_change_apply({.scaleX = scale.x - frame->scale.x, .scaleY = scale.y - frame->scale.y}, ChangeType::ADD); - }; - auto frame_shear_apply = [&](vec2 shear) - { - if (!frame) return; - frame_change_apply({.shearX = shear.x - frame->shear.x, .shearY = shear.y - frame->shear.y}, ChangeType::ADD); - }; - auto frames_changed = [&]() - { manager.command_push({manager.selected, [](Manager&, Document& document) { document.change(); }}); }; - auto null_rect_change = [&](vec2 topLeft, vec2 rectSize) - { - topLeft = vec2(ivec2(topLeft)); - rectSize = vec2(ivec2(rectSize)); - frame_change_apply({.positionX = topLeft.x + rectSize.x * 0.5f - frame->position.x, - .positionY = topLeft.y + rectSize.y * 0.5f - frame->position.y, - .scaleX = rectSize.x - frame->scale.x, - .scaleY = rectSize.y - frame->scale.y}, - ChangeType::ADD); + auto rectSize = maxPoint - minPoint; + frames_change({.positionX = minPoint.x + rectSize.x * 0.5f - frame->position.x, + .positionY = minPoint.y + rectSize.y * 0.5f - frame->position.y, + .scaleX = rectSize.x - frame->scale.x, + .scaleY = rectSize.y - frame->scale.y}); }; - auto& toolInfo = tool::INFO[useTool]; - auto& areaType = toolInfo.areaType; - bool isAreaAllowed = areaType == tool::ALL || areaType == tool::ANIMATION_PREVIEW; - bool isFrameRequired = - !(useTool == tool::PAN || useTool == tool::DRAW || useTool == tool::ERASE || useTool == tool::COLOR_PICKER); - bool isFrameAvailable = !isFrameRequired || frame; - auto cursor = (isAreaAllowed && isFrameAvailable) ? toolInfo.cursor : ImGuiMouseCursor_NotAllowed; - ImGui::SetMouseCursor(cursor); - ImGui::SetKeyboardFocusHere(); + tool_cursor_update(useTool, tool::ANIMATION_PREVIEW, frame != nullptr, true, + input.isLeftDown || input.isRightDown || input.isMiddleDown || input.isArrowDown, + TEXT_SELECT_FRAME); if (useTool != tool::MOVE) isMoveDragging = false; - switch (useTool) + if (useTool == tool::PAN && (input.isLeftDown || input.isRightDown || input.isMiddleDown)) + pan += input.mouseDelta; + + if (frame && useTool == tool::MOVE) { - case tool::PAN: - if (isMouseDown || isMouseMiddleDown) pan += vec2(mouseDelta.x, mouseDelta.y); - break; - case tool::MOVE: - if (!item || !frame || selectedFrameReferences.empty()) break; - if (isToolBegin) - { - frame_snapshot(EDIT_FRAME_POSITION); - if (isToolMouseClicked) - { - auto origin = isSelectedNullRect ? null_rect_top_left(*frame) : frame->position; - moveOffset = settings.inputIsMoveToolSnapToMouse ? vec2() : mousePos - origin; - isMoveDragging = true; - } - } - if (isToolMouseDown && isMoveDragging) - { - auto position = mousePos - moveOffset; - if (isSelectedNullRect) - frame_position_apply(vec2((float)(int)(position.x + frame->scale.x * 0.5f), - (float)(int)(position.y + frame->scale.y * 0.5f))); - else - frame_position_apply(vec2((float)(int)position.x, (float)(int)position.y)); - } - - if (isLeftPressed) frame_change_apply({.positionX = step}, ChangeType::SUBTRACT); - if (isRightPressed) frame_change_apply({.positionX = step}, ChangeType::ADD); - if (isUpPressed) frame_change_apply({.positionY = step}, ChangeType::SUBTRACT); - if (isDownPressed) frame_change_apply({.positionY = step}, ChangeType::ADD); - - if (isToolMouseReleased) isMoveDragging = false; - if (isToolEnd) frames_changed(); - if (isToolDuring) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted(std::vformat(localize.get(FORMAT_POSITION), - std::make_format_args(frame->position.x, frame->position.y)) - .c_str()); - ImGui::EndTooltip(); - } - } - break; - case tool::SCALE: - if (!item || !frame || selectedFrameReferences.empty()) break; - if (isToolBegin) - { - frame_snapshot(EDIT_FRAME_SCALE); - if (isToolMouseClicked && isSelectedNullRect) nullRectScaleAnchor = null_rect_top_left(*frame); - } - if (isToolMouseDown) - { - if (isSelectedNullRect) - { - auto size = mousePos - nullRectScaleAnchor; - if (isMod) - { - auto squareSize = std::max(std::abs(size.x), std::abs(size.y)); - size = {std::copysign(squareSize, size.x), std::copysign(squareSize, size.y)}; - } - - auto minPoint = glm::min(nullRectScaleAnchor, nullRectScaleAnchor + size); - auto maxPoint = glm::max(nullRectScaleAnchor, nullRectScaleAnchor + size); - minPoint = vec2(ivec2(minPoint)); - maxPoint = vec2(ivec2(maxPoint)); - null_rect_change(minPoint, maxPoint - minPoint); - } - else - { - auto scale = frame->scale + vec2(mouseDelta.x, mouseDelta.y); - if (isMod) scale = {scale.x, scale.x}; - frame_scale_apply(scale); - } - } - - if (isSelectedNullRect) - { - if (isLeftPressed) frame_change_apply({.positionX = step * 0.5f, .scaleX = step}, ChangeType::SUBTRACT); - if (isRightPressed) frame_change_apply({.positionX = step * 0.5f, .scaleX = step}, ChangeType::ADD); - if (isUpPressed) frame_change_apply({.positionY = step * 0.5f, .scaleY = step}, ChangeType::SUBTRACT); - if (isDownPressed) frame_change_apply({.positionY = step * 0.5f, .scaleY = step}, ChangeType::ADD); - } - else - { - if (isLeftPressed) frame_change_apply({.scaleX = step}, ChangeType::SUBTRACT); - if (isRightPressed) frame_change_apply({.scaleX = step}, ChangeType::ADD); - if (isUpPressed) frame_change_apply({.scaleY = step}, ChangeType::SUBTRACT); - if (isDownPressed) frame_change_apply({.scaleY = step}, ChangeType::ADD); - } - - if (isToolDuring) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_SCALE), std::make_format_args(frame->scale.x, frame->scale.y)) - .c_str()); - ImGui::EndTooltip(); - } - } - - if (isToolEnd) - { - if (isSelectedNullRect) - { - auto topLeft = null_rect_top_left(*frame); - auto minPoint = glm::min(topLeft, topLeft + frame->scale); - auto maxPoint = glm::max(topLeft, topLeft + frame->scale); - minPoint = vec2(ivec2(minPoint)); - maxPoint = vec2(ivec2(maxPoint)); - null_rect_change(minPoint, maxPoint - minPoint); - } - frames_changed(); - } - break; - case tool::ROTATE: - if (!item || !frame || selectedFrameReferences.empty()) break; - if (isToolBegin) frame_snapshot(EDIT_FRAME_ROTATION); - if (isToolMouseDown) frame_change_apply({.rotation = (float)(int)mouseDelta.x}, ChangeType::ADD); - if (isLeftPressed || isDownPressed) frame_change_apply({.rotation = step}, ChangeType::SUBTRACT); - if (isUpPressed || isRightPressed) frame_change_apply({.rotation = step}, ChangeType::ADD); - - if (isToolDuring) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_ROTATION), std::make_format_args(frame->rotation)).c_str()); - ImGui::EndTooltip(); - } - } - - if (isToolEnd) frames_changed(); - break; - case tool::SHEAR: - if (!item || !frame || selectedFrameReferences.empty()) break; - if (isToolBegin) frame_snapshot(EDIT_FRAME_SHEAR); - if (isToolMouseDown) - { - auto shear = frame->shear + vec2(mouseDelta.x, mouseDelta.y); - if (isMod) - { - if (std::abs(mouseDelta.x) >= std::abs(mouseDelta.y)) - shear.y = frame->shear.y; - else - shear.x = frame->shear.x; - } - frame_shear_apply(shear); - } - if (isLeftPressed) frame_change_apply({.shearX = step}, ChangeType::SUBTRACT); - if (isRightPressed) frame_change_apply({.shearX = step}, ChangeType::ADD); - if (isUpPressed) frame_change_apply({.shearY = step}, ChangeType::SUBTRACT); - if (isDownPressed) frame_change_apply({.shearY = step}, ChangeType::ADD); - - if (isToolDuring) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_SHEAR), std::make_format_args(frame->shear.x, frame->shear.y)) - .c_str()); - ImGui::EndTooltip(); - } - } - - if (isToolEnd) frames_changed(); - break; - default: - break; - } - - if ((isMouseDown || isKeyDown) && useTool != tool::PAN) - { - if (!isAreaAllowed && areaType == tool::SPRITESHEET_EDITOR) + if (isBegin) edit_begin_push(manager, EDIT_FRAME_POSITION); + if (isToolClicked) { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted(localize.get(TEXT_TOOL_SPRITESHEET_EDITOR)); - ImGui::EndTooltip(); - } + auto origin = isSelectedNullRect ? null_rect_top_left(*frame) : frame->position; + moveOffset = settings.inputIsMoveToolSnapToMouse ? vec2() : mousePos - origin; + isMoveDragging = true; } - else if (isFrameRequired && !isFrameAvailable) + if (isToolDown && isMoveDragging) { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted(localize.get(TEXT_SELECT_FRAME)); - ImGui::EndTooltip(); - } + auto position = vec2(ivec2(mousePos - moveOffset + (isSelectedNullRect ? frame->scale * 0.5f : vec2()))); + frames_change({.positionX = position.x - frame->position.x, .positionY = position.y - frame->position.y}); } + if (isArrow) frames_change({.positionX = arrow.x, .positionY = arrow.y}); + if (isToolReleased) isMoveDragging = false; + if (isDuring) tooltip_lines_draw({localize_format(FORMAT_POSITION, frame->position.x, frame->position.y)}); + if (isEnd) document_change_push(manager); } - if (mouseWheel != 0 || isZoomIn || isZoomOut) + if (frame && useTool == tool::SCALE) { - zoom_step(zoom, pan, vec2(mousePos), (mouseWheel > 0 || isZoomIn) ? ZOOM_LEVEL_STEP : -ZOOM_LEVEL_STEP); + if (isBegin) edit_begin_push(manager, EDIT_FRAME_SCALE); + if (isToolClicked && isSelectedNullRect) nullRectScaleAnchor = null_rect_top_left(*frame); + if (isToolDown && isSelectedNullRect) + { + auto rectSize = mousePos - nullRectScaleAnchor; + if (input.isMod) + { + auto squareSize = std::max(std::abs(rectSize.x), std::abs(rectSize.y)); + rectSize = {std::copysign(squareSize, rectSize.x), std::copysign(squareSize, rectSize.y)}; + } + auto [minPoint, maxPoint] = rect_snap(nullRectScaleAnchor, nullRectScaleAnchor + rectSize, false, {}, {}); + null_rect_set(minPoint, maxPoint); + } + else if (isToolDown) + frames_change( + {.scaleX = input.mouseDelta.x, + .scaleY = input.isMod ? frame->scale.x + input.mouseDelta.x - frame->scale.y : input.mouseDelta.y}); + if (isArrow) + frames_change({.positionX = isSelectedNullRect ? arrow.x * 0.5f : 0.0f, + .positionY = isSelectedNullRect ? arrow.y * 0.5f : 0.0f, + .scaleX = arrow.x, + .scaleY = arrow.y}); + if (isDuring) tooltip_lines_draw({localize_format(FORMAT_SCALE, frame->scale.x, frame->scale.y)}); + if (isEnd && isSelectedNullRect) + { + auto topLeft = null_rect_top_left(*frame); + auto [minPoint, maxPoint] = rect_snap(topLeft, topLeft + frame->scale, false, {}, {}); + null_rect_set(minPoint, maxPoint); + } + if (isEnd) document_change_push(manager); } + + if (frame && useTool == tool::ROTATE) + { + if (isBegin) edit_begin_push(manager, EDIT_FRAME_ROTATION); + if (isToolDown) frames_change({.rotation = input.mouseDelta.x}); + if (auto turn = glm::sign(input.arrow.x - input.arrow.y); turn != 0.0f) + frames_change({.rotation = turn * input.step}); + if (isDuring) tooltip_lines_draw({localize_format(FORMAT_ROTATION, frame->rotation)}); + if (isEnd) document_change_push(manager); + } + + if (frame && useTool == tool::SHEAR) + { + if (isBegin) edit_begin_push(manager, EDIT_FRAME_SHEAR); + if (isToolDown) + { + auto delta = input.mouseDelta; + if (input.isMod) (std::abs(delta.x) >= std::abs(delta.y) ? delta.y : delta.x) = 0.0f; + frames_change({.shearX = delta.x, .shearY = delta.y}); + } + if (isArrow) frames_change({.shearX = arrow.x, .shearY = arrow.y}); + if (isDuring) tooltip_lines_draw({localize_format(FORMAT_SHEAR, frame->shear.x, frame->shear.y)}); + if (isEnd) document_change_push(manager); + } + + if (input.wheel != 0 || input.isZoomIn || input.isZoomOut) + zoom_step(zoom, pan, vec2(mousePos), + (input.wheel > 0 || input.isZoomIn) ? ZOOM_LEVEL_STEP : -ZOOM_LEVEL_STEP); } } diff --git a/src/imgui/window/canvas_view.cpp b/src/imgui/window/canvas_view.cpp index 62b3c70..9132278 100644 --- a/src/imgui/window/canvas_view.cpp +++ b/src/imgui/window/canvas_view.cpp @@ -3,15 +3,110 @@ #include #include "actions.hpp" +#include "edit/edit.hpp" +#include "util/imgui/constants.hpp" #include "util/imgui/input.hpp" #include "util/imgui/layout.hpp" #include "util/imgui/shortcut.hpp" #include "util/imgui/tooltip.hpp" +using namespace anm2ed::types; +using namespace anm2ed::util; using namespace glm; namespace anm2ed::imgui { + CanvasInput canvas_input_get(Manager& manager, bool isFocused) + { + auto key_axis_get = [](ImGuiKey negative, ImGuiKey positive) + { return (float)ImGui::IsKeyPressed(positive) - (float)ImGui::IsKeyPressed(negative); }; + auto is_any = [](auto&& is_key) + { + return is_key(ImGuiKey_LeftArrow) || is_key(ImGuiKey_RightArrow) || is_key(ImGuiKey_UpArrow) || + is_key(ImGuiKey_DownArrow); + }; + auto& io = ImGui::GetIO(); + auto isMod = ImGui::IsKeyDown(ImGuiMod_Shift); + return {.arrow = {key_axis_get(ImGuiKey_LeftArrow, ImGuiKey_RightArrow), + key_axis_get(ImGuiKey_UpArrow, ImGuiKey_DownArrow)}, + .mouseDelta = vec2(ivec2(to_vec2(io.MouseDelta))), + .wheel = io.MouseWheel, + .step = (float)(isMod ? STEP_FAST : STEP), + .isArrowBegin = is_any([](ImGuiKey key) { return ImGui::IsKeyPressed(key, false); }), + .isArrowDown = is_any([](ImGuiKey key) { return ImGui::IsKeyDown(key); }), + .isArrowEnd = is_any([](ImGuiKey key) { return ImGui::IsKeyReleased(key); }), + .isLeftClicked = ImGui::IsMouseClicked(ImGuiMouseButton_Left), + .isLeftDown = ImGui::IsMouseDown(ImGuiMouseButton_Left), + .isLeftReleased = ImGui::IsMouseReleased(ImGuiMouseButton_Left), + .isRightClicked = ImGui::IsMouseClicked(ImGuiMouseButton_Right), + .isRightDown = ImGui::IsMouseDown(ImGuiMouseButton_Right), + .isRightReleased = ImGui::IsMouseReleased(ImGuiMouseButton_Right), + .isMiddleDown = ImGui::IsMouseDown(ImGuiMouseButton_Middle), + .isZoomIn = isFocused && shortcut(manager.chords[SHORTCUT_ZOOM_IN], shortcut::GLOBAL), + .isZoomOut = isFocused && shortcut(manager.chords[SHORTCUT_ZOOM_OUT], shortcut::GLOBAL), + .isMod = isMod}; + } + + void edit_begin_push(Manager& manager, StringType label) + { + manager.command_push({manager.selected, [label](Manager&, Document& document) { document.edit_begin(label); }}); + } + + void document_change_push(Manager& manager) + { + manager.command_push({manager.selected, [](Manager&, Document& document) { document.change(); }}); + } + + void frames_change_push(Manager& manager, const std::set& frames, FrameChange change, ChangeType type) + { + manager.command_push({manager.selected, [=](Manager&, Document& document) + { edit::frames_change_apply(document.model, {.references = frames}, change, type); }}); + } + + // The rectangle spanned by two corners, grown outward to the grid when snapping, in whole pixels. + std::pair rect_snap(vec2 corner, vec2 otherCorner, bool isSnap, ivec2 gridSize, ivec2 gridOffset) + { + auto minPoint = glm::min(corner, otherCorner); + auto maxPoint = glm::max(corner, otherCorner); + for (int axis = 0; axis < 2 && isSnap; ++axis) + { + if (gridSize[axis] == 0) continue; + auto offset = (float)gridOffset[axis]; + auto size = (float)gridSize[axis]; + minPoint[axis] = std::floor((minPoint[axis] - offset) / size) * size + offset; + maxPoint[axis] = std::ceil((maxPoint[axis] - offset) / size) * size + offset; + } + return {vec2(ivec2(minPoint)), vec2(ivec2(maxPoint))}; + } + + void tooltip_lines_draw(std::initializer_list lines) + { + if (!ImGui::BeginTooltip()) return; + for (const auto& line : lines) + ImGui::TextUnformatted(line.c_str()); + ImGui::EndTooltip(); + } + + // The tool's cursor, or a not-allowed cursor and (while in use) why: the tool belongs to the other canvas, or needs + // a spritesheet or a frame first. + void tool_cursor_update(tool::Type type, tool::AreaType area, bool isFrameAvailable, bool isSpritesheetAvailable, + bool isUsing, StringType frameHint) + { + auto& info = tool::INFO[type]; + auto isAreaAllowed = info.areaType == tool::ALL || info.areaType == area; + auto isFrameMissing = info.isFrameRequired && !isFrameAvailable; + auto isSpritesheetMissing = info.isSpritesheetRequired && !isSpritesheetAvailable; + auto isAvailable = isAreaAllowed && !isFrameMissing && !isSpritesheetMissing; + ImGui::SetMouseCursor(isAvailable ? info.cursor : ImGuiMouseCursor_NotAllowed); + ImGui::SetKeyboardFocusHere(); + if (!isUsing || type == tool::PAN || isAvailable) return; + auto hint = !isAreaAllowed + ? (area == tool::ANIMATION_PREVIEW ? TEXT_TOOL_SPRITESHEET_EDITOR : TEXT_TOOL_ANIMATION_PREVIEW) + : isSpritesheetMissing ? TEXT_SELECT_SPRITESHEET + : frameHint; + tooltip_lines_draw({localize.get(hint)}); + } + CanvasView::CanvasView() : Canvas(vec2()) {} bool CanvasView::is_focused_get() const { return isFocused; } diff --git a/src/imgui/window/canvas_view.hpp b/src/imgui/window/canvas_view.hpp index fd3d4f7..a14b0d7 100644 --- a/src/imgui/window/canvas_view.hpp +++ b/src/imgui/window/canvas_view.hpp @@ -1,13 +1,48 @@ #pragma once #include +#include +#include +#include +#include #include "canvas.hpp" #include "manager.hpp" #include "settings.hpp" +#include "tool.hpp" namespace anm2ed::imgui { + // One frame of canvas input. `arrow` is the arrow keys pressed this frame (with key repeat), x right and y down. + struct CanvasInput + { + glm::vec2 arrow{}; + glm::vec2 mouseDelta{}; + float wheel{}; + float step{}; + bool isArrowBegin{}; + bool isArrowDown{}; + bool isArrowEnd{}; + bool isLeftClicked{}; + bool isLeftDown{}; + bool isLeftReleased{}; + bool isRightClicked{}; + bool isRightDown{}; + bool isRightReleased{}; + bool isMiddleDown{}; + bool isZoomIn{}; + bool isZoomOut{}; + bool isMod{}; + }; + + CanvasInput canvas_input_get(Manager&, bool); + void edit_begin_push(Manager&, StringType); + void document_change_push(Manager&); + void frames_change_push(Manager&, const std::set&, FrameChange, ChangeType = ChangeType::ADJUST); + std::pair rect_snap(glm::vec2, glm::vec2, bool, glm::ivec2, glm::ivec2); + void tooltip_lines_draw(std::initializer_list); + void tool_cursor_update(tool::Type, tool::AreaType, bool, bool, bool, StringType); + class CanvasView : public Canvas { protected: diff --git a/src/imgui/window/spritesheet_editor.cpp b/src/imgui/window/spritesheet_editor.cpp index af375b2..9c25984 100644 --- a/src/imgui/window/spritesheet_editor.cpp +++ b/src/imgui/window/spritesheet_editor.cpp @@ -263,47 +263,15 @@ namespace anm2ed::imgui if (ImGui::IsItemHovered()) { - auto isMouseLeftClicked = ImGui::IsMouseClicked(ImGuiMouseButton_Left); - auto isMouseLeftReleased = ImGui::IsMouseReleased(ImGuiMouseButton_Left); - auto isMouseLeftDown = ImGui::IsMouseDown(ImGuiMouseButton_Left); - auto isMouseMiddleDown = ImGui::IsMouseDown(ImGuiMouseButton_Middle); - auto isMouseRightClicked = ImGui::IsMouseClicked(ImGuiMouseButton_Right); - auto isMouseRightDown = ImGui::IsMouseDown(ImGuiMouseButton_Right); - auto isMouseRightReleased = ImGui::IsMouseReleased(ImGuiMouseButton_Right); - auto mouseDelta = to_ivec2(ImGui::GetIO().MouseDelta); - auto mouseWheel = ImGui::GetIO().MouseWheel; - auto isMouseClicked = isMouseLeftClicked || isMouseRightClicked; - auto isMouseDown = isMouseLeftDown || isMouseRightDown; - auto isMouseReleased = isMouseLeftReleased || isMouseRightReleased; - - auto isLeftJustPressed = ImGui::IsKeyPressed(ImGuiKey_LeftArrow, false); - auto isRightJustPressed = ImGui::IsKeyPressed(ImGuiKey_RightArrow, false); - auto isUpJustPressed = ImGui::IsKeyPressed(ImGuiKey_UpArrow, false); - auto isDownJustPressed = ImGui::IsKeyPressed(ImGuiKey_DownArrow, false); - auto isLeftPressed = ImGui::IsKeyPressed(ImGuiKey_LeftArrow); - auto isRightPressed = ImGui::IsKeyPressed(ImGuiKey_RightArrow); - auto isUpPressed = ImGui::IsKeyPressed(ImGuiKey_UpArrow); - auto isDownPressed = ImGui::IsKeyPressed(ImGuiKey_DownArrow); - auto isLeftDown = ImGui::IsKeyDown(ImGuiKey_LeftArrow); - auto isRightDown = ImGui::IsKeyDown(ImGuiKey_RightArrow); - auto isUpDown = ImGui::IsKeyDown(ImGuiKey_UpArrow); - auto isDownDown = ImGui::IsKeyDown(ImGuiKey_DownArrow); - auto isLeftReleased = ImGui::IsKeyReleased(ImGuiKey_LeftArrow); - auto isRightReleased = ImGui::IsKeyReleased(ImGuiKey_RightArrow); - auto isUpReleased = ImGui::IsKeyReleased(ImGuiKey_UpArrow); - auto isDownReleased = ImGui::IsKeyReleased(ImGuiKey_DownArrow); - auto isKeyJustPressed = isLeftJustPressed || isRightJustPressed || isUpJustPressed || isDownJustPressed; - auto isKeyDown = isLeftDown || isRightDown || isUpDown || isDownDown; - auto isKeyReleased = isLeftReleased || isRightReleased || isUpReleased || isDownReleased; - - auto isZoomIn = isFocused && shortcut(manager.chords[SHORTCUT_ZOOM_IN], shortcut::GLOBAL); - auto isZoomOut = isFocused && shortcut(manager.chords[SHORTCUT_ZOOM_OUT], shortcut::GLOBAL); - - auto isBegin = isMouseClicked || isKeyJustPressed; - auto isDuring = isMouseDown || isKeyDown; - auto isEnd = isMouseReleased || isKeyReleased; - - auto isMod = ImGui::IsKeyDown(ImGuiMod_Shift); + 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, @@ -312,601 +280,257 @@ namespace anm2ed::imgui if (!selectedFrameReferences.contains(reference) && !selectedFrameReferences.empty()) editReference = *selectedFrameReferences.begin(); auto frame = model.frame_get(editReference); - auto item = model.track_get(editReference); auto layer = model::item_get(model.content.layers, editReference.itemID); bool isReferenceLayerOnSpritesheet = frame && editReference.itemID > -1 && layer && layer->spritesheetId == referenceSpritesheet; if (selectedFrameReferences.empty() && isReferenceLayerOnSpritesheet) selectedFrameReferences.insert(editReference); - auto useTool = tool; - auto step = (float)(isMod ? STEP_FAST : STEP); - auto stepX = isGridSnap ? step * gridSize.x : step; - auto stepY = isGridSnap ? step * gridSize.y : step; previousMousePos = mousePos; mousePos = position_translate(zoom, pan, to_ivec2(ImGui::GetMousePos()) - to_ivec2(cursorScreenPos)); - auto textureMousePos = mousePos; - auto previousTextureMousePos = previousMousePos; - auto snap_rect = [&](glm::vec2 minPoint, glm::vec2 maxPoint) - { - if (isGridSnap) - { - if (gridSize.x != 0) - { - auto offsetX = (float)(gridOffset.x); - auto sizeX = (float)(gridSize.x); - minPoint.x = std::floor((minPoint.x - offsetX) / sizeX) * sizeX + offsetX; - maxPoint.x = std::ceil((maxPoint.x - offsetX) / sizeX) * sizeX + offsetX; - } - if (gridSize.y != 0) - { - auto offsetY = (float)(gridOffset.y); - auto sizeY = (float)(gridSize.y); - minPoint.y = std::floor((minPoint.y - offsetY) / sizeY) * sizeY + offsetY; - maxPoint.y = std::ceil((maxPoint.y - offsetY) / sizeY) * sizeY + offsetY; - } - } - minPoint = vec2(ivec2(minPoint)); - maxPoint = vec2(ivec2(maxPoint)); - return std::pair{minPoint, maxPoint}; - }; - - auto clamp_vec2_to_int = [](const vec2& value) { return vec2(ivec2(value)); }; auto pivot_snap = [&](vec2 value, vec2 current) - { - if (isGridSnap) return clamp_vec2_to_int(value); - auto fraction = current - glm::floor(current); - return glm::floor(value) + fraction; - }; - auto edit_begin_push = [&](StringType label) + { return isGridSnap ? vec2(ivec2(value)) : glm::floor(value) + (current - glm::floor(current)); }; + // Region edits apply to the given regions of the edited spritesheet when the command runs. + auto regions_update = [&](std::set ids, std::function update) { manager.command_push( - {manager.selected, [label](Manager&, Document& document) { document.edit_begin(label); }}); + {manager.selected, [=, spritesheetId = referenceSpritesheet](Manager&, Document& document) + { + auto spritesheet = model::item_get(document.model.content.spritesheets, spritesheetId); + for (auto id : ids) + if (auto region = spritesheet ? model::item_get(spritesheet->regions, id) : nullptr) update(*region); + }}); }; - auto document_change_push = [&]() - { manager.command_push({manager.selected, [](Manager&, Document& document) { document.change(); }}); }; - auto frame_change_apply_to = [&](const std::set& frameReferences, FrameChange frameChange, - ChangeType changeType = ChangeType::ADJUST) + auto region_pivot_update = [&](std::function pivot_get) { - auto queuedFrameReferences = frameReferences; - manager.command_push({manager.selected, [=](Manager&, Document& document) - { - std::map> groupedFrames{}; - for (auto frameReference : queuedFrameReferences) - { - auto itemReference = frameReference; - itemReference.frameIndex = -1; - groupedFrames[itemReference].insert(frameReference.frameIndex); - } - - for (auto& [itemReference, itemFrames] : groupedFrames) - { - auto itemType = static_cast(itemReference.itemType); - if (auto item = document.model.track_edit(itemReference)) - model::frames_change(*item, frameChange, itemType, changeType, itemFrames); - } - }}); - }; - auto frame_change_apply = [&](FrameChange frameChange, ChangeType changeType = ChangeType::ADJUST) - { frame_change_apply_to(selectedFrameReferences, frameChange, changeType); }; - auto frame_change_from_current_apply_to = [&](const std::set& frameReferences, auto frameChangeGet, - ChangeType changeType = ChangeType::ADJUST) - { - auto queuedReference = editReference; - auto queuedFrameReferences = frameReferences; - manager.command_push({manager.selected, [=](Manager&, Document& document) - { - auto framePointer = document.model.frame_get(queuedReference); - if (!framePointer) return; - auto frame = *framePointer; - - std::map> groupedFrames{}; - for (auto frameReference : queuedFrameReferences) - { - auto itemReference = frameReference; - itemReference.frameIndex = -1; - groupedFrames[itemReference].insert(frameReference.frameIndex); - } - - for (auto& [itemReference, itemFrames] : groupedFrames) - { - auto itemType = static_cast(itemReference.itemType); - if (auto item = document.model.track_edit(itemReference)) - model::frames_change(*item, frameChangeGet(frame), itemType, changeType, - itemFrames); - } - }}); - }; - auto frame_crop_normalize_apply_to = - [&](const std::set& frameReferences, bool isSnap, ivec2 snapGridSize, ivec2 snapGridOffset) - { - frame_change_from_current_apply_to( - frameReferences, - [=](const model::Frame& frame) - { - auto minPoint = glm::min(frame.crop, frame.crop + frame.size); - auto maxPoint = glm::max(frame.crop, frame.crop + frame.size); - - if (isSnap) - { - if (snapGridSize.x != 0) - { - auto offsetX = (float)snapGridOffset.x; - auto sizeX = (float)snapGridSize.x; - minPoint.x = std::floor((minPoint.x - offsetX) / sizeX) * sizeX + offsetX; - maxPoint.x = std::ceil((maxPoint.x - offsetX) / sizeX) * sizeX + offsetX; - } - if (snapGridSize.y != 0) - { - auto offsetY = (float)snapGridOffset.y; - auto sizeY = (float)snapGridSize.y; - minPoint.y = std::floor((minPoint.y - offsetY) / sizeY) * sizeY + offsetY; - maxPoint.y = std::ceil((maxPoint.y - offsetY) / sizeY) * sizeY + offsetY; - } - } - - return FrameChange{.regionId = -1, - .cropX = minPoint.x, - .cropY = minPoint.y, - .sizeX = maxPoint.x - minPoint.x, - .sizeY = maxPoint.y - minPoint.y}; - }); - }; - auto region_update = [&](int id, auto update) - { - auto queuedSpritesheet = referenceSpritesheet; - manager.command_push({manager.selected, [=](Manager&, Document& document) - { - auto spritesheet = - model::item_get(document.model.content.spritesheets, queuedSpritesheet); - if (!spritesheet) return; - if (auto region = model::item_get(spritesheet->regions, id)) update(*region); - }}); - }; - auto region_update_all = [&](auto update) - { - auto queuedSpritesheet = referenceSpritesheet; - std::set queuedSelection(regionSelection.begin(), regionSelection.end()); - manager.command_push({manager.selected, [=](Manager&, Document& document) - { - auto spritesheet = - model::item_get(document.model.content.spritesheets, queuedSpritesheet); - if (!spritesheet) return; - for (auto id : queuedSelection) - { - if (auto region = model::item_get(spritesheet->regions, id)) update(*region); - } - }}); - }; - auto region_set_all = [&](const vec2& crop, const vec2& size) - { - auto queuedCrop = clamp_vec2_to_int(crop); - auto queuedSize = clamp_vec2_to_int(size); - region_update_all( - [=](model::Region& region) - { - region.crop = queuedCrop; - region.size = queuedSize; - }); - }; - auto region_offset_all = [&](const vec2& delta) - { - region_update_all( - [=](model::Region& region) - { - region.crop = clamp_vec2_to_int(region.crop + delta); - region.size = clamp_vec2_to_int(region.size); - }); - }; - auto region_crop_normalize_all = [&](bool isSnap, ivec2 snapGridSize, ivec2 snapGridOffset) - { - region_update_all( - [=](model::Region& region) - { - auto minPoint = glm::min(region.crop, region.crop + region.size); - auto maxPoint = glm::max(region.crop, region.crop + region.size); - - if (isSnap) - { - if (snapGridSize.x != 0) - { - auto offsetX = (float)snapGridOffset.x; - auto sizeX = (float)snapGridSize.x; - minPoint.x = std::floor((minPoint.x - offsetX) / sizeX) * sizeX + offsetX; - maxPoint.x = std::ceil((maxPoint.x - offsetX) / sizeX) * sizeX + offsetX; - } - if (snapGridSize.y != 0) - { - auto offsetY = (float)snapGridOffset.y; - auto sizeY = (float)snapGridSize.y; - minPoint.y = std::floor((minPoint.y - offsetY) / sizeY) * sizeY + offsetY; - maxPoint.y = std::ceil((maxPoint.y - offsetY) / sizeY) * sizeY + offsetY; - } - } - - region.crop = clamp_vec2_to_int(minPoint); - region.size = clamp_vec2_to_int(maxPoint - minPoint); - }); - }; - auto texture_line_apply = [&](ivec2 start, ivec2 end, vec4 color) - { - auto queuedSpritesheet = referenceSpritesheet; - manager.command_push({manager.selected, [=](Manager&, Document& document) - { - if (auto texture = document.texture_edit(queuedSpritesheet)) - texture->pixel_line(start, end, color); - }}); - }; - auto texture_change_push = [&]() - { - auto queuedSpritesheet = referenceSpritesheet; - manager.command_push( - {manager.selected, [=](Manager&, Document& document) { document.texture_change(queuedSpritesheet); }}); + regions_update({regionReference}, + [=](model::Region& region) + { + region.origin = Origin::CUSTOM; + region.pivot = pivot_get(region.pivot); + }); }; - auto region_selection_set = [&](int id) - { - document.editTarget = Document::EditTarget::REGION; - regionReference = id; - regionSelection = {id}; - document.selected_ids_set(SelectionKind::REGIONS, {id}); - document.focused_id_set(SelectionKind::REGIONS, id); - }; - auto region_pivot_set = [&](int id, vec2 pivot) - { - auto queuedPivot = pivot; - region_update(id, - [=](model::Region& region) - { - region.origin = Origin::CUSTOM; - region.pivot = queuedPivot; - }); - }; - auto region_pivot_offset = [&](int id, vec2 delta) - { - region_update(id, - [=](model::Region& region) - { - region.origin = Origin::CUSTOM; - region.pivot += delta; - if (isGridSnap) region.pivot = clamp_vec2_to_int(region.pivot); - }); - }; - - if (isMouseMiddleDown) useTool = tool::PAN; - if (tool == tool::MOVE && isMouseRightDown) useTool = tool::CROP; - if (tool == tool::CROP && isMouseRightDown) useTool = tool::MOVE; - if (tool == tool::DRAW && isMouseRightDown) useTool = tool::ERASE; - if (tool == tool::ERASE && isMouseRightDown) useTool = tool::DRAW; + auto useTool = (tool::Type)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; hoveredRegionId = -1; - if (useTool == tool::PAN && spritesheet && texture && texture->is_valid() && isMouseOverCanvas) - { for (auto& region : spritesheet->regions) { auto minPoint = glm::min(region.crop, region.crop + region.size); auto maxPoint = glm::max(region.crop, region.crop + region.size); - if (hoverMousePos.x >= minPoint.x && hoverMousePos.x <= maxPoint.x && hoverMousePos.y >= minPoint.y && - hoverMousePos.y <= maxPoint.y) + if (glm::all(glm::greaterThanEqual(hoverMousePos, minPoint)) && + glm::all(glm::lessThanEqual(hoverMousePos, 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{} : 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::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& toolInfo = tool::INFO[useTool]; - auto& areaType = toolInfo.areaType; - auto isRegionEditTarget = document.editTarget == Document::EditTarget::REGION; - auto selectedFrameToolReferences = isRegionEditTarget ? std::set{} : selectedFrameReferences; - bool isAreaAllowed = areaType == tool::ALL || areaType == tool::SPRITESHEET_EDITOR; - bool isFrameRequired = - !(useTool == tool::PAN || useTool == tool::DRAW || useTool == tool::ERASE || useTool == tool::COLOR_PICKER); - bool isMultiFrameToolSelection = selectedFrameToolReferences.size() > 1; - bool isRegionInUse = !isRegionEditTarget && !isMultiFrameToolSelection && frame && frame->regionId != -1 && - (useTool == tool::CROP || useTool == tool::MOVE); - bool isFrameAvailable = !isFrameRequired || (frame && !isRegionInUse && !selectedFrameToolReferences.empty()) || - (useTool == tool::CROP && !regionSelection.empty()) || - (useTool == tool::MOVE && regionReference != -1); - bool isSpritesheetRequired = useTool == tool::DRAW || useTool == tool::ERASE || useTool == tool::COLOR_PICKER; - bool isSpritesheetAvailable = !isSpritesheetRequired || (texture && texture->is_valid()); - auto cursor = (isAreaAllowed && isFrameAvailable && isSpritesheetAvailable) ? toolInfo.cursor - : ImGuiMouseCursor_NotAllowed; - ImGui::SetMouseCursor(cursor); - ImGui::SetKeyboardFocusHere(); - - switch (useTool) + auto region = isRegionTool ? region_get(regionReference) : nullptr; + if (useTool == tool::MOVE && region) { - case tool::PAN: - if (isMouseLeftClicked && hoveredRegionId != -1) - { - region_selection_set(hoveredRegionId); - if (isReferenceLayerOnSpritesheet) - { - edit_begin_push(EDIT_FRAME_REGION); - FrameChange change{}; - change.regionId = hoveredRegionId; - if (auto region = region_get(hoveredRegionId)) - { - change.cropX = region->crop.x; - change.cropY = region->crop.y; - change.sizeX = region->size.x; - change.sizeY = region->size.y; - change.pivotX = region->pivot.x; - change.pivotY = region->pivot.y; - } - frame_change_apply(change); - document_change_push(); - } - } - if (isMouseDown || isMouseMiddleDown) pan += mouseDelta; - break; - case tool::MOVE: - if ((isRegionEditTarget || isRegionInUse || selectedFrameToolReferences.empty()) && regionReference != -1) - { - if (!spritesheet) break; - auto region = region_get(regionReference); - if (!region) break; - - if (isBegin) edit_begin_push(EDIT_REGION_MOVE); - if (isMouseDown) region_pivot_set(regionReference, pivot_snap(mousePos - region->crop, region->pivot)); - if (isLeftPressed) region_pivot_offset(regionReference, vec2(-step, 0)); - if (isRightPressed) region_pivot_offset(regionReference, vec2(step, 0)); - if (isUpPressed) region_pivot_offset(regionReference, vec2(0, -step)); - if (isDownPressed) region_pivot_offset(regionReference, vec2(0, step)); - - if (isDuring) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_PIVOT), std::make_format_args(region->pivot.x, region->pivot.y)) - .c_str()); - ImGui::EndTooltip(); - } - } - - if (isEnd) document_change_push(); - break; - } - - if (!item || !frame || selectedFrameToolReferences.empty() || isRegionInUse) break; - if (isBegin) edit_begin_push(EDIT_FRAME_PIVOT); - if (isMouseDown) - { - auto pivot = pivot_snap(mousePos - frame->crop, frame->pivot); - frame_change_apply_to(selectedFrameToolReferences, - {.regionId = -1, .pivotX = pivot.x, .pivotY = pivot.y}); - } - if (isLeftPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .pivotX = step}, - ChangeType::SUBTRACT); - if (isRightPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .pivotX = step}, ChangeType::ADD); - if (isUpPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .pivotY = step}, - ChangeType::SUBTRACT); - if (isDownPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .pivotY = step}, ChangeType::ADD); - - if (isDuring) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_PIVOT), std::make_format_args(frame->pivot.x, frame->pivot.y)) - .c_str()); - ImGui::EndTooltip(); - } - } - - if (isEnd) document_change_push(); - break; - case tool::CROP: - if ((isRegionEditTarget || isRegionInUse || selectedFrameToolReferences.empty()) && - !regionSelection.empty()) - { - if (!spritesheet || regionSelection.empty()) break; - if (isBegin) edit_begin_push(EDIT_REGION_CROP); - - if (isMouseClicked) - { - cropAnchor = mousePos; - region_set_all(vec2((int)cropAnchor.x, (int)cropAnchor.y), vec2()); - } - if (isMouseDown) - { - auto [minPoint, maxPoint] = snap_rect(glm::min(cropAnchor, mousePos), glm::max(cropAnchor, mousePos)); - region_set_all(vec2(minPoint), vec2(maxPoint - minPoint)); - } - if (isLeftPressed) region_offset_all(vec2(stepX * -1, 0)); - if (isRightPressed) region_offset_all(vec2(stepX, 0)); - if (isUpPressed) region_offset_all(vec2(0, stepY * -1)); - if (isDownPressed) region_offset_all(vec2(0, stepY)); - - if (isDuring) - { - if (!isMouseDown) region_crop_normalize_all(isGridSnap, gridSize, gridOffset); - - if (ImGui::BeginTooltip()) - { - if (auto region = region_get(*regionSelection.begin())) - { - 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()); - } - ImGui::EndTooltip(); - } - } - - if (isEnd) document_change_push(); - break; - } - - if (!item || !frame || selectedFrameToolReferences.empty() || isRegionInUse) break; - if (isBegin) edit_begin_push(EDIT_FRAME_CROP); - - if (isMouseClicked) - { - cropAnchor = mousePos; - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, - .cropX = (float)(int)cropAnchor.x, - .cropY = (float)(int)cropAnchor.y, - .sizeX = {}, - .sizeY = {}}); - } - if (isMouseDown) - { - auto [minPoint, maxPoint] = snap_rect(glm::min(cropAnchor, mousePos), glm::max(cropAnchor, mousePos)); - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, - .cropX = minPoint.x, - .cropY = minPoint.y, - .sizeX = maxPoint.x - minPoint.x, - .sizeY = maxPoint.y - minPoint.y}); - } - if (isLeftPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .cropX = stepX}, - ChangeType::SUBTRACT); - if (isRightPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .cropX = stepX}, ChangeType::ADD); - if (isUpPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .cropY = stepY}, - ChangeType::SUBTRACT); - if (isDownPressed) - frame_change_apply_to(selectedFrameToolReferences, {.regionId = -1, .cropY = stepY}, ChangeType::ADD); - - if (isDuring) - { - if (!isMouseDown) - { - frame_crop_normalize_apply_to(selectedFrameToolReferences, isGridSnap, gridSize, gridOffset); - } - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_CROP), std::make_format_args(frame->crop.x, frame->crop.y)) - .c_str()); - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_SIZE), std::make_format_args(frame->size.x, frame->size.y)) - .c_str()); - ImGui::EndTooltip(); - } - } - if (isEnd) document_change_push(); - break; - case tool::DRAW: - case tool::ERASE: + 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) { - if (!texture) break; - auto color = useTool == tool::DRAW ? toolColor : vec4(); - if (isMouseClicked) edit_begin_push(useTool == tool::DRAW ? EDIT_DRAW : EDIT_ERASE); - if (isMouseDown) texture_line_apply(ivec2(previousTextureMousePos), ivec2(textureMousePos), color); - if (isMouseReleased) texture_change_push(); - break; + auto pivot = pivot_snap(mousePos - frame->crop, frame->pivot); + frames_change({.regionId = -1, .pivotX = pivot.x, .pivotY = pivot.y}); } - case tool::COLOR_PICKER: + 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 (texture && isDuring) - { - toolColor = texture->pixel_read(textureMousePos); - if (ImGui::BeginTooltip()) - { - ImGui::ColorButton("##Color Picker Button", to_imvec4(toolColor)); - ImGui::SameLine(); - auto rgba8 = glm::clamp(ivec4(toolColor * 255.0f + 0.5f), ivec4(0), ivec4(255)); - auto hex = std::format("#{:02X}{:02X}{:02X}{:02X}", rgba8.r, rgba8.g, rgba8.b, rgba8.a); - ImGui::TextUnformatted(hex.c_str()); - ImGui::SameLine(); - ImGui::Text("(%d, %d, %d, %d)", rgba8.r, rgba8.g, rgba8.b, rgba8.a); - ImGui::EndTooltip(); - } - } - break; + if (isRegionCrop) + regions_update(regionSelection, + [=](model::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, + [=](model::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](model::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); + edit::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); + } + + if ((useTool == tool::DRAW || useTool == tool::ERASE) && 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 = glm::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(); } - default: - break; } if (tool == tool::PAN && hoveredRegionId != -1 && spritesheet) - { - if (auto region = region_get(hoveredRegionId)) + if (auto region = region_get(hoveredRegionId); region && ImGui::BeginTooltip()) { - if (ImGui::BeginTooltip()) - { - ImGui::PushFont(resources.fonts[font::BOLD].get(), font::SIZE); - ImGui::TextUnformatted(region->name.c_str()); - ImGui::PopFont(); - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_ID), std::make_format_args(hoveredRegionId)).c_str()); - 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()); - if (region->origin == Origin::CUSTOM) - { - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_PIVOT), std::make_format_args(region->pivot.x, region->pivot.y)) - .c_str()); - } - else - { - StringType originString = LABEL_REGION_ORIGIN_CENTER; - if (region->origin == Origin::TOP_LEFT) originString = LABEL_REGION_ORIGIN_TOP_LEFT; - auto originLabel = localize.get(originString); - ImGui::TextUnformatted( - std::vformat(localize.get(FORMAT_ORIGIN), std::make_format_args(originLabel)).c_str()); - } - ImGui::EndTooltip(); - } + ImGui::PushFont(resources.fonts[font::BOLD].get(), 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 (const 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 ((isMouseDown || isKeyDown) && useTool != tool::PAN) + if (input.wheel != 0 || input.isZoomIn || input.isZoomOut) { - if (!isAreaAllowed && areaType == tool::ANIMATION_PREVIEW) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted(localize.get(TEXT_TOOL_ANIMATION_PREVIEW)); - ImGui::EndTooltip(); - } - } - else if (isSpritesheetRequired && !isSpritesheetAvailable) - { - if (ImGui::BeginTooltip()) - { - ImGui::TextUnformatted(localize.get(TEXT_SELECT_SPRITESHEET)); - ImGui::EndTooltip(); - } - } - else if (isFrameRequired && !isFrameAvailable) - { - if (ImGui::BeginTooltip()) - { - if (isRegionInUse) - ImGui::TextUnformatted(localize.get(TEXT_REGION_IN_USE)); - else if (useTool == tool::CROP) - ImGui::TextUnformatted(localize.get(TEXT_SELECT_FRAME_OR_REGION)); - else - ImGui::TextUnformatted(localize.get(TEXT_SELECT_FRAME)); - ImGui::EndTooltip(); - } - } - } - - if (mouseWheel != 0 || isZoomIn || isZoomOut) - { - auto focus = mouseWheel != 0 ? vec2(mousePos) : vec2(); - if (texture && mouseWheel == 0) focus = texture->size / 2; - - zoom_step(zoom, pan, focus, (mouseWheel > 0 || isZoomIn) ? ZOOM_LEVEL_STEP : -ZOOM_LEVEL_STEP); + 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); } } } diff --git a/src/resource/strings.hpp b/src/resource/strings.hpp index d80ff77..5f582f7 100644 --- a/src/resource/strings.hpp +++ b/src/resource/strings.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include + namespace anm2ed { #define SELECT_ENGLISH(symbol, english, spanish, russian, chinese, korean) english, @@ -815,3 +818,12 @@ namespace anm2ed } extern anm2ed::Localizer localize; + +namespace anm2ed +{ + // A localized format string filled with arguments. + template std::string localize_format(StringType format, Args... args) + { + return std::vformat(localize.get(format), std::make_format_args(args...)); + } +} diff --git a/src/tool.hpp b/src/tool.hpp index 55ebf22..586d253 100644 --- a/src/tool.hpp +++ b/src/tool.hpp @@ -38,32 +38,35 @@ namespace anm2ed::tool AreaType areaType; const char* label{}; StringType tooltip{}; + bool isFrameRequired{}; + bool isSpritesheetRequired{}; }; constexpr Info INFO[] = { {ImGuiMouseCursor_Hand, resource::icon::PAN, SHORTCUT_PAN, ALL, "##Pan", TOOLTIP_TOOL_PAN}, - {ImGuiMouseCursor_ResizeAll, resource::icon::MOVE, SHORTCUT_MOVE, ALL, "##Move", TOOLTIP_TOOL_MOVE}, + {ImGuiMouseCursor_ResizeAll, resource::icon::MOVE, SHORTCUT_MOVE, ALL, "##Move", TOOLTIP_TOOL_MOVE, true}, {ImGuiMouseCursor_Arrow, resource::icon::ROTATE, SHORTCUT_ROTATE, ANIMATION_PREVIEW, "##Rotate", - TOOLTIP_TOOL_ROTATE}, + TOOLTIP_TOOL_ROTATE, true}, {ImGuiMouseCursor_ResizeNESW, resource::icon::SCALE, SHORTCUT_SCALE, ANIMATION_PREVIEW, "##Scale", - TOOLTIP_TOOL_SCALE}, + TOOLTIP_TOOL_SCALE, true}, {ImGuiMouseCursor_ResizeNESW, resource::icon::SHEAR, SHORTCUT_SHEAR, ANIMATION_PREVIEW, "##Shear", - TOOLTIP_TOOL_SHEAR}, + TOOLTIP_TOOL_SHEAR, true}, - {ImGuiMouseCursor_Arrow, resource::icon::CROP, SHORTCUT_CROP, SPRITESHEET_EDITOR, "##Crop", TOOLTIP_TOOL_CROP}, + {ImGuiMouseCursor_Arrow, resource::icon::CROP, SHORTCUT_CROP, SPRITESHEET_EDITOR, "##Crop", TOOLTIP_TOOL_CROP, + true}, - {ImGuiMouseCursor_Arrow, resource::icon::DRAW, SHORTCUT_DRAW, SPRITESHEET_EDITOR, "##Draw", - TOOLTIP_TOOL_DRAW}, + {ImGuiMouseCursor_Arrow, resource::icon::DRAW, SHORTCUT_DRAW, SPRITESHEET_EDITOR, "##Draw", TOOLTIP_TOOL_DRAW, + false, true}, - {ImGuiMouseCursor_Arrow, resource::icon::ERASE, SHORTCUT_ERASE, SPRITESHEET_EDITOR, "##Erase", - TOOLTIP_TOOL_ERASE}, + {ImGuiMouseCursor_Arrow, resource::icon::ERASE, SHORTCUT_ERASE, SPRITESHEET_EDITOR, "##Erase", TOOLTIP_TOOL_ERASE, + false, true}, - {ImGuiMouseCursor_Arrow, resource::icon::COLOR_PICKER, SHORTCUT_COLOR_PICKER, SPRITESHEET_EDITOR, "##Color Picker", - TOOLTIP_TOOL_COLOR_PICKER}, + {ImGuiMouseCursor_Arrow, resource::icon::COLOR_PICKER, SHORTCUT_COLOR_PICKER, SPRITESHEET_EDITOR, + "##Color Picker", TOOLTIP_TOOL_COLOR_PICKER, false, true}, {ImGuiMouseCursor_None, resource::icon::UNDO, SHORTCUT_UNDO, ALL, "##Undo", TOOLTIP_TOOL_UNDO},