1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

remove offset from draw method and use a matrix instead

This commit is contained in:
Ellpeck 2019-09-04 17:19:31 +02:00
parent 9deb6bbce3
commit 619db6bb94
9 changed files with 37 additions and 39 deletions

View file

@ -22,7 +22,7 @@ namespace MLEM.Ui.Elements {
this.Tooltip = new Tooltip(tooltipWidth, tooltipText, this); this.Tooltip = new Tooltip(tooltipWidth, tooltipText, this);
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
var tex = this.Texture; var tex = this.Texture;
var color = Color.White * alpha; var color = Color.White * alpha;
if (this.IsMouseOver) { if (this.IsMouseOver) {
@ -30,8 +30,8 @@ namespace MLEM.Ui.Elements {
tex = this.HoveredTexture; tex = this.HoveredTexture;
color = this.HoveredColor * alpha; color = this.HoveredColor * alpha;
} }
batch.Draw(tex, this.DisplayArea.OffsetCopy(offset), color, this.Scale); batch.Draw(tex, this.DisplayArea, color, this.Scale);
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -46,7 +46,7 @@ namespace MLEM.Ui.Elements {
return size; return size;
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
var tex = this.Texture; var tex = this.Texture;
var color = Color.White * alpha; var color = Color.White * alpha;
if (this.IsMouseOver) { if (this.IsMouseOver) {
@ -55,11 +55,11 @@ namespace MLEM.Ui.Elements {
color = this.HoveredColor * alpha; color = this.HoveredColor * alpha;
} }
var boxDisplayArea = new Rectangle(this.DisplayArea.Location, new Point(this.DisplayArea.Height)).OffsetCopy(offset); var boxDisplayArea = new Rectangle(this.DisplayArea.Location, new Point(this.DisplayArea.Height));
batch.Draw(tex, boxDisplayArea, color, this.Scale); batch.Draw(tex, boxDisplayArea, color, this.Scale);
if (this.Checked) if (this.Checked)
batch.Draw(this.Checkmark, boxDisplayArea, Color.White * alpha); batch.Draw(this.Checkmark, boxDisplayArea, Color.White * alpha);
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -428,16 +428,16 @@ namespace MLEM.Ui.Elements {
child.Update(time); child.Update(time);
} }
public virtual void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public virtual void Draw(GameTime time, SpriteBatch batch, float alpha) {
this.System.OnElementDrawn?.Invoke(this, time, batch, alpha, offset); this.System.OnElementDrawn?.Invoke(this, time, batch, alpha);
foreach (var child in this.SortedChildren) { foreach (var child in this.SortedChildren) {
if (!child.IsHidden) if (!child.IsHidden)
child.Draw(time, batch, alpha * child.DrawAlpha, offset); child.Draw(time, batch, alpha * child.DrawAlpha);
} }
if (this.IsSelected) if (this.IsSelected)
this.System.OnSelectedElementDrawn?.Invoke(this, time, batch, alpha, offset); this.System.OnSelectedElementDrawn?.Invoke(this, time, batch, alpha);
} }
public virtual void DrawEarly(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { public virtual void DrawEarly(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
@ -474,7 +474,7 @@ namespace MLEM.Ui.Elements {
public delegate void OtherElementCallback(Element thisElement, Element otherElement); public delegate void OtherElementCallback(Element thisElement, Element otherElement);
public delegate void DrawCallback(Element element, GameTime time, SpriteBatch batch, float alpha, Point offset); public delegate void DrawCallback(Element element, GameTime time, SpriteBatch batch, float alpha);
} }
} }

View file

@ -22,15 +22,15 @@ namespace MLEM.Ui.Elements {
return this.ScaleToImage ? this.Texture.Size : base.CalcActualSize(parentArea); return this.ScaleToImage ? this.Texture.Size : base.CalcActualSize(parentArea);
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
if (this.MaintainImageAspect) { if (this.MaintainImageAspect) {
var scale = Math.Min(this.DisplayArea.Width / (float) this.Texture.Width, this.DisplayArea.Height / (float) this.Texture.Height); var scale = Math.Min(this.DisplayArea.Width / (float) this.Texture.Width, this.DisplayArea.Height / (float) this.Texture.Height);
var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.Texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.Texture.Height * scale / 2); var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.Texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.Texture.Height * scale / 2);
batch.Draw(this.Texture, (this.DisplayArea.Location + offset).ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0); batch.Draw(this.Texture, this.DisplayArea.Location.ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
} else { } else {
batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), this.Color * alpha); batch.Draw(this.Texture, this.DisplayArea, this.Color * alpha);
} }
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} }
} }

View file

@ -92,14 +92,14 @@ namespace MLEM.Ui.Elements {
child.ScrollOffset = new Point(0, offset); child.ScrollOffset = new Point(0, offset);
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), Color.White * alpha, this.Scale); batch.Draw(this.Texture, this.DisplayArea, Color.White * alpha, this.Scale);
// if we handle overflow, draw using the render target in DrawUnbound // if we handle overflow, draw using the render target in DrawUnbound
if (!this.scrollOverflow) { if (!this.scrollOverflow) {
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} else if (this.renderTarget != null) { } else if (this.renderTarget != null) {
// draw the actual render target (don't apply the alpha here because it's already drawn onto with alpha) // draw the actual render target (don't apply the alpha here because it's already drawn onto with alpha)
batch.Draw(this.renderTarget, this.GetRenderTargetArea().OffsetCopy(offset), Color.White); batch.Draw(this.renderTarget, this.GetRenderTargetArea(), Color.White);
} }
} }
@ -108,11 +108,10 @@ namespace MLEM.Ui.Elements {
// draw children onto the render target // draw children onto the render target
batch.GraphicsDevice.SetRenderTarget(this.renderTarget); batch.GraphicsDevice.SetRenderTarget(this.renderTarget);
batch.GraphicsDevice.Clear(Color.Transparent); batch.GraphicsDevice.Clear(Color.Transparent);
// we don't apply the matrix here because it's already applied when drawing the render target
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState);
// offset children by the render target's location // offset children by the render target's location
var area = this.GetRenderTargetArea(); var area = this.GetRenderTargetArea();
base.Draw(time, batch, alpha, new Point(-area.X, -area.Y)); batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, Matrix.CreateTranslation(-area.X, -area.Y, 0));
base.Draw(time, batch, alpha);
batch.End(); batch.End();
batch.GraphicsDevice.SetRenderTarget(null); batch.GraphicsDevice.SetRenderTarget(null);
} }

View file

@ -66,17 +66,16 @@ namespace MLEM.Ui.Elements {
this.Text = this.GetTextCallback(this); this.Text = this.GetTextCallback(this);
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
if (this.Background != null) if (this.Background != null)
batch.Draw(this.Background, this.Area.OffsetCopy(offset), this.BackgroundColor * alpha); batch.Draw(this.Background, this.Area, this.BackgroundColor * alpha);
var pos = this.DisplayArea.Location.ToVector2(); var pos = this.DisplayArea.Location.ToVector2();
var off = offset.ToVector2();
var sc = this.TextScale * this.Scale; var sc = this.TextScale * this.Scale;
// if we don't have any formatting codes, then we don't need to do complex drawing // if we don't have any formatting codes, then we don't need to do complex drawing
if (this.codeLocations.Count <= 0) { if (this.codeLocations.Count <= 0) {
this.regularFont.DrawString(batch, this.splitText, pos + off, this.TextColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0); this.regularFont.DrawString(batch, this.splitText, pos, this.TextColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0);
} else { } else {
// if we have formatting codes, we need to go through each index and see how it should be drawn // if we have formatting codes, we need to go through each index and see how it should be drawn
var characterCounter = 0; var characterCounter = 0;
@ -112,12 +111,12 @@ namespace MLEM.Ui.Elements {
innerOffset.X = 0; innerOffset.X = 0;
innerOffset.Y += this.regularFont.LineHeight * sc; innerOffset.Y += this.regularFont.LineHeight * sc;
} else { } else {
currFont.DrawString(batch, cSt, pos + off + innerOffset, currColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0); currFont.DrawString(batch, cSt, pos + innerOffset, currColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0);
innerOffset.X += this.regularFont.MeasureString(cSt).X * sc; innerOffset.X += this.regularFont.MeasureString(cSt).X * sc;
} }
} }
} }
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -118,16 +118,16 @@ namespace MLEM.Ui.Elements {
} }
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
batch.Draw(this.Background, this.DisplayArea.OffsetCopy(offset), Color.White * alpha, this.Scale); batch.Draw(this.Background, this.DisplayArea, Color.White * alpha, this.Scale);
var scrollerPos = new Point(this.DisplayArea.X + offset.X + this.ScrollerOffset.X, this.DisplayArea.Y + offset.Y + this.ScrollerOffset.Y); var scrollerPos = new Point(this.DisplayArea.X + this.ScrollerOffset.X, this.DisplayArea.Y + this.ScrollerOffset.Y);
var scrollerOffset = new Point( var scrollerOffset = new Point(
!this.Horizontal ? 0 : (this.currValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale)).Floor(), !this.Horizontal ? 0 : (this.currValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale)).Floor(),
this.Horizontal ? 0 : (this.currValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale)).Floor()); this.Horizontal ? 0 : (this.currValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale)).Floor());
var scrollerRect = new Rectangle(scrollerPos + scrollerOffset, this.ScrollerSize.Multiply(this.Scale)); var scrollerRect = new Rectangle(scrollerPos + scrollerOffset, this.ScrollerSize.Multiply(this.Scale));
batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale); batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale);
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -91,7 +91,7 @@ namespace MLEM.Ui.Elements {
this.caretBlinkTimer = 0; this.caretBlinkTimer = 0;
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
var tex = this.Texture; var tex = this.Texture;
var color = Color.White * alpha; var color = Color.White * alpha;
if (this.IsMouseOver) { if (this.IsMouseOver) {
@ -99,9 +99,9 @@ namespace MLEM.Ui.Elements {
tex = this.HoveredTexture; tex = this.HoveredTexture;
color = this.HoveredColor * alpha; color = this.HoveredColor * alpha;
} }
batch.Draw(tex, this.DisplayArea.OffsetCopy(offset), color, this.Scale); batch.Draw(tex, this.DisplayArea, color, this.Scale);
var textPos = this.DisplayArea.Location.ToVector2() + new Vector2(offset.X + this.TextOffsetX * this.Scale, offset.Y + this.DisplayArea.Height / 2); var textPos = this.DisplayArea.Location.ToVector2() + new Vector2(this.TextOffsetX * this.Scale, this.DisplayArea.Height / 2);
if (this.text.Length > 0 || this.IsSelected) { if (this.text.Length > 0 || this.IsSelected) {
var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : ""; var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : "";
var display = this.text.ToString(this.textStartIndex, this.text.Length - this.textStartIndex) + caret; var display = this.text.ToString(this.textStartIndex, this.text.Length - this.textStartIndex) + caret;
@ -109,7 +109,7 @@ namespace MLEM.Ui.Elements {
} else if (this.PlaceholderText != null) { } else if (this.PlaceholderText != null) {
this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true); this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true);
} }
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha);
} }
public void SetText(object text, bool removeMismatching = false) { public void SetText(object text, bool removeMismatching = false) {

View file

@ -74,9 +74,9 @@ namespace MLEM.Ui {
root.Element.AndChildren(e => e.OnTextInput?.Invoke(e, key, character)); root.Element.AndChildren(e => e.OnTextInput?.Invoke(e, key, character));
}); });
this.OnSelectedElementDrawn = (element, time, batch, alpha, offset) => { this.OnSelectedElementDrawn = (element, time, batch, alpha) => {
if (!this.Controls.SelectedLastElementWithMouse && element.SelectionIndicator != null) { if (!this.Controls.SelectedLastElementWithMouse && element.SelectionIndicator != null) {
batch.Draw(element.SelectionIndicator, element.DisplayArea.OffsetCopy(offset), Color.White * alpha); batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha);
} }
}; };
} }
@ -100,7 +100,7 @@ namespace MLEM.Ui {
if (root.Element.IsHidden) if (root.Element.IsHidden)
continue; continue;
batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState, null, null, null, root.Transform); batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState, null, null, null, root.Transform);
root.Element.Draw(time, batch, this.DrawAlpha * root.Element.DrawAlpha, Point.Zero); root.Element.Draw(time, batch, this.DrawAlpha * root.Element.DrawAlpha);
batch.End(); batch.End();
} }
} }