Skip to content

Commit

Permalink
v.1.0.1 added primitives, Circle, Line, FilledRectangle
Browse files Browse the repository at this point in the history
fixed som bugs
  • Loading branch information
jni759 committed Apr 20, 2018
1 parent 20996d6 commit 63c76c9
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 37 deletions.
103 changes: 95 additions & 8 deletions FormsTest/MyControls.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using MonoGame.UI.Forms;
using MonoGame.UI.Forms.Effects;

namespace FormsTest
{
Expand Down Expand Up @@ -35,7 +31,7 @@ public override void InitializeComponent()
Title = "Calculator",
IsMovable = true,
Location = new Vector2(100, 100),
Size = new Vector2(260, 290),
Size = new Vector2(230, 290),
};
SetDefaultFormTextures(form1);

Expand Down Expand Up @@ -90,7 +86,13 @@ public override void InitializeComponent()
SetDefaultFormTextures(form2);

// btn
btn = new Button() { Text = "Button 1", Size = new Vector2(90, 45), Location = new Vector2(20, 40) };
btn = new Button()
{
Text = "Button 1",
Size = new Vector2(90, 45),
Location = new Vector2(20, 40),
HoverEffect = new ZoomEffect() { Duration = 15, ZoomTo = 1.2f }
};
SetButtonTextures("Blue", btn);
btn.Clicked += Btn_Clicked;

Expand All @@ -114,14 +116,99 @@ with multiple
bar = new Progressbar() { Location = new Vector2(30, 100), Size = new Vector2(200, 25) };

form2.Controls.AddRange(new Control[]{btn, lbl1, lbl2, bar, txtArea});

//form3
var form3 = new Form()
{
Title = "Alignment test",
IsMovable = true,
Location = new Vector2(350, 250),
Size = new Vector2(180, 200)
};

var btnTL = new Button() { Text = "TL", Size = new Vector2(45, 45), Location = new Vector2(10, 40), TextAlign = ContentAlignment.TopLeft};
var btnTC = new Button() { Text = "TC", Size = new Vector2(45, 45), Location = new Vector2(60, 40), TextAlign = ContentAlignment.TopCenter};
var btnTR = new Button() { Text = "TR", Size = new Vector2(45, 45), Location = new Vector2(110, 40), TextAlign = ContentAlignment.TopRight};
var btnML = new Button() { Text = "ML", Size = new Vector2(45, 45), Location = new Vector2(10, 90), TextAlign = ContentAlignment.MiddleLeft};
var btnMC = new Button() { Text = "MC", Size = new Vector2(45, 45), Location = new Vector2(60, 90), TextAlign = ContentAlignment.MiddleCenter};
var btnMR = new Button() { Text = "MR", Size = new Vector2(45, 45), Location = new Vector2(110, 90), TextAlign = ContentAlignment.MiddleRight};
var btnBL = new Button() { Text = "BL", Size = new Vector2(45, 45), Location = new Vector2(10, 140), TextAlign = ContentAlignment.BottomLeft};
var btnBC = new Button() { Text = "BC", Size = new Vector2(45, 45), Location = new Vector2(60, 140), TextAlign = ContentAlignment.BottomCenter};
var btnBR = new Button() { Text = "BR", Size = new Vector2(45, 45), Location = new Vector2(110, 140), TextAlign = ContentAlignment.BottomRight};

SetButtonTextures("Red", btnTL, btnTC, btnTR, btnML, btnMC, btnMR, btnBL, btnBC, btnBR);
var form3Buttons = new Control[] { btnTL, btnTC, btnTR, btnML, btnMC, btnMR, btnBL, btnBC, btnBR };
form3.Controls.AddRange(form3Buttons);

SetDefaultFormTextures(form3);

//form4
var form4 = new Form()
{
Title = "Primitives test",
IsMovable = true,
Location = new Vector2(350, 20),
Size = new Vector2(140, 200)
};
SetDefaultFormTextures(form4);

var btnCircle = new Button() { Text = "Circle", Size = new Vector2(75, 45), Location = new Vector2(10, 40) };
var btnLine = new Button() { Text = "Line", Size = new Vector2(75, 45), Location = new Vector2(10, 90) };
var btnRectangle = new Button() { Text = "Rectangle", Size = new Vector2(75, 45), Location = new Vector2(10, 140) };

SetButtonTextures("Blue", btnCircle, btnLine, btnRectangle);
var form4Buttons = new Control[] { btnLine, btnCircle, btnRectangle };
btnCircle.Clicked += BtnAddPrimitive;
btnLine.Clicked += BtnAddPrimitive;
btnRectangle.Clicked += BtnAddPrimitive;
form4.Controls.AddRange(form4Buttons);

Controls.Add(form1);
Controls.Add(form2);
Controls.Add(form3);
Controls.Add(form4);
}

private void BtnAddPrimitive(object sender, EventArgs e)
{
var btn = sender as Button;
var rnd = new Random();
Control control = null;

switch (btn.Text)
{
case "Circle":
control = new Circle(new Vector2(rnd.Next(10, 500), rnd.Next(10, 400)), rnd.Next(5, 50));
break;
case "Line":
control = new Line(new Vector2(rnd.Next(10,500), rnd.Next(10, 400)),
new Vector2(rnd.Next(10, 500), rnd.Next(10,400))) { LineThickness = rnd.Next(1, 20)};
break;
default:
control = new FilledRectangle(rnd.Next(10, 500), rnd.Next(10, 400), rnd.Next(1, 100), rnd.Next(1, 100));
break;
}

control.MouseEnter += PrimitiveMouseEnter;
control.MouseLeave += PrimitiveMouseLeave;
Controls.Add(control);
}

private void PrimitiveMouseLeave(object sender, EventArgs e)
{
Control c = sender as Control;
c.BackgroundColor = Color.White;
}

private void PrimitiveMouseEnter(object sender, EventArgs e)
{
Control c = sender as Control;
c.BackgroundColor = Color.DarkCyan;
}

private void Btn_Clicked(object sender, EventArgs e)
{
btn.Text = "Clicked!";
//txtArea.Text += "newline" + Environment.NewLine;
bar.Value += 10;
}

Expand Down
37 changes: 26 additions & 11 deletions MonoGame.UI.Forms/AlignmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,45 @@ namespace MonoGame.UI.Forms
{
class AlignmentHelper
{
public static Vector2 Align(Vector2 container, Vector2 content, ContentAlignment alignment)
public static Vector2 Align(Vector2 container, Vector2 content, ContentAlignment alignment, bool round = true)
{
var ret = Vector2.Zero;

switch (alignment)
{
case ContentAlignment.BottomCenter:
return new Vector2(container.X / 2 - content.X / 2, container.Y - content.Y);
ret = new Vector2(container.X / 2 - content.X / 2, container.Y - content.Y);
break;
case ContentAlignment.BottomRight:
return new Vector2(container.X - content.X, container.Y - content.Y);
ret = new Vector2(container.X - content.X, container.Y - content.Y);
break;
case ContentAlignment.BottomLeft:
return new Vector2(0, container.Y - content.Y);
ret = new Vector2(0, container.Y - content.Y);
break;
case ContentAlignment.MiddleCenter:
return new Vector2(container.X / 2 - content.X / 2, container.Y / 2 - content.Y / 2);
ret = new Vector2(container.X / 2 - content.X / 2, container.Y / 2 - content.Y / 2);
break;
case ContentAlignment.MiddleLeft:
return new Vector2(0, container.Y / 2 - content.Y / 2);
ret = new Vector2(0, container.Y / 2 - content.Y / 2);
break;
case ContentAlignment.MiddleRight:
return new Vector2(container.X - content.X, container.Y / 2 - content.Y / 2);
ret = new Vector2(container.X - content.X, container.Y / 2 - content.Y / 2);
break;
case ContentAlignment.TopCenter:
return new Vector2(container.X / 2 - content.X / 2, 0);
ret = new Vector2(container.X / 2 - content.X / 2, 0);
break;
case ContentAlignment.TopLeft:
return new Vector2(0, 0);
ret = new Vector2(0, 0);
break;
case ContentAlignment.TopRight:
return new Vector2(container.X - content.X, 0);
ret = new Vector2(container.X - content.X, 0);
break;
}
return Vector2.Zero;

if(round)
ret = new Vector2((float)Math.Round(ret.X), (float)Math.Round(ret.Y));

return ret;
}
}
}
2 changes: 1 addition & 1 deletion MonoGame.UI.Forms/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal override void Draw(DrawHelper helper, Vector2 offset)
start + new Vector2(Size.X - topRightOffset.X, topLeftOffset.Y), BtnMiddleTexture, blend);

var txtSize = helper.MeasureString(FontName, Text);
helper.DrawString(this, Location + AlignmentHelper.Align(Size, txtSize, TextAlign) + offset, Text, TextColor);
helper.DrawString(this, Location + AlignmentHelper.Align(Size, txtSize, TextAlign) + offset, Text, TextColor, Zoom);
}

internal override void LoadContent(DrawHelper helper)
Expand Down
35 changes: 35 additions & 0 deletions MonoGame.UI.Forms/Circle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.Xna.Framework;

namespace MonoGame.UI.Forms
{
public class Circle : Control
{
public float Radius { get; set; }

public Vector2 Center
{
get => new Vector2(Location.X + Radius, Location.Y + Radius);
set => Location = new Vector2(value.X - Radius, value.Y - Radius);
}

public Circle(Vector2 center, float radius)
{
Radius = radius;
Center = center;
BackgroundColor = Color.White;
Size = new Vector2(radius * 2, radius * 2);
}

internal override void Draw(DrawHelper helper, Vector2 offset)
{
helper.DrawCircle(Center + offset, Radius, BackgroundColor);
}

public override bool Contains(Point point)
{
var delta = point.ToVector2() - Center;
return (delta.LengthSquared() < (float)(Radius * Radius));
}
}
}
28 changes: 27 additions & 1 deletion MonoGame.UI.Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using MonoGame.UI.Forms.Effects;

namespace MonoGame.UI.Forms
{
Expand All @@ -18,6 +19,8 @@ public abstract class Control
public Rectangle HitBox => new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y);
public int ZIndex { get; set; }

public Effect HoverEffect { get; set; }

public event EventHandler Clicked;
public event EventHandler MouseDown;
public event EventHandler MouseUp;
Expand All @@ -26,13 +29,21 @@ public abstract class Control

protected bool IsPressed;
protected bool IsHovering;

protected float Zoom = 1.0f;

private bool _wasHovering;

protected Control()
{
FontName = "defaultFont";
IsVisible = true;
}

public virtual bool Contains(Point point)
{
return HitBox.Contains(point);
}

internal abstract void Draw(DrawHelper helper, Vector2 offset);

internal virtual void Draw(DrawHelper helper)
Expand All @@ -41,6 +52,21 @@ internal virtual void Draw(DrawHelper helper)
Draw(helper, Vector2.Zero);
}

internal virtual void Update(GameTime gameTime)
{
Zoom = 1.0f;

if (IsHovering && HoverEffect != null)
{
if(!_wasHovering)
HoverEffect.Reset();
HoverEffect.Update(gameTime);
Zoom = HoverEffect.Zoom;
}

_wasHovering = IsHovering;
}

internal virtual void LoadContent(DrawHelper helper)
{
if (!string.IsNullOrEmpty(FontName))
Expand Down
26 changes: 15 additions & 11 deletions MonoGame.UI.Forms/ControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class ControlManager : DrawableGameComponent
private SpriteBatch _spriteBatch;
private DrawHelper _drawHelper;
private Control _selectedControl;
private Control _hoverControl;
private Control _lastHoveredControl;

private Vector2 _dragPositionOffset;
private bool _isDragging;
Expand Down Expand Up @@ -51,22 +51,21 @@ public override void Update(GameTime gameTime)
var mouseState = Mouse.GetState();

var hoverControl = FindControlAt(mouseState.Position, Controls);
if (_hoverControl != null)
if (_lastHoveredControl != hoverControl)
{
if (_hoverControl != hoverControl)
{
_hoverControl.OnMouseLeave();
hoverControl?.OnMouseEnter();
}
_lastHoveredControl?.OnMouseLeave();
hoverControl?.OnMouseEnter();
}
_hoverControl = hoverControl;


_lastHoveredControl = hoverControl;

if (mouseState.LeftButton == ButtonState.Pressed
&& _prevMouseState.LeftButton == ButtonState.Released)
{
if (_hoverControl != null)
if (_lastHoveredControl != null)
{
_selectedControl = _hoverControl;
_selectedControl = _lastHoveredControl;
_selectedControl.OnMouseDown();

if (_selectedControl is Form)
Expand Down Expand Up @@ -102,6 +101,11 @@ public override void Update(GameTime gameTime)

Controls = Controls.OrderBy(c => c.ZIndex).ToList();
_prevMouseState = mouseState;

foreach (var control in Controls)
{
control.Update(gameTime);
}
}

public override void Draw(GameTime gameTime)
Expand All @@ -122,7 +126,7 @@ public override void Draw(GameTime gameTime)

private Control FindControlAt(Point position, IEnumerable<Control> controls)
{
var control = controls.LastOrDefault(c => c.HitBox.Contains(position));
var control = controls.LastOrDefault(c => c.Contains(position));
if (control is IControls)
return ((IControls) control).FindControlAt(position);
else
Expand Down
Loading

0 comments on commit 63c76c9

Please sign in to comment.