1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

fixed custom draw groups not working outside of a render target

This commit is contained in:
Ellpeck 2019-09-20 13:22:05 +02:00
parent ae849c63e7
commit c5170917fb
15 changed files with 130 additions and 30 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) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture; var tex = this.Texture;
var color = Color.White * alpha; var color = Color.White * alpha;
if (this.IsMouseOver) { if (this.IsMouseOver) {
@ -31,7 +31,7 @@ namespace MLEM.Ui.Elements {
color = this.HoveredColor * alpha; color = this.HoveredColor * alpha;
} }
batch.Draw(tex, this.DisplayArea, color, this.Scale); batch.Draw(tex, this.DisplayArea, color, this.Scale);
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
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) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture; var tex = this.Texture;
var color = Color.White * alpha; var color = Color.White * alpha;
if (this.IsMouseOver) { if (this.IsMouseOver) {
@ -59,7 +59,7 @@ namespace MLEM.Ui.Elements {
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); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -15,17 +15,17 @@ namespace MLEM.Ui.Elements {
public delegate void BeginDelegate(CustomDrawGroup element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix); public delegate void BeginDelegate(CustomDrawGroup element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix);
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
// this is left empty because child components are drawn in DrawEarly // end the usual draw so that we can begin our own
} batch.End();
public override void DrawEarly(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var mat = matrix * this.Transform; var mat = matrix * this.Transform;
this.BeginImpl(this, time, batch, alpha, blendState, samplerState, mat); this.BeginImpl(this, time, batch, alpha, blendState, samplerState, mat);
// draw child components in custom begin call // draw child components in custom begin call
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha, blendState, samplerState, mat);
// end our draw
batch.End(); batch.End();
base.DrawEarly(time, batch, alpha, blendState, samplerState, mat); // begin the usual draw again for other elements
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, matrix);
} }
} }

View file

@ -420,16 +420,13 @@ namespace MLEM.Ui.Elements {
child.Update(time); child.Update(time);
} }
public virtual void Draw(GameTime time, SpriteBatch batch, float alpha) { public virtual void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
this.System.OnElementDrawn?.Invoke(this, time, batch, alpha); 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); child.Draw(time, batch, alpha * child.DrawAlpha, blendState, samplerState, matrix);
} }
if (this.IsSelected)
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) {

View file

@ -46,7 +46,7 @@ namespace MLEM.Ui.Elements {
return this.texture != null && this.scaleToImage ? this.texture.Size : base.CalcActualSize(parentArea); return this.texture != null && this.scaleToImage ? this.texture.Size : base.CalcActualSize(parentArea);
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var center = new Vector2(this.texture.Width / 2F, this.texture.Height / 2F); var center = new Vector2(this.texture.Width / 2F, this.texture.Height / 2F);
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);
@ -56,7 +56,7 @@ namespace MLEM.Ui.Elements {
var scale = new Vector2(1F / this.texture.Width, 1F / this.texture.Height) * this.DisplayArea.Size.ToVector2(); var scale = new Vector2(1F / this.texture.Width, 1F / this.texture.Height) * this.DisplayArea.Size.ToVector2();
batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale, this.Color * alpha, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale, this.Color * alpha, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0);
} }
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
} }

View file

@ -92,11 +92,11 @@ 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) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
batch.Draw(this.Texture, this.DisplayArea, 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); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} 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(), Color.White); batch.Draw(this.renderTarget, this.GetRenderTargetArea(), Color.White);
@ -113,7 +113,7 @@ namespace MLEM.Ui.Elements {
var trans = Matrix.CreateTranslation(-area.X, -area.Y, 0); var trans = Matrix.CreateTranslation(-area.X, -area.Y, 0);
// do the usual draw, but within the render target // do the usual draw, but within the render target
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, trans); batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, trans);
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha, blendState, samplerState, trans);
batch.End(); batch.End();
// also draw any children early within the render target with the translation applied // also draw any children early within the render target with the translation applied
base.DrawEarly(time, batch, alpha, blendState, samplerState, trans); base.DrawEarly(time, batch, alpha, blendState, samplerState, trans);

View file

@ -69,7 +69,7 @@ namespace MLEM.Ui.Elements {
this.TimeIntoAnimation += time.ElapsedGameTime; this.TimeIntoAnimation += time.ElapsedGameTime;
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
if (this.Background != null) if (this.Background != null)
batch.Draw(this.Background, this.Area, this.BackgroundColor * alpha); batch.Draw(this.Background, this.Area, this.BackgroundColor * alpha);
@ -83,7 +83,7 @@ namespace MLEM.Ui.Elements {
// if we have formatting codes, we should do it // if we have formatting codes, we should do it
this.regularFont.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, this.TextColor * alpha, sc, this.boldFont, this.italicFont, 0, this.TimeIntoAnimation); this.regularFont.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, this.TextColor * alpha, sc, this.boldFont, this.italicFont, 0, this.TimeIntoAnimation);
} }
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -32,7 +32,7 @@ namespace MLEM.Ui.Elements {
this.CanBeSelected = false; this.CanBeSelected = false;
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
batch.Draw(this.Texture, this.DisplayArea, this.Color * alpha, this.Scale); batch.Draw(this.Texture, this.DisplayArea, this.Color * alpha, this.Scale);
var percentage = this.CurrentValue / this.MaxValue; var percentage = this.CurrentValue / this.MaxValue;
@ -63,7 +63,7 @@ namespace MLEM.Ui.Elements {
} else { } else {
batch.Draw(batch.GetBlankTexture(), offsetArea, this.ProgressColor * alpha); batch.Draw(batch.GetBlankTexture(), offsetArea, this.ProgressColor * alpha);
} }
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -118,7 +118,7 @@ namespace MLEM.Ui.Elements {
} }
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
batch.Draw(this.Background, this.DisplayArea, Color.White * alpha, this.Scale); batch.Draw(this.Background, this.DisplayArea, Color.White * alpha, this.Scale);
var scrollerPos = new Point(this.DisplayArea.X + this.ScrollerOffset.X, this.DisplayArea.Y + this.ScrollerOffset.Y); var scrollerPos = new Point(this.DisplayArea.X + this.ScrollerOffset.X, this.DisplayArea.Y + this.ScrollerOffset.Y);
@ -127,7 +127,7 @@ namespace MLEM.Ui.Elements {
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, new Point((this.ScrollerSize.X * this.Scale).Ceil(), (this.ScrollerSize.Y * this.Scale).Ceil())); var scrollerRect = new Rectangle(scrollerPos + scrollerOffset, new Point((this.ScrollerSize.X * this.Scale).Ceil(), (this.ScrollerSize.Y * this.Scale).Ceil()));
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); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
protected override void InitStyle(UiStyle style) { protected override void InitStyle(UiStyle style) {

View file

@ -132,7 +132,7 @@ namespace MLEM.Ui.Elements {
this.caretBlinkTimer = 0; this.caretBlinkTimer = 0;
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture; var tex = this.Texture;
var color = Color.White * alpha; var color = Color.White * alpha;
if (this.IsMouseOver) { if (this.IsMouseOver) {
@ -150,7 +150,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); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }
public void SetText(object text, bool removeMismatching = false) { public void SetText(object text, bool removeMismatching = false) {

View file

@ -111,7 +111,10 @@ 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); var alpha = this.DrawAlpha * root.Element.DrawAlpha;
root.Element.Draw(time, batch, alpha, this.BlendState, this.SamplerState, root.Transform);
if (root.SelectedElement != null)
this.OnSelectedElementDrawn?.Invoke(root.SelectedElement, time, batch, alpha);
batch.End(); batch.End();
} }
} }

View file

@ -24,3 +24,22 @@
/processor:TiledMapTilesetProcessor /processor:TiledMapTilesetProcessor
/build:Tiled/Tileset.tsx /build:Tiled/Tileset.tsx
#begin Textures/Test.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Textures/Test.png
#begin Fonts/TestFont.spritefont
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/build:Fonts/TestFont.spritefont

View file

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
<Asset Type="Graphics:FontDescription">
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Arial</FontName>
<!--
Size is a float value, measured in points. Modify this value to change
the size of the font.
-->
<Size>32</Size>
<!--
Spacing is a float value, measured in pixels. Modify this value to change
the amount of spacing in between characters.
-->
<Spacing>0</Spacing>
<!--
UseKerning controls the layout of the font. If this value is true, kerning information
will be used when placing characters.
-->
<UseKerning>true</UseKerning>
<!--
Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
and "Bold, Italic", and are case sensitive.
-->
<Style>Regular</Style>
<!--
If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font.
-->
<DefaultCharacter>*</DefaultCharacter>
<!--
CharacterRegions control what letters are available in the font. Every
character from Start to End will be built and made available for drawing. The
default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
character set. The characters are ordered according to the Unicode standard.
See the documentation for more information.
-->
<CharacterRegions>
<CharacterRegion>
<Start>&#32;</Start>
<End>&#591;</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>

Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

View file

@ -3,7 +3,12 @@ using Microsoft.Xna.Framework.Graphics;
using MLEM.Cameras; using MLEM.Cameras;
using MLEM.Extended.Extensions; using MLEM.Extended.Extensions;
using MLEM.Extended.Tiled; using MLEM.Extended.Tiled;
using MLEM.Font;
using MLEM.Startup; using MLEM.Startup;
using MLEM.Textures;
using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
namespace Sandbox { namespace Sandbox {
@ -28,6 +33,22 @@ namespace Sandbox {
Scale = 2, Scale = 2,
LookingPosition = new Vector2(25, 25) * this.map.GetTileSize() LookingPosition = new Vector2(25, 25) * this.map.GetTileSize()
}; };
var tex = LoadContent<Texture2D>("Textures/Test");
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")),
TextScale = 0.1F,
PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4)
};
this.UiSystem.AutoScaleReferenceSize = new Point(1280, 720);
this.UiSystem.AutoScaleWithScreen = true;
this.UiSystem.GlobalScale = 5;
var root = new Panel(Anchor.Center, new Vector2(50, 50), Vector2.Zero);
this.UiSystem.Add("Root", root);
var group = root.AddChild(new CustomDrawGroup(Anchor.AutoLeft, new Vector2(1, 10)));
group.AddChild(new Button(Anchor.AutoLeft, Vector2.One, "Test text"));
} }
protected override void DoDraw(GameTime gameTime) { protected override void DoDraw(GameTime gameTime) {