From 940b8e67e6db7604920888bb5d0f46098b3cd9bf Mon Sep 17 00:00:00 2001 From: "DeepSeek V4.1 Flash" <> Date: Tue, 13 Oct 2026 20:20:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E6=9D=BF/=E6=8B=BE=E8=89=B2=E5=99=A8?= =?UTF-8?q?=E5=B8=83=E5=B1=80=E5=BE=AE=E8=B0=83=20+=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E2=80=9C=E6=8B=96=E6=8B=BD=E5=85=89=E6=A0=87=E9=A3=9E=E5=88=B0?= =?UTF-8?q?=E8=A7=86=E7=95=8C=E8=BE=B9=E7=BC=98=E2=80=9D=EF=BC=88=E5=90=AB?= =?UTF-8?q?=E6=A0=B9=E5=9B=A0=E4=B8=8E=E9=98=B2=E5=A4=8D=E5=8F=91=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一、样式区「系数/偏移」二级面板:两个偏移输入框等宽对齐 - 带说明文字的 EDIT 行原来各自按自己的文本长度 + 单位后缀算宽度,于是「时间轴」 (文本更长、还带 `s` 后缀)的输入框比「Y轴」宽一截。现在同一次排布里所有 带说明文字的输入框共用**同一个宽度**(取其中最大自然宽度,再按“给最宽的说明文字 留位 / 不超过内容区 3/4”夹取),因此「Y轴」与「时间轴」严格等宽、右边界对齐。 - 断言:两个输入框 126.0 / 126.0,右边界 277.8 / 277.8。 二、自定义颜色浮层(拾色器)的绘制与排版 - **圆角不再漏像素**:色域方块的位图与色相条的渐变填充都**裁剪进同一个圆角路径** (新增公开的 `UiBuildRoundRectPath`,与 `UiFillRoundRect` / `UiDrawRoundRect` 逐点同源),色相条的圆角与色域方块完全一致。以前填充是直角矩形,会在圆角描边之外 露出四个尖角(色域左下/右下本身就是接近黑色的极值,看起来就是"角落里多出黑色像素点")。 - 色域位图改用 `WrapModeTileFlipXY` 贴图:插值不再采样到边界之外,边缘也不会被压暗。 - 色相条改用**一条带 7 个色标的线性渐变**画满(颜色与原来 6 段拼色一致), 顺带去掉段与段之间的接缝横线。 - RGB 三行:轨道**两侧间隙相等**(左:字母 ↔ 轨道;右:轨道 ↔ 数值区,共用同一常量), 数值区固定 **3 个字符宽**(与数字本身无关),三行轨道等长、同 x,R/G/B 字母与数值 都**右对齐**,因此三个字母、三列数字纵向对齐。 - 色号:固定 **6+1 个字符宽**,**右边界与三行数值的右边界对齐**(像素级验证:两者 最后一个字符同列);预览色块的长宽不变、左边界与轨道左边界对齐。 - 数值/色号正好 3 / 7 个字符宽,编辑态需要把文本区向外扩出输入框内边距,否则最后 一个字符会被输入框内边距裁掉(新增 `PaddedEditRect`,绘制、命中与插入符定位共用)。 三、修复“用选区放大把视界放大到某个临界点后,拖拽光标就飞到视界边缘” 两个独立原因,都属于“不变量只在某一个调用点维护”: 1. **吸附到采样点**没有边界检查:只要采样点间距 ≥6px 就吸附,而视界一旦比一个采样间隔还窄 (选区放大很容易做到),“最近的采样点”就落在视界之外,于是每一次鼠标移动都把光标 吸到视界外、再被夹回边缘 → 表现为光标“飞到视界边缘后跟不动鼠标”。现在吸附要求 视界内至少放得下 2 个采样点,且只接受**可见区间内**的采样点。 2. **选区放大没有最小跨度下限**:滚轮缩放有 `数据跨度 × 1e-9` 的下限,选区放大没有, 可以一直放大到视界宽度只有几个 double ULP。此时“像素 → 时间”的映射退化 (相邻像素映射到同一个 double),鼠标一动光标就跨几百像素。现在新增 `MinimumTimeSpan`(数据跨度 × 1e-9 与 `ULP(时间) × 画布宽度` 取大者)与 **视界的唯一写入口 `SetTimeView`**(拒绝非有限值、纠正左右颠倒、抬到下限之上、 再按数据范围收敛),滚轮缩放 / 选区放大 / 平移 / 适应视图 / 时间轴适应网格全部走它。 - 另新增**光标位置的唯一写入口 `PlaceCursorTime`**(拖拽光标必须经由此处): 拒绝非有限值 → 吸附(带上限与可见区间检查)→ 夹回可见区间。`XToTime` 也补上了 跨度退化时的兜底。 - ⚠️ 为什么会反复出现:这两个不变量(“视界必须仍然可表示/可用”“光标必须落在可见区间内、 且对应鼠标位置”)过去是**在每个调用点各自维护**的。后来加入的路径(选区放大、采样点吸附、 派生信号/偏移等新算子)各自绕过了其中一个环节,而故障只有在放大到极限时才可见, 于是每加一个新功能就可能复发一次。现在视界与光标各只有一个守门人,新功能必须经过它们。 四、离屏验证(窗口隐藏、不碰屏幕/鼠标/键盘) - 新增 [AQ]:视界比采样间隔还窄时拖光标必须跟随鼠标(修复前偏差 630px、直接贴边); 视界内采样点仍精确吸附;连续橡胶带放大不会再突破可分辨下限(停在 1e-9 数据跨度), 且在极限缩放下拖拽仍精确跟随(修复前 490px 偏差)。 - 新增 [AR]:色域方块与色相条四角之外不再有填充像素(角像素从填充色变为面板底色)、 两侧间隙相等、数值列 3 字符、色号 7 字符且与数值右边界同列(像素级)、预览色块左边界 与轨道左边界对齐、三条轨道等长同 x。 - [AP] 增加两个偏移输入框等宽/右对齐断言。 - ⚠️ 修正 harness 的一个**假通过**:`SetAllVisible(false)` 会清掉标签页的“视图已初始化”标记, 首次渲染的 `FitAll()` 会把测试设置的深缩放覆盖掉([Z]/[AI]/[AQ] 的“深缩放”其实一直在 整段数据上跑,所以这类回归能溜过去)。现在相关用例显式置位该标记,[Z]/[AI] 真正进入 深缩放;并补上用例间残留弹出菜单的隔离(面板停在窗口下半部会向上翻转盖住按钮, 吞掉下一个用例的首次点击)。 - 合计 669/669 通过(连续 4 次);`make release` 零警告(Target.exe 正在运行,链接待其关闭, 已用临时输出名验证链接自身成功)。 --- Components/PlotView/Inc/PlotView.hpp | 50 ++++++++ Components/PlotView/Src/PlotView.cpp | 157 +++++++++++++++++++------- Utilities/Inc/ColorPickerPopup.hpp | 22 ++++ Utilities/Inc/PaintUtil.hpp | 12 ++ Utilities/Inc/PopupMenu.hpp | 8 ++ Utilities/Src/ColorPickerPopup.cpp | 163 +++++++++++++++++++-------- Utilities/Src/PaintUtil.cpp | 75 ++++++------ Utilities/Src/PopupMenu.cpp | 34 ++++++ 8 files changed, 394 insertions(+), 127 deletions(-) diff --git a/Components/PlotView/Inc/PlotView.hpp b/Components/PlotView/Inc/PlotView.hpp index c71c6cb..79bec2f 100644 --- a/Components/PlotView/Inc/PlotView.hpp +++ b/Components/PlotView/Inc/PlotView.hpp @@ -291,6 +291,13 @@ namespace jdi * 仅在横向放大到采样点间距足够大(可精确分辨)时生效, * 避免缩小时光标出现“抖动”难以微调。 * + * ⚠️ 吸附有**上下两个门限**: + * * 下门限:采样点间距 ≥ `kCursorSnapMinPixels`(缩小时不吸附,否则难以微调); + * * 上门限:视界内至少能容纳 **2 个**采样点(`pixels <= plotWidth / 2`)。 + * 视界比采样间隔还窄时“最近采样点”在视界之外,吸附会把光标一把甩到视界边缘 + * 并钉住(表现为“一拖光标就飞到视界边缘、跟不动鼠标”)。 + * * 并且只接受**落在当前可见区间内**的采样点。 + * * @param [IN] time 原始时间。 * @param [OUT] snapped 吸附后的时间。 * @return 成功吸附返回 true。 @@ -925,6 +932,49 @@ namespace jdi */ void ClampCursorToView(int which); + /** + * @brief 计算当前数据下**最小可用的时间跨度**(时间视界的下限)。 + * + * 下限由两件事共同决定: + * * 数据本身:`数据跨度 × 1e-9`(与滚轮缩放一致)——小于它就是把视界缩进采样间隔内部; + * * `double` 的可表示精度:视界宽度小于 `ULP(时间) × 画布宽度` 时, + * 「像素 → 时间」的映射会退化(相邻像素映射到同一个 double), + * 鼠标每动一下只能落在少数几个可表示的时间值上, + * 表现为**光标跟不上鼠标、一拖就跨几百像素或直接贴到视界边缘**。 + * + * @param [IN] low 视界下限(用于估算时间量级)。 + * @param [IN] high 视界上限。 + * @return 最小可用跨度。 + */ + double MinimumTimeSpan(double low, double high) const; + + /** + * @brief **时间视界的唯一写入入口**(所有不变量的守门人)。 + * + * 负责:拒绝非有限值、纠正左右颠倒、把跨度抬到 `MinimumTimeSpan` 之上、 + * 以及最后按数据范围做边界收敛。 + * + * ⚠️ 缩放(滚轮 / 选区放大)、平移、适应视图都必须经过这里: + * 过去每个路径各自维护“跨度下限”,新增的路径(如选区放大)漏掉了这件事, + * 于是“能一直放大到视界只有几个 ULP”这类问题反复出现。 + * + * @param [IN] low 新的视界下限。 + * @param [IN] high 新的视界上限。 + */ + void SetTimeView(double low, double high); + + /** + * @brief **光标时间位置的统一写入入口**(拖拽光标时必须经由此处)。 + * + * 负责:拒绝非有限值、按 `SnapTimeToSample` 吸附加视界内采样点、 + * 以及把结果夹回可见区间。三步缺一不可——光标必须在**可见区间内**且与鼠标位置对应, + * 否则用户看到的是“拖拽时光标自己飞到视界边缘”。 + * + * @param [IN] which 光标序号(1 或 2)。 + * @param [IN] time 目标时间(鼠标位置换算)。 + */ + void PlaceCursorTime(int which, double time); + /** * @brief 计算双光标之间的时间差(绝对值)。 * @return 时间差;非双光标模式返回 0。 diff --git a/Components/PlotView/Src/PlotView.cpp b/Components/PlotView/Src/PlotView.cpp index 037b3ff..3f68115 100644 --- a/Components/PlotView/Src/PlotView.cpp +++ b/Components/PlotView/Src/PlotView.cpp @@ -1,4 +1,4 @@ -#include "PlotView.hpp" +#include "PlotView.hpp" #include #include @@ -876,12 +876,13 @@ namespace jdi double PlotView::XToTime(float x) const { const double viewMin = Tab().ViewMin(); - if (m_plotRect.Width <= 0.0f) + const double span = Tab().ViewMax() - viewMin; + if (m_plotRect.Width <= 0.0f || !(span > 0.0)) { return viewMin; } const double ratio = static_cast(x - m_plotRect.X) / static_cast(m_plotRect.Width); - return viewMin + ratio * (Tab().ViewMax() - viewMin); + return viewMin + ratio * span; } float PlotView::ValueToY(double value, const LaneVisual& lane) const @@ -954,9 +955,57 @@ namespace jdi } } + double PlotView::MinimumTimeSpan(double low, double high) const + { + double dataMin = 0.0; + double dataMax = 1.0; + TimeBounds(dataMin, dataMax); + const double dataSpan = std::max(dataMax - dataMin, kMinTimeSpan); + + // double 在“时间量级”上的最小刻度(ULP):视界宽度低于 `ULP × 画布宽度` 时, + // 一列像素对应的 double 已经无法区分,映射彻底退化。 + const double magnitude = std::max(std::fabs(low), std::fabs(high)); + const double ulp = + std::nextafter(magnitude, std::numeric_limits::infinity()) - magnitude; + const double width = std::max(64.0, static_cast(m_plotRect.Width)); + + return std::max(dataSpan * 1.0e-9, std::max(ulp, kMinTimeSpan) * width); + } + + void PlotView::SetTimeView(double low, double high) + { + if (!std::isfinite(low) || !std::isfinite(high)) + { + return; + } + if (high < low) + { + std::swap(low, high); + } + + const double minimum = MinimumTimeSpan(low, high); + if (!(high - low >= minimum)) + { + // 低于下限:以中心为锚点对称展开(而不是静默地放大到不可表示)。 + const double center = (low + high) * 0.5; + low = center - minimum * 0.5; + high = center + minimum * 0.5; + if (!(high - low > 0.0)) + { + // 连下限都无法表示(时间量级远超可分辨范围):放弃这次修改, + // 保留上一个仍然可用的视界比制造一个空视界更安全。 + return; + } + } + + Tab().ViewMin() = low; + Tab().ViewMax() = high; + ClampTimeView(); + } + void PlotView::ZoomTimeAt(double factor, double anchorTime) { - if (m_dataSet == nullptr || m_dataSet->Empty() || !(factor > 0.0)) + if (m_dataSet == nullptr || m_dataSet->Empty() || !(factor > 0.0) || !std::isfinite(anchorTime)) { return; } @@ -967,12 +1016,18 @@ namespace jdi const double dataSpan = std::max(dataMax - dataMin, kMinTimeSpan); const double span = Tab().ViewMax() - Tab().ViewMin(); - double newSpan = ClampDouble(span / factor, dataSpan * 1.0e-9, dataSpan * 100.0); + if (!(span > 0.0)) + { + return; + } + // 下限与选区放大共用同一个来源(见 `MinimumTimeSpan`), + // 否则“滚轮缩放有限制、选区放大没有”会让视界退化到不可用。 + const double minimum = MinimumTimeSpan(Tab().ViewMin(), Tab().ViewMax()); + const double newSpan = ClampDouble(span / factor, minimum, dataSpan * 100.0); - const double ratio = (span > 0.0) ? (anchorTime - Tab().ViewMin()) / span : 0.5; - Tab().ViewMin() = anchorTime - ratio * newSpan; - Tab().ViewMax() = Tab().ViewMin() + newSpan; - ClampTimeView(); + const double ratio = (anchorTime - Tab().ViewMin()) / span; + const double low = anchorTime - ratio * newSpan; + SetTimeView(low, low + newSpan); } void PlotView::ZoomTimeStep(double factor) @@ -985,9 +1040,7 @@ namespace jdi { const double span = Tab().ViewMax() - Tab().ViewMin(); const double delta = span * fraction; - Tab().ViewMin() += delta; - Tab().ViewMax() += delta; - ClampTimeView(); + SetTimeView(Tab().ViewMin() + delta, Tab().ViewMax() + delta); } void PlotView::ScrollLanesByFraction(double fraction) @@ -1001,8 +1054,7 @@ namespace jdi { if (m_dataSet == nullptr || m_dataSet->Empty()) { - Tab().ViewMin() = 0.0; - Tab().ViewMax() = 1.0; + SetTimeView(0.0, 1.0); Tab().ViewInitialized() = true; return; } @@ -1017,8 +1069,7 @@ namespace jdi high += padding; } - Tab().ViewMin() = low; - Tab().ViewMax() = high; + SetTimeView(low, high); Tab().ViewInitialized() = true; Tab().LaneScroll() = 0.0f; // 只放下进行中的选区与拖拽,**不改变「选区放大」等缩放模式的启用状态** @@ -1307,8 +1358,7 @@ namespace jdi return false; } - Tab().ViewMin() = low; - Tab().ViewMax() = high; + SetTimeView(low, high); Tab().ViewInitialized() = true; // 与“适应视图”保持一致:退出选区放大模式,避免模式按钮与实际状态不符。 Tab().ZoomMode() = PlotZoomMode::NONE; @@ -1404,6 +1454,12 @@ namespace jdi } const double minPixels = static_cast(kCursorSnapMinPixels) * static_cast(m_scale); + // 吸附的上门限:视界内至少要放得下 2 个采样点。 + // 视界比采样间隔还窄时(选区放大很容易做到),"最近采样点"落在视界之外或半个视界之外, + // 吸附会把光标一把甩到视界边缘并钉住 —— 用户报告的"拖光标直接飞到视界外"。 + const double maxPixels = 0.5 * static_cast(m_plotRect.Width); + const double viewMin = Tab().ViewMin(); + const double viewMax = Tab().ViewMax(); bool found = false; double bestDistance = std::numeric_limits::infinity(); @@ -1413,7 +1469,7 @@ namespace jdi { // 只有采样点在屏幕上足够稀疏时才吸附,否则缩小视图后光标会“跳格”。 double pixels = 0.0; - if (!SampleSpacingPixels(file, time, pixels) || pixels < minPixels) + if (!SampleSpacingPixels(file, time, pixels) || pixels < minPixels || pixels > maxPixels) { continue; } @@ -1428,6 +1484,11 @@ namespace jdi { continue; } + // 只吸附**当前可见区间内**的采样点:视界之外的采样点不是合法的落点。 + if (candidate < viewMin || candidate > viewMax) + { + continue; + } const double distance = std::fabs(candidate - time); if (distance < bestDistance) @@ -1445,6 +1506,32 @@ namespace jdi return found; } + void PlotView::PlaceCursorTime(int which, double time) + { + if (!std::isfinite(time)) + { + // NaN/Inf 会让后面所有比较都失效(`ClampDouble` 也拦不住 NaN), + // 光标会被画到画布之外:直接丢弃这次位置更新。 + return; + } + + double snapped = 0.0; + if (SnapTimeToSample(time, snapped)) + { + time = snapped; + } + + if (which == 2) + { + Tab().CursorTimeAlt() = time; + } + else + { + Tab().CursorTime() = time; + } + ClampCursorToView(which); + } + PlotZoomMode PlotView::CurrentZoomMode() const { return Tab().ZoomMode(); @@ -1628,9 +1715,11 @@ namespace jdi return false; } - Tab().ViewMin() = timeA; - Tab().ViewMax() = timeB; - ClampTimeView(); + // 统一入口:内含最小跨度下限。 + // ⚠️ 选区放大过去**没有**这个下限,可以一直放大到视界宽度只有几个 double ULP; + // 到那时「像素 → 时间」已经退化(相邻像素映射到同一个 double), + // 用户看到的就是“拖光标跟不动、一下跳到视界边缘/几百像素外”。 + SetTimeView(timeA, timeB); // 注意:不在这里移动光标——即使选区不包含光标,也只把它的读数贴在网格边缘显示。 // 「选区 Y 轴自适应」开关开启的网格:时间选区放大后自动把 Y 轴匹配到新的可见区间。 @@ -4131,27 +4220,13 @@ namespace jdi case PlotDragMode::CURSOR_MOVE: { // 从时间读数标签上抓起时保留横向抓取偏移,避免一按下光标就跳到鼠标中心。 - double target = XToTime(fx - static_cast(m_cursorGrabOffset)); - double snapped = target; - if (SnapTimeToSample(target, snapped)) - { - target = snapped; - } - + const double target = XToTime(fx - static_cast(m_cursorGrabOffset)); const bool second = (m_hoverCursor == 2 && Tab().CursorMode() == PlotCursorMode::DOUBLE); - if (second) - { - Tab().CursorTimeAlt() = target; - } - else - { - Tab().CursorTime() = target; - } - // 抓取读数标签时可能带有一个较大的横向抓取偏移(放大到一定程度后该偏移 - // 换算成的时间可能落到视界之外)——拖拽结束前必须把光标夹回当前可见区间, - // 否则用户会看到光标线"跑到视界外"而无法继续操作。 - ClampCursorToView(second ? 2 : 1); + // 吸附 + 夹取统一在 `PlaceCursorTime` 里完成(光标位置不变量只在那里维护): + // * 吸附只接受可见区间内、且视界内还能分辨的采样点; + // * 结果必定落在当前可见区间内(否则用户看到的是一拖就"飞到视界边缘")。 + PlaceCursorTime(second ? 2 : 1, target); m_dragMoved = true; m_cursorHover = true; return true; diff --git a/Utilities/Inc/ColorPickerPopup.hpp b/Utilities/Inc/ColorPickerPopup.hpp index 9971a50..d3e7017 100644 --- a/Utilities/Inc/ColorPickerPopup.hpp +++ b/Utilities/Inc/ColorPickerPopup.hpp @@ -219,6 +219,17 @@ namespace jdi */ gd::RectF EditFieldRect() const; + /** + * @brief 把“文本区矩形”扩成**编辑态输入框**矩形(向外扩出输入框的内边距)。 + * + * ⚠️ 数值区正好 3 个字符宽、色号正好 6+1 个字符宽,若直接拿文本区当输入框, + * 输入框内部的 6px 内边距会把最后一个字符裁掉。 + * + * @param [IN] textRect 文本区矩形(数值区或色号区)。 + * @return 输入框矩形。 + */ + gd::RectF PaddedEditRect(const gd::RectF& textRect) const; + /** * @brief 查询坐标命中的滑块下标。 * @param [IN] x 客户区横坐标。 @@ -227,6 +238,17 @@ namespace jdi */ int SliderAtPoint(int x, int y) const; + /** + * @brief 计算通道标签(R/G/B)的矩形。 + * + * 标签**右边界**到轨道左端的距离与轨道右端到数值区的距离相等 + * (用户要求:滑块两侧留白一致,三个字母纵向对齐)。 + * + * @param [IN] index 通道下标(0=R,1=G,2=B)。 + * @return 标签矩形;下标非法时返回空矩形。 + */ + gd::RectF ChannelLabelRect(int index) const; + /** * @brief 应用滑块拖拽位置到对应通道。 * @param [IN] index 滑块下标。 diff --git a/Utilities/Inc/PaintUtil.hpp b/Utilities/Inc/PaintUtil.hpp index ae79872..bc4c452 100644 --- a/Utilities/Inc/PaintUtil.hpp +++ b/Utilities/Inc/PaintUtil.hpp @@ -115,6 +115,18 @@ namespace jdi */ void UiDrawRoundRect(gd::Graphics& g, const gd::RectF& rect, float radius, std::uint32_t argb, float width); + /** + * @brief 构造圆角矩形路径(与 `UiFillRoundRect` / `UiDrawRoundRect` 使用完全相同的几何)。 + * + * 需要把一张位图 / 渐变**裁剪进圆角形状**时使用: + * `g.SetClip(&path)` 之后再填充,就不会在圆角之外漏出直角矩形的角。 + * + * @param [IN,OUT] path 目标路径对象(不会被清空,直接追加)。 + * @param [IN] rect 目标矩形。 + * @param [IN] radius 圆角半径。 + */ + void UiBuildRoundRectPath(gd::GraphicsPath& path, const gd::RectF& rect, float radius); + /** * @brief 绘制一条直线。 * @param [IN,OUT] g 绘图上下文。 diff --git a/Utilities/Inc/PopupMenu.hpp b/Utilities/Inc/PopupMenu.hpp index 0f2e56e..68a8834 100644 --- a/Utilities/Inc/PopupMenu.hpp +++ b/Utilities/Inc/PopupMenu.hpp @@ -397,6 +397,14 @@ namespace jdi gd::RectF m_rect; /**< 菜单整体矩形。 */ std::vector m_itemRects; /**< 各项矩形。 */ + /** + * 同一次排布中,**带说明文字的 EDIT 项**共用的输入框宽度(0 表示没有这类项)。 + * + * 宽度取这些项里最宽的自然宽度,并在排布时按“不能挤掉说明文字 / 不超过内容区”夹好。 + * 同一面板内的输入框因此一样宽、右边界对齐(用户要求「Y轴」与「时间轴」等宽对齐, + * 而不是各自按自己的文本长度自适应)。 + */ + float m_editFieldWidth = 0.0f; std::vector> m_swatchRects; /**< 各项内的色块矩形。 */ int m_hoverIndex = -1; /**< 当前悬停项。 */ diff --git a/Utilities/Src/ColorPickerPopup.cpp b/Utilities/Src/ColorPickerPopup.cpp index 8b480b5..1b20608 100644 --- a/Utilities/Src/ColorPickerPopup.cpp +++ b/Utilities/Src/ColorPickerPopup.cpp @@ -20,8 +20,12 @@ namespace jdi const float kRowGap = 2.0f; /**< 滑块行间距。 */ const float kPreviewHeight = 24.0f; /**< 预览行(预览色块 + 色号)高度。 */ const float kLabelWidth = 16.0f; /**< 通道标签宽度。 */ - const float kLabelGap = 6.0f; /**< 标签与滑块间距。 */ - const float kValueWidth = 40.0f; /**< 数值文本宽度。 */ + /** + * 滑块**两侧**的间隙(左:通道字母 ↔ 轨道;右:轨道 ↔ 数值区)。 + * + * ⚠️ 必须共用同一个常量:用户明确要求轨道左右两侧的留白相等。 + */ + const float kSliderGap = 8.0f; const float kTrackHeight = 8.0f; /**< 滑块轨道高度。 */ const float kCorner = 6.0f; /**< 面板圆角半径。 */ /** 色域方块 / 色相条 / 预览色块 / 色号框共用同一圆角(视觉上统一)。 */ @@ -31,7 +35,7 @@ namespace jdi const float kGamutGap = 10.0f; /**< 色域方块与色相条间距。 */ const float kColumnGap = 12.0f; /**< 色相条与滑块列间距。 */ const float kSwatchWidth = 52.0f; /**< 预览色块宽度。 */ - const float kSwatchHexGap = 8.0f; /**< 预览色块与色号间距。 */ + const float kSwatchHexGap = 8.0f; /**< 预览色块与色号间距(下限,空间不足时以右对齐优先)。 */ const float kGamutBorder = 1.0f; /**< 色域方块/色相条描边宽度。 */ /** @brief 通道显示名称。 */ @@ -270,16 +274,29 @@ namespace jdi const float previewRowGap = std::max(0.0f, kGamutSize - kPreviewHeight - (kRowHeight * 3.0f + kRowGap * 2.0f)) * m_scale; - m_previewRect = gd::RectF(columnLeft, contentTop, kSwatchWidth * m_scale, kPreviewHeight * m_scale); - m_hexRect = gd::RectF(m_previewRect.X + m_previewRect.Width + kSwatchHexGap * m_scale, contentTop, - std::max(40.0f * m_scale, - columnWidth - (kSwatchWidth + kSwatchHexGap) * m_scale), - kPreviewHeight * m_scale); + // 通道数值区固定占 **3 个字符宽**(0-255 最多三位):与数字本身的宽度无关, + // 这样三行数值的右边界严格一致(右对齐后三列数字纵向对齐)。 + const float valueWidth = + std::max(1.0f, UiMeasureTextWidth(m_fonts->monoSmall, L"000")); + // 轨道两侧用**同一个**间隙(用户要求两侧留白相等)。 + const float sliderGap = kSliderGap * m_scale; + const float labelWidth = kLabelWidth * m_scale; + const float trackLeft = columnLeft + labelWidth + sliderGap; + const float trackWidth = std::max( + 24.0f, columnWidth - (labelWidth + sliderGap + sliderGap + valueWidth)); + const float valueLeft = trackLeft + trackWidth + sliderGap; + const float valueRight = valueLeft + valueWidth; + + // 预览色块的**左边界与轨道左边界对齐**(长宽不变)。 + m_previewRect = + gd::RectF(trackLeft, contentTop, kSwatchWidth * m_scale, kPreviewHeight * m_scale); + // 色号 "#RRGGBB" 固定 **6+1 个字符宽**,并且**右边界与三行数值的右边界对齐**。 + const float hexWidth = UiMeasureTextWidth(m_fonts->monoSmall, L"#000000"); + const float hexLeft = std::max(m_previewRect.X + m_previewRect.Width + kSwatchHexGap * m_scale, + valueRight - hexWidth); + m_hexRect = gd::RectF(hexLeft, contentTop, valueRight - hexLeft, kPreviewHeight * m_scale); const float sliderTop = m_previewRect.Y + m_previewRect.Height + previewRowGap; - const float trackLeft = columnLeft + (kLabelWidth + kLabelGap) * m_scale; - const float trackWidth = - std::max(24.0f, columnWidth - (kLabelWidth + kLabelGap + kValueWidth + 6.0f) * m_scale); for (int index = 0; index < 3; ++index) { @@ -288,8 +305,7 @@ namespace jdi gd::RectF(trackLeft, rowTop + (kRowHeight * m_scale - kTrackHeight * m_scale) * 0.5f, trackWidth, kTrackHeight * m_scale); m_values[static_cast(index)] = - gd::RectF(trackLeft + trackWidth + 6.0f * m_scale, rowTop, kValueWidth * m_scale, - kRowHeight * m_scale); + gd::RectF(valueLeft, rowTop, valueWidth, kRowHeight * m_scale); } } @@ -447,19 +463,42 @@ namespace jdi switch (m_editField) { case EditField::R: - return m_values[0]; + return PaddedEditRect(m_values[0]); case EditField::G: - return m_values[1]; + return PaddedEditRect(m_values[1]); case EditField::B: - return m_values[2]; + return PaddedEditRect(m_values[2]); case EditField::HEX: - return m_hexRect; + return PaddedEditRect(m_hexRect); case EditField::NONE: default: return gd::RectF(); } } + gd::RectF ColorPickerPopup::PaddedEditRect(const gd::RectF& textRect) const + { + // 与 `UiDrawEditField` 的内边距(6*scale)一致:文本区向外扩这么多, + // 编辑态才能完整显示 3 位数值 / 7 位色号(否则最后一个字符被裁掉)。 + const float padding = 6.0f * m_scale; + return gd::RectF(textRect.X - padding, textRect.Y, textRect.Width + padding * 2.0f, + textRect.Height); + } + + gd::RectF ColorPickerPopup::ChannelLabelRect(int index) const + { + if (index < 0 || index >= 3) + { + return gd::RectF(); + } + const std::size_t slot = static_cast(index); + const gd::RectF& track = m_tracks[slot]; + // 间隙直接由实际几何推出(轨道右端 → 数值区左端),因此标签侧与数值侧**必然**相等。 + const float gap = std::max(0.0f, m_values[slot].X - (track.X + track.Width)); + return gd::RectF(track.X - gap - kLabelWidth * m_scale, m_values[slot].Y, + kLabelWidth * m_scale, kRowHeight * m_scale); + } + bool ColorPickerPopup::BeginEdit(EditField field) { if (!m_open || field == EditField::NONE) @@ -796,16 +835,18 @@ namespace jdi } // 数值与色号文本优先于滑块:两者几何重叠时要能进入输入状态。 + // 命中区用**编辑框**矩形(比 3 / 7 个字符宽的文本区宽出输入框内边距): + // 文本区只有 3 个字符宽,命中区太小不便点击。 for (int index = 0; index < 3; ++index) { - if (UiPointInRect(m_values[static_cast(index)], static_cast(x), - static_cast(y))) + if (UiPointInRect(PaddedEditRect(m_values[static_cast(index)]), + static_cast(x), static_cast(y))) { BeginEdit(static_cast(index + 1)); return true; } } - if (UiPointInRect(m_hexRect, static_cast(x), static_cast(y))) + if (UiPointInRect(PaddedEditRect(m_hexRect), static_cast(x), static_cast(y))) { BeginEdit(EditField::HEX); return true; @@ -924,35 +965,57 @@ namespace jdi // ---- 色域方块(横轴饱和度、纵轴明度)---- // 方块按当前色相预渲染到缓存位图里,避免每帧重画上百条渐变; // 色相变化时才重建一次。 + // + // ⚠️ 填充必须**裁剪进圆角路径**里: + // * 缓存的位图是直角矩形,直接贴上去会在四个角的多出来像素(用户报告的"角落里出现 + // 莫名其妙的黑色像素点"——方块左下/右下本身就是接近黑色的饱和/明度极值)。 + // * `WrapModeTileFlipXY` 让采样到边界之外的插值镜像取边,不再混入透明黑, + // 边界也就不会再被"压暗"出一圈深色。 if (EnsureGamutCache()) { - g.DrawImage(m_gamutCache.get(), m_gamutRect); + gd::GraphicsPath gamutPath; + UiBuildRoundRectPath(gamutPath, m_gamutRect, kSwatchCorner * m_scale); + gd::ImageAttributes attributes; + attributes.SetWrapMode(gd::WrapModeTileFlipXY, gd::Color(), FALSE); + const int saveState = g.Save(); + g.SetClip(&gamutPath); + g.DrawImage(m_gamutCache.get(), m_gamutRect, 0.0f, 0.0f, + static_cast(m_gamutCacheSize), + static_cast(m_gamutCacheSize), gd::UnitPixel, &attributes); + g.Restore(saveState); } UiDrawRoundRect(g, m_gamutRect, kSwatchCorner * m_scale, m_theme->border, kGamutBorder); - // ---- 色相条(纵向 0-1):6 段线性渐变拼出完整色相环 ---- + // ---- 色相条(纵向 0-1)---- { - for (int segment = 0; segment < 6; ++segment) + // 一条带 7 个色标的线性渐变画满整条(0、1/6 … 1,每段内 RGB 线性): + // 与"6 段拼色"的颜色完全一致,但不会有段与段之间的接缝横线。 + gd::Color stops[7]; + gd::REAL positions[7]; + for (int index = 0; index < 7; ++index) { - const double hue0 = static_cast(segment) / 6.0; - const double hue1 = static_cast(segment + 1) / 6.0; - const std::array rgb0 = FromHsv(hue0, 1.0, 1.0); - const std::array rgb1 = FromHsv(hue1, 1.0, 1.0); - const std::uint32_t color0 = - 0xFF000000u | (ToByte(rgb0[0]) << 16) | (ToByte(rgb0[1]) << 8) | ToByte(rgb0[2]); - const std::uint32_t color1 = - 0xFF000000u | (ToByte(rgb1[0]) << 16) | (ToByte(rgb1[1]) << 8) | ToByte(rgb1[2]); - - const float top = m_hueRect.Y + m_hueRect.Height * static_cast(hue0); - const float bottom = m_hueRect.Y + m_hueRect.Height * static_cast(hue1); - const gd::Point from(static_cast(std::lround(m_hueRect.X)), - static_cast(std::lround(top))); - const gd::Point to(static_cast(std::lround(m_hueRect.X)), - static_cast(std::lround(bottom))); - gd::LinearGradientBrush brush(from, to, ToGdiColor(color0), ToGdiColor(color1)); - // 渐变端点会各自向两侧延伸半段,逐段微重叠地填充即可无缝拼接。 - g.FillRectangle(&brush, m_hueRect.X, top, m_hueRect.Width, bottom - top); + const double hue = static_cast(index) / 6.0; + const std::array rgb = FromHsv((hue > 1.0) ? 1.0 : hue, 1.0, 1.0); + stops[index] = ToGdiColor(0xFF000000u | (ToByte(rgb[0]) << 16) | (ToByte(rgb[1]) << 8) | + ToByte(rgb[2])); + positions[index] = static_cast(static_cast(index) / 6.0); } + gd::LinearGradientBrush brush( + gd::Point(static_cast(std::lround(m_hueRect.X)), + static_cast(std::lround(m_hueRect.Y))), + gd::Point(static_cast(std::lround(m_hueRect.X)), + static_cast(std::lround(m_hueRect.Y + m_hueRect.Height))), + stops[0], stops[6]); + brush.SetInterpolationColors(stops, positions, 7); + + // 与色域方块**共用同一个圆角**(`kSwatchCorner`),并且同样裁剪进圆角路径: + // 直角填充会在圆角描边之外露出四个尖角(用户报告的"四个角的杂色像素点")。 + gd::GraphicsPath huePath; + UiBuildRoundRectPath(huePath, m_hueRect, kSwatchCorner * m_scale); + const int saveState = g.Save(); + g.SetClip(&huePath); + g.FillRectangle(&brush, m_hueRect); + g.Restore(saveState); UiDrawRoundRect(g, m_hueRect, kSwatchCorner * m_scale, m_theme->border, kGamutBorder); // ---- 色域方块上的选点:中心就是当前选中的颜色,外面只加一圈白色描边 ---- @@ -988,17 +1051,19 @@ namespace jdi const std::size_t slot = static_cast(index); const gd::RectF& track = m_tracks[slot]; - // 通道标签放在滑块同一行的左侧,与轨道/数值纵向对齐。 - const gd::RectF labelRect(track.X - (kLabelWidth + kLabelGap) * m_scale, m_values[slot].Y, - kLabelWidth * m_scale, kRowHeight * m_scale); - UiText(g, kChannelNames[index], m_fonts->uiSmall, labelRect, m_theme->textMuted, TextAlign::LEFT); + // 通道标签放在滑块同一行的左侧,**右对齐**到"轨道左端 − 间隙": + // 这样 R/G/B 三个字母的右边界与轨道的距离严格等于轨道到数值区的距离 + // (用户要求两侧留白相同,且三个字母纵向对齐)。 + const gd::RectF labelRect = ChannelLabelRect(index); + UiText(g, kChannelNames[index], m_fonts->uiSmall, labelRect, m_theme->textMuted, + TextAlign::RIGHT); const bool active = (m_draggingSlider == index) || (m_hoverSlider == index); DrawChannelSlider(g, track, m_channels[slot], kChannelColors[index], active, m_scale); if (m_editField == static_cast(index + 1)) { - UiDrawEditField(g, m_values[slot], m_edit, m_fonts->monoSmall, m_scale); + UiDrawEditField(g, PaddedEditRect(m_values[slot]), m_edit, m_fonts->monoSmall, m_scale); } else { @@ -1014,14 +1079,16 @@ namespace jdi if (m_editField == EditField::HEX) { - UiDrawEditField(g, m_hexRect, m_edit, m_fonts->monoSmall, m_scale); + UiDrawEditField(g, PaddedEditRect(m_hexRect), m_edit, m_fonts->monoSmall, m_scale); } else { wchar_t buffer[16]; swprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), L"#%02X%02X%02X", (color >> 16) & 0xFFu, (color >> 8) & 0xFFu, color & 0xFFu); - UiText(g, std::wstring(buffer), m_fonts->monoSmall, m_hexRect, m_theme->text, TextAlign::LEFT); + // 与三行数值一样**右对齐**:色号的最后一位与数值的最后一位纵向对齐。 + UiText(g, std::wstring(buffer), m_fonts->monoSmall, m_hexRect, m_theme->text, + TextAlign::RIGHT); } } diff --git a/Utilities/Src/PaintUtil.cpp b/Utilities/Src/PaintUtil.cpp index 44779a1..853aaff 100644 --- a/Utilities/Src/PaintUtil.cpp +++ b/Utilities/Src/PaintUtil.cpp @@ -29,45 +29,44 @@ namespace jdi * 固定开销反而不划算,因此在阈值以下仍走 GDI+。 */ const float kGdiFillAreaThreshold = 20000.0f; + } - /** - * @brief 构造圆角矩形路径。 - * @param [IN,OUT] path 目标路径对象。 - * @param [IN] rect 目标矩形。 - * @param [IN] radius 圆角半径。 - */ - void BuildRoundRectPath(gd::GraphicsPath& path, const gd::RectF& rect, float radius) + // 圆角矩形路径是公开的几何构建器:`UiFillRoundRect` / `UiDrawRoundRect` / 需要把位图或渐变 + // **裁剪进同一个圆角形状**的调用方共用它,保证"填充的形状"与"描边的形状"逐点一致。 + void UiBuildRoundRectPath(gd::GraphicsPath& path, const gd::RectF& rect, float radius) + { + if (rect.Width <= 0.0f || rect.Height <= 0.0f) { - if (rect.Width <= 0.0f || rect.Height <= 0.0f) - { - return; - } - - float diameter = radius * 2.0f; - if (diameter > rect.Width) - { - diameter = rect.Width; - } - if (diameter > rect.Height) - { - diameter = rect.Height; - } - if (diameter < 1.0f) - { - path.AddRectangle(rect); - return; - } - - const float right = rect.X + rect.Width; - const float bottom = rect.Y + rect.Height; - - path.AddArc(rect.X, rect.Y, diameter, diameter, 180.0f, 90.0f); - path.AddArc(right - diameter, rect.Y, diameter, diameter, 270.0f, 90.0f); - path.AddArc(right - diameter, bottom - diameter, diameter, diameter, 0.0f, 90.0f); - path.AddArc(rect.X, bottom - diameter, diameter, diameter, 90.0f, 90.0f); - path.CloseFigure(); + return; } + float diameter = radius * 2.0f; + if (diameter > rect.Width) + { + diameter = rect.Width; + } + if (diameter > rect.Height) + { + diameter = rect.Height; + } + if (diameter < 1.0f) + { + path.AddRectangle(rect); + return; + } + + const float right = rect.X + rect.Width; + const float bottom = rect.Y + rect.Height; + + path.AddArc(rect.X, rect.Y, diameter, diameter, 180.0f, 90.0f); + path.AddArc(right - diameter, rect.Y, diameter, diameter, 270.0f, 90.0f); + path.AddArc(right - diameter, bottom - diameter, diameter, diameter, 0.0f, 90.0f); + path.AddArc(rect.X, bottom - diameter, diameter, diameter, 90.0f, 90.0f); + path.CloseFigure(); + } + + namespace + { /** * @brief 创建指定字号的界面字体。 * @param [IN] face 字体名称。 @@ -224,7 +223,7 @@ namespace jdi return; } gd::GraphicsPath path; - BuildRoundRectPath(path, rect, radius); + UiBuildRoundRectPath(path, rect, radius); gd::SolidBrush brush(ToGdiColor(argb)); g.FillPath(&brush, &path); } @@ -236,7 +235,7 @@ namespace jdi return; } gd::GraphicsPath path; - BuildRoundRectPath(path, rect, radius); + UiBuildRoundRectPath(path, rect, radius); gd::Pen pen(ToGdiColor(argb), width); g.DrawPath(&pen, &path); } @@ -255,7 +254,7 @@ namespace jdi { continue; } - BuildRoundRectPath(path, rect, radius); + UiBuildRoundRectPath(path, rect, radius); } if (path.GetPointCount() > 0) { diff --git a/Utilities/Src/PopupMenu.cpp b/Utilities/Src/PopupMenu.cpp index da56bc9..3ffc624 100644 --- a/Utilities/Src/PopupMenu.cpp +++ b/Utilities/Src/PopupMenu.cpp @@ -169,6 +169,33 @@ namespace jdi const float width = contentWidth; const float height = totalHeight; + // 带说明文字的 EDIT 项共用**同一个输入框宽度**:取其中最大的自然宽度, + // 再按“给最宽的说明文字留位 / 不超过内容区 3/4”夹取。 + // ⚠️ 若每项各自按自己的文本长度算,同一面板里就会出现一个宽一个窄 + // (时间轴行的文本更长、还带单位后缀,于是它的输入框比 Y 轴行宽一截)。 + float labeledFieldWidth = 0.0f; + float widestLabel = 0.0f; + for (const MenuItem& item : m_items) + { + if (item.kind != MenuItemKind::EDIT || item.label.empty()) + { + continue; + } + labeledFieldWidth = std::max(labeledFieldWidth, NaturalEditFieldWidth(item)); + widestLabel = std::max(widestLabel, + UiMeasureTextWidth(m_fonts->uiSmall, item.label) + + kEditFieldGap * m_scale); + } + if (labeledFieldWidth > 0.0f) + { + const float rowContent = + std::max(40.0f * m_scale, (width - 2.0f * (kPadH + kItemPad) * m_scale)); + const float limit = std::max(40.0f * m_scale, + std::min(rowContent * 0.75f, rowContent - widestLabel)); + labeledFieldWidth = std::min(labeledFieldWidth, limit); + } + m_editFieldWidth = labeledFieldWidth; + float left = static_cast(x); float top = static_cast(y); @@ -374,6 +401,8 @@ namespace jdi // 避免菜单被最大宽度夹取后输入框把左侧标签挤成省略号。 // ⚠️ 说明文字为空(例如「系数调整」面板的整行输入框)时不设 3/4 上限, // 让输入框独占整行。 + // ⚠️ 带说明文字的项统一使用排布阶段算好的 `m_editFieldWidth`: + // 同一面板里所有这类输入框必须一样宽、右边界对齐。 const gd::RectF& rect = m_itemRects[static_cast(index)]; const float contentWidth = std::max(40.0f * m_scale, rect.Width - 2.0f * kItemPad * m_scale); const float labelWidth = UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale; @@ -386,6 +415,11 @@ namespace jdi } else { + if (m_editFieldWidth > 0.0f) + { + width = m_editFieldWidth; + } + width = std::min(width, std::max(40.0f * m_scale, contentWidth - labelWidth)); width = std::min(width, contentWidth * 0.75f); }