public void Draw(IDrawDevice device) { if (_buffer == null) { _buffer = new CanvasBuffer(); } Canvas canvas = new Canvas(device, _buffer); canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White)); canvas.State.ColorTint = ColorRgba.Green;//.WithAlpha(0.5f); //if (Font != null) // canvas.State.TextFont = Font; const float lineSpacing = 15; float y = 0; foreach (string text in _texts) { canvas.DrawText(text, 0, y); y += lineSpacing; } _texts.Clear(); }
public Hud() { canvasBuffer = new CanvasBuffer(); fontSmall = new BitmapFont("ui/font_small", 17, 18, 15, 32, 256, -2, canvasBuffer); Metadata m = ContentManager.Current.RequestMetadata("UI/HUD"); graphics = m.Graphics; #if DEBUG debugString = new StringBuilder(); #endif }
public void initialize(CanvasConfig config, CanvasCameraConfig canvasCameraConfig, Texture2D texture = null) { canvasBuffer = new CanvasBuffer(config.canvasSize, config.bufferSize); this.config = config; canvasCamera = new CanvasCamera(canvasCameraConfig, config.canvasSize, gameObject); backLayer = new CanvasLayer(config.canvasSize, 100, config.layersShader, canvasCamera); frontLayer = new CanvasLayer(config.canvasSize, 20, config.layersShader, canvasCamera); radialLayer = new CanvasRadialLayer(config.canvasSize, 25, config.radialFillShader, config.radialFillTexture, canvasCamera); canvasCollider = CanvasCollider.createCanvasCollider(config.canvasSize, 10, gameObject, canvasCamera); setNewPicture(texture); initialized = true; handleInnerEvents(); }
public void Draw(IDrawDevice device) { // Create a buffer to cache and re-use vertices. Not required, but will boost performance. if (this.buffer == null) { this.buffer = new CanvasBuffer(); } // Create a Canvas to auto-generate vertices from high-level drawing commands. Canvas canvas = new Canvas(device, this.buffer); canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White)); // Draw a circle at the mouse position canvas.FillCircle(DualityApp.Mouse.X, DualityApp.Mouse.Y, 10f); }
void ICmpRenderer.Draw(IDrawDevice device) { // Create a buffer to cache and re-use vertices. Not required, but will boost performance. if (this.buffer == null) { this.buffer = new CanvasBuffer(); } // Create a Canvas to auto-generate vertices from high-level drawing commands. Canvas canvas = new Canvas(device, this.buffer); canvas.State.TextFont = this.font; // If the game is over or won, display "game over" screen if (this.gameOver || this.gameWin) { // Various animation timing variables. float animOffset = this.gameWin ? 0.0f : 2500.0f; float animTime = this.gameWin ? 10000.0f : 4500.0f; float blendDurationRatio = this.gameWin ? 0.6f : 0.5f; float textOffsetRatio = this.gameWin ? 0.2f : 0.0f; float timeSinceGameOver = (float)Time.MainTimer.TotalMilliseconds - this.lastTimeAnyAlive; float gameOverAnimProgress = MathF.Clamp((timeSinceGameOver - animOffset) / animTime, 0.0f, 1.0f); float controlInfoAnimProgress = MathF.Clamp(((timeSinceGameOver - animOffset) - animTime - 2000.0f) / 2000.0f, 0.0f, 1.0f); float blendAnimProgress = MathF.Clamp(gameOverAnimProgress / blendDurationRatio, 0.0f, 1.0f); float textAnimProgress = MathF.Clamp((gameOverAnimProgress - blendDurationRatio - textOffsetRatio) / (1.0f - blendDurationRatio - textOffsetRatio), 0.0f, 1.0f); if (this.blendMaterial != null && blendAnimProgress > 0.0f) { canvas.PushState(); if (this.gameOver) { // Set up our special blending Material and specify the threshold to blend to this.blendMaterial.SetUniform("threshold", 1.0f - blendAnimProgress); canvas.State.SetMaterial(this.blendMaterial); canvas.State.ColorTint = ColorRgba.Black; // Specify a texture coordinate rect so it spans the entire screen repeating itself, instead of being stretched if (this.blendMaterial.MainTexture != null) { Random rnd = new Random((int)this.lastTimeAnyAlive); Vector2 randomTranslate = rnd.NextVector2(0.0f, 0.0f, canvas.State.TextureBaseSize.X, canvas.State.TextureBaseSize.Y); canvas.State.TextureCoordinateRect = new Rect( randomTranslate.X, randomTranslate.Y, device.TargetSize.X / canvas.State.TextureBaseSize.X, device.TargetSize.Y / canvas.State.TextureBaseSize.Y); } } else { // If we won, simply fade to white canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Add, ColorRgba.White)); canvas.State.ColorTint = ColorRgba.White.WithAlpha(blendAnimProgress); } // Fill the screen with a rect of our Material canvas.FillRect(0, 0, device.TargetSize.X, device.TargetSize.Y); canvas.PopState(); } if (this.font != null && textAnimProgress > 0.0f) { canvas.PushState(); // Determine which text to draw to screen and where to draw it string gameOverText = this.gameWin ? "is it over..?" : "darkness..."; Vector2 fullTextSize = canvas.MeasureText(gameOverText); Vector2 textPos = device.TargetSize * 0.5f - fullTextSize * 0.5f; gameOverText = gameOverText.Substring(0, MathF.RoundToInt(gameOverText.Length * textAnimProgress)); // Make sure not to draw inbetween pixels, so the text is perfectly sharp. textPos.X = MathF.Round(textPos.X); textPos.Y = MathF.Round(textPos.Y); // Draw the text to screen canvas.State.ColorTint = this.gameWin ? ColorRgba.Black : ColorRgba.White; canvas.DrawText(gameOverText, textPos.X, textPos.Y); canvas.PopState(); } if (controlInfoAnimProgress > 0.0f) { Vector2 infoBasePos = device.TargetSize * 0.5f + new Vector2(0.0f, device.TargetSize.Y * 0.25f); if (this.controlInfoMouseKeyboard != null) { canvas.PushState(); Vector2 texSize = this.controlInfoMouseKeyboard.Res.MainTexture.Res.Size * 0.5f; canvas.State.SetMaterial(this.controlInfoMouseKeyboard); canvas.State.ColorTint = ColorRgba.White.WithAlpha(controlInfoAnimProgress); canvas.FillRect( infoBasePos.X - texSize.X * 0.5f, infoBasePos.Y - texSize.Y - 10, texSize.X, texSize.Y); canvas.PopState(); } if (this.controlInfoGamepad != null) { canvas.PushState(); Vector2 texSize = this.controlInfoGamepad.Res.MainTexture.Res.Size * 0.5f; canvas.State.SetMaterial(this.controlInfoGamepad); canvas.State.ColorTint = ColorRgba.White.WithAlpha(controlInfoAnimProgress); canvas.FillRect( infoBasePos.X - texSize.X * 0.5f, infoBasePos.Y + 10, texSize.X, texSize.Y); canvas.PopState(); } } } }
void ICmpRenderer.Draw(IDrawDevice device) { // Create a buffer to cache and re-use vertices. Not required, but will boost performance. if (this.buffer == null) this.buffer = new CanvasBuffer(); // Create a Canvas to auto-generate vertices from high-level drawing commands. Canvas canvas = new Canvas(device, this.buffer); canvas.State.TextFont = this.font; // If the game is over or won, display "game over" screen if (this.gameOver || this.gameWin) { // Various animation timing variables. float animOffset = this.gameWin ? 0.0f : 2500.0f; float animTime = this.gameWin ? 10000.0f : 4500.0f; float blendDurationRatio = this.gameWin ? 0.6f : 0.5f; float textOffsetRatio = this.gameWin ? 0.2f : 0.0f; float timeSinceGameOver = (float)Time.MainTimer.TotalMilliseconds - this.lastTimeAnyAlive; float gameOverAnimProgress = MathF.Clamp((timeSinceGameOver - animOffset) / animTime, 0.0f, 1.0f); float controlInfoAnimProgress = MathF.Clamp(((timeSinceGameOver - animOffset) - animTime - 2000.0f) / 2000.0f, 0.0f, 1.0f); float blendAnimProgress = MathF.Clamp(gameOverAnimProgress / blendDurationRatio, 0.0f, 1.0f); float textAnimProgress = MathF.Clamp((gameOverAnimProgress - blendDurationRatio - textOffsetRatio) / (1.0f - blendDurationRatio - textOffsetRatio), 0.0f, 1.0f); if (this.blendMaterial != null && blendAnimProgress > 0.0f) { canvas.PushState(); if (this.gameOver) { // Set up our special blending Material and specify the threshold to blend to this.blendMaterial.SetUniform("threshold", 1.0f - blendAnimProgress); canvas.State.SetMaterial(this.blendMaterial); canvas.State.ColorTint = ColorRgba.Black; // Specify a texture coordinate rect so it spans the entire screen repeating itself, instead of being stretched if (this.blendMaterial.MainTexture != null) { Random rnd = new Random((int)this.lastTimeAnyAlive); Vector2 randomTranslate = rnd.NextVector2(0.0f, 0.0f, canvas.State.TextureBaseSize.X, canvas.State.TextureBaseSize.Y); canvas.State.TextureCoordinateRect = new Rect( randomTranslate.X, randomTranslate.Y, device.TargetSize.X / canvas.State.TextureBaseSize.X, device.TargetSize.Y / canvas.State.TextureBaseSize.Y); } } else { // If we won, simply fade to white canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Add, ColorRgba.White)); canvas.State.ColorTint = ColorRgba.White.WithAlpha(blendAnimProgress); } // Fill the screen with a rect of our Material canvas.FillRect(0, 0, device.TargetSize.X, device.TargetSize.Y); canvas.PopState(); } if (this.font != null && textAnimProgress > 0.0f) { canvas.PushState(); // Determine which text to draw to screen and where to draw it string gameOverText = this.gameWin ? "is it over..?" : "darkness..."; Vector2 fullTextSize = canvas.MeasureText(gameOverText); Vector2 textPos = device.TargetSize * 0.5f - fullTextSize * 0.5f; gameOverText = gameOverText.Substring(0, MathF.RoundToInt(gameOverText.Length * textAnimProgress)); // Make sure not to draw inbetween pixels, so the text is perfectly sharp. textPos.X = MathF.Round(textPos.X); textPos.Y = MathF.Round(textPos.Y); // Draw the text to screen canvas.State.ColorTint = this.gameWin ? ColorRgba.Black : ColorRgba.White; canvas.DrawText(gameOverText, textPos.X, textPos.Y); canvas.PopState(); } if (controlInfoAnimProgress > 0.0f) { Vector2 infoBasePos = device.TargetSize * 0.5f + new Vector2(0.0f, device.TargetSize.Y * 0.25f); if (this.controlInfoMouseKeyboard != null) { canvas.PushState(); Vector2 texSize = this.controlInfoMouseKeyboard.Res.MainTexture.Res.Size * 0.5f; canvas.State.SetMaterial(this.controlInfoMouseKeyboard); canvas.State.ColorTint = ColorRgba.White.WithAlpha(controlInfoAnimProgress); canvas.FillRect( infoBasePos.X - texSize.X * 0.5f, infoBasePos.Y - texSize.Y - 10, texSize.X, texSize.Y); canvas.PopState(); } if (this.controlInfoGamepad != null) { canvas.PushState(); Vector2 texSize = this.controlInfoGamepad.Res.MainTexture.Res.Size * 0.5f; canvas.State.SetMaterial(this.controlInfoGamepad); canvas.State.ColorTint = ColorRgba.White.WithAlpha(controlInfoAnimProgress); canvas.FillRect( infoBasePos.X - texSize.X * 0.5f, infoBasePos.Y + 10, texSize.X, texSize.Y); canvas.PopState(); } } } }
public void initialize(CanvasConfig config, CanvasCameraConfig canvasCameraConfig, Texture2D texture=null) { canvasBuffer = new CanvasBuffer(config.canvasSize, config.bufferSize); this.config = config; canvasCamera = new CanvasCamera(canvasCameraConfig, config.canvasSize, gameObject); backLayer = new CanvasLayer(config.canvasSize, 100, config.layersShader, canvasCamera); frontLayer = new CanvasLayer(config.canvasSize, 20, config.layersShader, canvasCamera); radialLayer = new CanvasRadialLayer(config.canvasSize, 25, config.radialFillShader, config.radialFillTexture, canvasCamera); canvasCollider = CanvasCollider.createCanvasCollider (config.canvasSize, 10, gameObject, canvasCamera); setNewPicture(texture); initialized = true; handleInnerEvents(); }
void ICmpRenderer.Draw(IDrawDevice device) { // Create a buffer to cache and re-use vertices. Not required, but will boost performance. if (this.buffer == null) { this.buffer = new CanvasBuffer(); } // Create a Canvas to auto-generate vertices from high-level drawing commands. Canvas canvas = new Canvas(device, this.buffer); canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White)); canvas.State.TextFont = this.font; // Retrieve players if (this.playerOne == null) { this.playerOne = Scene.Current.FindComponents <Player>().Where(p => p.Id == PlayerId.PlayerOne).FirstOrDefault(); } if (this.playerTwo == null) { this.playerTwo = Scene.Current.FindComponents <Player>().Where(p => p.Id == PlayerId.PlayerTwo).FirstOrDefault(); } // Is someone playing using mouse / keyboard? Display a mouse cursor then if (Player.AlivePlayers.Any(p => p.InputMethod == InputMethod.MouseAndKeyboard)) { canvas.FillCircle(DualityApp.Mouse.X, DualityApp.Mouse.Y, 2.0f); } // Is any player alive? Keep that value in mind, won't change here anyway. bool isAnyPlayerAlive = Player.IsAnyPlayerAlive; // Draw health and info of player one if (this.IsPlayerActive(this.playerOne)) { Ship playerShip = this.playerOne.ControlObject; if (playerShip.Active) { // Draw a health bar when alive float health = playerShip.Hitpoints; canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(12 - 1, device.TargetSize.Y - 10 - 198 - 1, 16 + 2, 196 + 2); canvas.State.ColorTint = this.playerOne.Color; canvas.DrawRect(10, device.TargetSize.Y - 10 - 200, 20, 200); canvas.FillRect(12, device.TargetSize.Y - 10 - health * 198.0f, 16, health * 196.0f); } else if (isAnyPlayerAlive && !this.playerOne.HasReachedGoal) { // Draw a respawn timer when dead float respawnPercentage = this.playerOne.RespawnTime / Player.RespawnDelay; string respawnText = string.Format("Respawn in {0:F1}", (Player.RespawnDelay - this.playerOne.RespawnTime) / 1000.0f); Vector2 textSize = canvas.MeasureText(string.Format("Respawn in {0:F1}", 0.0f)); canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(10 - 1, device.TargetSize.Y - 10 - textSize.Y - 2, textSize.X + 5, textSize.Y + 8); canvas.State.ColorTint = this.playerOne.Color; canvas.DrawText(respawnText, 10, device.TargetSize.Y - 10, 0.0f, Alignment.BottomLeft); canvas.FillRect(10, device.TargetSize.Y - 10 - textSize.Y, textSize.X * respawnPercentage, 3); canvas.FillRect(10, device.TargetSize.Y - 10, textSize.X * respawnPercentage, 3); } } // Draw health and info of player two if (this.IsPlayerActive(this.playerTwo)) { Ship playerShip = this.playerTwo.ControlObject; if (playerShip.Active) { // Draw a health bar when alive float health = playerShip.Hitpoints; canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(device.TargetSize.X - 12 - 16 - 1, device.TargetSize.Y - 10 - 198 - 1, 16 + 2, 196 + 2); canvas.State.ColorTint = this.playerTwo.Color; canvas.DrawRect(device.TargetSize.X - 10 - 20, device.TargetSize.Y - 10 - 200, 20, 200); canvas.FillRect(device.TargetSize.X - 12 - 16, device.TargetSize.Y - 10 - health * 198.0f, 16, health * 196.0f); } else if (isAnyPlayerAlive && !this.playerTwo.HasReachedGoal) { // Draw a respawn timer when dead float respawnPercentage = this.playerTwo.RespawnTime / Player.RespawnDelay; string respawnText = string.Format("{0:F1} to Respawn", (Player.RespawnDelay - this.playerTwo.RespawnTime) / 1000.0f); Vector2 textSize = canvas.MeasureText(string.Format("{0:F1} to Respawn", 0.0f)); canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(device.TargetSize.X - 10 - textSize.X - 3, device.TargetSize.Y - 10 - textSize.Y - 2, textSize.X + 2, textSize.Y + 10); canvas.State.ColorTint = this.playerTwo.Color; canvas.DrawText(respawnText, device.TargetSize.X - 10, device.TargetSize.Y - 10, 0.0f, Alignment.BottomRight); canvas.FillRect(device.TargetSize.X - 10 - textSize.X * respawnPercentage, device.TargetSize.Y - 10 - textSize.Y, textSize.X * respawnPercentage, 3); canvas.FillRect(device.TargetSize.X - 10 - textSize.X * respawnPercentage, device.TargetSize.Y - 10, textSize.X * respawnPercentage, 3); } } }
void ICmpRenderer.Draw(IDrawDevice device) { // Create a buffer to cache and re-use vertices. Not required, but will boost performance. if (this.buffer == null) this.buffer = new CanvasBuffer(); // Create a Canvas to auto-generate vertices from high-level drawing commands. Canvas canvas = new Canvas(device, this.buffer); canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White)); canvas.State.TextFont = this.font; // Retrieve players if (this.playerOne == null) this.playerOne = Scene.Current.FindComponents<Player>().Where(p => p.Id == PlayerId.PlayerOne).FirstOrDefault(); if (this.playerTwo == null) this.playerTwo = Scene.Current.FindComponents<Player>().Where(p => p.Id == PlayerId.PlayerTwo).FirstOrDefault(); // Is someone playing using mouse / keyboard? Display a mouse cursor then if (Player.AlivePlayers.Any(p => p.InputMethod == InputMethod.MouseAndKeyboard)) { canvas.FillCircle(DualityApp.Mouse.X, DualityApp.Mouse.Y, 2.0f); } // Is any player alive? Keep that value in mind, won't change here anyway. bool isAnyPlayerAlive = Player.IsAnyPlayerAlive; // Draw health and info of player one if (this.IsPlayerActive(this.playerOne)) { Ship playerShip = this.playerOne.ControlObject; if (playerShip.Active) { // Draw a health bar when alive float health = playerShip.Hitpoints; canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(12 - 1, device.TargetSize.Y - 10 - 198 - 1, 16 + 2, 196 + 2); canvas.State.ColorTint = this.playerOne.Color; canvas.DrawRect(10, device.TargetSize.Y - 10 - 200, 20, 200); canvas.FillRect(12, device.TargetSize.Y - 10 - health * 198.0f, 16, health * 196.0f); } else if (isAnyPlayerAlive && !this.playerOne.HasReachedGoal) { // Draw a respawn timer when dead float respawnPercentage = this.playerOne.RespawnTime / Player.RespawnDelay; string respawnText = string.Format("Respawn in {0:F1}", (Player.RespawnDelay - this.playerOne.RespawnTime) / 1000.0f); Vector2 textSize = canvas.MeasureText(string.Format("Respawn in {0:F1}", 0.0f)); canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(10 - 1, device.TargetSize.Y - 10 - textSize.Y - 2, textSize.X + 5, textSize.Y + 8); canvas.State.ColorTint = this.playerOne.Color; canvas.DrawText(respawnText, 10, device.TargetSize.Y - 10, 0.0f, Alignment.BottomLeft); canvas.FillRect(10, device.TargetSize.Y - 10 - textSize.Y, textSize.X * respawnPercentage, 3); canvas.FillRect(10, device.TargetSize.Y - 10, textSize.X * respawnPercentage, 3); } } // Draw health and info of player two if (this.IsPlayerActive(this.playerTwo)) { Ship playerShip = this.playerTwo.ControlObject; if (playerShip.Active) { // Draw a health bar when alive float health = playerShip.Hitpoints; canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(device.TargetSize.X - 12 - 16 - 1, device.TargetSize.Y - 10 - 198 - 1, 16 + 2, 196 + 2); canvas.State.ColorTint = this.playerTwo.Color; canvas.DrawRect(device.TargetSize.X - 10 - 20, device.TargetSize.Y - 10 - 200, 20, 200); canvas.FillRect(device.TargetSize.X - 12 - 16, device.TargetSize.Y - 10 - health * 198.0f, 16, health * 196.0f); } else if (isAnyPlayerAlive && !this.playerTwo.HasReachedGoal) { // Draw a respawn timer when dead float respawnPercentage = this.playerTwo.RespawnTime / Player.RespawnDelay; string respawnText = string.Format("{0:F1} to Respawn", (Player.RespawnDelay - this.playerTwo.RespawnTime) / 1000.0f); Vector2 textSize = canvas.MeasureText(string.Format("{0:F1} to Respawn", 0.0f)); canvas.State.ColorTint = ColorRgba.Black.WithAlpha(0.5f); canvas.FillRect(device.TargetSize.X - 10 - textSize.X - 3, device.TargetSize.Y - 10 - textSize.Y - 2, textSize.X + 2, textSize.Y + 10); canvas.State.ColorTint = this.playerTwo.Color; canvas.DrawText(respawnText, device.TargetSize.X - 10, device.TargetSize.Y - 10, 0.0f, Alignment.BottomRight); canvas.FillRect(device.TargetSize.X - 10 - textSize.X * respawnPercentage, device.TargetSize.Y - 10 - textSize.Y, textSize.X * respawnPercentage, 3); canvas.FillRect(device.TargetSize.X - 10 - textSize.X * respawnPercentage, device.TargetSize.Y - 10, textSize.X * respawnPercentage, 3); } } }