[ScreenRuler] Multiple measurements for measuring tools (#33494)

[ScreenRuler] Multiple Measurements
This commit is contained in:
Ani 2024-08-20 17:14:49 +02:00 committed by GitHub
parent 45ad7ebc5e
commit 9f491c8f73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 306 additions and 114 deletions

View File

@ -2,13 +2,40 @@
#include "BoundsToolOverlayUI.h"
#include "CoordinateSystemConversion.h"
#include "Clipboard.h"
#include "constants.h"
#include <common/utils/window.h>
#define MOUSEEVENTF_FROMTOUCH 0xFF515700
#include <vector>
namespace
{
Measurement GetMeasurement(const CursorDrag& currentBounds, POINT cursorPos)
{
D2D1_RECT_F rect;
std::tie(rect.left, rect.right) =
std::minmax(static_cast<float>(cursorPos.x), currentBounds.startPos.x);
std::tie(rect.top, rect.bottom) =
std::minmax(static_cast<float>(cursorPos.y), currentBounds.startPos.y);
return Measurement(rect);
}
void CopyToClipboard(HWND window, const BoundsToolState& toolState, POINT cursorPos)
{
std::vector<Measurement> allMeasurements;
for (const auto& [handle, perScreen] : toolState.perScreen)
{
allMeasurements.append_range(perScreen.measurements);
if (handle == window && perScreen.currentBounds)
{
allMeasurements.push_back(GetMeasurement(*perScreen.currentBounds, cursorPos));
}
}
SetClipboardToMeasurements(allMeasurements, true, true, toolState.commonState->units);
}
void ToggleCursor(const bool show)
{
if (show)
@ -52,22 +79,16 @@ namespace
{
ToggleCursor(true);
ClipCursor(nullptr);
CopyToClipboard(window, *toolState, cursorPos);
toolState->commonState->overlayBoxText.Read([](const OverlayBoxText& text) {
SetClipBoardToText(text.buffer.data());
});
auto& perScreen = toolState->perScreen[window];
if (const bool shiftPress = GetKeyState(VK_SHIFT) & 0x8000; shiftPress && toolState->perScreen[window].currentBounds)
if (const bool shiftPress = GetKeyState(VK_SHIFT) & 0x80000; shiftPress && perScreen.currentBounds)
{
D2D1_RECT_F rect;
std::tie(rect.left, rect.right) =
std::minmax(static_cast<float>(cursorPos.x), toolState->perScreen[window].currentBounds->startPos.x);
std::tie(rect.top, rect.bottom) =
std::minmax(static_cast<float>(cursorPos.y), toolState->perScreen[window].currentBounds->startPos.y);
toolState->perScreen[window].measurements.push_back(Measurement{ rect });
perScreen.measurements.push_back(GetMeasurement(*perScreen.currentBounds, cursorPos));
}
toolState->perScreen[window].currentBounds = std::nullopt;
perScreen.currentBounds = std::nullopt;
}
}
@ -86,12 +107,17 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
case WM_KEYUP:
if (wparam == VK_ESCAPE)
{
if (const auto* toolState = GetWindowParam<BoundsToolState*>(window))
{
CopyToClipboard(window, *toolState, convert::FromSystemToWindow(window, toolState->commonState->cursorPosSystemSpace));
}
PostMessageW(window, WM_CLOSE, {}, {});
}
break;
case WM_LBUTTONDOWN:
{
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
if (touchEvent)
break;
@ -164,7 +190,7 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
case WM_MOUSEMOVE:
{
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
if (touchEvent)
break;
@ -180,7 +206,7 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
case WM_LBUTTONUP:
{
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
if (touchEvent)
break;
@ -195,24 +221,32 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
}
case WM_RBUTTONUP:
{
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
if (touchEvent)
break;
ToggleCursor(true);
auto toolState = GetWindowParam<BoundsToolState*>(window);
auto* toolState = GetWindowParam<BoundsToolState*>(window);
if (!toolState)
break;
if (toolState->perScreen[window].currentBounds)
toolState->perScreen[window].currentBounds = std::nullopt;
auto& perScreen = toolState->perScreen[window];
if (perScreen.currentBounds)
{
perScreen.currentBounds = std::nullopt;
}
else
{
if (toolState->perScreen[window].measurements.empty())
if (perScreen.measurements.empty())
{
PostMessageW(window, WM_CLOSE, {}, {});
}
else
toolState->perScreen[window].measurements.clear();
{
perScreen.measurements.clear();
}
}
break;
}
@ -242,10 +276,6 @@ namespace
true,
commonState.units);
commonState.overlayBoxText.Access([&](OverlayBoxText& v) {
v = text;
});
D2D_POINT_2F textBoxPos;
if (textBoxCenter)
textBoxPos = *textBoxCenter;

View File

@ -2,6 +2,8 @@
#include "Clipboard.h"
#include <sstream>
void SetClipBoardToText(const std::wstring_view text)
{
if (!OpenClipboard(nullptr))
@ -26,3 +28,25 @@ void SetClipBoardToText(const std::wstring_view text)
SetClipboardData(CF_UNICODETEXT, handle.get());
CloseClipboard();
}
void SetClipboardToMeasurements(const std::vector<Measurement>& measurements,
bool printWidth,
bool printHeight,
Measurement::Unit units)
{
if (measurements.empty())
{
return;
}
std::wostringstream stream;
bool isFirst = true;
for (const auto& measurement : measurements)
{
measurement.PrintToStream(stream, !isFirst, printWidth, printHeight, units);
isFirst = false;
}
SetClipBoardToText(stream.str());
}

View File

@ -1,5 +1,13 @@
#pragma once
#include <string_view>
#include "Measurement.h"
void SetClipBoardToText(const std::wstring_view text);
#include <string_view>
#include <vector>
void SetClipBoardToText(const std::wstring_view text);
void SetClipboardToMeasurements(const std::vector<Measurement>& measurements,
bool printWidth,
bool printHeight,
Measurement::Unit units);

View File

@ -8,8 +8,51 @@
#include <common/utils/window.h>
#include <exception>
#include <iostream>
#include <utility>
#include <vector>
namespace
{
constexpr std::pair<bool, bool> GetHorizontalVerticalLines(MeasureToolState::Mode mode)
{
switch (mode)
{
case MeasureToolState::Mode::Cross:
return { true, true };
case MeasureToolState::Mode::Vertical:
return { false, true };
case MeasureToolState::Mode::Horizontal:
return { true, false };
default:
throw std::runtime_error("Unknown MeasureToolState Mode");
}
}
void CopyToClipboard(HWND window, const MeasureToolState& toolState)
{
std::vector<Measurement> allMeasurements;
for (const auto& [handle, perScreen] : toolState.perScreen)
{
for (const auto& [_, measurement] : perScreen.prevMeasurements)
{
allMeasurements.push_back(measurement);
}
if (handle == window && perScreen.measuredEdges)
{
allMeasurements.push_back(*perScreen.measuredEdges);
}
}
const auto [printWidth, printHeight] = GetHorizontalVerticalLines(toolState.global.mode);
SetClipboardToMeasurements(allMeasurements, printWidth, printHeight, toolState.commonState->units);
}
inline std::pair<D2D_POINT_2F, D2D_POINT_2F> ComputeCrossFeetLine(D2D_POINT_2F center, const bool horizontal)
{
D2D_POINT_2F start = center, end = center;
@ -27,6 +70,92 @@ namespace
return { start, end };
}
bool HandleCursorUp(HWND window, MeasureToolState* toolState, const POINT cursorPos)
{
ClipCursor(nullptr);
CopyToClipboard(window, *toolState);
auto& perScreen = toolState->perScreen[window];
const bool shiftPress = GetKeyState(VK_SHIFT) & 0x8000;
if (shiftPress && perScreen.measuredEdges)
{
perScreen.prevMeasurements.push_back(MeasureToolState::PerScreen::PrevMeasurement(cursorPos, perScreen.measuredEdges.value()));
}
perScreen.measuredEdges = std::nullopt;
return !shiftPress;
}
void DrawMeasurement(const Measurement& measurement,
D2DState& d2dState,
bool drawFeetOnCross,
MeasureToolState::Mode mode,
POINT cursorPos,
const CommonState& commonState,
HWND window)
{
const auto [drawHorizontalCrossLine, drawVerticalCrossLine] = GetHorizontalVerticalLines(mode);
const float hMeasure = measurement.Width(Measurement::Unit::Pixel);
const float vMeasure = measurement.Height(Measurement::Unit::Pixel);
d2dState.ToggleAliasedLinesMode(true);
if (drawHorizontalCrossLine)
{
const D2D_POINT_2F hLineStart{ .x = measurement.rect.left, .y = static_cast<float>(cursorPos.y) };
D2D_POINT_2F hLineEnd{ .x = hLineStart.x + hMeasure, .y = hLineStart.y };
d2dState.dxgiWindowState.rt->DrawLine(hLineStart, hLineEnd, d2dState.solidBrushes[Brush::line].get());
if (drawFeetOnCross)
{
// To fill all pixels which are close, we call DrawLine with end point one pixel too far, since
// it doesn't get filled, i.e. end point of the range is excluded. However, we want to draw cross
// feet *on* the last pixel row, so we must subtract 1px from the corresponding axis.
hLineEnd.x -= 1.f;
const auto [left_start, left_end] = ComputeCrossFeetLine(hLineStart, false);
const auto [right_start, right_end] = ComputeCrossFeetLine(hLineEnd, false);
d2dState.dxgiWindowState.rt->DrawLine(left_start, left_end, d2dState.solidBrushes[Brush::line].get());
d2dState.dxgiWindowState.rt->DrawLine(right_start, right_end, d2dState.solidBrushes[Brush::line].get());
}
}
if (drawVerticalCrossLine)
{
const D2D_POINT_2F vLineStart{ .x = static_cast<float>(cursorPos.x), .y = measurement.rect.top };
D2D_POINT_2F vLineEnd{ .x = vLineStart.x, .y = vLineStart.y + vMeasure };
d2dState.dxgiWindowState.rt->DrawLine(vLineStart, vLineEnd, d2dState.solidBrushes[Brush::line].get());
if (drawFeetOnCross)
{
vLineEnd.y -= 1.f;
const auto [top_start, top_end] = ComputeCrossFeetLine(vLineStart, true);
const auto [bottom_start, bottom_end] = ComputeCrossFeetLine(vLineEnd, true);
d2dState.dxgiWindowState.rt->DrawLine(top_start, top_end, d2dState.solidBrushes[Brush::line].get());
d2dState.dxgiWindowState.rt->DrawLine(bottom_start, bottom_end, d2dState.solidBrushes[Brush::line].get());
}
}
d2dState.ToggleAliasedLinesMode(false);
OverlayBoxText text;
const auto [crossSymbolPos, measureStringBufLen] =
measurement.Print(text.buffer.data(),
text.buffer.size(),
drawHorizontalCrossLine,
drawVerticalCrossLine,
commonState.units);
d2dState.DrawTextBox(text.buffer.data(),
measureStringBufLen,
crossSymbolPos,
D2D_POINT_2F{ static_cast<float>(cursorPos.x), static_cast<float>(cursorPos.y) },
true,
window);
}
}
winrt::com_ptr<ID2D1Bitmap> ConvertID3D11Texture2DToD2D1Bitmap(winrt::com_ptr<ID2D1RenderTarget> rt,
@ -85,17 +214,29 @@ LRESULT CALLBACK MeasureToolWndProc(HWND window, UINT message, WPARAM wparam, LP
}
break;
case WM_RBUTTONUP:
{
PostMessageW(window, WM_CLOSE, {}, {});
break;
}
case WM_LBUTTONUP:
{
bool shouldClose = true;
if (auto state = GetWindowParam<Serialized<MeasureToolState>*>(window))
{
state->Read([](const MeasureToolState& s) { s.commonState->overlayBoxText.Read([](const OverlayBoxText& text) {
SetClipBoardToText(text.buffer.data());
}); });
state->Access([&](MeasureToolState& s) {
shouldClose = HandleCursorUp(window,
&s,
convert::FromSystemToWindow(window, s.commonState->cursorPosSystemSpace));
});
}
if (shouldClose)
{
PostMessageW(window, WM_CLOSE, {}, {});
}
PostMessageW(window, WM_CLOSE, {}, {});
break;
}
case WM_MOUSEWHEEL:
if (auto state = GetWindowParam<Serialized<MeasureToolState>*>(window))
{
@ -119,29 +260,29 @@ void DrawMeasureToolTick(const CommonState& commonState,
{
bool continuousCapture = {};
bool drawFeetOnCross = {};
bool drawHorizontalCrossLine = true;
bool drawVerticalCrossLine = true;
Measurement measuredEdges{};
std::optional<Measurement> measuredEdges{};
MeasureToolState::Mode mode = {};
winrt::com_ptr<ID2D1Bitmap> backgroundBitmap;
const MappedTextureView* backgroundTextureToConvert = nullptr;
bool gotMeasurement = false;
std::vector<MeasureToolState::PerScreen::PrevMeasurement> prevMeasurements;
toolState.Read([&](const MeasureToolState& state) {
continuousCapture = state.global.continuousCapture;
drawFeetOnCross = state.global.drawFeetOnCross;
mode = state.global.mode;
if (auto it = state.perScreen.find(window); it != end(state.perScreen))
if (const auto it = state.perScreen.find(window); it != end(state.perScreen))
{
const auto& perScreen = it->second;
prevMeasurements = perScreen.prevMeasurements;
if (!perScreen.measuredEdges)
{
return;
}
gotMeasurement = true;
measuredEdges = *perScreen.measuredEdges;
measuredEdges = perScreen.measuredEdges;
if (continuousCapture)
return;
@ -157,23 +298,9 @@ void DrawMeasureToolTick(const CommonState& commonState,
}
});
if (!gotMeasurement)
return;
switch (mode)
if (!measuredEdges && prevMeasurements.empty())
{
case MeasureToolState::Mode::Cross:
drawHorizontalCrossLine = true;
drawVerticalCrossLine = true;
break;
case MeasureToolState::Mode::Vertical:
drawHorizontalCrossLine = false;
drawVerticalCrossLine = true;
break;
case MeasureToolState::Mode::Horizontal:
drawHorizontalCrossLine = true;
drawVerticalCrossLine = false;
break;
return;
}
if (!continuousCapture && !backgroundBitmap && backgroundTextureToConvert)
@ -189,73 +316,23 @@ void DrawMeasureToolTick(const CommonState& commonState,
}
if (continuousCapture || !backgroundBitmap)
{
d2dState.dxgiWindowState.rt->Clear();
const float hMeasure = measuredEdges.Width(Measurement::Unit::Pixel);
const float vMeasure = measuredEdges.Height(Measurement::Unit::Pixel);
}
if (!continuousCapture && backgroundBitmap)
{
d2dState.dxgiWindowState.rt->DrawBitmap(backgroundBitmap.get());
}
const auto cursorPos = convert::FromSystemToWindow(window, commonState.cursorPosSystemSpace);
d2dState.ToggleAliasedLinesMode(true);
if (drawHorizontalCrossLine)
for (const auto& [prevCursorPos, prevMeasurement] : prevMeasurements)
{
const D2D_POINT_2F hLineStart{ .x = measuredEdges.rect.left, .y = static_cast<float>(cursorPos.y) };
D2D_POINT_2F hLineEnd{ .x = hLineStart.x + hMeasure, .y = hLineStart.y };
d2dState.dxgiWindowState.rt->DrawLine(hLineStart, hLineEnd, d2dState.solidBrushes[Brush::line].get());
if (drawFeetOnCross)
{
// To fill all pixels which are close, we call DrawLine with end point one pixel too far, since
// it doesn't get filled, i.e. end point of the range is excluded. However, we want to draw cross
// feet *on* the last pixel row, so we must subtract 1px from the corresponding axis.
hLineEnd.x -= 1.f;
auto [left_start, left_end] = ComputeCrossFeetLine(hLineStart, false);
auto [right_start, right_end] = ComputeCrossFeetLine(hLineEnd, false);
d2dState.dxgiWindowState.rt->DrawLine(left_start, left_end, d2dState.solidBrushes[Brush::line].get());
d2dState.dxgiWindowState.rt->DrawLine(right_start, right_end, d2dState.solidBrushes[Brush::line].get());
}
DrawMeasurement(prevMeasurement, d2dState, drawFeetOnCross, mode, prevCursorPos, commonState, window);
}
if (drawVerticalCrossLine)
if (measuredEdges)
{
const D2D_POINT_2F vLineStart{ .x = static_cast<float>(cursorPos.x), .y = measuredEdges.rect.top };
D2D_POINT_2F vLineEnd{ .x = vLineStart.x, .y = vLineStart.y + vMeasure };
d2dState.dxgiWindowState.rt->DrawLine(vLineStart, vLineEnd, d2dState.solidBrushes[Brush::line].get());
if (drawFeetOnCross)
{
vLineEnd.y -= 1.f;
auto [top_start, top_end] = ComputeCrossFeetLine(vLineStart, true);
auto [bottom_start, bottom_end] = ComputeCrossFeetLine(vLineEnd, true);
d2dState.dxgiWindowState.rt->DrawLine(top_start, top_end, d2dState.solidBrushes[Brush::line].get());
d2dState.dxgiWindowState.rt->DrawLine(bottom_start, bottom_end, d2dState.solidBrushes[Brush::line].get());
}
const auto cursorPos = convert::FromSystemToWindow(window, commonState.cursorPosSystemSpace);
DrawMeasurement(*measuredEdges, d2dState, drawFeetOnCross, mode, cursorPos, commonState, window);
}
d2dState.ToggleAliasedLinesMode(false);
OverlayBoxText text;
const auto [crossSymbolPos, measureStringBufLen] =
measuredEdges.Print(text.buffer.data(),
text.buffer.size(),
drawHorizontalCrossLine,
drawVerticalCrossLine,
commonState.units);
commonState.overlayBoxText.Access([&](OverlayBoxText& v) {
v = text;
});
d2dState.DrawTextBox(text.buffer.data(),
measureStringBufLen,
crossSymbolPos,
D2D_POINT_2F{ static_cast<float>(cursorPos.x), static_cast<float>(cursorPos.y) },
true,
window);
}

View File

@ -2,6 +2,8 @@
#include "Measurement.h"
#include <iostream>
Measurement::Measurement(RECT winRect)
{
rect.left = static_cast<float>(winRect.left);
@ -89,3 +91,41 @@ Measurement::PrintResult Measurement::Print(wchar_t* buf,
return result;
}
void Measurement::PrintToStream(std::wostream& stream,
const bool prependNewLine,
const bool printWidth,
const bool printHeight,
const Unit units) const
{
if (prependNewLine)
{
stream << std::endl;
}
if (printWidth)
{
stream << Width(units);
if (printHeight)
{
stream << L" \x00D7 ";
}
}
if (printHeight)
{
stream << Height(units);
}
switch (units)
{
case Measurement::Unit::Inch:
stream << L" in";
break;
case Measurement::Unit::Centimetre:
stream << L" cm";
break;
}
}

View File

@ -2,6 +2,7 @@
#include <dcommon.h>
#include <windef.h>
#include <iosfwd>
struct Measurement
{
@ -35,4 +36,10 @@ struct Measurement
const bool printWidth,
const bool printHeight,
const Unit units) const;
void PrintToStream(std::wostream& stream,
const bool prependNewLine,
const bool printWidth,
const bool printHeight,
const Unit units) const;
};

View File

@ -31,7 +31,6 @@ struct CommonState
Measurement::Unit units = Measurement::Unit::Pixel;
mutable Serialized<OverlayBoxText> overlayBoxText;
POINT cursorPosSystemSpace = {}; // updated atomically
std::atomic_bool closeOnOtherMonitors = false;
};
@ -77,9 +76,13 @@ struct MeasureToolState
struct PerScreen
{
using PrevMeasurement = std::pair<POINT, Measurement>;
bool cursorInLeftScreenHalf = false;
bool cursorInTopScreenHalf = false;
std::optional<Measurement> measuredEdges;
std::vector<PrevMeasurement> prevMeasurements;
// While not in a continuous capturing mode, we need to draw captured backgrounds. These are passed
// directly from a capturing thread.
const MappedTextureView* capturedScreenTexture = nullptr;

View File

@ -1,5 +1,6 @@
#pragma once
#include <windef.h>
#include <chrono>
namespace consts
@ -18,4 +19,6 @@ namespace consts
/* Offset to not try not to use the cursor immediate pixels in measuring, but it seems only necessary for continuous mode. */
constexpr inline long CURSOR_OFFSET_AMOUNT_X = 4;
constexpr inline long CURSOR_OFFSET_AMOUNT_Y = 4;
constexpr inline LPARAM MOUSEEVENTF_FROMTOUCH = 0xFF515700;
}