使用Skiasharp SkControl检测鼠标单击特定的SKPATH
我正在使用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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论