From 3f7f06f98fad392d757e73b36d9924512af4f1ea Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 10 Aug 2019 13:42:18 +0200 Subject: [PATCH] added draw alpha to all components --- MLEM.Ui/Elements/AutoScaledText.cs | 6 +++--- MLEM.Ui/Elements/Button.cs | 7 ++++--- MLEM.Ui/Elements/Element.cs | 9 +++++---- MLEM.Ui/Elements/Image.cs | 6 +++--- MLEM.Ui/Elements/Panel.cs | 8 ++++---- MLEM.Ui/Elements/Paragraph.cs | 10 +++++----- MLEM.Ui/UiSystem.cs | 6 +++--- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/MLEM.Ui/Elements/AutoScaledText.cs b/MLEM.Ui/Elements/AutoScaledText.cs index 4da441f..500521e 100644 --- a/MLEM.Ui/Elements/AutoScaledText.cs +++ b/MLEM.Ui/Elements/AutoScaledText.cs @@ -37,10 +37,10 @@ namespace MLEM.Ui.Elements { } while (measure.X <= this.DisplayArea.Size.X && measure.Y <= this.DisplayArea.Size.Y); } - public override void Draw(GameTime time, SpriteBatch batch, Color color) { + public override void Draw(GameTime time, SpriteBatch batch, float alpha) { var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2; - this.font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color.CopyAlpha(color), true, true); - base.Draw(time, batch, color); + this.font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color * alpha, true, true); + base.Draw(time, batch, alpha); } } diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index 369b6c4..b717339 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -28,15 +28,16 @@ namespace MLEM.Ui.Elements { } } - public override void Draw(GameTime time, SpriteBatch batch, Color color) { + public override void Draw(GameTime time, SpriteBatch batch, float alpha) { var tex = this.Texture; + var color = Color.White * alpha; if (this.IsMouseOver) { if (this.HoveredTexture != null) tex = this.HoveredTexture; - color = this.HoveredColor.CopyAlpha(color); + color = this.HoveredColor * alpha; } batch.Draw(tex, this.DisplayArea, color); - base.Draw(time, batch, color); + base.Draw(time, batch, alpha); } } diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index ee67848..77d9c19 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -67,6 +67,7 @@ namespace MLEM.Ui.Elements { } } public bool IgnoresMouse; + public float DrawAlpha = 1; private Rectangle area; public Rectangle Area { @@ -260,17 +261,17 @@ namespace MLEM.Ui.Elements { child.Update(time); } - public virtual void Draw(GameTime time, SpriteBatch batch, Color color) { + public virtual void Draw(GameTime time, SpriteBatch batch, float alpha) { foreach (var child in this.children) { if (!child.IsHidden) - child.Draw(time, batch, color); + child.Draw(time, batch, alpha * child.DrawAlpha); } } - public virtual void DrawUnbound(GameTime time, SpriteBatch batch, Color color, BlendState blendState = null, SamplerState samplerState = null) { + public virtual void DrawUnbound(GameTime time, SpriteBatch batch, float alpha, BlendState blendState = null, SamplerState samplerState = null) { foreach (var child in this.children) { if (!child.IsHidden) - child.DrawUnbound(time, batch, color, blendState, samplerState); + child.DrawUnbound(time, batch, alpha * child.DrawAlpha, blendState, samplerState); } } diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index f0a7c23..72a3506 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -20,9 +20,9 @@ namespace MLEM.Ui.Elements { return this.scaleToImage ? this.texture.Size : base.CalcActualSize(parentArea); } - public override void Draw(GameTime time, SpriteBatch batch, Color color) { - batch.Draw(this.texture, this.DisplayArea, this.Color.CopyAlpha(color)); - base.Draw(time, batch, color); + public override void Draw(GameTime time, SpriteBatch batch, float alpha) { + batch.Draw(this.texture, this.DisplayArea, this.Color * alpha); + base.Draw(time, batch, alpha); } } diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 503e0f9..6e2ddfe 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -6,7 +6,7 @@ namespace MLEM.Ui.Elements { public class Panel : Element { public static NinePatch DefaultTexture; - + private readonly NinePatch texture; public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture = null) : base(anchor, size) { @@ -15,9 +15,9 @@ namespace MLEM.Ui.Elements { this.ChildPadding = new Point(5); } - public override void Draw(GameTime time, SpriteBatch batch, Color color) { - batch.Draw(this.texture, this.DisplayArea, color); - base.Draw(time, batch, color); + public override void Draw(GameTime time, SpriteBatch batch, float alpha) { + batch.Draw(this.texture, this.DisplayArea, Color.White * alpha); + base.Draw(time, batch, alpha); } } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 2f7899c..4b14086 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -10,7 +10,7 @@ namespace MLEM.Ui.Elements { public class Paragraph : Element { public static IGenericFont DefaultFont; - + private string text; private float lineHeight; private string[] splitText; @@ -49,16 +49,16 @@ namespace MLEM.Ui.Elements { return new Point(size.X, height.Ceil()); } - public override void Draw(GameTime time, SpriteBatch batch, Color color) { - base.Draw(time, batch, color); + public override void Draw(GameTime time, SpriteBatch batch, float alpha) { + base.Draw(time, batch, alpha); var pos = this.DisplayArea.Location.ToVector2(); var offset = new Vector2(); foreach (var line in this.splitText) { if (this.centerText) { - this.font.DrawCenteredString(batch, line, pos + offset + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale, color); + this.font.DrawCenteredString(batch, line, pos + offset + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale, Color.White * alpha); } else { - this.font.DrawString(batch, line, pos + offset, color, 0, Vector2.Zero, this.TextScale, SpriteEffects.None, 0); + this.font.DrawString(batch, line, pos + offset, Color.White * alpha, 0, Vector2.Zero, this.TextScale, SpriteEffects.None, 0); } offset.Y += this.lineHeight + 1; } diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 338178b..b3fd108 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -32,7 +32,7 @@ namespace MLEM.Ui { } public Vector2 MousePos => this.inputHandler.MousePosition.ToVector2() / this.globalScale; public Element MousedElement { get; private set; } - public Color DrawColor = Color.White; + public float DrawAlpha = 1; public BlendState BlendState; public SamplerState SamplerState = SamplerState.PointClamp; @@ -77,13 +77,13 @@ namespace MLEM.Ui { batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState, transformMatrix: Matrix.CreateScale(this.globalScale)); foreach (var root in this.rootElements) { if (!root.Element.IsHidden) - root.Element.Draw(time, batch, this.DrawColor); + root.Element.Draw(time, batch, this.DrawAlpha * root.Element.DrawAlpha); } batch.End(); foreach (var root in this.rootElements) { if (!root.Element.IsHidden) - root.Element.DrawUnbound(time, batch, this.DrawColor, this.BlendState, this.SamplerState); + root.Element.DrawUnbound(time, batch, this.DrawAlpha * root.Element.DrawAlpha, this.BlendState, this.SamplerState); } }