1using System.Collections.ObjectModel;
4using System.Runtime.Serialization;
17 [JsonProperty]
private readonly List<Sprite> _children =
new();
38 foreach (var sprite
in sprites)
51 private void OnDeserialized(StreamingContext context)
53 foreach (var sprite
in _children)
54 sprite.Disposing += sprite_Disposing;
59 #region IMovable / IMovableOnSceneLayer
76 if (_children.Count == 0)
77 throw new InvalidOperationException(
"CompositeSprite has no children; SceneLayer is undefined.");
80 return _children[0].SceneLayer;
93 if (r == Rectangle.Empty)
100 r.Left + (r.Width / 2f),
101 r.Top + (r.Height / 2f)),
102 _ =>
new PointF(r.Left, r.Top)
105 PointF anchorGrid =
SceneLayer.WorldPxToGrid(anchorWorldPx);
106 return new Vector2(anchorGrid.X, anchorGrid.Y);
116 Vector2 delta = pos - oldAnchor;
118 if (delta == Vector2.Zero || _children.Count == 0)
123 foreach (var sprite
in _children)
125 Vector2 childPos = GetSpritePosition(sprite);
126 sprite.SetPosition(childPos + delta);
136 if (delta == Vector2.Zero)
142 #endregion IMovable / IMovableOnSceneLayer
155 throw new ArgumentNullException(nameof(sprite));
157 if (_children.Contains(sprite))
160 EnforceSameSceneLayer(sprite);
162 sprite.Disposing += sprite_Disposing;
163 _children.Add(sprite);
176 throw new ArgumentNullException(nameof(sprite));
178 if (_children.Contains(sprite))
181 EnforceSameSceneLayer(sprite);
183 sprite.Disposing += sprite_Disposing;
184 _children.Add(sprite);
200 throw new ArgumentNullException(nameof(sprite));
202 if (_children.Contains(sprite))
205 EnforceSameSceneLayer(sprite);
210 if (_children.Count == 0)
212 sprite.Disposing += sprite_Disposing;
213 _children.Add(sprite);
215 Vector2 firstPos = GetSpritePosition(sprite);
216 Vector2 target = firstPos + offsetFromCompositeAnchor;
218 if (offsetFromCompositeAnchor != Vector2.Zero)
238 if (!_children.Contains(sprite))
241 sprite.Disposing -= sprite_Disposing;
242 _children.Remove(sprite);
245 private void sprite_Disposing(
Sprite sprite)
247 sprite.Disposing -= sprite_Disposing;
248 _children.Remove(sprite);
251 private void EnforceSameSceneLayer(Sprite sprite)
253 if (_children.Count == 0)
256 if (!ReferenceEquals(sprite.SceneLayer, _children[0].SceneLayer))
257 throw new InvalidOperationException(
"All sprites in a CompositeSprite must belong to the same SceneLayer.");
260 private static Vector2 GetSpritePosition(Sprite sprite)
264 PointF p = sprite.SceneLayerCoordinates;
265 return new Vector2(p.X, p.Y);
276 public ReadOnlyCollection<Sprite>
Children => _children.AsReadOnly();
287 if (_children.Count == 0)
288 return Rectangle.Empty;
290 int minX =
int.MaxValue;
291 int minY =
int.MaxValue;
292 int maxX =
int.MinValue;
293 int maxY =
int.MinValue;
295 foreach (var sprite
in _children)
297 var r = sprite.DrawLocationWorld;
312 return Rectangle.FromLTRB(
320 #endregion properties
Vector2 GetPosition()
Gets the current position of the composite sprite in scene-layer grid coordinates,...
void AddChildWithOffset(Sprite sprite, Vector2 offsetFromCompositeAnchor)
Add a sprite and place it by offset from the composite anchor. Offset is interpreted in grid coordina...
SceneLayer SceneLayer
Gets the scene layer that all child sprites belong to.
void Translate(Vector2 delta)
Translates the composite sprite by the specified delta vector.
void SetPosition(Vector2 pos)
Sets the position of the composite sprite by moving all child sprites to maintain their relative posi...
ReadOnlyCollection< Sprite > Children
Gets a read-only collection of all child sprites in this composite.
CompositeSprite(params Sprite[] sprites)
Initializes a new instance of the CompositeSprite class with the specified array of sprites.
CompositeSprite(List< Sprite > sprites)
Initializes a new instance of the CompositeSprite class with the specified list of sprites.
void AddChildAtPosition(Sprite sprite, Vector2 absolutePosition)
Add a sprite and place it at an absolute position (in the composite's PositionSpace).
Rectangle Range
Gets the bounding rectangle that encompasses all child sprites. Returns Rectangle....
void Remove(Sprite sprite)
Removes a sprite from this composite.
CompositeAnchorMode AnchorMode
Gets or sets the anchor mode that determines how the composite's position is calculated relative to i...
void Add(Sprite sprite)
Adds an existing sprite at its current position (no reposition).
MovementSpace PositionSpace
Gets the movement space used for positioning this composite sprite.
CompositeSprite()
Initializes a new instance of the CompositeSprite class.
Represents a movable visual element on a scene layer.
void SetPosition(Vector2 pos)
Sets the sprite position in scene-layer grid coordinates and invalidates its old and new bounds.
Implemented by grid-space IMovable objects that belong to a specific Scenes.SceneLayer.
CompositeAnchorMode
Defines the anchor mode for a composite sprite, determining how its position is calculated.
@ Center
The anchor is at the center of the bounding rectangle.
@ TopLeft
The anchor is at the top-left corner of the bounding rectangle.
MovementSpace
Specifies the coordinate system used for movement calculations and updates.