gpt4 book ai didi

silverlight - WP7 : Suppressing XNA touch input when handled by Silverlight?

转载 作者:行者123 更新时间:2023-12-04 18:14:16 26 4
gpt4 key购买 nike

我在Mango中有一个XNA + Silverlight游戏:主要是XNA,顶部还有一些Silverlight UI。我遇到的问题是,当您按下按钮或与Silverlight控件进行交互时,触摸信息仍会传递到XNA游戏循环中。您如何抑制这一点?

最佳答案

安排了一个类为我做跟踪。页面加载后(在Loaded处理程序中),创建此页面并为其指定根元素(以便它可以附加到LayoutUpdated事件)。注册在游戏过程中可能会覆盖游戏表面的所有控件。然后只需调用TouchesControl并传递触摸位置即可确定是否应该忽略该点。它缓存控件的区域并在布局更新时对其进行更新。

应该适用于移动,更改大小或折叠/展开的矩形元素。

public class ControlTouchTracker
{
private List<FrameworkElement> controls = new List<FrameworkElement>();
private Dictionary<FrameworkElement, ControlRegion> controlBounds = new Dictionary<FrameworkElement, ControlRegion>();

public ControlTouchTracker(FrameworkElement rootElement)
{
rootElement.LayoutUpdated += this.OnLayoutUpdated;
}

public void RegisterControl(FrameworkElement control)
{
controls.Add(control);
}

public void RemoveControl(FrameworkElement control)
{
controls.Remove(control);
controlBounds.Remove(control);
}

private void OnLayoutUpdated(object sender, EventArgs e)
{
foreach (Control control in this.controls)
{
this.RefreshControlBounds(control);
}
}

private void RefreshControlBounds(FrameworkElement control)
{
if (this.ControlIsVisible(control))
{
try
{
GeneralTransform controlTransform = control.TransformToVisual(Application.Current.RootVisual);
Point offset = controlTransform.Transform(new Point(0, 0));

this.controlBounds[control] = new ControlRegion
{
Left = (float)offset.X,
Right = (float)(offset.X + control.ActualWidth),
Top = (float)offset.Y,
Bottom = (float)(offset.Y + control.ActualHeight)
};
}
catch (ArgumentException)
{
}
}
else
{
if (this.controlBounds.ContainsKey(control))
{
this.controlBounds.Remove(control);
}
}
}

private bool ControlIsVisible(FrameworkElement control)
{
// End case
if (control == null)
{
return true;
}

if (control.Visibility == Visibility.Collapsed)
{
return false;
}

return this.ControlIsVisible(control.Parent as FrameworkElement);
}

public bool TouchesControl(Vector2 touchPosition)
{
foreach (ControlRegion region in this.controlBounds.Values)
{
if (touchPosition.X >= region.Left && touchPosition.X <= region.Right &&
touchPosition.Y >= region.Top && touchPosition.Y <= region.Bottom)
{
return true;
}
}

return false;
}

public class ControlRegion
{
public float Left { get; set; }
public float Right { get; set; }
public float Top { get; set; }
public float Bottom { get; set; }
}
}

(编辑)更新了示例,以与更改 Visibility的父元素一起使用。

关于silverlight - WP7 : Suppressing XNA touch input when handled by Silverlight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6862267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com