| 1 | using Avalonia; |
| 2 | using Avalonia.Controls; |
| 3 | using Avalonia.Input; |
| 4 | using Avalonia.Media; |
| 5 | |
| 6 | namespace CascadeIDE.Views; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Slot-scoped host for Skia-backed cockpit rendering. |
| 10 | /// Keeps layout/input participation in the visual tree and provides |
| 11 | /// a single integration point for slot-local rendering/input routing. |
| 12 | /// </summary> |
| 13 | public class SkiaHost : Decorator |
| 14 | { |
| 15 | public static readonly StyledProperty<SkiaHostSlot> SlotProperty = |
| 16 | AvaloniaProperty.Register<SkiaHost, SkiaHostSlot>(nameof(Slot), SkiaHostSlot.Forward); |
| 17 | |
| 18 | private ISkiaSceneRenderer? _renderer; |
| 19 | |
| 20 | static SkiaHost() |
| 21 | { |
| 22 | AffectsRender<SkiaHost>(SlotProperty); |
| 23 | } |
| 24 | |
| 25 | public SkiaHostSlot Slot |
| 26 | { |
| 27 | get => GetValue(SlotProperty); |
| 28 | set => SetValue(SlotProperty, value); |
| 29 | } |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Slot-local scene renderer. When set, SkiaHost delegates visual drawing and pointer events |
| 33 | /// to this renderer while remaining a normal Avalonia layout/input participant. |
| 34 | /// </summary> |
| 35 | public ISkiaSceneRenderer? Renderer |
| 36 | { |
| 37 | get => _renderer; |
| 38 | set |
| 39 | { |
| 40 | if (ReferenceEquals(_renderer, value)) |
| 41 | return; |
| 42 | _renderer = value; |
| 43 | InvalidateVisual(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public override void Render(DrawingContext context) |
| 48 | { |
| 49 | base.Render(context); |
| 50 | if (Bounds.Width <= 0 || Bounds.Height <= 0) |
| 51 | return; |
| 52 | |
| 53 | _renderer?.Render(context, new Rect(Bounds.Size), Slot); |
| 54 | } |
| 55 | |
| 56 | protected override void OnPointerPressed(PointerPressedEventArgs e) |
| 57 | { |
| 58 | base.OnPointerPressed(e); |
| 59 | if (_renderer is null) |
| 60 | return; |
| 61 | |
| 62 | var position = e.GetPosition(this); |
| 63 | _renderer.OnPointerPressed(position, e, Slot); |
| 64 | } |
| 65 | |
| 66 | protected override void OnPointerMoved(PointerEventArgs e) |
| 67 | { |
| 68 | base.OnPointerMoved(e); |
| 69 | if (_renderer is null) |
| 70 | return; |
| 71 | |
| 72 | var position = e.GetPosition(this); |
| 73 | _renderer.OnPointerMoved(position, e, Slot); |
| 74 | } |
| 75 | |
| 76 | protected override void OnPointerReleased(PointerReleasedEventArgs e) |
| 77 | { |
| 78 | base.OnPointerReleased(e); |
| 79 | if (_renderer is null) |
| 80 | return; |
| 81 | |
| 82 | var position = e.GetPosition(this); |
| 83 | _renderer.OnPointerReleased(position, e, Slot); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public enum SkiaHostSlot |
| 88 | { |
| 89 | Pfd, |
| 90 | Forward, |
| 91 | Mfd |
| 92 | } |
| 93 | |
| 94 | /// <summary> |
| 95 | /// Contract for slot-local Skia scene integration. |
| 96 | /// Implementations may render and handle pointer input for a specific host slot. |
| 97 | /// </summary> |
| 98 | public interface ISkiaSceneRenderer |
| 99 | { |
| 100 | void Render(DrawingContext context, Rect bounds, SkiaHostSlot slot); |
| 101 | |
| 102 | void OnPointerPressed(Point position, PointerPressedEventArgs e, SkiaHostSlot slot) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | void OnPointerMoved(Point position, PointerEventArgs e, SkiaHostSlot slot) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | void OnPointerReleased(Point position, PointerReleasedEventArgs e, SkiaHostSlot slot) |
| 111 | { |
| 112 | } |
| 113 | } |
| 114 | |