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

made tooltips not go off screen and fixed the ui system breaking clearing

This commit is contained in:
Ellpeck 2019-08-15 14:59:15 +02:00
parent 8d648cd5dc
commit 5d8e010bad
10 changed files with 57 additions and 22 deletions

View file

@ -51,7 +51,7 @@ namespace Demos {
this.group.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.Black);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(10));
@ -60,7 +60,7 @@ namespace Demos {
this.SpriteBatch.Draw(this.group.CurrentRegion, new Vector2(10, 10), Color.White);
this.SpriteBatch.End();
base.Draw(gameTime);
base.DoDraw(gameTime);
}
}

View file

@ -35,7 +35,7 @@ namespace Demos {
};
}
protected override void Draw(GameTime gameTime) {
protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.Black);
// the texture region supplied to the AutoTiling method should only encompass the first filler tile's location and size
@ -67,7 +67,7 @@ namespace Demos {
}
this.SpriteBatch.End();
base.Draw(gameTime);
base.DoDraw(gameTime);
}
}

View file

@ -17,6 +17,7 @@ namespace Demos {
/// If using your own game class that derives from <see cref="Game"/>, however, you will have to do a few additional things to get MLEM.Ui up and running:
/// - Create an instance of <see cref="UiSystem"/>
/// - Call the instance's Update method in your game's Update method
/// - Call the instance's DrawEarly method before clearing your <see cref="GraphicsDevice"/>
/// - Call the instance's Draw method in your game's Draw method
/// </remarks>
public class UiDemo : MlemGame {
@ -110,11 +111,21 @@ namespace Demos {
root.AddChild(new VerticalSpace(3));
root.AddChild(new RadioButton(Anchor.AutoLeft, new Vector2(1, 10), "Radio button 1!"));
root.AddChild(new RadioButton(Anchor.AutoLeft, new Vector2(1, 10), "Radio button 2!") {PositionOffset = new Vector2(0, 1)});
var tooltip = new Tooltip(50, "This is a test tooltip to see the window bounding") {IsHidden = true};
this.UiSystem.Add("TestTooltip", tooltip);
root.AddChild(new VerticalSpace(3));
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Test Tooltip") {
OnClicked = (element, button) => {
if (button == MouseButton.Left)
tooltip.IsHidden = !tooltip.IsHidden;
}
});
}
protected override void Draw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.Black);
base.Draw(gameTime);
protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.CornflowerBlue);
base.DoDraw(gameTime);
}
}

View file

@ -60,11 +60,16 @@ namespace MLEM.Startup {
}
protected override void Draw(GameTime gameTime) {
base.Draw(gameTime);
this.UiSystem.DrawEarly(gameTime, this.SpriteBatch);
this.DoDraw(gameTime);
this.UiSystem.Draw(gameTime, this.SpriteBatch);
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
}
protected virtual void DoDraw(GameTime gameTime) {
base.Draw(gameTime);
}
public static T LoadContent<T>(string name) {
return instance.Content.Load<T>(name);
}

View file

@ -218,6 +218,8 @@ namespace MLEM.Ui.Elements {
public virtual void ForceUpdateArea() {
this.areaDirty = false;
if (this.IsHidden)
return;
var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : this.system.Viewport;
var parentCenterX = parentArea.X + parentArea.Width / 2;
@ -361,10 +363,10 @@ namespace MLEM.Ui.Elements {
}
}
public virtual void DrawUnbound(GameTime time, SpriteBatch batch, float alpha, BlendState blendState = null, SamplerState samplerState = null) {
public virtual void DrawEarly(GameTime time, SpriteBatch batch, float alpha, BlendState blendState = null, SamplerState samplerState = null) {
foreach (var child in this.SortedChildren) {
if (!child.IsHidden)
child.DrawUnbound(time, batch, alpha * child.DrawAlpha, blendState, samplerState);
child.DrawEarly(time, batch, alpha * child.DrawAlpha, blendState, samplerState);
}
}

View file

@ -91,7 +91,7 @@ namespace MLEM.Ui.Elements {
}
}
public override void DrawUnbound(GameTime time, SpriteBatch batch, float alpha, BlendState blendState = null, SamplerState samplerState = null) {
public override void DrawEarly(GameTime time, SpriteBatch batch, float alpha, BlendState blendState = null, SamplerState samplerState = null) {
if (this.scrollOverflow) {
// draw children onto the render target
batch.GraphicsDevice.SetRenderTarget(this.renderTarget);
@ -103,7 +103,7 @@ namespace MLEM.Ui.Elements {
batch.End();
batch.GraphicsDevice.SetRenderTarget(null);
}
base.DrawUnbound(time, batch, alpha, blendState, samplerState);
base.DrawEarly(time, batch, alpha, blendState, samplerState);
}
private Rectangle GetRenderTargetArea() {

View file

@ -29,6 +29,7 @@ namespace MLEM.Ui.Elements {
this.SetAreaDirty();
}
}
public bool AutoAdjustWidth;
public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
this.text = text;
@ -39,7 +40,7 @@ namespace MLEM.Ui.Elements {
protected override Point CalcActualSize(Rectangle parentArea) {
var size = base.CalcActualSize(parentArea);
this.splitText = this.font.SplitString(this.text, size.X, this.TextScale * this.Scale).ToArray();
this.splitText = this.font.SplitString(this.text, size.X - this.ScaledPadding.X * 2, this.TextScale * this.Scale).ToArray();
this.lineHeight = 0;
this.longestLineLength = 0;
@ -52,14 +53,12 @@ namespace MLEM.Ui.Elements {
if (strgScale.X > this.longestLineLength)
this.longestLineLength = strgScale.X;
}
return new Point(size.X, height.Ceil());
return new Point(this.AutoAdjustWidth ? (this.longestLineLength + this.ScaledPadding.X * 2).Ceil() : size.X, height.Ceil() + this.ScaledPadding.Y * 2);
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
if (this.Background != null) {
var backgroundArea = new Rectangle(this.Area.X + offset.X, this.Area.Y + offset.Y, this.longestLineLength.Ceil() + this.ScaledPadding.X * 2, this.Area.Height + this.ScaledPadding.Y * 2);
batch.Draw(this.Background, backgroundArea, this.BackgroundColor * alpha);
}
if (this.Background != null)
batch.Draw(this.Background, this.Area.OffsetCopy(offset), this.BackgroundColor * alpha);
var pos = this.DisplayArea.Location.ToVector2();
var off = offset.ToVector2();

View file

@ -11,12 +11,24 @@ namespace MLEM.Ui.Elements {
public Tooltip(float width, string text, IGenericFont font = null) :
base(Anchor.TopLeft, width, text, false, font) {
this.AutoAdjustWidth = true;
this.Padding = new Point(2);
}
public override void Update(GameTime time) {
base.Update(time);
this.PositionOffset = this.MousePos.ToVector2() / this.Scale + this.MouseOffset;
var viewport = this.System.Viewport.Size;
var offset = this.MousePos.ToVector2() / this.Scale + this.MouseOffset;
if (offset.X < 0)
offset.X = 0;
if (offset.Y < 0)
offset.Y = 0;
if (offset.X * this.Scale + this.Area.Width >= viewport.X)
offset.X = (viewport.X - this.Area.Width) / this.Scale;
if (offset.Y * this.Scale + this.Area.Height >= viewport.Y)
offset.Y = (viewport.Y - this.Area.Height) / this.Scale;
this.PositionOffset = offset;
}
public override void ForceUpdateArea() {

View file

@ -93,12 +93,14 @@ namespace MLEM.Ui {
root.Element.Update(time);
}
public void Draw(GameTime time, SpriteBatch batch) {
public void DrawEarly(GameTime time, SpriteBatch batch) {
foreach (var root in this.rootElements) {
if (!root.Element.IsHidden)
root.Element.DrawUnbound(time, batch, this.DrawAlpha * root.Element.DrawAlpha, this.BlendState, this.SamplerState);
root.Element.DrawEarly(time, batch, this.DrawAlpha * root.Element.DrawAlpha, this.BlendState, this.SamplerState);
}
}
public void Draw(GameTime time, SpriteBatch batch) {
foreach (var root in this.rootElements) {
if (root.Element.IsHidden)
continue;

View file

@ -28,6 +28,10 @@ namespace MLEM.Extensions {
return new Point((point.X * f).Floor(), (point.Y * f).Floor());
}
public static Point Divide(this Point point, float f) {
return new Point((point.X / f).Floor(), (point.Y / f).Floor());
}
public static Rectangle OffsetCopy(this Rectangle rect, Point offset) {
rect.X += offset.X;
rect.Y += offset.Y;