Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
VideoWidget.cs
1using System.Drawing;
2using System.Numerics;
8
10
18public sealed class VideoWidget : DraggableWidgetBase
19{
20 private readonly IVideoPlayer _player;
21
30 RenderSurfaceHostBase renderSurfaceHost,
31 View view,
32 Rectangle bounds,
33 VideoSource source,
34 Func<IVideoPlayer>? playerFactory = null,
35 string? nickname = null)
36 : base(
37 renderSurfaceHost,
39 ValidateBounds(bounds).Location,
40 nickname)
41 {
42 try
43 {
44 ArgumentNullException.ThrowIfNull(view);
45 ArgumentNullException.ThrowIfNull(source);
46 IVideoPlayer? player = null;
47
48 Video = new DirectVideo(
49 () => player = (playerFactory ?? DefaultPlayerFactory)(),
50 source,
51 renderSurfaceHost,
52 view,
53 bounds,
54 $"{Nickname}.video");
55
56 _player = player!;
57 InitializeVideo();
58 }
59 catch
60 {
61 base.Dispose(); // DirectComposite registered this instance before construction completed.
62 throw;
63 }
64 }
65
74 RenderSurfaceHostBase renderSurfaceHost,
75 SceneLayer sceneLayer,
76 Rectangle bounds,
77 VideoSource source,
78 Func<IVideoPlayer>? playerFactory = null,
79 string? nickname = null)
80 : base(
81 renderSurfaceHost,
83 ValidateBounds(bounds).Location,
84 nickname)
85 {
86 try
87 {
88 ArgumentNullException.ThrowIfNull(sceneLayer);
89 ArgumentNullException.ThrowIfNull(source);
90 IVideoPlayer? player = null;
91
92 Video = new DirectVideo(
93 () => player = (playerFactory ?? DefaultPlayerFactory)(),
94 source,
95 renderSurfaceHost,
96 sceneLayer,
97 bounds,
98 $"{Nickname}.video");
99
100 _player = player!;
101 InitializeVideo();
102 }
103 catch
104 {
105 base.Dispose();
106 throw;
107 }
108 }
109
118 RenderSurfaceHostBase renderSurfaceHost,
119 View view,
120 Rectangle bounds,
121 VideoSource source,
122 IVideoPlayer player,
123 string? nickname = null)
124 : this(
125 renderSurfaceHost,
126 view,
127 bounds,
128 source,
129 OwnedPlayerFactory(player),
130 nickname)
131 {
132 }
133
142 RenderSurfaceHostBase renderSurfaceHost,
143 SceneLayer sceneLayer,
144 Rectangle bounds,
145 VideoSource source,
146 IVideoPlayer player,
147 string? nickname = null)
148 : this(
149 renderSurfaceHost,
150 sceneLayer,
151 bounds,
152 source,
153 OwnedPlayerFactory(player),
154 nickname)
155 {
156 }
157
159 public DirectVideo Video { get; }
160
162 public Rectangle Bounds => Mode == DirectDrawingMode.View ? Video.ScreenBounds : Video.WorldBounds;
163
167 public VideoWidget SetBounds(Rectangle bounds)
168 {
169 ValidateBounds(bounds);
170 SetPosition(bounds.X, bounds.Y);
171 SetLocalOffset(Video, Vector2.Zero);
172
173 if (Mode == DirectDrawingMode.View)
174 Video.ScreenBounds = bounds;
175 else
176 Video.WorldBounds = bounds;
177
178 return this;
179 }
180
182 public void Play() => Video.Play();
183
185 public void Pause() => Video.Pause();
186
188 public void Stop() => Video.Stop();
189
192 public void Seek(TimeSpan position) => Video.Seek(position);
193
196 public void Open(VideoSource source) => Video.Open(source);
197
200 public void Open(Uri source) => Video.Open(source);
201
203 public double PlaybackRate { get => Video.PlaybackRate; set => Video.PlaybackRate = value; }
204
206 public bool Loop { get => Video.Loop; set => Video.Loop = value; }
207
209 public StretchMode Stretch { get => Video.Stretch; set => Video.Stretch = value; }
210
212 public bool IsPlaying => _player.IsPlaying;
213
215 public bool CanControlAudio => Video.CanControlAudio;
216
218 public float Volume
219 {
220 get => Video.Volume;
221 set => Video.Volume = value;
222 }
223
225 public bool IsMuted
226 {
227 get => Video.IsMuted;
228 set => Video.IsMuted = value;
229 }
230
232 public TimeSpan Duration => _player.Duration;
233
235 public TimeSpan Position => _player.Position;
236
238 public (int width, int height) NaturalSize => Video.NaturalSize;
239
241 public bool HasAudio => _player.HasAudio;
242
244 public bool IsMetadataReady => _player.IsMetadataReady;
245
247 public VideoMetadata Metadata => Video.Metadata;
248
250 public event EventHandler Started { add => _player.Started += value; remove => _player.Started -= value; }
251
253 public event EventHandler Paused { add => _player.Paused += value; remove => _player.Paused -= value; }
254
256 public event EventHandler Stopped { add => _player.Stopped += value; remove => _player.Stopped -= value; }
257
259 public event EventHandler Ended { add => _player.Ended += value; remove => _player.Ended -= value; }
260
262 public event EventHandler<VideoStateChangedEventArgs> StateChanged
263 {
264 add => _player.StateChanged += value;
265 remove => _player.StateChanged -= value;
266 }
267
268 private void InitializeVideo()
269 {
270 try
271 {
272 Add(Video, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
273 }
274 catch
275 {
276 Video.Dispose();
277 throw;
278 }
279
280 IsDragEnabled = false;
281 CanReceiveFocus = true;
283 }
284
285 private static IVideoPlayer DefaultPlayerFactory() => new VlcVideoPlayer();
286
287 private static Func<IVideoPlayer> OwnedPlayerFactory(IVideoPlayer player)
288 {
289 ArgumentNullException.ThrowIfNull(player);
290 return () => player;
291 }
292
293 private static Rectangle ValidateBounds(Rectangle bounds)
294 {
295 if (bounds.Width <= 0 || bounds.Height <= 0)
296 throw new ArgumentOutOfRangeException(nameof(bounds), bounds, "Video widget size must be positive.");
297
298 return bounds;
299 }
300}
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
DirectComposite SetLocalOffset(IDirectCompositeChild child, Vector2 newLocalOffsetPx)
Changes one child's local offset and reapplies its position.
View? View
Gets the view shared by all children in view mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
void SetPosition(Vector2 position)
Sets the composite anchor and repositions all children from their stored offsets.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
Draws video frames provided by IVideoPlayer onto the backbuffer. Audio is handled by the IVideoPlayer...
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
A URI, borrowed/owned stream, or GAF asset to open with a video backend.
Definition VideoSource.cs:7
bool IsMuted
Mutes audio without changing the retained volume.
TimeSpan Duration
Gets media duration; check metadata readiness before interpreting unknown values.
VideoWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, VideoSource source, IVideoPlayer player, string? nickname=null)
Creates a scene-layer widget transferring player ownership to DirectVideo.
DirectVideo Video
Gets the owned drawing for advanced configuration. Do not dispose it separately.
void Stop()
Stops playback; DirectVideo retains its last consumed frame.
void Seek(TimeSpan position)
Seeks to a media position.
void Play()
Starts or resumes playback.
int width
Gets actual decoded dimensions, or (0, 0) before discovery.
bool HasAudio
Gets audio-track presence; false is inconclusive until metadata is ready.
VideoMetadata Metadata
Gets the authoritative metadata snapshot.
Rectangle Bounds
Gets presentation bounds in screen pixels (View) or world pixels (SceneLayer).
VideoWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, VideoSource source, Func< IVideoPlayer >? playerFactory=null, string? nickname=null)
Creates a scene-layer/world-space widget. The default factory creates VlcVideoPlayer.
VideoWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, VideoSource source, IVideoPlayer player, string? nickname=null)
Creates a view widget transferring player ownership to DirectVideo.
VideoWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, VideoSource source, Func< IVideoPlayer >? playerFactory=null, string? nickname=null)
Creates a view/screen-space widget. The default factory creates VlcVideoPlayer.
VideoWidget SetBounds(Rectangle bounds)
Moves the composite and resizes its video using existing drawing bounds.
float Volume
Audible output gain from zero to one.
bool Loop
Gets or sets looping.
EventHandler Ended
Forwards end notifications on the player's event thread.
EventHandler Paused
Forwards pause notifications on the player's event thread.
TimeSpan Position
Gets the current media position.
EventHandler Stopped
Forwards stop notifications on the player's event thread.
bool IsMetadataReady
Gets whether metadata is available for this source.
StretchMode Stretch
Gets or sets scaling within the presentation bounds.
bool IsPlaying
Gets the player's current playing state.
void Open(VideoSource source)
Replaces the source and starts playback through DirectVideo.
EventHandler< VideoStateChangedEventArgs > StateChanged
Forwards state/metadata notifications, retaining the player's sender and threading.
EventHandler Started
Forwards playback start notifications on the player's event thread.
void Pause()
Pauses playback without hiding the widget.
void Open(Uri source)
Replaces a URI/file source and starts playback through DirectVideo.
bool CanControlAudio
Whether the backend supports volume and mute controls.
double PlaybackRate
Gets or sets playback speed.
bool IsDragEnabled
Gets or sets whether new drag operations may begin.
DraggableWidgetBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the DraggableWidgetBase class.
bool CanReceiveFocus
Gets or sets a value indicating whether the widget can receive keyboard focus.
bool IsKeyboardInputEnabled
Gets or sets a value indicating whether the widget can receive keyboard input.
Defines the contract for a video player that can play, pause, stop, and seek video content.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
StretchMode
Defines how video content should be stretched or scaled to fit within its display bounds.
Definition StretchMode.cs:7
record VideoMetadata(VideoMetadataStatus Status, bool HasAudio, TimeSpan Duration)
An immutable metadata snapshot. Values are authoritative only when status is Ready.