internal bool Pick <NodeType>(Cv_Renderer renderer, Vector2 screenPosition, List <Cv_EntityID> entities) where NodeType : Cv_SceneNode { if (this is Cv_HolderNode) { CaravelApp.Instance.Scene.PushAndSetTransform(Transform); } var success = false; foreach (var child in Children) { if (child is Cv_HolderNode && child.Pick <NodeType>(renderer, screenPosition, entities)) { success = true; } else if (child is NodeType && child.VPick(renderer, screenPosition, entities)) { success = true; } } if (this is Cv_HolderNode) { CaravelApp.Instance.Scene.PopTransform(); } return(success); }
//TODO(JM): Maybe change rendering to draw text into a texture and then reuse texture internal override void VRender(Cv_Renderer renderer) { var textComponent = (Cv_TextComponent)Component; var scene = CaravelApp.Instance.Scene; var pos = scene.Transform.Position; var rot = scene.Transform.Rotation; var scale = scene.Transform.Scale; var camTransf = scene.Camera.GetViewTransform(renderer.VirtualWidth, renderer.VirtualHeight, Cv_Transform.Identity); if (textComponent.Parallax != 1 && textComponent.Parallax != 0) { var zoomFactor = ((1 + ((scene.Camera.Zoom - 1) * textComponent.Parallax)) / scene.Camera.Zoom); scale = scale * zoomFactor; //Magic formula pos += ((textComponent.Parallax - 1) * new Vector3(camTransf.Position.X, camTransf.Position.Y, 0)); pos += ((new Vector3(scene.Transform.Position.X, scene.Transform.Position.Y, 0)) * (1 - zoomFactor) * (textComponent.Parallax - 1)); } var noCamera = textComponent.Parallax == 0; textComponent.DrawSelectionHighlight(renderer); if (!textComponent.Visible || textComponent.Text == null || textComponent.Text == "" || textComponent.FontResource == null || textComponent.FontResource == "") { return; } Cv_SpriteFontResource resource = Cv_ResourceManager.Instance.GetResource <Cv_SpriteFontResource>(textComponent.FontResource, textComponent.Owner.ResourceBundle); var font = resource.GetFontData().Font; var textToDisplay = ""; if (textComponent.LiteralText) { textToDisplay = textComponent.Text; } else { textToDisplay = CaravelApp.Instance.GetString(textComponent.Text); } var text = WrapText(textToDisplay, font, textComponent.Width, textComponent.Height); var layerDepth = (int)Parent.Position.Z; layerDepth = layerDepth % Cv_Renderer.MaxLayers; var bounds = new Rectangle((int)(pos.X - (textComponent.Width * scene.Transform.Origin.X)), (int)(pos.Y - (textComponent.Width * scene.Transform.Origin.Y)), (int)textComponent.Width, (int)textComponent.Width); renderer.DrawText(font, text, bounds, textComponent.HorizontalAlignment, textComponent.VerticalAlignment, textComponent.Color, rot, Math.Min(scale.X, scale.Y), SpriteEffects.None, layerDepth / (float)Cv_Renderer.MaxLayers, noCamera); }
public override void VOnPostRender(Cv_Renderer renderer) { if (m_Root != null) { m_Root.VFinishedRender(renderer); } }
internal override void VRender(Cv_Renderer renderer) { if (renderer.DebugDrawClickAreas) { var clickableComponent = (Cv_ClickableComponent)Component; var scene = CaravelApp.Instance.Scene; var pos = scene.Transform.Position; var rot = scene.Transform.Rotation; var scale = scene.Transform.Scale; var offsetX = clickableComponent.AnchorPoint.X; var offsetY = clickableComponent.AnchorPoint.Y; var rotMatrixZ = Matrix.CreateRotationZ(rot); Vector2 point1; Vector2 point2; List <Vector2> points = new List <Vector2>(); var width = clickableComponent.Width * scale.X; var height = clickableComponent.Height * scale.Y; points.Add(new Vector2(0, 0)); points.Add(new Vector2(width, 0)); points.Add(new Vector2(width, height)); points.Add(new Vector2(0, height)); for (int i = 0, j = 1; i < points.Count; i++, j++) { if (j >= points.Count) { j = 0; } point1 = new Vector2(points[i].X, points[i].Y); point2 = new Vector2(points[j].X, points[j].Y); point1 -= new Vector2(scene.Transform.Origin.X * width, scene.Transform.Origin.Y * height); point2 -= new Vector2(scene.Transform.Origin.X * width, scene.Transform.Origin.Y * height); point1 -= new Vector2(offsetX * scale.X, offsetY * scale.Y); point2 -= new Vector2(offsetX * scale.X, offsetY * scale.Y); point1 = Vector2.Transform(point1, rotMatrixZ); point2 = Vector2.Transform(point2, rotMatrixZ); point1 += new Vector2(pos.X, pos.Y); point2 += new Vector2(pos.X, pos.Y); var thickness = (int)Math.Ceiling(3 / scene.Camera.Zoom); if (thickness <= 0) { thickness = 1; } Cv_DrawUtils.DrawLine(renderer, point1, point2, thickness, Cv_Renderer.MaxLayers - 1, Color.White); } } }
internal override bool VPick(Cv_Renderer renderer, Vector2 screenPosition, List <Cv_EntityID> entities) { CaravelApp.Instance.Scene.PushAndSetTransform(Transform); var rtrnVal = base.VPick(renderer, screenPosition, entities); CaravelApp.Instance.Scene.PopTransform(); return(rtrnVal); }
internal override void VPreRender(Cv_Renderer renderer) { CaravelApp.Instance.Scene.PushAndSetTransform(Transform); if (Properties.Radius > 0 && m_DebugCircleTex == null && renderer.DebugDrawRadius) { m_DebugCircleTex = Cv_DrawUtils.CreateCircle((int)Properties.Radius / 2); } }
internal override bool VPick(Cv_Renderer renderer, Vector2 screenPosition, List <Cv_EntityID> entities) { if (Component != null) { var clickableComp = (Cv_ClickableComponent)Component; var camMatrix = renderer.CamMatrix; var worldTransform = CaravelApp.Instance.Scene.Transform; var pos = new Vector2(worldTransform.Position.X, worldTransform.Position.Y); var rot = worldTransform.Rotation; var scale = worldTransform.Scale; var offsetX = clickableComp.AnchorPoint.X; var offsetY = clickableComp.AnchorPoint.Y; var transformedVertices = new List <Vector2>(); var point1 = new Vector2(-(((worldTransform.Origin.X * clickableComp.Width) + offsetX) * scale.X), -(((worldTransform.Origin.Y * clickableComp.Height) + offsetY) * scale.Y)); var point2 = new Vector2(point1.X + (clickableComp.Width * scale.X), point1.Y); var point3 = new Vector2(point2.X, point1.Y + (clickableComp.Height * scale.Y)); var point4 = new Vector2(point1.X, point3.Y); Matrix rotMat = Matrix.CreateRotationZ(rot); point1 = Vector2.Transform(point1, rotMat); point2 = Vector2.Transform(point2, rotMat); point3 = Vector2.Transform(point3, rotMat); point4 = Vector2.Transform(point4, rotMat); point1 += pos; point2 += pos; point3 += pos; point4 += pos; transformedVertices.Add(point1); transformedVertices.Add(point2); transformedVertices.Add(point3); transformedVertices.Add(point4); var invertedTransform = Matrix.Invert(camMatrix); var worldPoint = Vector2.Transform(screenPosition, invertedTransform); if (Cv_DrawUtils.PointInPolygon(worldPoint, transformedVertices)) { entities.Add(Properties.EntityID); return(true); } else { return(false); } } return(false); }
internal virtual void VFinishedRender(Cv_Renderer renderer) { TransformChanged = false; foreach (var child in Children) { child.VFinishedRender(renderer); } }
public override void VOnRender(float time, float elapsedTime, Cv_Renderer renderer) { if (m_Root != null && Camera != null) { m_TransformStack.Clear(); renderer.BeginDraw(Camera); m_Root.VPreRender(renderer); m_Root.VRender(renderer); m_Root.VRenderChildren(renderer); m_Root.VPostRender(renderer); renderer.EndDraw(); } }
internal override void VRender(Cv_Renderer renderer) { if (renderer.DebugDrawRadius && GetRadius(renderer) > 0 && m_DebugCircleTex != null) { var pos = CaravelApp.Instance.Scene.Transform.Position; var radius = GetRadius(renderer); Rectangle r2 = new Rectangle((int)(pos.X - radius), (int)(pos.Y - radius), (int)(radius * 2), (int)(radius * 2)); renderer.Draw(m_DebugCircleTex, r2, null, Color.Blue, 0, Vector2.Zero, SpriteEffects.None, pos.Z); } }
internal virtual bool VPick(Cv_Renderer renderer, Vector2 screenPosition, List <Cv_EntityID> entities) { var success = false; foreach (var child in Children) { if (child.VPick(renderer, screenPosition, entities)) { success = true; } } return(success); }
public static void DrawRectangle(Cv_Renderer r, Rectangle rectangleToDraw, int thickness, Color color) { // Draw top line r.Draw(m_DrawPixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, rectangleToDraw.Width, thickness), color); // Draw left line r.Draw(m_DrawPixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, thickness, rectangleToDraw.Height), color); // Draw right line r.Draw(m_DrawPixel, new Rectangle((rectangleToDraw.X + rectangleToDraw.Width - thickness), rectangleToDraw.Y, thickness, rectangleToDraw.Height), color); // Draw bottom line r.Draw(m_DrawPixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y + rectangleToDraw.Height - thickness, rectangleToDraw.Width, thickness), color); }
internal virtual void VRenderChildren(Cv_Renderer renderer) { foreach (var child in Children) { child.VPreRender(renderer); if (child.VIsVisible(renderer)) { child.VRender(renderer); child.VRenderChildren(renderer); } child.VPostRender(renderer); } }
public override void VOnRender(float time, float elapsedTime, Cv_Renderer renderer) { m_iFrameCounter++; string fps = string.Format(m_Format, "{0} fps", m_iFrameRate); var fontResource = Cv_ResourceManager.Instance.GetResource <Cv_SpriteFontResource>("FramerateCounterFont", "Default"); if (fontResource != null) { renderer.BeginDraw(); renderer.DrawText(fontResource.GetFontData().Font, fps, m_Position + Vector2.One, Color.Black); renderer.DrawText(fontResource.GetFontData().Font, fps, m_Position, Color.White); renderer.EndDraw(); } }
internal override float GetRadius(Cv_Renderer renderer) { if (Properties.Radius < 0) { var transf = Parent.Transform; var originFactorX = Math.Abs(transf.Origin.X - 0.5) + 0.5; var originFactorY = Math.Abs(transf.Origin.Y - 0.5) + 0.5; var originFactor = (float)Math.Max(originFactorX, originFactorY); var comp = ((Cv_TextComponent)Component); Properties.Radius = (float)Math.Sqrt(comp.Width * comp.Width + comp.Height * comp.Height) * originFactor; Properties.Radius *= Math.Max(transf.Scale.X, transf.Scale.Y); } return(Properties.Radius); }
internal override float GetRadius(Cv_Renderer renderer) { Properties.Radius = 0; foreach (var child in Children) { var childPos = child.Position * new Vector3(WorldTransform.Scale, 1); var radius = childPos.Length() + child.GetRadius(renderer); if (radius > Properties.Radius) { Properties.Radius = radius; } } return(Properties.Radius); }
internal override float GetRadius(Cv_Renderer renderer) { Properties.Radius = -1; //Force radius recalculation each time if (Properties.Radius < 0) { var transf = Parent.WorldTransform; var originFactorX = Math.Abs(transf.Origin.X - 0.5) + 0.5; var originFactorY = Math.Abs(transf.Origin.Y - 0.5) + 0.5; var originFactor = (float)Math.Max(originFactorX, originFactorY); var comp = ((Cv_SpriteComponent)Component); Properties.Radius = (float)Math.Sqrt(comp.Width * comp.Width + comp.Height * comp.Height) * originFactor; Properties.Radius *= Math.Max(transf.Scale.X, transf.Scale.Y); } return(Properties.Radius); }
internal override float GetRadius(Cv_Renderer renderer) { if (Properties.Radius < 0 || ((Cv_CameraComponent)Component).ZoomChanged) { var transf = Parent.Transform; var originFactorX = Math.Abs(transf.Origin.X - 0.5) + 0.5; var originFactorY = Math.Abs(transf.Origin.Y - 0.5) + 0.5; var originFactor = (float)Math.Max(originFactorX, originFactorY); var zoom = Zoom; var width = renderer.VirtualWidth / zoom; var height = renderer.VirtualHeight / zoom; Properties.Radius = (float)Math.Sqrt(width * width + height * height) * originFactor; Properties.Radius *= Math.Max(transf.Scale.X, transf.Scale.Y); } return(Properties.Radius); }
internal bool Pick(Vector2 screenPosition, out Cv_EntityID[] entities, Cv_Renderer renderer) { var entityList = new List <Cv_EntityID>(); var scaledPosition = renderer.ScaleScreenToViewCoordinates(screenPosition); var result = false; if (scaledPosition.X >= 0 && scaledPosition.X <= renderer.Viewport.Width && scaledPosition.Y >= 0 && scaledPosition.Y <= renderer.Viewport.Height) { m_TransformStack.Clear(); result = m_Root.VPick(renderer, scaledPosition, entityList); } entities = entityList.ToArray(); entities = entities.OrderBy(e => Caravel.Logic.GetEntity(e).GetComponent <Cv_TransformComponent>() != null ? 1 : 2) .ThenByDescending(e => Caravel.Logic.GetEntity(e).GetComponent <Cv_TransformComponent>() != null ? Caravel.Logic.GetEntity(e).GetComponent <Cv_TransformComponent>().Position.Z : 0).ToArray(); return(result); }
public static void DrawLine(Cv_Renderer r, Vector2 start, Vector2 end, int thickness, int z, Color color, bool noCamera = false) { Vector2 edge = end - start; // calculate angle to rotate line float angle = (float)Math.Atan2(edge.Y, edge.X); float layer = (float)z / Cv_Renderer.MaxLayers; r.Draw(m_DrawPixel, new Rectangle( // rectangle defines shape of line and position of start of line (int)start.X, (int)start.Y, (int)edge.Length(), //sb will strech the texture to fill this rectangle thickness), //width of line, change this to make thicker line null, color, //colour of line angle, //angle of line (calulated above) Vector2.One, new Vector2(0, 0), // point in line about which to rotate SpriteEffects.None, layer, noCamera); }
internal override float GetRadius(Cv_Renderer renderer) { if (Properties.Radius < 0) { var transf = Parent.Transform; var originFactorX = Math.Abs(transf.Origin.X - 0.5) + 0.5; var originFactorY = Math.Abs(transf.Origin.Y - 0.5) + 0.5; var originFactor = (float)Math.Max(originFactorX, originFactorY); var particleComponent = (Cv_ParticleEmitterComponent)Component; var particleMaxTravel = ((particleComponent.EmitterVelocity.Length() + particleComponent.Gravity.Length() * particleComponent.ParticleLifeTime / 1000) * particleComponent.ParticleLifeTime / 1000); particleMaxTravel *= 1.5f; var comp = ((Cv_ParticleEmitterComponent)Component); Properties.Radius = (float)Math.Sqrt((comp.Width + particleMaxTravel) * (comp.Width + particleMaxTravel) + comp.Height * comp.Height) * originFactor; Properties.Radius *= Math.Max(transf.Scale.X, transf.Scale.Y); } return(Properties.Radius); }
internal abstract void VPostRender(Cv_Renderer renderer);
internal abstract bool VIsVisible(Cv_Renderer renderer);
internal virtual float GetRadius(Cv_Renderer renderer) { return(Properties.Radius); }
internal override void VRender(Cv_Renderer renderer) { var particleComponent = (Cv_ParticleEmitterComponent)Component; var scene = CaravelApp.Instance.Scene; particleComponent.DrawSelectionHighlight(renderer); if (!particleComponent.Visible || particleComponent.Texture == null || particleComponent.Texture == "") { return; } Cv_RawTextureResource resource = Cv_ResourceManager.Instance.GetResource <Cv_RawTextureResource>(particleComponent.Texture, particleComponent.Owner.ResourceBundle); var tex = resource.GetTexture().Texture; foreach (var particle in m_Particles) { if (!particle.IsAlive) { break; } var layerDepth = (int)particle.Transform.Position.Z; layerDepth = (layerDepth % Cv_Renderer.MaxLayers); var layer = layerDepth / (float)Cv_Renderer.MaxLayers; var newColorVec = EaseValue(new Vector4(particleComponent.Color.R, particleComponent.Color.G, particleComponent.Color.B, particleComponent.Color.A), new Vector4(particleComponent.FinalColor.R, particleComponent.FinalColor.G, particleComponent.FinalColor.B, particleComponent.FinalColor.A), particleComponent.ParticleLifeTime, particleComponent.ParticleLifeTime - particle.TTL, particleComponent.ColorChangePoint); var newColor = new Color((int)newColorVec.X, (int)newColorVec.Y, (int)newColorVec.Z, (int)newColorVec.W); var pos = particle.Transform.Position; var scale = particle.Transform.Scale; var camTransf = scene.Camera.GetViewTransform(renderer.VirtualWidth, renderer.VirtualHeight, Cv_Transform.Identity); if (particleComponent.Parallax != 1 && particleComponent.Parallax != 0) { var zoomFactor = ((1 + ((scene.Camera.Zoom - 1) * particleComponent.Parallax)) / scene.Camera.Zoom); scale = scale * zoomFactor; //Magic formula pos += ((particleComponent.Parallax - 1) * new Vector3(camTransf.Position.X, camTransf.Position.Y, 0)); pos += ((new Vector3(scene.Transform.Position.X, scene.Transform.Position.Y, 0)) * (1 - zoomFactor) * (particleComponent.Parallax - 1)); } var noCamera = particleComponent.Parallax == 0; renderer.Draw(tex, new Rectangle((int)(pos.X), (int)(pos.Y), (int)tex.Width, (int)tex.Height), null, newColor, particle.Transform.Rotation, scale, new Vector2(tex.Width * 0.5f, tex.Height * 0.5f), SpriteEffects.None, layer, noCamera); } }
internal override void VPreRender(Cv_Renderer renderer) { }
internal override bool VIsVisible(Cv_Renderer renderer) { return(true); }
internal override bool VPick(Cv_Renderer renderer, Vector2 screenPosition, List <Cv_EntityID> entities) { var particleComponent = (Cv_ParticleEmitterComponent)Component; return(particleComponent.Pick(renderer, screenPosition, entities)); }
internal override void VRender(Cv_Renderer renderer) { var scene = CaravelApp.Instance.Scene; if (renderer.DebugDrawCameras) { var zoom = ((Cv_CameraComponent)Component).Zoom; var rot = scene.Transform.Rotation; var pos = scene.Transform.Position; var rotMatrixZ = Matrix.CreateRotationZ(rot); Vector2 point1; Vector2 point2; List <Vector2> points = new List <Vector2>(); points.Add(new Vector2(0, 0)); points.Add(new Vector2((renderer.VirtualWidth / zoom), 0)); points.Add(new Vector2((renderer.VirtualWidth / zoom), (renderer.VirtualHeight / zoom))); points.Add(new Vector2(0, (renderer.VirtualHeight / zoom))); for (int i = 0, j = 1; i < points.Count; i++, j++) { if (j >= points.Count) { j = 0; } point1 = new Vector2(points[i].X, points[i].Y); point2 = new Vector2(points[j].X, points[j].Y); point1 -= new Vector2((renderer.VirtualWidth / zoom) * 0.5f, (renderer.VirtualHeight / zoom) * 0.5f); point2 -= new Vector2((renderer.VirtualWidth / zoom) * 0.5f, (renderer.VirtualHeight / zoom) * 0.5f); point1 = Vector2.Transform(point1, rotMatrixZ); point2 = Vector2.Transform(point2, rotMatrixZ); point1 += new Vector2(pos.X, pos.Y); point2 += new Vector2(pos.X, pos.Y); var thickness = (int)Math.Ceiling(3 / scene.Camera.Zoom); if (thickness <= 0) { thickness = 1; } Cv_DrawUtils.DrawLine(renderer, point1, point2, thickness, Cv_Renderer.MaxLayers - 2, Color.Purple); } if (scene.EditorSelectedEntity == Properties.EntityID) { for (int i = 0, j = 1; i < points.Count; i++, j++) { if (j >= points.Count) { j = 0; } point1 = new Vector2(points[i].X, points[i].Y); point2 = new Vector2(points[j].X, points[j].Y); point1 = Vector2.Transform(point1, rotMatrixZ); point2 = Vector2.Transform(point2, rotMatrixZ); point1 += new Vector2(pos.X, pos.Y); point2 += new Vector2(pos.X, pos.Y); point1 -= new Vector2((renderer.VirtualWidth / zoom) * 0.5f, (renderer.VirtualHeight / zoom) * 0.5f); point2 -= new Vector2((renderer.VirtualWidth / zoom) * 0.5f, (renderer.VirtualHeight / zoom) * 0.5f); var thickness = (int)Math.Ceiling(3 / scene.Camera.Zoom); if (thickness <= 0) { thickness = 1; } Cv_DrawUtils.DrawLine(renderer, point1, point2, thickness, Cv_Renderer.MaxLayers - 1, Color.Yellow); } } } }
internal override void VFinishedRender(Cv_Renderer renderer) { base.VFinishedRender(renderer); ((Cv_CameraComponent)Component).ZoomChanged = false; }