Format like smack-a-brother, and write comments as sentences

Four-space indents, braces on their own lines, and one-statement bodies without braces.
Semicolons in comments became full stops.
Designated initializers may leave fields out without a warning.
This commit is contained in:
shweet committed 2026-11-22 17:30:03 -05:00
1 parent 40f6b554b5
commit 7afdf709c3
176 files changed
+20500 -19048

No files matched your search

+456 -437
View File
@@ -21,8 +21,8 @@
using namespace glm;
void SpritesheetEditor::update(Manager& manager, Settings& settings, Resources& resources)
{
void SpritesheetEditor::update(Manager& manager, Settings& settings, Resources& resources)
{
isFocused = false;
auto& document = *manager.get();
@@ -51,485 +51,504 @@ using namespace glm;
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 center_view = [&]() {
pan = -size * 0.5f;
};
auto region_get = [&](int id) { return spritesheet ? item_get(spritesheet->regions, id) : nullptr; };
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);
isFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
auto childSize = ImVec2(row_widget_width_get(3),
(ImGui::GetTextLineHeightWithSpacing() * 4) + (ImGui::GetStyle().WindowPadding.y * 2));
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);
grid_child_draw(childSize, isGrid, gridColor, gridSize, gridOffset, &isGridSnap);
ImGui::SameLine();
ImGui::SameLine();
if (ImGui::BeginChild("##View Child", childSize, true, ImGuiWindowFlags_HorizontalScrollbar))
{
ImGui::InputFloat(localize.get(BASIC_ZOOM), &zoom, 0.0f, 0.0f, "%.0f%%");
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_EDITOR_ZOOM));
view_buttons_draw(manager, settings, center_view, fit_view);
auto mousePosInt = ivec2(mousePos);
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_POSITION_SPACED), std::make_format_args(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))
if (ImGui::BeginChild("##View Child", childSize, true, ImGuiWindowFlags_HorizontalScrollbar))
{
ImGui::BeginDisabled(isTransparent);
{
ImGui::ColorEdit3(localize.get(LABEL_BACKGROUND_COLOR), value_ptr(backgroundColor),
ImGuiColorEditFlags_NoInputs);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_BACKGROUND_COLOR));
}
ImGui::EndDisabled();
ImGui::InputFloat(localize.get(BASIC_ZOOM), &zoom, 0.0f, 0.0f, "%.0f%%");
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_EDITOR_ZOOM));
ImGui::Checkbox(localize.get(LABEL_BORDER), &isBorder);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_SPRITESHEET_BORDER));
view_buttons_draw(manager, settings, center_view, fit_view);
auto mousePosInt = ivec2(mousePos);
ImGui::TextUnformatted(
std::vformat(localize.get(FORMAT_POSITION_SPACED), std::make_format_args(mousePosInt.x, mousePosInt.y))
.c_str());
}
ImGui::EndChild();
ImGui::SameLine();
if (ImGui::BeginChild("##Background Child 2", subChildSize))
if (ImGui::BeginChild("##Background Child", childSize, true, ImGuiWindowFlags_HorizontalScrollbar))
{
ImGui::Checkbox(localize.get(LABEL_TRANSPARENT), &isTransparent);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_TRANSPARENT));
}
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), value_ptr(backgroundColor),
ImGuiColorEditFlags_NoInputs);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_BACKGROUND_COLOR));
}
ImGui::EndDisabled();
ImGui::Checkbox(localize.get(LABEL_BORDER), &isBorder);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_SPRITESHEET_BORDER));
}
ImGui::EndChild();
ImGui::SameLine();
if (ImGui::BeginChild("##Background Child 2", subChildSize))
{
ImGui::Checkbox(localize.get(LABEL_TRANSPARENT), &isTransparent);
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_TRANSPARENT));
}
ImGui::EndChild();
}
ImGui::EndChild();
}
ImGui::EndChild();
auto drawList = ImGui::GetCurrentWindow()->DrawList;
size_set(to_vec2(ImGui::GetContentRegionAvail()));
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);
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)) : 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<int> ids, std::function<void(Region&)> update)
hoveredRegionId = -1;
if (is_canvas_hovered(min, max))
{
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 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 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;
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));
if (useTool == TOOL_PAN && spritesheet && texture && texture->is_valid())
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 (glm::all(glm::greaterThanEqual(mousePos, minPoint)) && glm::all(glm::lessThanEqual(mousePos, maxPoint)))
auto pivot_snap = [&](vec2 value, vec2 current) {
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<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 = glm::min(region.crop, region.crop + region.size);
auto maxPoint = glm::max(region.crop, region.crop + region.size);
if (glm::all(glm::greaterThanEqual(mousePos, minPoint)) &&
glm::all(glm::lessThanEqual(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)
{
hoveredRegionId = region.id;
break;
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;
}
}
// 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)
auto region = isRegionTool ? region_get(regionReference) : nullptr;
if (useTool == TOOL_MOVE && region)
{
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 (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 = 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();
}
}
if (tool == TOOL_PAN && hoveredRegionId != -1 && spritesheet)
if (auto region = region_get(hoveredRegionId); region && ImGui::BeginTooltip())
{
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 (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);
}
}
if (isMouseDown || input.isMiddleDown) pan += input.mouseDelta;
}
auto region = isRegionTool ? region_get(regionReference) : nullptr;
if (useTool == TOOL_MOVE && region)
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())
{
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 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);
}
}
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);
unbind();
// 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 = 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();
}
}
if (tool == TOOL_PAN && hoveredRegionId != -1 && spritesheet)
if (auto region = region_get(hoveredRegionId); region && ImGui::BeginTooltip())
{
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 (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));
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()));
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;
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();
}
}