Custom cursors
Posted: 17 Dec 2013, 22:34
Is there any way to incorporate custom cursors in Noesis? Thanks.
Welcome to our official discussion site. You’ll find topics on features, bugs, development, and general support here
https://www.noesisengine.com/forums/
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.Resources>
<ImageBrush x:Key="arrowCursor" ImageSource="icons/arrow.png"/>
<!-- ... -->
</Grid.Resources>
<Grid x:Name="LayoutRoot">
<!-- ... -->
</Grid>
<Canvas>
<Rectangle x:Name="cursor" Fill="{StaticResource arrowCursor}" IsHitTestVisible="False"
Canvas.Left="0" Canvas.Top="0" Width="32" Height="32"/>
</Canvas>
</Grid>UIElement* element; // an element of the UI tree
//...
UIElement* captured = element->GetMouse()->GetCaptured();
if (captured != 0)
{
// mouse is captured by 'captured' element
}
else
{
// mouse is not captured
} // list of cursor types
public enum UiCursor : int
{
None = 0,
Pointer = 1,
Attack = 2,
Speak = 3
// you can add other types of cursors
}
public class CursorService : DependencyObject
{
#region Static Fields
public static DependencyProperty CursorProperty = DependencyProperty.RegisterAttached(
"Cursor",
typeof(UiCursor),
typeof(CursorService),
new FrameworkPropertyMetadata(UiCursor.None, CursorAttachedHandler));
#endregion
#region Public Methods and Operators
public static UiCursor GetCursor(FrameworkElement element)
{
return (UiCursor)element.GetValue(CursorProperty);
}
public static void RegisterCursor(FrameworkElement frameworkElement)
{
frameworkElement.MouseEnter += FrameworkElementMouseEnterHandler;
}
public static void SetCursor(FrameworkElement element, UiCursor value)
{
element.SetValue(CursorProperty, value);
}
#endregion
#region Methods
private static void CursorAttachedHandler(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
RegisterCursor((FrameworkElement)obj);
}
private static void FrameworkElementMouseEnterHandler(object sender, MouseEventArgs e)
{
FrameworkElement frameworkElement = (FrameworkElement)sender;
UiCursor cursor = (UiCursor)frameworkElement.GetValue(CursorProperty);
frameworkElement.MouseLeave += FrameworkElementMouseLeaveHandler;
frameworkElement.Unloaded += FrameworkElementMouseUnloadedHandler;
CursorServiceEvents.OnCursorEnter(frameworkElement, cursor);
}
private static void FrameworkElementMouseLeaveHandler(object sender, MouseEventArgs e)
{
FrameworkElement frameworkElement = (FrameworkElement)sender;
ResetCursor(frameworkElement);
}
private static void FrameworkElementMouseUnloadedHandler(object sender, RoutedEventArgs arg1)
{
FrameworkElement frameworkElement = (FrameworkElement)sender;
ResetCursor(frameworkElement);
}
private static void ResetCursor(FrameworkElement frameworkElement)
{
frameworkElement.MouseLeave -= FrameworkElementMouseLeaveHandler;
frameworkElement.Unloaded -= FrameworkElementMouseUnloadedHandler;
CursorServiceEvents.OnCursorLeave(frameworkElement);
}
#endregion
}
public static class CursorServiceEvents
{
#region Public Events
public static event Action<FrameworkElement, UiCursor> CursorEnter;
public static event Action<FrameworkElement> CursorLeave;
#endregion
#region Public Methods and Operators
public static void OnCursorEnter(FrameworkElement arg1, UiCursor arg2)
{
Action<FrameworkElement, UiCursor> handler = CursorEnter;
if (handler != null)
{
handler(arg1, arg2);
}
}
public static void OnCursorLeave(FrameworkElement obj)
{
Action<FrameworkElement> handler = CursorLeave;
if (handler != null)
{
handler(obj);
}
}
#endregion
}
public static class CursorManager
{
static CursorManager()
{
CursorServiceEvents.CursorEnter += this.CursorEnterHandler;
CursorServiceEvents.CursorLeave += this.CursorLeaveHandler;
}
private void CursorEnterHandler(FrameworkElement frameworkElement, UiCursor cursor)
{
switch (cursor)
{
// set cursor depending on cursor type
}
}
private void CursorLeaveHandler(FrameworkElement frameworkElement)
{
// reset cursor to previous
}
}
<Button Content="Attack me" services:CursorService.Cursor="Attack" /><Style x:Key="ButtonAttackStyle" TargetType="{x:Type Button}">
<Setter Property="services:CursorService.Cursor" Value="Attack" />
</Style>