按钮与面板: - 「系数」按钮改名为「系数/偏移」;点击展开的二级面板在原「系数调整」输入框下方新增「偏移调整」: 与「系数调整」同一套绘制(标题 + 标题栏右侧的还原图标),下面两行分别是「Y轴」「时间轴」, 每行左侧说明文字、右侧内联输入框(整行数值框)。 - 「Y轴」默认 0.00,非编辑态**始终带正负号**(+0.00 / -12.50),小数不足两位时补两位、更高精度保留更多位。 - 「时间轴」默认 0.00,非编辑态显示与双光标差值同一套复合单位文本(`Δt:500ms`、`Δt:1h 1min 2s`); 进入编辑态时显示以**秒**为单位的数值,并在输入框最右侧提示单位 `s`。 - 「偏移调整」的还原图标把两个偏移恢复为默认值 0;「系数调整」的还原图标仍只重置系数。 - 面板/输入框的编辑初值与显示文本分离(`MenuItem::editValue` / `editSuffix`), 非编辑态(`Δt:…`)与编辑态(`3662`)不一致时也能正确回显。 语义与结算顺序: - 结算顺序变为 **系数 → 绝对值 → 镜像 → 偏移**(前三步不变)。 - Y 偏移:给绘制值加常数(波形上下移动);时间偏移:采样绘制在 `t + offsetTime` 处 (例如 t=1s、偏移 -0.5s → 画在 0.5s)。 - ⚠️ Y 偏移以**未镜像空间**为准:垂直镜像开关变化时同步取反偏移量, 使绘制等价于「先加上偏移再镜像」(开关来回切换可逆)。 - 绘制链路全部跟随偏移:`BuildPolyline`(逐点描边 / 像素列包络 / 跨列边界插值 / 粗粒度分组层, 保序的 Y 偏移直接加在分组极值上)、派生预览曲线、光标取值读数(含 Δt)、 可见区间 Y 轴自适应、整段自适应的区间映射;折线缓存键、场景指纹、样式快照与图层指纹都纳入两个偏移。 派生信号(实验性功能「构建为新信号」): - 修复「预览是调整后的波形、构建出来却按原始状态绘制」:新信号**继承源信号的调整变换** (系数/绝对值/两个镜像/两个偏移)——网格预览绘制的是 `变换(滤波结果)`, 而构建出的信号只存了滤波结果,因此必须把变换一并继承; 时间轴偏移**只能**靠继承(派生信号与源信号共用同一文件时间轴,无法把时间平移烘进数据)。 离屏验证(不干扰用户): - ⚠️ harness 现在在每次注入鼠标消息后**立即释放本窗口的鼠标捕获** (宿主 WM_LBUTTONDOWN 会 SetCapture,隐藏窗口同样会把用户的真实鼠标输入吸走); 后续消息本来就是直接投递,无需捕获。 - 新增 [AP]:按钮标签不裁切、面板 5 行顺序与「偏移调整」位于系数输入框下方、 Y轴/时间轴行的标签与输入框同行且默认值/单位提示正确、时间轴编辑态显示秒值并在最右侧提示 `s`、 Y 偏移提交/显示正负号/波形整体平移量、时间偏移提交/Δt 复合单位(含 3662 → Δt:1h 1min 2s)/ 编辑态为秒、**按"偏移若干个采样点"构造的精确几何验证**(采样被画到移位后的像素位置、且不再画在原处)、 读数跟随、两个还原图标、垂直镜像对 Y 偏移取反(可逆)、 以及派生信号继承变换;[AL] 补充「系数→绝对值→镜像→偏移」的顺序与区间映射(含 Y 偏移)。 - 合计 645/645 通过;make clean && make all 零警告。
1469 lines
46 KiB
C++
1469 lines
46 KiB
C++
#include "PopupMenu.hpp"
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
|
||
namespace jdi
|
||
{
|
||
|
||
namespace
|
||
{
|
||
const float kRowHeight = 27.0f; /**< 普通项高度。 */
|
||
const float kHeaderHeight = 22.0f; /**< 分组标题高度。 */
|
||
const float kSeparatorHeight = 9.0f; /**< 分隔线高度。 */
|
||
const float kSwatchRowHeight = 32.0f; /**< 色块行高度。 */
|
||
const float kPadV = 5.0f; /**< 菜单上下内边距。 */
|
||
const float kPadH = 5.0f; /**< 菜单左右内边距。 */
|
||
const float kItemPad = 8.0f; /**< 条目内文本的左右内缩(所有条目统一)。 */
|
||
const float kMarkerColumn = 18.0f; /**< 条目**右侧**的选中标记列宽度(对勾画在这里)。 */
|
||
const float kMarkerGap = 4.0f; /**< 文本与右侧标记列之间的间隔。 */
|
||
const float kMinWidth = 148.0f; /**< 菜单最小宽度。 */
|
||
const float kMaxWidth = 340.0f; /**< 菜单最大宽度。 */
|
||
const float kCorner = 6.0f; /**< 菜单圆角半径。 */
|
||
const float kSwatchSize = 22.0f; /**< 色块尺寸。 */
|
||
const float kSwatchGap = 5.0f; /**< 色块间距。 */
|
||
const float kEditFieldWidth = 88.0f; /**< EDIT 项输入框宽度。 */
|
||
const float kEditFieldGap = 6.0f; /**< 说明文字与输入框之间的最小间隔。 */
|
||
const float kEditFieldPadV = 5.0f; /**< 输入框上下内缩。 */
|
||
const float kSubArrowSize = 4.5f; /**< 子菜单指示箭头尺寸。 */
|
||
const float kSubOverlap = 2.0f; /**< 子菜单与主菜单重叠的宽度(避免中间露出缝隙)。 */
|
||
const float kHeaderIconSize = 16.0f; /**< `HEADER_ACTION` 右侧图标按钮的边长。 */
|
||
const float kHeaderIconGap = 6.0f; /**< 标题文字与图标按钮之间的最小间隔。 */
|
||
const float kRowFieldWidth = 132.0f; /**< 整行输入框(无说明文字)的默认宽度。 */
|
||
|
||
/**
|
||
* @brief 绘制「还原/重置」图标(逆时针回环箭头)。
|
||
*
|
||
* 用一段圆弧 + 一个三角箭头拼成,与界面里其它自绘图标(加号、叉号)保持同一笔画风格。
|
||
* 不依赖任何字体或资源,因此在任何 DPI 下都清晰。
|
||
*
|
||
* @param [IN,OUT] g 绘图上下文。
|
||
* @param [IN] rect 图标外接矩形(正方形)。
|
||
* @param [IN] color 笔画颜色。
|
||
* @param [IN] scale DPI 缩放比例。
|
||
*/
|
||
void PaintResetIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t color, float scale)
|
||
{
|
||
const float inset = 2.5f * scale;
|
||
gd::RectF box(rect.X + inset, rect.Y + inset, rect.Width - inset * 2.0f,
|
||
rect.Height - inset * 2.0f);
|
||
if (box.Width <= 1.0f || box.Height <= 1.0f)
|
||
{
|
||
return;
|
||
}
|
||
gd::Pen pen(gd::Color((color >> 24) & 0xFFu, (color >> 16) & 0xFFu, (color >> 8) & 0xFFu,
|
||
color & 0xFFu),
|
||
std::max(1.0f, 1.5f * scale));
|
||
pen.SetStartCap(gd::LineCapRound);
|
||
pen.SetEndCap(gd::LineCapRound);
|
||
// 缺口留在右上角(从 300° 到 20° 处断开),与常见的"重置/刷新"图标一致。
|
||
g.DrawArc(&pen, box.X, box.Y, box.Width, box.Height, 300.0f, 300.0f);
|
||
|
||
// 箭头:位于缺口末端(约 300° 方向),指向前进方向。
|
||
const float cx = box.X + box.Width * 0.5f;
|
||
const float cy = box.Y + box.Height * 0.5f;
|
||
const float radius = box.Width * 0.5f;
|
||
const float angle = 300.0f * 3.14159265f / 180.0f;
|
||
const float tipX = cx + std::cos(angle) * radius;
|
||
const float tipY = cy + std::sin(angle) * radius;
|
||
const float size = std::max(2.5f, 3.2f * scale);
|
||
gd::PointF arrow[3];
|
||
arrow[0] = gd::PointF(tipX, tipY);
|
||
arrow[1] = gd::PointF(tipX - size, tipY - size * 0.25f);
|
||
arrow[2] = gd::PointF(tipX - size * 0.25f, tipY - size);
|
||
gd::SolidBrush brush(gd::Color((color >> 24) & 0xFFu, (color >> 16) & 0xFFu,
|
||
(color >> 8) & 0xFFu, color & 0xFFu));
|
||
g.FillPolygon(&brush, arrow, 3);
|
||
}
|
||
}
|
||
|
||
PopupMenu::PopupMenu() = default;
|
||
|
||
void PopupMenu::Setup(const UiFonts* fonts, const Theme* theme)
|
||
{
|
||
m_fonts = fonts;
|
||
m_theme = theme;
|
||
}
|
||
|
||
void PopupMenu::SetScale(float scale)
|
||
{
|
||
m_scale = (scale > 0.1f) ? scale : 1.0f;
|
||
}
|
||
|
||
float PopupMenu::ItemHeight(const MenuItem& item) const
|
||
{
|
||
switch (item.kind)
|
||
{
|
||
case MenuItemKind::SEPARATOR:
|
||
return kSeparatorHeight * m_scale;
|
||
case MenuItemKind::HEADER:
|
||
case MenuItemKind::HEADER_ACTION:
|
||
return kHeaderHeight * m_scale;
|
||
case MenuItemKind::SWATCHES:
|
||
return kSwatchRowHeight * m_scale;
|
||
case MenuItemKind::EDIT:
|
||
// 编辑项比普通项略高,给输入框留出上下呼吸空间。
|
||
return (kRowHeight + 4.0f) * m_scale;
|
||
case MenuItemKind::NORMAL:
|
||
default:
|
||
return kRowHeight * m_scale;
|
||
}
|
||
}
|
||
|
||
void PopupMenu::BuildLayout(const gd::RectF& bounds, int x, int y)
|
||
{
|
||
m_itemRects.assign(m_items.size(), gd::RectF());
|
||
m_swatchRects.assign(m_items.size(), std::vector<gd::RectF>());
|
||
|
||
if (m_fonts == nullptr)
|
||
{
|
||
m_rect = gd::RectF();
|
||
return;
|
||
}
|
||
|
||
float contentWidth = kMinWidth * m_scale;
|
||
float totalHeight = kPadV * 2.0f * m_scale;
|
||
|
||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||
{
|
||
const MenuItem& item = m_items[index];
|
||
totalHeight += ItemHeight(item);
|
||
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
const int count = std::max(0, std::min(item.swatchCount, 16));
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||
static_cast<float>(count) * kSwatchSize * m_scale +
|
||
static_cast<float>(std::max(0, count - 1)) * kSwatchGap * m_scale;
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
else if (item.kind == MenuItemKind::HEADER || item.kind == MenuItemKind::HEADER_ACTION)
|
||
{
|
||
// 标题与普通项共用同一文字列与右侧标记列,保证整个菜单的左右边距一致。
|
||
const float iconExtra = (item.kind == MenuItemKind::HEADER_ACTION)
|
||
? (kHeaderIconSize + kHeaderIconGap) * m_scale
|
||
: 0.0f;
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||
kMarkerGap * m_scale + iconExtra +
|
||
UiMeasureTextWidth(m_fonts->uiSmallBold, item.label);
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
else if (item.kind == MenuItemKind::NORMAL)
|
||
{
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||
kMarkerGap * m_scale + UiMeasureTextWidth(m_fonts->uiSmall, item.label);
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
else if (item.kind == MenuItemKind::EDIT)
|
||
{
|
||
// 说明文字 + 输入框;输入框宽度按数值文本自适应(与 EditFieldRect 保持一致)。
|
||
const float fieldWidth = NaturalEditFieldWidth(item);
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||
UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale +
|
||
fieldWidth;
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
}
|
||
|
||
contentWidth = std::min(contentWidth, kMaxWidth * m_scale);
|
||
const float width = contentWidth;
|
||
const float height = totalHeight;
|
||
|
||
float left = static_cast<float>(x);
|
||
float top = static_cast<float>(y);
|
||
|
||
// 优先向右下展开;越界时向左或向上翻转,最后做边界收敛。
|
||
if (left + width > bounds.X + bounds.Width)
|
||
{
|
||
left = static_cast<float>(x) - width;
|
||
}
|
||
if (top + height > bounds.Y + bounds.Height)
|
||
{
|
||
top = static_cast<float>(y) - height;
|
||
}
|
||
left = std::max(bounds.X + 2.0f, std::min(left, bounds.X + bounds.Width - width - 2.0f));
|
||
top = std::max(bounds.Y + 2.0f, std::min(top, bounds.Y + bounds.Height - height - 2.0f));
|
||
|
||
m_rect = gd::RectF(left, top, width, height);
|
||
|
||
float cursorY = top + kPadV * m_scale;
|
||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||
{
|
||
const MenuItem& item = m_items[index];
|
||
const float itemHeight = ItemHeight(item);
|
||
m_itemRects[index] = gd::RectF(left + kPadH * m_scale, cursorY,
|
||
std::max(0.0f, width - kPadH * 2.0f * m_scale), itemHeight);
|
||
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
const int count = std::max(0, std::min(item.swatchCount, 16));
|
||
std::vector<gd::RectF>& rects = m_swatchRects[index];
|
||
rects.reserve(static_cast<std::size_t>(count));
|
||
const float swatchY = cursorY + (itemHeight - kSwatchSize * m_scale) * 0.5f;
|
||
const float startX = left + (kPadH + kItemPad) * m_scale;
|
||
for (int swatch = 0; swatch < count; ++swatch)
|
||
{
|
||
const float swatchX =
|
||
startX + static_cast<float>(swatch) * (kSwatchSize + kSwatchGap) * m_scale;
|
||
rects.push_back(gd::RectF(swatchX, swatchY, kSwatchSize * m_scale, kSwatchSize * m_scale));
|
||
}
|
||
}
|
||
|
||
cursorY += itemHeight;
|
||
}
|
||
}
|
||
|
||
float PopupMenu::NaturalEditFieldWidth(const MenuItem& item) const
|
||
{
|
||
const std::wstring& widest =
|
||
(item.editValue.size() > item.value.size()) ? item.editValue : item.value;
|
||
const float natural = std::max(kEditFieldWidth * m_scale,
|
||
UiMeasureTextWidth(m_fonts->uiSmall, widest) + 20.0f * m_scale);
|
||
const float withSuffix = natural + EditSuffixWidth(item);
|
||
// 无说明文字 = 整行输入框(如「系数调整」面板):给一个更宽的默认值,视觉上独占整行。
|
||
return item.label.empty() ? std::max(withSuffix, kRowFieldWidth * m_scale) : withSuffix;
|
||
}
|
||
|
||
void PopupMenu::CloseSubmenu()
|
||
{
|
||
m_subOpen = false;
|
||
m_subParentIndex = -1;
|
||
m_subRect = gd::RectF();
|
||
m_subItems.clear();
|
||
m_subItemRects.clear();
|
||
m_subHoverIndex = -1;
|
||
m_subPressedIndex = -1;
|
||
}
|
||
|
||
void PopupMenu::BuildSubLayout(int parentIndex)
|
||
{
|
||
CloseSubmenu();
|
||
if (parentIndex < 0 || parentIndex >= static_cast<int>(m_items.size()))
|
||
{
|
||
return;
|
||
}
|
||
const MenuItem& parent = m_items[static_cast<std::size_t>(parentIndex)];
|
||
if (parent.kind != MenuItemKind::SUBMENU || parent.submenu.empty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
m_subItems = parent.submenu;
|
||
|
||
// 与主菜单一致:宽度取条目中的最大者并夹在上下限之间。
|
||
float contentWidth = kMinWidth * m_scale;
|
||
float totalHeight = kPadV * 2.0f * m_scale;
|
||
for (const MenuItem& item : m_subItems)
|
||
{
|
||
totalHeight += ItemHeight(item);
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
const int count = std::max(0, std::min(item.swatchCount, 16));
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||
static_cast<float>(count) * kSwatchSize * m_scale +
|
||
static_cast<float>(std::max(0, count - 1)) * kSwatchGap * m_scale;
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
else if (item.kind == MenuItemKind::HEADER)
|
||
{
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||
kMarkerGap * m_scale +
|
||
UiMeasureTextWidth(m_fonts->uiSmallBold, item.label);
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
else if (item.kind == MenuItemKind::EDIT)
|
||
{
|
||
const float fieldWidth = NaturalEditFieldWidth(item);
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||
UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale +
|
||
fieldWidth;
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
else if (item.kind == MenuItemKind::NORMAL || item.kind == MenuItemKind::SUBMENU)
|
||
{
|
||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||
kMarkerGap * m_scale + UiMeasureTextWidth(m_fonts->uiSmall, item.label);
|
||
contentWidth = std::max(contentWidth, width);
|
||
}
|
||
}
|
||
contentWidth = std::min(contentWidth, kMaxWidth * m_scale);
|
||
|
||
const float width = contentWidth;
|
||
const float height = totalHeight;
|
||
const gd::RectF& parentRect = m_itemRects[static_cast<std::size_t>(parentIndex)];
|
||
const gd::RectF& bounds = m_buildBounds;
|
||
|
||
// 默认贴主菜单右缘并向上与父项顶端对齐;右侧空间不足时翻到左侧。
|
||
float left = m_rect.X + m_rect.Width - kSubOverlap * m_scale;
|
||
if (left + width > bounds.X + bounds.Width - 2.0f)
|
||
{
|
||
left = m_rect.X - width + kSubOverlap * m_scale;
|
||
}
|
||
left = std::max(bounds.X + 2.0f, std::min(left, bounds.X + bounds.Width - width - 2.0f));
|
||
|
||
float top = parentRect.Y - kPadV * m_scale;
|
||
top = std::max(bounds.Y + 2.0f, std::min(top, bounds.Y + bounds.Height - height - 2.0f));
|
||
|
||
m_subRect = gd::RectF(left, top, width, height);
|
||
m_subItemRects.assign(m_subItems.size(), gd::RectF());
|
||
|
||
float cursorY = top + kPadV * m_scale;
|
||
for (std::size_t index = 0; index < m_subItems.size(); ++index)
|
||
{
|
||
const float itemHeight = ItemHeight(m_subItems[index]);
|
||
m_subItemRects[index] = gd::RectF(left + kPadH * m_scale, cursorY,
|
||
std::max(0.0f, width - kPadH * 2.0f * m_scale), itemHeight);
|
||
cursorY += itemHeight;
|
||
}
|
||
|
||
m_subParentIndex = parentIndex;
|
||
m_subOpen = true;
|
||
m_subHoverIndex = -1;
|
||
m_subPressedIndex = -1;
|
||
}
|
||
|
||
int PopupMenu::SubItemAtPoint(int x, int y) const
|
||
{
|
||
if (!m_subOpen)
|
||
{
|
||
return -1;
|
||
}
|
||
const float fx = static_cast<float>(x);
|
||
const float fy = static_cast<float>(y);
|
||
for (std::size_t index = 0; index < m_subItems.size(); ++index)
|
||
{
|
||
const MenuItem& item = m_subItems[index];
|
||
if (item.kind == MenuItemKind::SEPARATOR || item.kind == MenuItemKind::HEADER)
|
||
{
|
||
continue;
|
||
}
|
||
if (!item.enabled)
|
||
{
|
||
continue;
|
||
}
|
||
if (UiPointInRect(m_subItemRects[index], fx, fy))
|
||
{
|
||
return static_cast<int>(index);
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
float PopupMenu::EditSuffixWidth(const MenuItem& item) const
|
||
{
|
||
if (item.kind != MenuItemKind::EDIT || item.editSuffix.empty())
|
||
{
|
||
return 0.0f;
|
||
}
|
||
return UiMeasureTextWidth(m_fonts->uiSmall, item.editSuffix) + 8.0f * m_scale;
|
||
}
|
||
|
||
gd::RectF PopupMenu::EditFieldRect(int index) const
|
||
{
|
||
if (index < 0 || index >= static_cast<int>(m_items.size()))
|
||
{
|
||
return gd::RectF();
|
||
}
|
||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||
if (item.kind != MenuItemKind::EDIT)
|
||
{
|
||
return gd::RectF();
|
||
}
|
||
|
||
// 输入框靠右对齐到条目内容区右边界;宽度取自然宽度,但必须给说明文字留出自然宽度,
|
||
// 避免菜单被最大宽度夹取后输入框把左侧标签挤成省略号。
|
||
// ⚠️ 说明文字为空(例如「系数调整」面板的整行输入框)时不设 3/4 上限,
|
||
// 让输入框独占整行。
|
||
const gd::RectF& rect = m_itemRects[static_cast<std::size_t>(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;
|
||
float width = NaturalEditFieldWidth(item);
|
||
width = std::min(width, std::max(40.0f * m_scale, contentWidth - labelWidth));
|
||
if (item.label.empty())
|
||
{
|
||
// 整行输入框(无说明文字):占满内容区,视觉上就是一个独立的一行输入框。
|
||
width = contentWidth;
|
||
}
|
||
else
|
||
{
|
||
width = std::min(width, contentWidth * 0.75f);
|
||
}
|
||
|
||
const float height = rect.Height - kEditFieldPadV * 2.0f * m_scale;
|
||
return gd::RectF(rect.X + rect.Width - kItemPad * m_scale - width,
|
||
rect.Y + (rect.Height - height) * 0.5f, width, height);
|
||
}
|
||
|
||
gd::RectF PopupMenu::HeaderIconRect(int index) const
|
||
{
|
||
if (index < 0 || index >= static_cast<int>(m_items.size()))
|
||
{
|
||
return gd::RectF();
|
||
}
|
||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||
if (item.kind != MenuItemKind::HEADER_ACTION)
|
||
{
|
||
return gd::RectF();
|
||
}
|
||
const gd::RectF& rect = m_itemRects[static_cast<std::size_t>(index)];
|
||
const float size = std::min(kHeaderIconSize * m_scale, std::max(6.0f, rect.Height - 4.0f * m_scale));
|
||
return gd::RectF(rect.X + rect.Width - kItemPad * m_scale - size,
|
||
rect.Y + (rect.Height - size) * 0.5f, size, size);
|
||
}
|
||
|
||
bool PopupMenu::IsOnEditField(int x, int y) const
|
||
{
|
||
if (m_editIndex < 0)
|
||
{ return false;
|
||
}
|
||
const gd::RectF rect = EditFieldRect(m_editIndex);
|
||
return UiPointInRect(rect, static_cast<float>(x), static_cast<float>(y));
|
||
}
|
||
|
||
bool PopupMenu::BeginItemEdit(int index)
|
||
{
|
||
if (!m_open || index < 0 || index >= static_cast<int>(m_items.size()))
|
||
{
|
||
return false;
|
||
}
|
||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||
if (item.kind != MenuItemKind::EDIT || !item.enabled)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 输入框初值取「编辑初值」(未设置时用显示值),整段选中,便于直接键入新数值。
|
||
m_edit.Begin(item.editValue.empty() ? item.value : item.editValue, true, item.filter, 0u);
|
||
m_editIndex = index;
|
||
m_hoverIndex = index;
|
||
return true;
|
||
}
|
||
|
||
bool PopupMenu::EndItemEdit(bool commit, MenuResult& result)
|
||
{
|
||
if (m_editIndex < 0)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
const int index = m_editIndex;
|
||
const std::wstring text = m_edit.Text();
|
||
m_editIndex = -1;
|
||
m_edit.End();
|
||
|
||
if (!commit || index >= static_cast<int>(m_items.size()))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||
// 立即回写显示值,使菜单上的数值与数据层一致(宿主会在派发后再纠正为夹取后的真实值);
|
||
// 键入文本同时记为**编辑初值**(`value` 与 `editValue` 不一致的行也能正确回显上次输入)。
|
||
item.value = text;
|
||
item.editValue = text;
|
||
|
||
result.valid = true;
|
||
result.scope = m_scope;
|
||
result.id = item.id;
|
||
result.editCommit = true;
|
||
result.editText = text;
|
||
return true;
|
||
}
|
||
|
||
void PopupMenu::RebuildLayout()
|
||
{
|
||
if (!m_open)
|
||
{
|
||
return;
|
||
}
|
||
const int subParent = m_subParentIndex;
|
||
BuildLayout(m_buildBounds, m_buildX, m_buildY);
|
||
// 主菜单重新排布后子菜单位置会失效:若之前是展开的则按新几何重建。
|
||
if (subParent >= 0)
|
||
{
|
||
BuildSubLayout(subParent);
|
||
}
|
||
}
|
||
|
||
bool PopupMenu::UpdateItemValue(int id, const std::wstring& value, const std::wstring& editValue)
|
||
{
|
||
for (MenuItem& item : m_items)
|
||
{
|
||
if (item.id == id && item.kind == MenuItemKind::EDIT)
|
||
{
|
||
const bool valueSame = (item.value == value);
|
||
const bool editSame = editValue.empty() || (item.editValue == editValue);
|
||
if (valueSame && editSame)
|
||
{
|
||
return true;
|
||
}
|
||
item.value = value;
|
||
if (!editValue.empty())
|
||
{
|
||
item.editValue = editValue;
|
||
}
|
||
RebuildLayout();
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool PopupMenu::UpdateItemLabel(int id, const std::wstring& label, bool checked, bool enabled)
|
||
{
|
||
for (MenuItem& item : m_items)
|
||
{
|
||
if (item.id == id && item.kind == MenuItemKind::NORMAL)
|
||
{
|
||
if (item.label == label && item.checked == checked && item.enabled == enabled)
|
||
{
|
||
return true;
|
||
}
|
||
item.label = label;
|
||
item.checked = checked;
|
||
item.enabled = enabled;
|
||
RebuildLayout();
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool PopupMenu::OnEditMouseDown(int x, int y)
|
||
{
|
||
if (m_editIndex < 0)
|
||
{
|
||
return false;
|
||
}
|
||
return UiEditFieldMouseDown(m_fonts->uiSmall, EditFieldRect(m_editIndex), m_edit, m_scale, x, y);
|
||
}
|
||
|
||
bool PopupMenu::OnEditMouseMove(int x)
|
||
{
|
||
if (m_editIndex < 0)
|
||
{
|
||
return false;
|
||
}
|
||
return UiEditFieldMouseMove(m_fonts->uiSmall, EditFieldRect(m_editIndex), m_edit, m_scale, x);
|
||
}
|
||
|
||
bool PopupMenu::OnEditMouseUp()
|
||
{
|
||
if (!m_edit.Dragging())
|
||
{
|
||
return false;
|
||
}
|
||
m_edit.EndDrag();
|
||
return true;
|
||
}
|
||
|
||
bool PopupMenu::OnChar(wchar_t character, MenuResult& result)
|
||
{
|
||
result = MenuResult();
|
||
if (m_editIndex < 0)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 剪贴板与全选快捷键经 WM_CHAR 送达(Ctrl+C/X/V/A 分别对应 3/24/22/1)。
|
||
switch (character)
|
||
{
|
||
case 0x03: // Ctrl+C
|
||
m_edit.CopyToClipboard(m_host);
|
||
return false;
|
||
case 0x18: // Ctrl+X
|
||
return m_edit.CutToClipboard(m_host);
|
||
case 0x16: // Ctrl+V
|
||
return m_edit.PasteFromClipboard(m_host);
|
||
case 0x01: // Ctrl+A
|
||
m_edit.SelectAll();
|
||
return true;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
if (character == L'\r' || character == L'\n')
|
||
{
|
||
// 回车提交:菜单保持打开,仅把新数值回传宿主。
|
||
EndItemEdit(true, result);
|
||
return true;
|
||
}
|
||
// 退格/删除已由 WM_KEYDOWN 处理,此处忽略控制字符避免重复删除。
|
||
if (character < 0x20 || character == 0x7F)
|
||
{
|
||
return false;
|
||
}
|
||
return m_edit.Insert(character);
|
||
}
|
||
|
||
void PopupMenu::Open(MenuScope scope, const gd::RectF& bounds, int x, int y,
|
||
const std::vector<MenuItem>& items)
|
||
{
|
||
m_items = items;
|
||
if (m_items.empty())
|
||
{
|
||
Close();
|
||
return;
|
||
}
|
||
|
||
m_scope = scope;
|
||
m_open = true;
|
||
m_hoverIndex = -1;
|
||
m_hoverIconIndex = -1;
|
||
m_hoverSwatch = -1;
|
||
m_pressedIndex = -1;
|
||
m_pressedSwatch = -1;
|
||
m_buildBounds = bounds;
|
||
m_buildX = x;
|
||
m_buildY = y;
|
||
|
||
CloseSubmenu();
|
||
CloseEditState();
|
||
BuildLayout(bounds, x, y);
|
||
}
|
||
|
||
void PopupMenu::Close()
|
||
{
|
||
m_open = false;
|
||
m_scope = MenuScope::NONE;
|
||
m_items.clear();
|
||
m_itemRects.clear();
|
||
m_swatchRects.clear();
|
||
m_hoverIndex = -1;
|
||
m_hoverIconIndex = -1;
|
||
m_hoverSwatch = -1;
|
||
m_pressedIndex = -1;
|
||
m_pressedSwatch = -1;
|
||
CloseSubmenu();
|
||
CloseEditState();
|
||
}
|
||
|
||
void PopupMenu::CloseEditState()
|
||
{
|
||
if (m_editIndex >= 0)
|
||
{
|
||
m_edit.End();
|
||
}
|
||
m_editIndex = -1;
|
||
}
|
||
|
||
bool PopupMenu::ContainsPoint(int x, int y) const
|
||
{
|
||
if (!m_open)
|
||
{
|
||
return false;
|
||
}
|
||
const float fx = static_cast<float>(x);
|
||
const float fy = static_cast<float>(y);
|
||
if (UiPointInRect(m_rect, fx, fy))
|
||
{
|
||
return true;
|
||
}
|
||
// 展开的二级子菜单同样属于菜单范围(否则在子菜单内点击会被当成“点菜单外”)。
|
||
return m_subOpen && UiPointInRect(m_subRect, fx, fy);
|
||
}
|
||
|
||
int PopupMenu::ItemAtPoint(int x, int y, int& swatchIndex) const
|
||
{
|
||
swatchIndex = -1;
|
||
if (!m_open)
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
const float fx = static_cast<float>(x);
|
||
const float fy = static_cast<float>(y);
|
||
|
||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||
{
|
||
const MenuItem& item = m_items[index];
|
||
if (item.kind == MenuItemKind::SEPARATOR || item.kind == MenuItemKind::HEADER)
|
||
{
|
||
continue;
|
||
}
|
||
if (!UiPointInRect(m_itemRects[index], fx, fy))
|
||
{
|
||
continue; }
|
||
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
const std::vector<gd::RectF>& rects = m_swatchRects[index];
|
||
for (std::size_t swatch = 0; swatch < rects.size(); ++swatch)
|
||
{
|
||
if (UiPointInRect(rects[swatch], fx, fy))
|
||
{
|
||
swatchIndex = static_cast<int>(swatch);
|
||
return static_cast<int>(index);
|
||
}
|
||
}
|
||
// 落在色块行的空白处:整行视为可点击,用于定位提示。
|
||
return -1;
|
||
}
|
||
|
||
if (!item.enabled)
|
||
{
|
||
return -1;
|
||
}
|
||
return static_cast<int>(index);
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
void PopupMenu::MoveHover(int step)
|
||
{
|
||
if (m_items.empty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
const int count = static_cast<int>(m_items.size());
|
||
int index = m_hoverIndex;
|
||
for (int attempt = 0; attempt < count; ++attempt)
|
||
{
|
||
index += step;
|
||
if (index < 0)
|
||
{
|
||
index = count - 1;
|
||
}
|
||
else if (index >= count)
|
||
{
|
||
index = 0;
|
||
}
|
||
|
||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||
if ((item.kind == MenuItemKind::NORMAL || item.kind == MenuItemKind::EDIT) && item.enabled)
|
||
{
|
||
m_hoverIndex = index;
|
||
m_hoverSwatch = -1;
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
bool PopupMenu::CommitHover(MenuResult& result)
|
||
{
|
||
if (m_hoverIndex < 0 || m_hoverIndex >= static_cast<int>(m_items.size()))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
const MenuItem& item = m_items[static_cast<std::size_t>(m_hoverIndex)];
|
||
if (!item.enabled)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 编辑项:回车等价于单击进入编辑,菜单不关闭。
|
||
if (item.kind == MenuItemKind::EDIT)
|
||
{
|
||
BeginItemEdit(m_hoverIndex);
|
||
return false;
|
||
}
|
||
if (item.kind != MenuItemKind::NORMAL)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
result.valid = true;
|
||
result.scope = m_scope;
|
||
result.id = item.id;
|
||
result.swatchIndex = -1;
|
||
result.itemRect = m_itemRects[static_cast<std::size_t>(m_hoverIndex)];
|
||
return true;
|
||
}
|
||
|
||
bool PopupMenu::OnMouseMove(int x, int y)
|
||
{
|
||
if (!m_open)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
const float fx = static_cast<float>(x);
|
||
const float fy = static_cast<float>(y);
|
||
bool changed = false;
|
||
|
||
// 鼠标在展开的子菜单内:只更新子菜单的悬停项(主菜单父项保持高亮)。
|
||
if (m_subOpen && UiPointInRect(m_subRect, fx, fy))
|
||
{
|
||
const int sub = SubItemAtPoint(x, y);
|
||
if (sub != m_subHoverIndex)
|
||
{
|
||
m_subHoverIndex = sub;
|
||
changed = true;
|
||
}
|
||
return changed;
|
||
}
|
||
|
||
int swatchIndex = -1;
|
||
const int index = ItemAtPoint(x, y, swatchIndex);
|
||
const bool onSubmenuParent =
|
||
(index >= 0) && (m_items[static_cast<std::size_t>(index)].kind == MenuItemKind::SUBMENU);
|
||
|
||
// 子菜单展开期间,鼠标只要仍停留在「主菜单 ∪ 子菜单」的范围内且尚未落到其它可点击
|
||
// 条目上(例如掠过父项右缘与子菜单之间的内边距窄带、分隔线或禁用项),就不收起子菜单。
|
||
// 这样可避免处理器速度较快、鼠标移动事件过密时,用户还没把指针移到子菜单上,
|
||
// 子菜单就被中途误关闭;只有明确移到其它条目或完全离开该范围时才收起。
|
||
if (m_subOpen && !onSubmenuParent && index < 0 &&
|
||
(UiPointInRect(m_rect, fx, fy) || UiPointInRect(m_subRect, fx, fy)))
|
||
{
|
||
// 保持在父项高亮,视觉上指明子菜单的来源。
|
||
if (m_hoverIndex != m_subParentIndex || m_hoverSwatch != -1)
|
||
{
|
||
m_hoverIndex = m_subParentIndex;
|
||
m_hoverSwatch = -1;
|
||
changed = true;
|
||
}
|
||
return changed;
|
||
}
|
||
|
||
if (index != m_hoverIndex || swatchIndex != m_hoverSwatch)
|
||
{
|
||
m_hoverIndex = index;
|
||
m_hoverSwatch = swatchIndex;
|
||
changed = true;
|
||
}
|
||
|
||
// `HEADER_ACTION` 的图标按钮单独跟踪悬停状态(用于高亮,其余标题区域不响应)。
|
||
{
|
||
int iconHover = -1;
|
||
for (std::size_t itemIndex = 0; itemIndex < m_items.size(); ++itemIndex)
|
||
{
|
||
if (m_items[itemIndex].kind != MenuItemKind::HEADER_ACTION)
|
||
{
|
||
continue;
|
||
}
|
||
const gd::RectF icon = HeaderIconRect(static_cast<int>(itemIndex));
|
||
if (icon.Width > 0.0f && UiPointInRect(icon, fx, fy))
|
||
{
|
||
iconHover = static_cast<int>(itemIndex);
|
||
break;
|
||
}
|
||
}
|
||
if (iconHover != m_hoverIconIndex)
|
||
{
|
||
m_hoverIconIndex = iconHover;
|
||
changed = true;
|
||
}
|
||
}
|
||
|
||
// 悬停到带子菜单的项 → 向右展开;移开到其它条目或离开菜单 → 收起。
|
||
if (onSubmenuParent)
|
||
{
|
||
if (!m_subOpen || m_subParentIndex != index)
|
||
{
|
||
BuildSubLayout(index);
|
||
changed = true;
|
||
}
|
||
}
|
||
else if (m_subOpen)
|
||
{
|
||
CloseSubmenu();
|
||
changed = true;
|
||
}
|
||
return changed;
|
||
}
|
||
|
||
bool PopupMenu::OnLButtonDown(int x, int y, MenuResult& result)
|
||
{
|
||
result = MenuResult();
|
||
if (!m_open)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (!ContainsPoint(x, y))
|
||
{
|
||
// 点击菜单外部:先提交正在编辑的数值,再关闭菜单。
|
||
EndItemEdit(true, result);
|
||
Close();
|
||
return true;
|
||
}
|
||
|
||
// 子菜单内的按下:记录待确认项,待抬起时提交。
|
||
if (m_subOpen && UiPointInRect(m_subRect, static_cast<float>(x), static_cast<float>(y)))
|
||
{
|
||
m_subPressedIndex = SubItemAtPoint(x, y);
|
||
m_pressedIndex = -1;
|
||
m_pressedSwatch = -1;
|
||
return true;
|
||
}
|
||
|
||
// 已经处于编辑状态时:点击当前输入框 → 重新定位插入符;
|
||
// 点击其它位置 → 先提交本次编辑(菜单不关闭),再按新位置继续处理。
|
||
if (m_editIndex >= 0)
|
||
{
|
||
if (IsOnEditField(x, y))
|
||
{
|
||
OnEditMouseDown(x, y);
|
||
m_pressedIndex = m_editIndex;
|
||
m_pressedSwatch = -1;
|
||
return true;
|
||
}
|
||
EndItemEdit(true, result);
|
||
}
|
||
|
||
int swatchIndex = -1;
|
||
m_pressedIndex = ItemAtPoint(x, y, swatchIndex);
|
||
m_pressedSwatch = swatchIndex;
|
||
return true;
|
||
}
|
||
|
||
bool PopupMenu::OnLButtonUp(int x, int y, MenuResult& result)
|
||
{
|
||
const MenuResult editResult = result;
|
||
result = MenuResult();
|
||
if (!m_open)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 子菜单内的抬起:按下与抬起命中同一项才提交(菜单随之关闭)。
|
||
if (m_subOpen && m_subPressedIndex >= 0)
|
||
{
|
||
const int subPressed = m_subPressedIndex;
|
||
m_subPressedIndex = -1;
|
||
if (UiPointInRect(m_subRect, static_cast<float>(x), static_cast<float>(y)) &&
|
||
SubItemAtPoint(x, y) == subPressed)
|
||
{
|
||
const MenuItem& item = m_subItems[static_cast<std::size_t>(subPressed)];
|
||
if (item.kind == MenuItemKind::NORMAL || item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
result.valid = true;
|
||
result.scope = m_scope;
|
||
result.id = item.id;
|
||
result.swatchIndex = -1;
|
||
result.itemRect = m_subItemRects[static_cast<std::size_t>(subPressed)];
|
||
Close();
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// 编辑态下松开:先结束拖拽框选,不当作普通项点击。
|
||
if (m_editIndex >= 0)
|
||
{
|
||
OnEditMouseUp();
|
||
m_pressedIndex = -1;
|
||
m_pressedSwatch = -1;
|
||
result = editResult;
|
||
return true;
|
||
}
|
||
|
||
int swatchIndex = -1;
|
||
const int index = ItemAtPoint(x, y, swatchIndex);
|
||
const int pressedIndex = m_pressedIndex;
|
||
const int pressedSwatch = m_pressedSwatch;
|
||
m_pressedIndex = -1;
|
||
m_pressedSwatch = -1;
|
||
|
||
// 在菜单之外释放鼠标时视为取消操作。
|
||
if (!ContainsPoint(x, y))
|
||
{
|
||
Close();
|
||
return true;
|
||
}
|
||
|
||
if (index < 0 || index != pressedIndex)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||
|
||
// 子菜单父项:单击不下发命令,只保证子菜单处于展开状态。
|
||
if (item.kind == MenuItemKind::SUBMENU)
|
||
{
|
||
if (!m_subOpen || m_subParentIndex != index)
|
||
{
|
||
BuildSubLayout(index);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// `HEADER_ACTION`:只有**点在右侧图标按钮上**才下发命令(标题区域不响应);
|
||
// 菜单保持打开(这是一个可反复使用的小按钮,如「还原系数」)。
|
||
if (item.kind == MenuItemKind::HEADER_ACTION)
|
||
{
|
||
const gd::RectF icon = HeaderIconRect(index);
|
||
if (icon.Width > 0.0f && UiPointInRect(icon, static_cast<float>(x), static_cast<float>(y)))
|
||
{
|
||
result.valid = true;
|
||
result.scope = m_scope;
|
||
result.id = item.id;
|
||
result.swatchIndex = -1;
|
||
result.itemRect = icon;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// 编辑项:单击进入菜单内联编辑,菜单保持打开。
|
||
if (item.kind == MenuItemKind::EDIT)
|
||
{
|
||
BeginItemEdit(index);
|
||
return true;
|
||
}
|
||
|
||
result.valid = true;
|
||
result.scope = m_scope;
|
||
result.id = item.id;
|
||
result.swatchIndex = -1;
|
||
result.itemRect = m_itemRects[static_cast<std::size_t>(index)];
|
||
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
if (swatchIndex < 0 || swatchIndex != pressedSwatch)
|
||
{
|
||
result.valid = false;
|
||
return true;
|
||
}
|
||
result.swatchIndex = swatchIndex;
|
||
}
|
||
|
||
// 带复选框的切换项(如镜像的水平/垂直)选择后**保持菜单打开**,便于连续勾选。
|
||
if (item.keepOpen)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
Close();
|
||
return true;
|
||
}
|
||
|
||
bool PopupMenu::OnKeyDown(WPARAM key, MenuResult& result)
|
||
{
|
||
result = MenuResult();
|
||
if (!m_open)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 编辑态下按键全部交给输入框(回车提交、Esc 取消本次编辑但菜单保持打开)。
|
||
if (m_editIndex >= 0)
|
||
{ const bool shift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
|
||
switch (key)
|
||
{
|
||
case VK_ESCAPE:
|
||
EndItemEdit(false, result);
|
||
return true;
|
||
case VK_RETURN:
|
||
EndItemEdit(true, result);
|
||
return true;
|
||
case VK_BACK:
|
||
m_edit.Backspace();
|
||
return true;
|
||
case VK_DELETE:
|
||
m_edit.DeleteForward();
|
||
return true;
|
||
case VK_LEFT:
|
||
m_edit.MoveCaret(-1, shift);
|
||
return true;
|
||
case VK_RIGHT:
|
||
m_edit.MoveCaret(1, shift);
|
||
return true;
|
||
case VK_HOME:
|
||
m_edit.MoveHome(shift);
|
||
return true;
|
||
case VK_END:
|
||
m_edit.MoveEnd(shift);
|
||
return true;
|
||
default:
|
||
break;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
switch (key)
|
||
{
|
||
case VK_ESCAPE:
|
||
// Esc 先收起展开的二级子菜单,再次按下才关闭整个菜单。
|
||
if (m_subOpen)
|
||
{
|
||
CloseSubmenu();
|
||
return true;
|
||
}
|
||
Close();
|
||
return true;
|
||
case VK_LEFT:
|
||
if (m_subOpen)
|
||
{
|
||
CloseSubmenu();
|
||
return true;
|
||
}
|
||
break;
|
||
case VK_RIGHT:
|
||
{
|
||
if (m_hoverIndex >= 0 &&
|
||
m_items[static_cast<std::size_t>(m_hoverIndex)].kind == MenuItemKind::SUBMENU)
|
||
{
|
||
BuildSubLayout(m_hoverIndex);
|
||
return true;
|
||
}
|
||
break;
|
||
}
|
||
case VK_DOWN:
|
||
if (m_subOpen && m_subItems.size() > 0)
|
||
{
|
||
const int count = static_cast<int>(m_subItems.size());
|
||
m_subHoverIndex = (m_subHoverIndex + 1 + count) % count;
|
||
return true;
|
||
}
|
||
MoveHover(1);
|
||
return true;
|
||
case VK_UP:
|
||
if (m_subOpen && m_subItems.size() > 0)
|
||
{
|
||
const int count = static_cast<int>(m_subItems.size());
|
||
m_subHoverIndex = (m_subHoverIndex - 1 + count) % count;
|
||
return true;
|
||
}
|
||
MoveHover(-1);
|
||
return true;
|
||
case VK_RETURN:
|
||
{
|
||
// 子菜单展开且已有悬停项时,回车提交该项。
|
||
if (m_subOpen && m_subHoverIndex >= 0 &&
|
||
m_subHoverIndex < static_cast<int>(m_subItems.size()))
|
||
{
|
||
const MenuItem& item = m_subItems[static_cast<std::size_t>(m_subHoverIndex)];
|
||
if (item.kind == MenuItemKind::NORMAL && item.enabled)
|
||
{
|
||
result.valid = true;
|
||
result.scope = m_scope;
|
||
result.id = item.id;
|
||
result.swatchIndex = -1;
|
||
result.itemRect = m_subItemRects[static_cast<std::size_t>(m_subHoverIndex)];
|
||
Close();
|
||
}
|
||
return true;
|
||
}
|
||
if (CommitHover(result))
|
||
{
|
||
Close();
|
||
}
|
||
return true;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void PopupMenu::Paint(gd::Graphics& g)
|
||
{
|
||
if (!m_open || m_theme == nullptr || m_fonts == nullptr || m_rect.Width <= 0.0f)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 投影:用两层递减透明度的圆角矩形模拟,避免引入系统阴影 API。
|
||
UiFillRoundRect(g, gd::RectF(m_rect.X + 1.0f * m_scale, m_rect.Y + 2.0f * m_scale, m_rect.Width,
|
||
m_rect.Height),
|
||
kCorner * m_scale, 0x14000000u);
|
||
UiFillRoundRect(g, gd::RectF(m_rect.X + 2.0f * m_scale, m_rect.Y + 4.0f * m_scale, m_rect.Width,
|
||
m_rect.Height),
|
||
kCorner * m_scale, 0x10000000u);
|
||
|
||
UiFillRoundRect(g, m_rect, kCorner * m_scale, m_theme->cardBg);
|
||
UiDrawRoundRect(g, m_rect, kCorner * m_scale, m_theme->borderStrong, 1.0f);
|
||
|
||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||
{
|
||
const MenuItem& item = m_items[index];
|
||
const gd::RectF& rect = m_itemRects[index];
|
||
|
||
if (item.kind == MenuItemKind::SEPARATOR)
|
||
{
|
||
const float lineY = rect.Y + rect.Height * 0.5f;
|
||
UiDrawLine(g, rect.X + kItemPad * m_scale, lineY,
|
||
rect.X + rect.Width - kItemPad * m_scale, lineY, m_theme->border, 1.0f);
|
||
continue;
|
||
}
|
||
|
||
if (item.kind == MenuItemKind::HEADER)
|
||
{
|
||
// 标题与普通项共用同一文字列,保证菜单内所有文本严格左对齐。
|
||
UiText(g, item.label, m_fonts->uiSmallBold,
|
||
gd::RectF(rect.X + kItemPad * m_scale, rect.Y, rect.Width, rect.Height),
|
||
m_theme->textMuted, TextAlign::LEFT);
|
||
continue;
|
||
}
|
||
|
||
if (item.kind == MenuItemKind::HEADER_ACTION)
|
||
{
|
||
// 标题 + 右侧「还原」图标按钮:文字与普通标题同一列;图标可点击(悬停高亮)。
|
||
const gd::RectF icon = HeaderIconRect(static_cast<int>(index));
|
||
const float labelWidth =
|
||
std::max(8.0f, icon.X - rect.X - kItemPad * m_scale - kHeaderIconGap * m_scale);
|
||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmallBold, labelWidth), m_fonts->uiSmallBold,
|
||
gd::RectF(rect.X + kItemPad * m_scale, rect.Y, labelWidth, rect.Height),
|
||
m_theme->textMuted, TextAlign::LEFT);
|
||
if (icon.Width > 0.0f)
|
||
{
|
||
const bool iconHovered = (static_cast<int>(index) == m_hoverIconIndex);
|
||
if (iconHovered)
|
||
{
|
||
UiFillRoundRect(g, gd::RectF(icon.X - 2.0f * m_scale, icon.Y - 2.0f * m_scale,
|
||
icon.Width + 4.0f * m_scale, icon.Height + 4.0f * m_scale),
|
||
4.0f * m_scale, WithAlpha(m_theme->accent, 0x33));
|
||
}
|
||
PaintResetIcon(g, icon, iconHovered ? m_theme->accent : m_theme->text, m_scale);
|
||
}
|
||
continue;
|
||
}
|
||
|
||
const bool hovered = (static_cast<int>(index) == m_hoverIndex) &&
|
||
(item.kind != MenuItemKind::SWATCHES || m_hoverSwatch < 0);
|
||
|
||
if (item.kind == MenuItemKind::EDIT)
|
||
{
|
||
// 编辑项:左侧说明文字 + 右侧内联输入框(与拾色器的 RGB 编辑同一种处理方式,
|
||
// 菜单始终不关闭,数值就地修改)。
|
||
if (hovered && item.enabled && static_cast<int>(index) != m_editIndex)
|
||
{
|
||
UiFillRoundRect(g, rect, 4.0f * m_scale, WithAlpha(m_theme->accent, 0x22));
|
||
}
|
||
|
||
const std::uint32_t labelColor = item.enabled ? m_theme->text : m_theme->textMuted;
|
||
// 说明文字与普通项使用同一文字列(kItemPad),保证菜单内所有文本左对齐。
|
||
const float labelLeft = rect.X + kItemPad * m_scale;
|
||
const gd::RectF fieldRect = EditFieldRect(static_cast<int>(index));
|
||
const float labelWidth = std::max(8.0f, fieldRect.X - labelLeft - kEditFieldGap * m_scale);
|
||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmall, labelWidth), m_fonts->uiSmall,
|
||
gd::RectF(labelLeft, rect.Y, labelWidth, rect.Height), labelColor, TextAlign::LEFT);
|
||
|
||
const float suffix = EditSuffixWidth(item);
|
||
if (static_cast<int>(index) == m_editIndex)
|
||
{
|
||
// 编辑态:输入框底板覆盖整行,正文区向右让出后缀宽度,
|
||
// 后缀(单位提示)靠右对齐画在它右侧。
|
||
// ⚠️ 必须用 `UiDrawEditFieldContent`(只画内容)——`UiDrawEditField`
|
||
// 会把底板/描边按传入矩形绘制,那样带后缀时外框会短一截。
|
||
const gd::RectF textRect =
|
||
(suffix > 0.0f)
|
||
? gd::RectF(fieldRect.X, fieldRect.Y, std::max(12.0f, fieldRect.Width - suffix),
|
||
fieldRect.Height)
|
||
: fieldRect;
|
||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->cardBg);
|
||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->accent, 1.6f);
|
||
UiDrawEditFieldContent(g, textRect, m_edit, m_fonts->uiSmall, m_scale);
|
||
if (suffix > 0.0f)
|
||
{
|
||
const float suffixLeft = textRect.X + textRect.Width;
|
||
UiText(g, item.editSuffix, m_fonts->uiSmall,
|
||
gd::RectF(suffixLeft, fieldRect.Y,
|
||
std::max(4.0f, fieldRect.X + fieldRect.Width - suffixLeft -
|
||
4.0f * m_scale),
|
||
fieldRect.Height),
|
||
m_theme->textMuted, TextAlign::RIGHT);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->pressed);
|
||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->border, 1.0f);
|
||
const std::wstring text =
|
||
UiEllipsize(item.value, m_fonts->uiSmall, fieldRect.Width - 12.0f * m_scale);
|
||
UiText(g, text, m_fonts->uiSmall,
|
||
gd::RectF(fieldRect.X + 6.0f * m_scale, fieldRect.Y,
|
||
std::max(8.0f, fieldRect.Width - 12.0f * m_scale), fieldRect.Height),
|
||
item.enabled ? m_theme->textStrong : m_theme->textMuted, TextAlign::LEFT);
|
||
}
|
||
continue;
|
||
}
|
||
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
const std::vector<gd::RectF>& rects = m_swatchRects[index];
|
||
for (std::size_t swatch = 0; swatch < rects.size(); ++swatch)
|
||
{
|
||
const gd::RectF& swatchRect = rects[swatch];
|
||
UiFillRoundRect(g, swatchRect, 4.0f * m_scale, item.swatches[swatch]);
|
||
|
||
const bool swatchHover = (static_cast<int>(index) == m_hoverIndex) &&
|
||
(static_cast<int>(swatch) == m_hoverSwatch);
|
||
if (static_cast<int>(swatch) == item.checkedSwatch || swatchHover)
|
||
{
|
||
const gd::RectF ring(swatchRect.X - 2.0f * m_scale, swatchRect.Y - 2.0f * m_scale,
|
||
swatchRect.Width + 4.0f * m_scale,
|
||
swatchRect.Height + 4.0f * m_scale);
|
||
UiDrawRoundRect(g, ring, 5.0f * m_scale,
|
||
(static_cast<int>(swatch) == item.checkedSwatch) ? m_theme->textStrong
|
||
: m_theme->borderStrong,
|
||
(static_cast<int>(swatch) == item.checkedSwatch) ? 1.8f : 1.0f);
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
|
||
// 普通项。
|
||
std::uint32_t background = 0;
|
||
if (hovered && item.enabled)
|
||
{
|
||
background = m_theme->accent;
|
||
}
|
||
else if (static_cast<int>(index) == m_pressedIndex)
|
||
{
|
||
background = m_theme->pressed;
|
||
}
|
||
if (background != 0)
|
||
{
|
||
UiFillRoundRect(g, rect, 4.0f * m_scale, background);
|
||
}
|
||
|
||
const std::uint32_t textColor = !item.enabled ? m_theme->textMuted
|
||
: ((hovered && item.enabled) ? m_theme->textOnAccent
|
||
: m_theme->text);
|
||
|
||
if (item.checked)
|
||
{
|
||
// 单选标记:对勾固定画在条目**右侧**的标记列内,左侧只留统一的小边距,
|
||
// 这样菜单文本左边距不会因为“可能存在的对勾”而被整体推右。
|
||
const float markerRight = rect.X + rect.Width - kItemPad * m_scale;
|
||
const float cx = markerRight - kMarkerColumn * m_scale * 0.5f;
|
||
const float cy = rect.Y + rect.Height * 0.5f;
|
||
const std::uint32_t markerColor = hovered ? m_theme->textOnAccent : m_theme->accent;
|
||
UiDrawLine(g, cx - 4.0f * m_scale, cy, cx - 1.0f * m_scale, cy + 3.0f * m_scale,
|
||
markerColor, 1.8f * m_scale);
|
||
UiDrawLine(g, cx - 1.0f * m_scale, cy + 3.0f * m_scale, cx + 4.5f * m_scale,
|
||
cy - 4.0f * m_scale, markerColor, 1.8f * m_scale);
|
||
}
|
||
|
||
// 带子菜单的项:在右侧标记列画一个指向右的三角,提示可以继续展开。
|
||
if (item.kind == MenuItemKind::SUBMENU)
|
||
{
|
||
const float markerRight = rect.X + rect.Width - kItemPad * m_scale;
|
||
const float cx = markerRight - kMarkerColumn * m_scale * 0.5f;
|
||
const float cy = rect.Y + rect.Height * 0.5f;
|
||
const float size = kSubArrowSize * m_scale;
|
||
gd::PointF arrow[3];
|
||
arrow[0] = gd::PointF(cx - size * 0.5f, cy - size);
|
||
arrow[1] = gd::PointF(cx - size * 0.5f, cy + size);
|
||
arrow[2] = gd::PointF(cx + size * 0.7f, cy);
|
||
gd::SolidBrush brush(ToGdiColor(textColor));
|
||
g.FillPolygon(&brush, arrow, 3);
|
||
}
|
||
|
||
const float textLeft = rect.X + kItemPad * m_scale;
|
||
const float textWidth =
|
||
rect.Width - 2.0f * kItemPad * m_scale - kMarkerColumn * m_scale - kMarkerGap * m_scale;
|
||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmall, textWidth), m_fonts->uiSmall,
|
||
gd::RectF(textLeft, rect.Y, std::max(8.0f, textWidth), rect.Height), textColor,
|
||
TextAlign::LEFT);
|
||
}
|
||
|
||
// 二级子菜单最后绘制,保证覆盖在主菜单之上。
|
||
PaintSubmenu(g);
|
||
}
|
||
|
||
void PopupMenu::PaintSubmenu(gd::Graphics& g)
|
||
{
|
||
if (!m_subOpen || m_subRect.Width <= 0.0f || m_subRect.Height <= 0.0f)
|
||
{
|
||
return;
|
||
}
|
||
|
||
UiFillRoundRect(g, gd::RectF(m_subRect.X + 1.0f * m_scale, m_subRect.Y + 2.0f * m_scale,
|
||
m_subRect.Width, m_subRect.Height),
|
||
kCorner * m_scale, 0x14000000u);
|
||
UiFillRoundRect(g, gd::RectF(m_subRect.X + 2.0f * m_scale, m_subRect.Y + 4.0f * m_scale,
|
||
m_subRect.Width, m_subRect.Height),
|
||
kCorner * m_scale, 0x10000000u);
|
||
|
||
UiFillRoundRect(g, m_subRect, kCorner * m_scale, m_theme->cardBg);
|
||
UiDrawRoundRect(g, m_subRect, kCorner * m_scale, m_theme->borderStrong, 1.0f);
|
||
|
||
for (std::size_t index = 0; index < m_subItems.size(); ++index)
|
||
{
|
||
const MenuItem& item = m_subItems[index];
|
||
const gd::RectF& rect = m_subItemRects[index];
|
||
|
||
if (item.kind == MenuItemKind::SEPARATOR)
|
||
{
|
||
const float lineY = rect.Y + rect.Height * 0.5f;
|
||
UiDrawLine(g, rect.X + kItemPad * m_scale, lineY,
|
||
rect.X + rect.Width - kItemPad * m_scale, lineY, m_theme->border, 1.0f);
|
||
continue;
|
||
}
|
||
|
||
if (item.kind == MenuItemKind::HEADER)
|
||
{
|
||
UiText(g, item.label, m_fonts->uiSmallBold,
|
||
gd::RectF(rect.X + kItemPad * m_scale, rect.Y, rect.Width, rect.Height),
|
||
m_theme->textMuted, TextAlign::LEFT);
|
||
continue;
|
||
}
|
||
|
||
const bool hovered = (static_cast<int>(index) == m_subHoverIndex) && item.enabled;
|
||
|
||
if (item.kind == MenuItemKind::EDIT)
|
||
{
|
||
// 子菜单中的编辑项:与主菜单一样就地编辑。
|
||
if (hovered && static_cast<int>(index) != m_editIndex)
|
||
{
|
||
UiFillRoundRect(g, rect, 4.0f * m_scale, WithAlpha(m_theme->accent, 0x22));
|
||
}
|
||
const std::uint32_t labelColor = item.enabled ? m_theme->text : m_theme->textMuted;
|
||
const float labelLeft = rect.X + kItemPad * m_scale;
|
||
const gd::RectF fieldRect = EditFieldRect(static_cast<int>(index));
|
||
const float labelWidth = std::max(8.0f, fieldRect.X - labelLeft - kEditFieldGap * m_scale);
|
||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmall, labelWidth), m_fonts->uiSmall,
|
||
gd::RectF(labelLeft, rect.Y, labelWidth, rect.Height), labelColor, TextAlign::LEFT);
|
||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->pressed);
|
||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->border, 1.0f);
|
||
const std::wstring text =
|
||
UiEllipsize(item.value, m_fonts->uiSmall, fieldRect.Width - 12.0f * m_scale);
|
||
UiText(g, text, m_fonts->uiSmall,
|
||
gd::RectF(fieldRect.X + 6.0f * m_scale, fieldRect.Y,
|
||
std::max(8.0f, fieldRect.Width - 12.0f * m_scale), fieldRect.Height),
|
||
item.enabled ? m_theme->textStrong : m_theme->textMuted, TextAlign::LEFT);
|
||
continue;
|
||
}
|
||
|
||
if (item.kind == MenuItemKind::SWATCHES)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
std::uint32_t background = 0;
|
||
if (hovered)
|
||
{
|
||
background = m_theme->accent;
|
||
}
|
||
else if (static_cast<int>(index) == m_subPressedIndex)
|
||
{
|
||
background = m_theme->pressed;
|
||
}
|
||
if (background != 0)
|
||
{
|
||
UiFillRoundRect(g, rect, 4.0f * m_scale, background);
|
||
}
|
||
|
||
const std::uint32_t textColor = !item.enabled ? m_theme->textMuted
|
||
: (hovered ? m_theme->textOnAccent
|
||
: m_theme->text);
|
||
|
||
// 选中标记(对勾):与主菜单一致画在右侧标记列内。
|
||
if (item.checked)
|
||
{
|
||
const float markerRight = rect.X + rect.Width - kItemPad * m_scale;
|
||
const float cx = markerRight - kMarkerColumn * m_scale * 0.5f;
|
||
const float cy = rect.Y + rect.Height * 0.5f;
|
||
const std::uint32_t markerColor = hovered ? m_theme->textOnAccent : m_theme->accent;
|
||
UiDrawLine(g, cx - 4.0f * m_scale, cy, cx - 1.0f * m_scale, cy + 3.0f * m_scale,
|
||
markerColor, 1.8f * m_scale);
|
||
UiDrawLine(g, cx - 1.0f * m_scale, cy + 3.0f * m_scale, cx + 4.5f * m_scale,
|
||
cy - 4.0f * m_scale, markerColor, 1.8f * m_scale);
|
||
}
|
||
|
||
const float textLeft = rect.X + kItemPad * m_scale;
|
||
const float textWidth =
|
||
rect.Width - 2.0f * kItemPad * m_scale - kMarkerColumn * m_scale - kMarkerGap * m_scale;
|
||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmall, textWidth), m_fonts->uiSmall,
|
||
gd::RectF(textLeft, rect.Y, std::max(8.0f, textWidth), rect.Height), textColor,
|
||
TextAlign::LEFT);
|
||
}
|
||
}
|
||
|
||
} // namespace jdi
|