Skip to content

Commit

Permalink
Fix sprited rendering of BookmarksLayer
Browse files Browse the repository at this point in the history
Switch a load of stuff over from D2D to Geometry2D/Pixel* structs

refs #507 - Clean up use of D2D1_{RECT,SIZE}_* except at D2D boundaries
  • Loading branch information
fredemmott committed Feb 23, 2024
1 parent 0b6eb16 commit ae71c4c
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 233 deletions.
5 changes: 1 addition & 4 deletions src/app/app-common/CachedLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ void CachedLayer::Render(
static_cast<uint32_t>(std::lround(where.left)),
static_cast<uint32_t>(std::lround(where.top)),
},
{
static_cast<uint32_t>(std::lround(where.right - where.left)),
static_cast<uint32_t>(std::lround(where.bottom - where.top)),
},
cacheDimensions,
};

auto sb = mDXR->mSpriteBatch.get();
Expand Down
49 changes: 19 additions & 30 deletions src/app/app-common/KneeboardView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,14 @@ KneeboardView::IPCRenderLayout KneeboardView::GetIPCRenderLayout() const {
});

const auto idealSize = metrics.mPreferredSize.mPixelSize;
const Geometry2D::Rect<float> contentArea {
{metrics.mContentArea.left, metrics.mContentArea.top},
{
metrics.mContentArea.right - metrics.mContentArea.left,
metrics.mContentArea.bottom - metrics.mContentArea.top,
},
};
const auto unscaledContentArea = metrics.mContentArea;
if (metrics.mPreferredSize.mScalingKind == ScalingKind::Bitmap) {
if (
idealSize.mWidth <= MaxViewRenderSize.mWidth
&& idealSize.mHeight <= MaxViewRenderSize.mHeight) {
return {
idealSize,
contentArea.Rounded<uint32_t>(),
unscaledContentArea,
};
}
const auto scaleX = MaxViewRenderSize.Width<float>() / idealSize.mWidth;
Expand All @@ -277,7 +271,7 @@ KneeboardView::IPCRenderLayout KneeboardView::GetIPCRenderLayout() const {
const auto divisor = static_cast<uint32_t>(std::ceil(1 / scale));
return {
idealSize / divisor,
(contentArea / divisor).Rounded<uint32_t>(),
(unscaledContentArea / divisor),
};
}

Expand All @@ -288,7 +282,7 @@ KneeboardView::IPCRenderLayout KneeboardView::GetIPCRenderLayout() const {

return {
size,
(contentArea * ratio).Rounded<uint32_t>(),
(unscaledContentArea.StaticCast<float>() * ratio).Rounded<uint32_t>(),
};
}
const auto now = SHM::ActiveConsumers::Clock::now();
Expand All @@ -300,7 +294,7 @@ KneeboardView::IPCRenderLayout KneeboardView::GetIPCRenderLayout() const {
const auto ratio = static_cast<float>(size.mWidth) / idealSize.mWidth;
return {
size,
(contentArea * ratio).Rounded<uint32_t>(),
(unscaledContentArea.StaticCast<float>() * ratio).Rounded<uint32_t>(),
};
}

Expand All @@ -317,15 +311,15 @@ KneeboardView::IPCRenderLayout KneeboardView::GetIPCRenderLayout() const {
const auto pos = view->mNonVR.Resolve(
metrics.mPreferredSize,
PixelRect {{0, 0}, metrics.mPreferredSize.mPixelSize},
contentArea.Rounded<uint32_t>(),
unscaledContentArea,
views);
if (pos) {
const auto rect = pos->mPosition.Layout(consumerSize, idealSize);
const auto ratio
= static_cast<float>(rect.mSize.mWidth) / idealSize.mWidth;
return {
rect.mSize,
(contentArea * ratio).Rounded<uint32_t>(),
(unscaledContentArea.StaticCast<float>() * ratio).Rounded<uint32_t>(),
};
}
} else {
Expand All @@ -339,7 +333,7 @@ KneeboardView::IPCRenderLayout KneeboardView::GetIPCRenderLayout() const {
const auto ratio = static_cast<float>(size.mWidth) / idealSize.mWidth;
return {
size,
(contentArea * ratio).Rounded<uint32_t>(),
(unscaledContentArea.StaticCast<float>() * ratio).Rounded<uint32_t>(),
};
}

Expand Down Expand Up @@ -378,7 +372,7 @@ void KneeboardView::PostCursorEvent(const CursorEvent& ev) {

void KneeboardView::RenderWithChrome(
RenderTarget* rt,
const D2D1_RECT_F& rect,
const PixelRect& rect,
bool isActiveForInput) noexcept {
OPENKNEEBOARD_TraceLoggingScope("KneeboardView::RenderWithChrome()");
if (!mCurrentTabView) {
Expand All @@ -402,16 +396,13 @@ void KneeboardView::RenderWithChrome(
rect);
}
if (mCursorCanvasPoint) {
const D2D1_SIZE_F size {
rect.right - rect.left,
rect.bottom - rect.top,
};
const auto& size = rect.mSize;
auto d2d = rt->d2d();
mCursorRenderer->Render(
d2d,
{
(mCursorCanvasPoint->x * size.width) + rect.left,
(mCursorCanvasPoint->y * size.height) + rect.top,
(mCursorCanvasPoint->x * size.mWidth) + rect.Left(),
(mCursorCanvasPoint->y * size.mHeight) + rect.Top(),
},
size);
}
Expand Down Expand Up @@ -477,17 +468,15 @@ D2D1_POINT_2F KneeboardView::GetCursorCanvasPoint(
});

const auto& contentArea = mapping.mContentArea;
const D2D1_SIZE_F contentSize {
contentArea.right - contentArea.left,
contentArea.bottom - contentArea.top,
};
const auto& canvasSize = mapping.mPreferredSize.mPixelSize;
const auto contentSize = contentArea.mSize;
const auto& canvasSize
= mapping.mPreferredSize.mPixelSize.StaticCast<float>();

D2D1_POINT_2F point {contentPoint};
point.x *= contentSize.width;
point.y *= contentSize.height;
point.x += contentArea.left;
point.y += contentArea.top;
point.x *= contentSize.Width();
point.y *= contentSize.Height();
point.x += contentArea.Left();
point.y += contentArea.Top();

return {
point.x / canvasSize.mWidth,
Expand Down
75 changes: 33 additions & 42 deletions src/app/app-common/UILayers/BookmarksUILayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void BookmarksUILayer::PostCursorEvent(

auto buttonsEvent = cursorEvent;
buttonsEvent.mX *= metrics.mPreferredSize.mPixelSize.mWidth;
buttonsEvent.mX /= metrics.mNextArea.left;
buttonsEvent.mX /= metrics.mNextArea.Left();

buttons->PostCursorEvent(eventContext, buttonsEvent);
}
Expand All @@ -102,31 +102,27 @@ IUILayer::Metrics BookmarksUILayer::GetMetrics(
if (!this->IsEnabled()) {
auto ret = nextMetrics;
ret.mNextArea = {
0,
0,
static_cast<FLOAT>(nextMetrics.mPreferredSize.mPixelSize.mWidth),
static_cast<FLOAT>(nextMetrics.mPreferredSize.mPixelSize.mHeight),
{},
nextMetrics.mPreferredSize.mPixelSize,
};
return ret;
}

const auto width
= (nextMetrics.mContentArea.bottom - nextMetrics.mContentArea.top)
* (BookmarksBarPercent / 100.0f);
const auto width = static_cast<uint32_t>(std::lround(
nextMetrics.mContentArea.mSize.mHeight * (BookmarksBarPercent / 100.0f)));

return Metrics {
nextMetrics.mPreferredSize.Extended({static_cast<uint32_t>(width), 0}),
{
width,
0,
nextMetrics.mPreferredSize.mPixelSize.mWidth + width,
static_cast<FLOAT>(nextMetrics.mPreferredSize.mPixelSize.mHeight),
{width, 0},
nextMetrics.mPreferredSize.mPixelSize,
},
{
width + nextMetrics.mContentArea.left,
nextMetrics.mContentArea.top,
width + nextMetrics.mContentArea.right,
nextMetrics.mContentArea.bottom,
{
width + nextMetrics.mContentArea.Left(),
nextMetrics.mContentArea.Top(),
},
nextMetrics.mContentArea.mSize,
},
};
}
Expand All @@ -135,7 +131,7 @@ void BookmarksUILayer::Render(
RenderTarget* rt,
const IUILayer::NextList& next,
const Context& context,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
OPENKNEEBOARD_TraceLoggingScope("BookmarksUILayer::Render()");
auto [first, rest] = Split(next);

Expand All @@ -146,22 +142,23 @@ void BookmarksUILayer::Render(

const auto metrics = this->GetMetrics(next, context);
const auto scale
= (rect.right - rect.left) / metrics.mPreferredSize.mPixelSize.mWidth;
= rect.Width<float>() / metrics.mPreferredSize.mPixelSize.mWidth;

auto d2d = rt->d2d();
d2d->FillRectangle(
{
0,
0,
metrics.mNextArea.left * scale,
metrics.mPreferredSize.mPixelSize.mHeight * scale,
PixelRect {
rect.mOffset,
{
static_cast<uint32_t>(std::lround(metrics.mNextArea.Left() * scale)),
rect.Height(),
},
},
mBackgroundBrush.get());

auto [hoverButton, buttons] = this->LayoutButtons()->GetState();

const auto height = metrics.mPreferredSize.mPixelSize.mHeight;
const auto width = metrics.mNextArea.left;
const auto width = metrics.mNextArea.Left();

FLOAT dpix, dpiy;
d2d->GetDpi(&dpix, &dpiy);
Expand All @@ -184,10 +181,10 @@ void BookmarksUILayer::Render(
size_t buttonNumber = 0;
for (const auto& button: buttons) {
const D2D1_RECT_F buttonRect {
rect.left,
rect.top + (button.mRect.top * height * scale),
rect.left + (width * scale),
rect.top + (button.mRect.bottom * height * scale),
rect.Left<float>(),
rect.Top() + (button.mRect.top * height * scale),
rect.Left() + (width * scale),
rect.Top() + (button.mRect.bottom * height * scale),
};
buttonNumber++;
const auto text = winrt::to_hstring(
Expand All @@ -200,29 +197,23 @@ void BookmarksUILayer::Render(
text.data(), text.size(), textFormat.get(), buttonRect, textBrush.get());
if (buttonNumber != 1) {
d2d->DrawLine(
{0, buttonRect.top},
{buttonRect.right, buttonRect.top},
{rect.Left<FLOAT>(), buttonRect.top},
{rect.Left<FLOAT>() + buttonRect.right, buttonRect.top},
mTextBrush.get(),
2.0f);
}
}

d2d.Release();
first->Render(
rt,
rest,
context,
{
metrics.mNextArea.left * scale,
metrics.mNextArea.top * scale,
metrics.mNextArea.right * scale,
metrics.mNextArea.bottom * scale,
});
auto nextArea
= (metrics.mNextArea.StaticCast<float>() * scale).Rounded<uint32_t>();
nextArea.mOffset += rect.mOffset;
first->Render(rt, rest, context, nextArea);
d2d.Reacquire();

d2d->DrawLine(
{width * scale, rect.top},
{width * scale, rect.bottom},
{rect.Left() + (width * scale), rect.Top<float>()},
{rect.Left() + (width * scale), rect.Bottom<float>()},
mTextBrush.get(),
2.0f);
}
Expand Down
21 changes: 9 additions & 12 deletions src/app/app-common/UILayers/ConfirmationUILayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void ConfirmationUILayer::Render(
RenderTarget* rt,
const NextList& next,
const Context& context,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
OPENKNEEBOARD_TraceLoggingScope("ConfirmationUILayer::Render()");
next.front()->Render(rt, next.subspan(1), context, rect);

Expand Down Expand Up @@ -146,16 +146,13 @@ void ConfirmationUILayer::Render(
}
}

void ConfirmationUILayer::UpdateLayout(const D2D1_RECT_F& canvasRect) {
const D2D1_SIZE_F canvasSize {
canvasRect.right - canvasRect.left,
canvasRect.bottom - canvasRect.top,
};
void ConfirmationUILayer::UpdateLayout(const PixelRect& canvasRect) {
const auto canvasSize = canvasRect.mSize;

const auto titleFontSize
= canvasSize.height * (HeaderPercent / 100.0f) * 0.5f;
const auto maxTextWidth
= std::min(titleFontSize * 40, canvasSize.width * 0.8f);
const auto titleFontSize = static_cast<uint32_t>(
std::lround(canvasSize.mHeight * (HeaderPercent / 100.0f) * 0.5f));
const auto maxTextWidth = static_cast<std::uint32_t>(
std::floor(std::min<float>(titleFontSize * 40, canvasSize.mWidth * 0.8f)));

auto dwf = mDXResources->mDWriteFactory;

Expand Down Expand Up @@ -245,8 +242,8 @@ void ConfirmationUILayer::UpdateLayout(const D2D1_RECT_F& canvasRect) {
};

const D2D1_POINT_2F dialogOrigin {
canvasRect.left + ((canvasSize.width - dialogSize.width) / 2),
canvasRect.top + ((canvasSize.height - dialogSize.height) / 2),
canvasRect.Left() + ((canvasSize.mWidth - dialogSize.width) / 2),
canvasRect.Top() + ((canvasSize.mHeight - dialogSize.height) / 2),
};

const D2D1_RECT_F dialogRect {
Expand Down
2 changes: 1 addition & 1 deletion src/app/app-common/UILayers/FlyoutMenuUILayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void FlyoutMenuUILayer::Render(
RenderTarget* rt,
const NextList& next,
const Context& context,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
OPENKNEEBOARD_TraceLoggingScope("FlyoutMenuUILayer::Render()");
auto previous = mPrevious;
if (previous && !mRecursiveCall) {
Expand Down
Loading

0 comments on commit ae71c4c

Please sign in to comment.