Files
XCEngine/new_editor/src/Widgets/UIEditorFieldRowLayout.cpp

52 lines
1.9 KiB
C++
Raw Normal View History

2026-04-08 02:52:28 +08:00
#include <XCEditor/Widgets/UIEditorFieldRowLayout.h>
#include <algorithm>
namespace XCEngine::UI::Editor::Widgets {
namespace {
float ClampNonNegative(float value) {
return (std::max)(0.0f, value);
}
} // namespace
UIEditorFieldRowLayout BuildUIEditorFieldRowLayout(
const ::XCEngine::UI::UIRect& bounds,
float minimumControlWidth,
const UIEditorFieldRowLayoutMetrics& metrics) {
const float rowHeight = bounds.height > 0.0f ? bounds.height : metrics.rowHeight;
const ::XCEngine::UI::UIRect rowBounds(bounds.x, bounds.y, bounds.width, rowHeight);
const float resolvedMinimumControlWidth =
ClampNonNegative((std::min)(minimumControlWidth, rowBounds.width));
const float preferredControlX = rowBounds.x + metrics.controlColumnStart;
const float maximumControlX =
rowBounds.x + rowBounds.width - metrics.controlTrailingInset - resolvedMinimumControlWidth;
const float controlX =
(std::clamp)(
(std::min)(preferredControlX, maximumControlX),
rowBounds.x,
rowBounds.x + rowBounds.width - metrics.controlTrailingInset);
const float controlInsetY = (std::min)(metrics.controlInsetY, rowBounds.height * 0.25f);
const float controlWidth =
ClampNonNegative(rowBounds.x + rowBounds.width - metrics.controlTrailingInset - controlX);
UIEditorFieldRowLayout layout = {};
layout.bounds = rowBounds;
layout.labelRect = ::XCEngine::UI::UIRect(
rowBounds.x + metrics.horizontalPadding,
rowBounds.y,
ClampNonNegative(controlX - metrics.labelControlGap - rowBounds.x - metrics.horizontalPadding),
rowBounds.height);
layout.controlRect = ::XCEngine::UI::UIRect(
controlX,
rowBounds.y + controlInsetY,
controlWidth,
ClampNonNegative(rowBounds.height - controlInsetY * 2.0f));
return layout;
}
} // namespace XCEngine::UI::Editor::Widgets