public static void RenderLinePrimitive(LinePrimitive linePrimitive, SpriteRenderer spriteRenderer, IRenderableIpso ipso, SystemManagers managers, bool isDotted) { linePrimitive.Position.X = ipso.GetAbsoluteX(); linePrimitive.Position.Y = ipso.GetAbsoluteY(); Renderer renderer; if (managers != null) { renderer = managers.Renderer; } else { renderer = Renderer.Self; } Texture2D textureToUse = renderer.SinglePixelTexture; if (isDotted) { textureToUse = renderer.DottedLineTexture; } linePrimitive.Render(spriteRenderer, managers, textureToUse, .2f * renderer.Camera.Zoom); }
public void SetValuesFrom(IRenderableIpso ipso) { //if(ipso is GraphicalUiElement gue && (gue.RenderableComponent as IRenderableIpso) != null) //{ // ipso = (gue.RenderableComponent as IRenderableIpso); //} this.mX = ipso.GetAbsoluteX(); this.mY = ipso.GetAbsoluteY(); this.mWidth = ipso.Width; this.mHeight = ipso.Height; this.mRotation = ipso.GetAbsoluteRotation(); if (ipso is GraphicalUiElement) { var asGue = ipso as GraphicalUiElement; originDisplay.SetOriginXPosition(asGue); originDisplay.UpdateTo(asGue); } UpdateToProperties(); }
public static SKRect?GetEffectiveClipRect(this IRenderableIpso renderableIpso) { if (renderableIpso is InvisibleRenderable invisibleRenderable && invisibleRenderable.ClipsChildren) { var left = renderableIpso.GetAbsoluteX(); var top = renderableIpso.GetAbsoluteY(); var right = left + renderableIpso.Width; var bottom = top + renderableIpso.Height; return(new SKRect(left, top, right, bottom)); }
private void SetLineRectangleAroundIpso(LineRectangle rectangle, IRenderableIpso pso) { float adjustedSelectionBorder = SelectionBorder / Renderer.Self.Camera.Zoom; rectangle.Visible = true; rectangle.X = pso.GetAbsoluteX() - adjustedSelectionBorder; rectangle.Y = pso.GetAbsoluteY() - adjustedSelectionBorder; rectangle.Width = pso.Width + adjustedSelectionBorder * 2; rectangle.Height = pso.Height + adjustedSelectionBorder * 2; rectangle.Rotation = pso.GetAbsoluteRotation(); }
public static bool HasCursorOver(this IRenderableIpso ipso, float x, float y) { float absoluteX = ipso.GetAbsoluteX(); float absoluteY = ipso.GetAbsoluteY(); // put the cursor in object space: x -= absoluteX; y -= absoluteY; // normally it's negative, but we are going to * -1 to rotate the other way var matrix = Matrix.CreateRotationZ(-MathHelper.ToRadians(ipso.Rotation) * -1); var relativePosition = new Vector2(x, y); relativePosition = Vector2.Transform(relativePosition, matrix); bool isXInRange = false; if (ipso.Width < 0) { isXInRange = relativePosition.X <0 && relativePosition.X> ipso.Width; } else { isXInRange = relativePosition.X > 0 && relativePosition.X < ipso.Width; } bool isYInRange = false; if (ipso.Height < 0) { isYInRange = relativePosition.Y <0 && relativePosition.Y> ipso.Height; } else { isYInRange = relativePosition.Y > 0 && relativePosition.Y < ipso.Height; } return(isXInRange && isYInRange); }
public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer, IRenderableIpso ipso, Texture2D texture, Color color, Rectangle?sourceRectangle = null, bool flipVertical = false, float rotationInDegrees = 0, bool treat0AsFullDimensions = false, // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states object objectCausingRenering = null ) { if (objectCausingRenering == null) { objectCausingRenering = ipso; } Renderer renderer = null; if (managers == null) { renderer = Renderer.Self; } else { renderer = managers.Renderer; } Texture2D textureToUse = texture; if (textureToUse == null) { textureToUse = LoaderManager.Self.InvalidTexture; if (textureToUse == null) { return; } } SpriteEffects effects = SpriteEffects.None; var flipHorizontal = ipso.GetAbsoluteFlipHorizontal(); var effectiveParentFlipHorizontal = ipso.Parent?.GetAbsoluteFlipHorizontal() ?? false; if (flipHorizontal) { effects |= SpriteEffects.FlipHorizontally; //rotationInDegrees *= -1; } var rotationInRadians = MathHelper.ToRadians(rotationInDegrees); float leftAbsolute = ipso.GetAbsoluteX(); float topAbsolute = ipso.GetAbsoluteY(); Vector2 origin = Vector2.Zero; //if(flipHorizontal) //{ // var offsetX = (float)System.Math.Cos(rotationInRadians); // var offsetY = (float)System.Math.Sin(rotationInRadians); // origin.X = 1; //} if (flipVertical) { effects |= SpriteEffects.FlipVertically; } var modifiedColor = color; if (Renderer.NormalBlendState == BlendState.AlphaBlend) { // we are using premult textures, so we need to premult the color: var alphaRatio = color.A / 255.0f; modifiedColor.R = (byte)(color.R * alphaRatio); modifiedColor.G = (byte)(color.G * alphaRatio); modifiedColor.B = (byte)(color.B * alphaRatio); } if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false) { Vector2 scale = Vector2.One; if (textureToUse == null) { scale = new Vector2(ipso.Width, ipso.Height); } else { float ratioWidth = 1; float ratioHeight = 1; if (sourceRectangle.HasValue) { ratioWidth = sourceRectangle.Value.Width / (float)textureToUse.Width; ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height; } scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width), ipso.Height / (ratioHeight * textureToUse.Height)); } #if DEBUG if (textureToUse != null && textureToUse.IsDisposed) { throw new ObjectDisposedException($"Texture is disposed. Texture name: {textureToUse.Name}, sprite scale: {scale}, Sprite name: {ipso.Name}"); } #endif spriteRenderer.Draw(textureToUse, new Vector2(leftAbsolute, topAbsolute), sourceRectangle, modifiedColor, -rotationInRadians, origin, scale, effects, 0, objectCausingRenering); } else { int width = textureToUse.Width; int height = textureToUse.Height; if (sourceRectangle != null && sourceRectangle.HasValue) { width = sourceRectangle.Value.Width; height = sourceRectangle.Value.Height; } Rectangle destinationRectangle = new Rectangle( (int)(leftAbsolute), (int)(topAbsolute), width, height); spriteRenderer.Draw(textureToUse, destinationRectangle, sourceRectangle, modifiedColor, rotationInRadians, origin, effects, 0, objectCausingRenering ); } }
public static float GetAbsoluteRight(this IRenderableIpso ipso) { return(ipso.GetAbsoluteX() + ipso.Width); }
public static float GetAbsoluteLeft(this IRenderableIpso ipso) { return(ipso.GetAbsoluteX()); }
public static float GetAbsoluteCenterX(this IRenderableIpso ipso) { return(ipso.GetAbsoluteX() + ipso.Width / 2.0f); }
public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer, IRenderableIpso ipso, Texture2D texture, Color color, Rectangle? sourceRectangle = null, bool flipHorizontal = false, bool flipVertical = false, float rotationInDegrees = 0, bool treat0AsFullDimensions = false, // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states object objectCausingRenering = null ) { if (objectCausingRenering == null) { objectCausingRenering = ipso; } Renderer renderer = null; if (managers == null) { renderer = Renderer.Self; } else { renderer = managers.Renderer; } Texture2D textureToUse = texture; if (textureToUse == null) { textureToUse = LoaderManager.Self.InvalidTexture; if (textureToUse == null) { return; } } SpriteEffects effects = SpriteEffects.None; if (flipHorizontal) { effects |= SpriteEffects.FlipHorizontally; } if (flipVertical) { effects |= SpriteEffects.FlipVertically; } var modifiedColor = color; if (Renderer.NormalBlendState == BlendState.AlphaBlend) { // we are using premult textures, so we need to premult the color: var alphaRatio = color.A / 255.0f; modifiedColor.R = (byte)(color.R * alphaRatio); modifiedColor.G = (byte)(color.G * alphaRatio); modifiedColor.B = (byte)(color.B * alphaRatio); } if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false) { Vector2 scale = Vector2.One; if (textureToUse == null) { scale = new Vector2(ipso.Width, ipso.Height); } else { float ratioWidth = 1; float ratioHeight = 1; if (sourceRectangle.HasValue) { ratioWidth = sourceRectangle.Value.Width / (float)textureToUse.Width; ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height; } scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width), ipso.Height / (ratioHeight * textureToUse.Height)); } if (textureToUse != null && textureToUse.IsDisposed) { throw new ObjectDisposedException("Texture is disposed. Texture name: " + textureToUse.Name + ", sprite scale: " + scale); } spriteRenderer.Draw(textureToUse, new Vector2(ipso.GetAbsoluteX(), ipso.GetAbsoluteY()), sourceRectangle, modifiedColor, Microsoft.Xna.Framework.MathHelper.TwoPi * -rotationInDegrees / 360.0f, Vector2.Zero, scale, effects, 0, objectCausingRenering); } else { int width = textureToUse.Width; int height = textureToUse.Height; if (sourceRectangle != null && sourceRectangle.HasValue) { width = sourceRectangle.Value.Width; height = sourceRectangle.Value.Height; } Rectangle destinationRectangle = new Rectangle( (int)(ipso.GetAbsoluteX()), (int)(ipso.GetAbsoluteY()), width, height); spriteRenderer.Draw(textureToUse, destinationRectangle, sourceRectangle, modifiedColor, rotationInDegrees / 360.0f, Vector2.Zero, effects, 0, objectCausingRenering ); } }
public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer, IRenderableIpso ipso, Texture2D texture, Color color, Rectangle?sourceRectangle = null, bool flipHorizontal = false, bool flipVertical = false, float rotationInDegrees = 0, bool treat0AsFullDimensions = false, // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states object objectCausingRenering = null ) { if (objectCausingRenering == null) { objectCausingRenering = ipso; } Renderer renderer = null; if (managers == null) { renderer = Renderer.Self; } else { renderer = managers.Renderer; } Texture2D textureToUse = texture; if (textureToUse == null) { textureToUse = LoaderManager.Self.InvalidTexture; if (textureToUse == null) { return; } } SpriteEffects effects = SpriteEffects.None; if (flipHorizontal) { effects |= SpriteEffects.FlipHorizontally; } if (flipVertical) { effects |= SpriteEffects.FlipVertically; } var modifiedColor = color; if (Renderer.NormalBlendState == BlendState.AlphaBlend) { // we are using premult textures, so we need to premult the color: var alphaRatio = color.A / 255.0f; modifiedColor.R = (byte)(color.R * alphaRatio); modifiedColor.G = (byte)(color.G * alphaRatio); modifiedColor.B = (byte)(color.B * alphaRatio); } if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false) { Vector2 scale = Vector2.One; if (textureToUse == null) { scale = new Vector2(ipso.Width, ipso.Height); } else { float ratioWidth = 1; float ratioHeight = 1; if (sourceRectangle.HasValue) { ratioWidth = sourceRectangle.Value.Width / (float)textureToUse.Width; ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height; } scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width), ipso.Height / (ratioHeight * textureToUse.Height)); } if (textureToUse != null && textureToUse.IsDisposed) { throw new ObjectDisposedException("Texture is disposed. Texture name: " + textureToUse.Name + ", sprite scale: " + scale); } spriteRenderer.Draw(textureToUse, new Vector2(ipso.GetAbsoluteX(), ipso.GetAbsoluteY()), sourceRectangle, modifiedColor, Microsoft.Xna.Framework.MathHelper.TwoPi * -rotationInDegrees / 360.0f, Vector2.Zero, scale, effects, 0, objectCausingRenering); } else { int width = textureToUse.Width; int height = textureToUse.Height; if (sourceRectangle != null && sourceRectangle.HasValue) { width = sourceRectangle.Value.Width; height = sourceRectangle.Value.Height; } Rectangle destinationRectangle = new Rectangle( (int)(ipso.GetAbsoluteX()), (int)(ipso.GetAbsoluteY()), width, height); spriteRenderer.Draw(textureToUse, destinationRectangle, sourceRectangle, modifiedColor, rotationInDegrees / 360.0f, Vector2.Zero, effects, 0, objectCausingRenering ); } }