Skip to content

Commit

Permalink
massive refactoring of RenderableComponent!
Browse files Browse the repository at this point in the history
moved all SpriteEffects code and origin to Sprite
  • Loading branch information
prime31 committed Oct 10, 2016
1 parent ef00d30 commit f6a7b03
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 262 deletions.
6 changes: 4 additions & 2 deletions Nez-PCL/ECS/Components/Physics/Collider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ public virtual void onEntityAddedToScene()
circle.radius = Math.Max( width, height ) * 0.5f;

// fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider
var localCenter = renderableBounds.center - entity.transform.position;
localOffset = localCenter;
localOffset = renderableBounds.center - entity.transform.position;
}
else
{
shape = new Box( width, height );

// fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider
localOffset = renderableBounds.center - entity.transform.position;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Nez-PCL/ECS/Components/Renderables/3D/Renderable3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public Vector3 rotation
/// <value>The rotation degrees.</value>
public Vector3 rotationDegrees
{
get { return new Vector3( _rotationXY, transform.rotation ) * 57.295779513082320876798154814105f; }
set { rotation = value *= 0.017453292519943295769236907684886f; }
get { return new Vector3( _rotationXY, transform.rotation ) * Mathf.rad2Deg; }
set { rotation = value *= Mathf.deg2Rad; }
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions Nez-PCL/ECS/Components/Renderables/LineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ public override void onEntityTransformChanged( Transform.Component comp )
if( useWorldSpace )
return;

_bounds.calculateBounds( entity.transform.position, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, width, height );
_bounds.calculateBounds( entity.transform.position, _localOffset, Vector2.Zero, entity.transform.scale, entity.transform.rotation, width, height );
}


Expand Down Expand Up @@ -841,8 +841,10 @@ public override void debugRender( Graphics graphics )
for( var i = 0; i < _vertices.length; i++ )
{
var v = _vertices[i];
Debug.drawPixel( v.Position.X, v.Position.Y, 4, Color.GhostWhite );
graphics.batcher.drawPixel( v.Position.X, v.Position.Y, Color.GhostWhite, 4 );
}

graphics.batcher.drawHollowRect( _bounds, DefaultColors.colliderBounds );
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Nez-PCL/ECS/Components/Renderables/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override RectangleF bounds
{
if( _areBoundsDirty )
{
_bounds.calculateBounds( entity.transform.position + _topLeftVertPosition, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, _width, _height );
_bounds.calculateBounds( entity.transform.position + _topLeftVertPosition, Vector2.Zero, Vector2.Zero, entity.transform.scale, entity.transform.rotation, _width, _height );
_areBoundsDirty = false;
}

Expand Down
210 changes: 42 additions & 168 deletions Nez-PCL/ECS/Components/Renderables/RenderableComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Nez
Expand All @@ -13,12 +12,6 @@ public abstract class RenderableComponent : Component, IComparable<RenderableCom
{
#region properties and fields

/// <summary>
/// used by Renderers to specify how this sprite should be rendered. If non-null, it is automatically disposed of when the Component
/// is removed from the Entity.
/// </summary>
public virtual Material material { get; set; }

/// <summary>
/// width of the RenderableComponent. subclasses that do not override the bounds property must implement this!
/// </summary>
Expand All @@ -32,19 +25,21 @@ public abstract class RenderableComponent : Component, IComparable<RenderableCom
public virtual float height { get { return bounds.height; } }

/// <summary>
/// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning.
/// the AABB that wraps this object
/// </summary>
/// <value>The local position.</value>
public Vector2 localOffset
/// <value>The bounds.</value>
public virtual RectangleF bounds
{
get { return _localOffset; }
set { setLocalOffset( value ); }
}
get
{
if( _areBoundsDirty )
{
_bounds.calculateBounds( entity.transform.position, _localOffset, Vector2.Zero, entity.transform.scale, entity.transform.rotation, width, height );
_areBoundsDirty = false;
}

public Vector2 origin
{
get { return _origin; }
set { setOrigin( value ); }
return _bounds;
}
}

/// <summary>
Expand All @@ -57,16 +52,6 @@ public float layerDepth
set { setLayerDepth( value ); }
}

/// <summary>
/// color passed along to the Batcher when rendering
/// </summary>
public Color color = Color.White;

/// <summary>
/// Batchers passed along to the Batcher when rendering. flipX/flipY are helpers for setting this.
/// </summary>
public SpriteEffects spriteEffects = SpriteEffects.None;

/// <summary>
/// lower renderLayers are in the front and higher are in the back, just like layerDepth but not clamped to 0-1. Note that this means
/// higher renderLayers are sent to the Batcher first. An important fact when using the stencil buffer.
Expand All @@ -79,63 +64,24 @@ public int renderLayer
}

/// <summary>
/// determines if the sprite should be rendered normally or flipped horizontally
/// </summary>
/// <value><c>true</c> if flip x; otherwise, <c>false</c>.</value>
public bool flipX
{
get
{
return ( spriteEffects & SpriteEffects.FlipHorizontally ) == SpriteEffects.FlipHorizontally;
}
set
{
spriteEffects = value ? ( spriteEffects | SpriteEffects.FlipHorizontally ) : ( spriteEffects & ~SpriteEffects.FlipHorizontally );
}
}

/// <summary>
/// determines if the sprite should be rendered normally or flipped vertically
/// color passed along to the Batcher when rendering
/// </summary>
/// <value><c>true</c> if flip y; otherwise, <c>false</c>.</value>
public bool flipY
{
get
{
return ( spriteEffects & SpriteEffects.FlipVertically ) == SpriteEffects.FlipVertically;
}
set
{
spriteEffects = value ? ( spriteEffects | SpriteEffects.FlipVertically ) : ( spriteEffects & ~SpriteEffects.FlipVertically );
}
}
public Color color = Color.White;

/// <summary>
/// the AABB that wraps this object
/// used by Renderers to specify how this sprite should be rendered. If non-null, it is automatically disposed of when the Component
/// is removed from the Entity.
/// </summary>
/// <value>The bounds.</value>
public virtual RectangleF bounds
{
get
{
if( _areBoundsDirty )
{
_bounds.calculateBounds( entity.transform.position, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, width, height );
_areBoundsDirty = false;
}

return _bounds;
}
}
public virtual Material material { get; set; }

/// <summary>
/// helper property for setting the origin in normalized fashion (0-1 for x and y)
/// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning.
/// </summary>
/// <value>The origin normalized.</value>
public Vector2 originNormalized
/// <value>The local position.</value>
public Vector2 localOffset
{
get { return new Vector2( _origin.X / width, _origin.Y / height ); }
set { setOrigin( new Vector2( value.X * width, value.Y * height ) ); }
get { return _localOffset; }
set { setLocalOffset( value ); }
}

/// <summary>
Expand All @@ -160,7 +106,6 @@ private set
}

protected Vector2 _localOffset;
protected Vector2 _origin;
protected float _layerDepth;
protected int _renderLayer;
protected RectangleF _bounds;
Expand Down Expand Up @@ -206,19 +151,19 @@ public override void debugRender( Graphics graphics )
/// isVisibleFromCamera for its culling check.
/// </summary>
protected virtual void onBecameVisible()
{}
{ }


/// <summary>
/// called when the renderable exits the camera frame. Note that these methods will not be called if your render method does not use
/// isVisibleFromCamera for its culling check.
/// </summary>
protected virtual void onBecameInvisible()
{}
{ }


public override void onRemovedFromEntity()
{}
{ }


/// <summary>
Expand All @@ -245,50 +190,6 @@ public RenderableComponent setMaterial( Material material )
}


/// <summary>
/// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning.
/// </summary>
/// <returns>The local offset.</returns>
/// <param name="offset">Offset.</param>
public RenderableComponent setLocalOffset( Vector2 offset )
{
if( _localOffset != offset )
{
_localOffset = offset;
_areBoundsDirty = true;
}
return this;
}


/// <summary>
/// sets the origin for the Renderable
/// </summary>
/// <returns>The origin.</returns>
/// <param name="origin">Origin.</param>
public RenderableComponent setOrigin( Vector2 origin )
{
if( _origin != origin )
{
_origin = origin;
_areBoundsDirty = true;
}
return this;
}


/// <summary>
/// helper for setting the origin in normalized fashion (0-1 for x and y)
/// </summary>
/// <returns>The origin normalized.</returns>
/// <param name="origin">Origin.</param>
public RenderableComponent setOriginNormalized( Vector2 value )
{
setOrigin( new Vector2( value.X * width, value.Y * height ) );
return this;
}


/// <summary>
/// standard Batcher layerdepth. 0 is in front and 1 is in back. Changing this value will trigger a sort of the renderableComponents
/// </summary>
Expand Down Expand Up @@ -336,6 +237,22 @@ public RenderableComponent setColor( Color color )
return this;
}


/// <summary>
/// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning.
/// </summary>
/// <returns>The local offset.</returns>
/// <param name="offset">Offset.</param>
public RenderableComponent setLocalOffset( Vector2 offset )
{
if( _localOffset != offset )
{
_localOffset = offset;
_areBoundsDirty = true;
}
return this;
}

#endregion


Expand All @@ -351,49 +268,6 @@ public T getMaterial<T>() where T : Material
return material as T;
}


/// <summary>
/// Draws the Renderable with an outline. Note that this should be called on disabled Renderables since they shouldnt take part in default
/// rendering if they need an ouline.
/// </summary>
/// <param name="graphics">Graphics.</param>
/// <param name="camera">Camera.</param>
/// <param name="offset">Offset.</param>
public void drawOutline( Graphics graphics, Camera camera, int offset = 1 )
{
drawOutline( graphics, camera, Color.Black, offset );
}


public void drawOutline( Graphics graphics, Camera camera, Color outlineColor, int offset = 1 )
{
// save the stuff we are going to modify so we can restore it later
var originalPosition = _localOffset;
var originalColor = color;
var originalLayerDepth = _layerDepth;

// set our new values
color = outlineColor;
_layerDepth += 0.01f;

for( var i = -1; i < 2; i++ )
{
for( var j = -1; j < 2; j++ )
{
if( i != 0 || j != 0 )
{
_localOffset = originalPosition + new Vector2( i * offset, j * offset );
render( graphics, camera );
}
}
}

// restore changed state
_localOffset = originalPosition;
color = originalColor;
_layerDepth = originalLayerDepth;
}

#endregion


Expand All @@ -418,7 +292,7 @@ public int CompareTo( RenderableComponent other )

if( other.material == null )
return -1;

return 1;
}
}
Expand Down
Loading

0 comments on commit f6a7b03

Please sign in to comment.