使用Skiasharp SkControl检测鼠标单击特定的SKPATH

发布于 2025-02-14 01:26:39 字数 1156 浏览 1 评论 0原文

我正在使用Skiasharp Windows表单SkControl来处理我的应用程序中的绘图操作。功能的一部分是能够单击画布以选择特定的“图层”,并允许用户对每一层应用不同的颜色。有关示例,请参见下图。

该图像中的三层被绘制为三个单独的路径。在大多数情况下,我拥有的代码工作很棒,除了这些“层”重叠时。如果我在高于最黑暗层的“点”的点上单击上图中的最轻层,它将选择较深的层。

这是有道理的,因为我使用path.getBounds()返回SkRect。在鼠标上,我正在从单击位置创建一个SKRECT,然后调用rect.IntersectSwith(Mouserect)以确定我单击哪个“ layer”。

如何验证给定路径不是简单的矩形?

这是我用于“ hittest”的代码(由于我进行调试以来,不优化)

            SKRect hitRect = SKRect.Create(scaledPixelPosition, new SKSize(20f, 20f));
            ElementLayerInfo elementLayerInfo = new ElementLayerInfo();

            foreach(Element element in _elements)
            {
                foreach (KeyValuePair<int, ElementLayerInfo>  kvp in element.ElementLayers)
                {
                    if (kvp.Value.LayerRect.IntersectsWith(hitRect))
                    {
                        elementLayerInfo = kvp.Value;
                    }
                }
            }

I am using the Skiasharp Windows Forms SKControl to handle drawing actions within my app. Part of the functionality is to be able to click on the canvas to select a specific "layer" and allow the user to apply a different color to each layer. See the image below for an example.

three layered image

The three layers in this image are drawn as three separate paths. For the most part, the code that I have is working great, except for when those "layers" overlap. If I click on the lightest layer in the above image at a point that is lower than the "point" of the darkest layer, it will select the darker layer.

This makes sense since I am using path.GetBounds() which returns an SKRect. On mouse click I am creating an SKRect from the click location and then calling rect.IntersectsWith(mouseRect) to determine which "layer" I clicked on.

How can I validate layer clicked given the paths are not simple rectangles?

Here is the code that I am using for the "HitTest" (non-optimized since I was debugging)

            SKRect hitRect = SKRect.Create(scaledPixelPosition, new SKSize(20f, 20f));
            ElementLayerInfo elementLayerInfo = new ElementLayerInfo();

            foreach(Element element in _elements)
            {
                foreach (KeyValuePair<int, ElementLayerInfo>  kvp in element.ElementLayers)
                {
                    if (kvp.Value.LayerRect.IntersectsWith(hitRect))
                    {
                        elementLayerInfo = kvp.Value;
                    }
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文