mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 11:17:53 +01:00
[ScreenRuler] Multiple measurements for measuring tools (#33494)
[ScreenRuler] Multiple Measurements
This commit is contained in:
@@ -2,13 +2,40 @@
|
|||||||
#include "BoundsToolOverlayUI.h"
|
#include "BoundsToolOverlayUI.h"
|
||||||
#include "CoordinateSystemConversion.h"
|
#include "CoordinateSystemConversion.h"
|
||||||
#include "Clipboard.h"
|
#include "Clipboard.h"
|
||||||
|
#include "constants.h"
|
||||||
|
|
||||||
#include <common/utils/window.h>
|
#include <common/utils/window.h>
|
||||||
|
#include <vector>
|
||||||
#define MOUSEEVENTF_FROMTOUCH 0xFF515700
|
|
||||||
|
|
||||||
namespace
|
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)
|
void ToggleCursor(const bool show)
|
||||||
{
|
{
|
||||||
if (show)
|
if (show)
|
||||||
@@ -52,22 +79,16 @@ namespace
|
|||||||
{
|
{
|
||||||
ToggleCursor(true);
|
ToggleCursor(true);
|
||||||
ClipCursor(nullptr);
|
ClipCursor(nullptr);
|
||||||
|
CopyToClipboard(window, *toolState, cursorPos);
|
||||||
|
|
||||||
toolState->commonState->overlayBoxText.Read([](const OverlayBoxText& text) {
|
auto& perScreen = toolState->perScreen[window];
|
||||||
SetClipBoardToText(text.buffer.data());
|
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
perScreen.measurements.push_back(GetMeasurement(*perScreen.currentBounds, cursorPos));
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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:
|
case WM_KEYUP:
|
||||||
if (wparam == VK_ESCAPE)
|
if (wparam == VK_ESCAPE)
|
||||||
{
|
{
|
||||||
|
if (const auto* toolState = GetWindowParam<BoundsToolState*>(window))
|
||||||
|
{
|
||||||
|
CopyToClipboard(window, *toolState, convert::FromSystemToWindow(window, toolState->commonState->cursorPosSystemSpace));
|
||||||
|
}
|
||||||
|
|
||||||
PostMessageW(window, WM_CLOSE, {}, {});
|
PostMessageW(window, WM_CLOSE, {}, {});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WM_LBUTTONDOWN:
|
case WM_LBUTTONDOWN:
|
||||||
{
|
{
|
||||||
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
|
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
|
||||||
if (touchEvent)
|
if (touchEvent)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -164,7 +190,7 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
|
|||||||
|
|
||||||
case WM_MOUSEMOVE:
|
case WM_MOUSEMOVE:
|
||||||
{
|
{
|
||||||
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
|
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
|
||||||
if (touchEvent)
|
if (touchEvent)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -180,7 +206,7 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
|
|||||||
|
|
||||||
case WM_LBUTTONUP:
|
case WM_LBUTTONUP:
|
||||||
{
|
{
|
||||||
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
|
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
|
||||||
if (touchEvent)
|
if (touchEvent)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -195,24 +221,32 @@ LRESULT CALLBACK BoundsToolWndProc(HWND window, UINT message, WPARAM wparam, LPA
|
|||||||
}
|
}
|
||||||
case WM_RBUTTONUP:
|
case WM_RBUTTONUP:
|
||||||
{
|
{
|
||||||
const bool touchEvent = (GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH;
|
const bool touchEvent = (GetMessageExtraInfo() & consts::MOUSEEVENTF_FROMTOUCH) == consts::MOUSEEVENTF_FROMTOUCH;
|
||||||
if (touchEvent)
|
if (touchEvent)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
ToggleCursor(true);
|
ToggleCursor(true);
|
||||||
|
|
||||||
auto toolState = GetWindowParam<BoundsToolState*>(window);
|
auto* toolState = GetWindowParam<BoundsToolState*>(window);
|
||||||
if (!toolState)
|
if (!toolState)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (toolState->perScreen[window].currentBounds)
|
auto& perScreen = toolState->perScreen[window];
|
||||||
toolState->perScreen[window].currentBounds = std::nullopt;
|
|
||||||
|
if (perScreen.currentBounds)
|
||||||
|
{
|
||||||
|
perScreen.currentBounds = std::nullopt;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (toolState->perScreen[window].measurements.empty())
|
if (perScreen.measurements.empty())
|
||||||
|
{
|
||||||
PostMessageW(window, WM_CLOSE, {}, {});
|
PostMessageW(window, WM_CLOSE, {}, {});
|
||||||
|
}
|
||||||
else
|
else
|
||||||
toolState->perScreen[window].measurements.clear();
|
{
|
||||||
|
perScreen.measurements.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -242,10 +276,6 @@ namespace
|
|||||||
true,
|
true,
|
||||||
commonState.units);
|
commonState.units);
|
||||||
|
|
||||||
commonState.overlayBoxText.Access([&](OverlayBoxText& v) {
|
|
||||||
v = text;
|
|
||||||
});
|
|
||||||
|
|
||||||
D2D_POINT_2F textBoxPos;
|
D2D_POINT_2F textBoxPos;
|
||||||
if (textBoxCenter)
|
if (textBoxCenter)
|
||||||
textBoxPos = *textBoxCenter;
|
textBoxPos = *textBoxCenter;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "Clipboard.h"
|
#include "Clipboard.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
void SetClipBoardToText(const std::wstring_view text)
|
void SetClipBoardToText(const std::wstring_view text)
|
||||||
{
|
{
|
||||||
if (!OpenClipboard(nullptr))
|
if (!OpenClipboard(nullptr))
|
||||||
@@ -26,3 +28,25 @@ void SetClipBoardToText(const std::wstring_view text)
|
|||||||
SetClipboardData(CF_UNICODETEXT, handle.get());
|
SetClipboardData(CF_UNICODETEXT, handle.get());
|
||||||
CloseClipboard();
|
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());
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
#pragma once
|
#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);
|
||||||
@@ -8,8 +8,51 @@
|
|||||||
|
|
||||||
#include <common/utils/window.h>
|
#include <common/utils/window.h>
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace
|
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)
|
inline std::pair<D2D_POINT_2F, D2D_POINT_2F> ComputeCrossFeetLine(D2D_POINT_2F center, const bool horizontal)
|
||||||
{
|
{
|
||||||
D2D_POINT_2F start = center, end = center;
|
D2D_POINT_2F start = center, end = center;
|
||||||
@@ -27,6 +70,92 @@ namespace
|
|||||||
|
|
||||||
return { start, end };
|
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,
|
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;
|
break;
|
||||||
case WM_RBUTTONUP:
|
case WM_RBUTTONUP:
|
||||||
|
{
|
||||||
PostMessageW(window, WM_CLOSE, {}, {});
|
PostMessageW(window, WM_CLOSE, {}, {});
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case WM_LBUTTONUP:
|
case WM_LBUTTONUP:
|
||||||
|
{
|
||||||
|
bool shouldClose = true;
|
||||||
|
|
||||||
if (auto state = GetWindowParam<Serialized<MeasureToolState>*>(window))
|
if (auto state = GetWindowParam<Serialized<MeasureToolState>*>(window))
|
||||||
{
|
{
|
||||||
state->Read([](const MeasureToolState& s) { s.commonState->overlayBoxText.Read([](const OverlayBoxText& text) {
|
state->Access([&](MeasureToolState& s) {
|
||||||
SetClipBoardToText(text.buffer.data());
|
shouldClose = HandleCursorUp(window,
|
||||||
}); });
|
&s,
|
||||||
|
convert::FromSystemToWindow(window, s.commonState->cursorPosSystemSpace));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldClose)
|
||||||
|
{
|
||||||
|
PostMessageW(window, WM_CLOSE, {}, {});
|
||||||
}
|
}
|
||||||
PostMessageW(window, WM_CLOSE, {}, {});
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case WM_MOUSEWHEEL:
|
case WM_MOUSEWHEEL:
|
||||||
if (auto state = GetWindowParam<Serialized<MeasureToolState>*>(window))
|
if (auto state = GetWindowParam<Serialized<MeasureToolState>*>(window))
|
||||||
{
|
{
|
||||||
@@ -119,29 +260,29 @@ void DrawMeasureToolTick(const CommonState& commonState,
|
|||||||
{
|
{
|
||||||
bool continuousCapture = {};
|
bool continuousCapture = {};
|
||||||
bool drawFeetOnCross = {};
|
bool drawFeetOnCross = {};
|
||||||
bool drawHorizontalCrossLine = true;
|
|
||||||
bool drawVerticalCrossLine = true;
|
|
||||||
|
|
||||||
Measurement measuredEdges{};
|
std::optional<Measurement> measuredEdges{};
|
||||||
MeasureToolState::Mode mode = {};
|
MeasureToolState::Mode mode = {};
|
||||||
winrt::com_ptr<ID2D1Bitmap> backgroundBitmap;
|
winrt::com_ptr<ID2D1Bitmap> backgroundBitmap;
|
||||||
const MappedTextureView* backgroundTextureToConvert = nullptr;
|
const MappedTextureView* backgroundTextureToConvert = nullptr;
|
||||||
|
std::vector<MeasureToolState::PerScreen::PrevMeasurement> prevMeasurements;
|
||||||
bool gotMeasurement = false;
|
|
||||||
toolState.Read([&](const MeasureToolState& state) {
|
toolState.Read([&](const MeasureToolState& state) {
|
||||||
continuousCapture = state.global.continuousCapture;
|
continuousCapture = state.global.continuousCapture;
|
||||||
drawFeetOnCross = state.global.drawFeetOnCross;
|
drawFeetOnCross = state.global.drawFeetOnCross;
|
||||||
mode = state.global.mode;
|
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;
|
const auto& perScreen = it->second;
|
||||||
|
|
||||||
|
prevMeasurements = perScreen.prevMeasurements;
|
||||||
|
|
||||||
if (!perScreen.measuredEdges)
|
if (!perScreen.measuredEdges)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gotMeasurement = true;
|
measuredEdges = perScreen.measuredEdges;
|
||||||
measuredEdges = *perScreen.measuredEdges;
|
|
||||||
|
|
||||||
if (continuousCapture)
|
if (continuousCapture)
|
||||||
return;
|
return;
|
||||||
@@ -157,23 +298,9 @@ void DrawMeasureToolTick(const CommonState& commonState,
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!gotMeasurement)
|
if (!measuredEdges && prevMeasurements.empty())
|
||||||
return;
|
|
||||||
|
|
||||||
switch (mode)
|
|
||||||
{
|
{
|
||||||
case MeasureToolState::Mode::Cross:
|
return;
|
||||||
drawHorizontalCrossLine = true;
|
|
||||||
drawVerticalCrossLine = true;
|
|
||||||
break;
|
|
||||||
case MeasureToolState::Mode::Vertical:
|
|
||||||
drawHorizontalCrossLine = false;
|
|
||||||
drawVerticalCrossLine = true;
|
|
||||||
break;
|
|
||||||
case MeasureToolState::Mode::Horizontal:
|
|
||||||
drawHorizontalCrossLine = true;
|
|
||||||
drawVerticalCrossLine = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!continuousCapture && !backgroundBitmap && backgroundTextureToConvert)
|
if (!continuousCapture && !backgroundBitmap && backgroundTextureToConvert)
|
||||||
@@ -189,73 +316,23 @@ void DrawMeasureToolTick(const CommonState& commonState,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (continuousCapture || !backgroundBitmap)
|
if (continuousCapture || !backgroundBitmap)
|
||||||
|
{
|
||||||
d2dState.dxgiWindowState.rt->Clear();
|
d2dState.dxgiWindowState.rt->Clear();
|
||||||
|
}
|
||||||
const float hMeasure = measuredEdges.Width(Measurement::Unit::Pixel);
|
|
||||||
const float vMeasure = measuredEdges.Height(Measurement::Unit::Pixel);
|
|
||||||
|
|
||||||
if (!continuousCapture && backgroundBitmap)
|
if (!continuousCapture && backgroundBitmap)
|
||||||
{
|
{
|
||||||
d2dState.dxgiWindowState.rt->DrawBitmap(backgroundBitmap.get());
|
d2dState.dxgiWindowState.rt->DrawBitmap(backgroundBitmap.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto cursorPos = convert::FromSystemToWindow(window, commonState.cursorPosSystemSpace);
|
for (const auto& [prevCursorPos, prevMeasurement] : prevMeasurements)
|
||||||
|
|
||||||
d2dState.ToggleAliasedLinesMode(true);
|
|
||||||
if (drawHorizontalCrossLine)
|
|
||||||
{
|
{
|
||||||
const D2D_POINT_2F hLineStart{ .x = measuredEdges.rect.left, .y = static_cast<float>(cursorPos.y) };
|
DrawMeasurement(prevMeasurement, d2dState, drawFeetOnCross, mode, prevCursorPos, commonState, window);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (drawVerticalCrossLine)
|
if (measuredEdges)
|
||||||
{
|
{
|
||||||
const D2D_POINT_2F vLineStart{ .x = static_cast<float>(cursorPos.x), .y = measuredEdges.rect.top };
|
const auto cursorPos = convert::FromSystemToWindow(window, commonState.cursorPosSystemSpace);
|
||||||
D2D_POINT_2F vLineEnd{ .x = vLineStart.x, .y = vLineStart.y + vMeasure };
|
DrawMeasurement(*measuredEdges, d2dState, drawFeetOnCross, mode, cursorPos, commonState, window);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "Measurement.h"
|
#include "Measurement.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
Measurement::Measurement(RECT winRect)
|
Measurement::Measurement(RECT winRect)
|
||||||
{
|
{
|
||||||
rect.left = static_cast<float>(winRect.left);
|
rect.left = static_cast<float>(winRect.left);
|
||||||
@@ -89,3 +91,41 @@ Measurement::PrintResult Measurement::Print(wchar_t* buf,
|
|||||||
|
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <dcommon.h>
|
#include <dcommon.h>
|
||||||
#include <windef.h>
|
#include <windef.h>
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
struct Measurement
|
struct Measurement
|
||||||
{
|
{
|
||||||
@@ -35,4 +36,10 @@ struct Measurement
|
|||||||
const bool printWidth,
|
const bool printWidth,
|
||||||
const bool printHeight,
|
const bool printHeight,
|
||||||
const Unit units) const;
|
const Unit units) const;
|
||||||
|
|
||||||
|
void PrintToStream(std::wostream& stream,
|
||||||
|
const bool prependNewLine,
|
||||||
|
const bool printWidth,
|
||||||
|
const bool printHeight,
|
||||||
|
const Unit units) const;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ struct CommonState
|
|||||||
|
|
||||||
Measurement::Unit units = Measurement::Unit::Pixel;
|
Measurement::Unit units = Measurement::Unit::Pixel;
|
||||||
|
|
||||||
mutable Serialized<OverlayBoxText> overlayBoxText;
|
|
||||||
POINT cursorPosSystemSpace = {}; // updated atomically
|
POINT cursorPosSystemSpace = {}; // updated atomically
|
||||||
std::atomic_bool closeOnOtherMonitors = false;
|
std::atomic_bool closeOnOtherMonitors = false;
|
||||||
};
|
};
|
||||||
@@ -77,9 +76,13 @@ struct MeasureToolState
|
|||||||
|
|
||||||
struct PerScreen
|
struct PerScreen
|
||||||
{
|
{
|
||||||
|
using PrevMeasurement = std::pair<POINT, Measurement>;
|
||||||
|
|
||||||
bool cursorInLeftScreenHalf = false;
|
bool cursorInLeftScreenHalf = false;
|
||||||
bool cursorInTopScreenHalf = false;
|
bool cursorInTopScreenHalf = false;
|
||||||
std::optional<Measurement> measuredEdges;
|
std::optional<Measurement> measuredEdges;
|
||||||
|
std::vector<PrevMeasurement> prevMeasurements;
|
||||||
|
|
||||||
// While not in a continuous capturing mode, we need to draw captured backgrounds. These are passed
|
// While not in a continuous capturing mode, we need to draw captured backgrounds. These are passed
|
||||||
// directly from a capturing thread.
|
// directly from a capturing thread.
|
||||||
const MappedTextureView* capturedScreenTexture = nullptr;
|
const MappedTextureView* capturedScreenTexture = nullptr;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <windef.h>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
namespace consts
|
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. */
|
/* 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_X = 4;
|
||||||
constexpr inline long CURSOR_OFFSET_AMOUNT_Y = 4;
|
constexpr inline long CURSOR_OFFSET_AMOUNT_Y = 4;
|
||||||
|
|
||||||
|
constexpr inline LPARAM MOUSEEVENTF_FROMTOUCH = 0xFF515700;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user