mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-25 08:22:15 +00:00
in the middle of a major refactor
This commit is contained in:
@@ -1,51 +1,58 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.Gui.Styles;
|
||||
using MonoGame.Extended.InputListeners;
|
||||
using MonoGame.Extended.Shapes;
|
||||
|
||||
namespace MonoGame.Extended.Gui.Controls
|
||||
{
|
||||
public class GuiButtonStyle : GuiControlStyle<GuiButton>
|
||||
{
|
||||
public GuiButtonStyle(IGuiDrawable normal)
|
||||
: this(normal, normal, normal)
|
||||
{
|
||||
}
|
||||
|
||||
public GuiButtonStyle(IGuiDrawable normal, IGuiDrawable pressed)
|
||||
: this(normal, pressed, normal)
|
||||
{
|
||||
}
|
||||
|
||||
public GuiButtonStyle(IGuiDrawable normal, IGuiDrawable pressed, IGuiDrawable hovered)
|
||||
{
|
||||
Normal = normal;
|
||||
Pressed = pressed;
|
||||
Hovered = hovered;
|
||||
}
|
||||
|
||||
public IGuiDrawable Normal { get; set; }
|
||||
public IGuiDrawable Pressed { get; set; }
|
||||
public IGuiDrawable Hovered { get; set; }
|
||||
|
||||
protected override IGuiDrawable GetCurrentDrawable(GuiButton control)
|
||||
{
|
||||
if (control.IsPressed)
|
||||
return Pressed;
|
||||
|
||||
if (control.IsHovered)
|
||||
return Hovered;
|
||||
|
||||
return Normal;
|
||||
}
|
||||
}
|
||||
|
||||
public class GuiButton : GuiControl
|
||||
{
|
||||
public GuiButton(GuiControlStyle style)
|
||||
: this(style, style, style)
|
||||
public GuiButton(GuiButtonStyle style)
|
||||
{
|
||||
}
|
||||
|
||||
public GuiButton(GuiControlStyle normalStyle, GuiControlStyle pressedStyle)
|
||||
: this(normalStyle, pressedStyle, normalStyle)
|
||||
{
|
||||
}
|
||||
|
||||
public GuiButton(GuiControlStyle normalStyle, GuiControlStyle pressedStyle, GuiControlStyle hoveredStyle)
|
||||
{
|
||||
NormalStyle = normalStyle;
|
||||
PressedStyle = pressedStyle;
|
||||
HoveredStyle = hoveredStyle;
|
||||
IsPressed = false;
|
||||
}
|
||||
|
||||
public GuiControlStyle NormalStyle { get; set; }
|
||||
public GuiControlStyle PressedStyle { get; set; }
|
||||
public GuiControlStyle HoveredStyle { get; set; }
|
||||
|
||||
public bool IsPressed { get; private set; }
|
||||
|
||||
public event EventHandler<MouseEventArgs> Clicked;
|
||||
|
||||
public override GuiControlStyle CurrentStyle
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsPressed)
|
||||
return PressedStyle;
|
||||
|
||||
if (IsHovered)
|
||||
return HoveredStyle;
|
||||
|
||||
return NormalStyle;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnMouseDown(object sender, MouseEventArgs args)
|
||||
{
|
||||
IsPressed = true;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.Gui.Styles;
|
||||
using MonoGame.Extended.InputListeners;
|
||||
@@ -15,20 +16,16 @@ namespace MonoGame.Extended.Gui.Controls
|
||||
|
||||
public bool IsHovered { get; private set; }
|
||||
|
||||
public abstract GuiControlStyle CurrentStyle { get; }
|
||||
|
||||
private IShapeF _shape;
|
||||
public IShapeF Shape
|
||||
{
|
||||
get { return _shape ?? CurrentStyle.BoundingShape; }
|
||||
set { _shape = value; }
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public Vector2 Position { get; set; }
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
CurrentStyle.Draw(this, spriteBatch);
|
||||
//CurrentStyle.Draw(this, spriteBatch);
|
||||
}
|
||||
|
||||
public virtual void Update(GameTime gameTime) { }
|
||||
|
||||
@@ -1,20 +1,32 @@
|
||||
using MonoGame.Extended.BitmapFonts;
|
||||
using MonoGame.Extended.Gui.Styles;
|
||||
using MonoGame.Extended.Shapes;
|
||||
|
||||
namespace MonoGame.Extended.Gui.Controls
|
||||
{
|
||||
public class GuiLabel : GuiControl
|
||||
public class GuiLabelStyle : GuiControlStyle<GuiLabel>
|
||||
{
|
||||
public GuiLabel(GuiTextStyle textStyle)
|
||||
private readonly BitmapFont _font;
|
||||
|
||||
public GuiLabelStyle(BitmapFont font)
|
||||
{
|
||||
TextStyle = textStyle;
|
||||
_font = font;
|
||||
}
|
||||
|
||||
public GuiTextStyle TextStyle { get; set; }
|
||||
public string Text { get; set; }
|
||||
|
||||
public override GuiControlStyle CurrentStyle
|
||||
protected override IGuiDrawable GetCurrentDrawable(GuiLabel control)
|
||||
{
|
||||
get { return TextStyle; }
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class GuiLabel : GuiControl
|
||||
{
|
||||
public GuiLabel(GuiLabelStyle style)
|
||||
{
|
||||
Style = style;
|
||||
}
|
||||
|
||||
public GuiLabelStyle Style { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,14 @@ namespace MonoGame.Extended.Gui.Styles.Drawables
|
||||
public GuiTextureRegionDrawable(TextureRegion2D region)
|
||||
{
|
||||
_region = region;
|
||||
Size = new Size(_region.Width, _region.Height);
|
||||
Color = Color.White;
|
||||
}
|
||||
|
||||
public Color Color { get; set; }
|
||||
|
||||
public Size Size { get; private set; }
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, Rectangle bounds)
|
||||
{
|
||||
spriteBatch.Draw(_region, bounds, Color);
|
||||
|
||||
@@ -7,12 +7,25 @@ namespace MonoGame.Extended.Gui.Styles
|
||||
{
|
||||
public interface IGuiDrawable
|
||||
{
|
||||
Size Size { get; }
|
||||
void Draw(SpriteBatch spriteBatch, Rectangle bounds);
|
||||
}
|
||||
|
||||
public abstract class GuiControlStyle
|
||||
public abstract class GuiControlStyle<T>
|
||||
where T : GuiControl
|
||||
{
|
||||
public abstract IShapeF BoundingShape { get; }
|
||||
public abstract void Draw(GuiControl control, SpriteBatch spriteBatch);
|
||||
protected abstract IGuiDrawable GetCurrentDrawable(T control);
|
||||
|
||||
protected virtual Size GetDesiredSize(T control)
|
||||
{
|
||||
var drawable = GetCurrentDrawable(control);
|
||||
return drawable.Size;
|
||||
}
|
||||
|
||||
public virtual void Draw(T control, SpriteBatch spriteBatch, Rectangle rectangle)
|
||||
{
|
||||
var drawable = GetCurrentDrawable(control);
|
||||
drawable.Draw(spriteBatch, rectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,54 +7,54 @@ using MonoGame.Extended.TextureAtlases;
|
||||
|
||||
namespace MonoGame.Extended.Gui.Styles
|
||||
{
|
||||
public class GuiSpriteStyle : GuiControlStyle
|
||||
{
|
||||
public GuiSpriteStyle(TextureRegion2D textureRegion)
|
||||
{
|
||||
_sprite = new Sprite(textureRegion);
|
||||
}
|
||||
//public class GuiSpriteStyle : GuiControlStyle
|
||||
//{
|
||||
// public GuiSpriteStyle(TextureRegion2D textureRegion)
|
||||
// {
|
||||
// _sprite = new Sprite(textureRegion);
|
||||
// }
|
||||
|
||||
private readonly Sprite _sprite;
|
||||
// private readonly Sprite _sprite;
|
||||
|
||||
public TextureRegion2D TextureRegion
|
||||
{
|
||||
get { return _sprite.TextureRegion; }
|
||||
set { _sprite.TextureRegion = value; }
|
||||
}
|
||||
// public TextureRegion2D TextureRegion
|
||||
// {
|
||||
// get { return _sprite.TextureRegion; }
|
||||
// set { _sprite.TextureRegion = value; }
|
||||
// }
|
||||
|
||||
public Color Color
|
||||
{
|
||||
get { return _sprite.Color; }
|
||||
set { _sprite.Color = value; }
|
||||
}
|
||||
// public Color Color
|
||||
// {
|
||||
// get { return _sprite.Color; }
|
||||
// set { _sprite.Color = value; }
|
||||
// }
|
||||
|
||||
public SpriteEffects Effect
|
||||
{
|
||||
get { return _sprite.Effect; }
|
||||
set { _sprite.Effect = value; }
|
||||
}
|
||||
// public SpriteEffects Effect
|
||||
// {
|
||||
// get { return _sprite.Effect; }
|
||||
// set { _sprite.Effect = value; }
|
||||
// }
|
||||
|
||||
public float Rotation
|
||||
{
|
||||
get { return _sprite.Rotation; }
|
||||
set { _sprite.Rotation = value; }
|
||||
}
|
||||
// public float Rotation
|
||||
// {
|
||||
// get { return _sprite.Rotation; }
|
||||
// set { _sprite.Rotation = value; }
|
||||
// }
|
||||
|
||||
public Vector2 Scale
|
||||
{
|
||||
get { return _sprite.Scale; }
|
||||
set { _sprite.Scale = value; }
|
||||
}
|
||||
// public Vector2 Scale
|
||||
// {
|
||||
// get { return _sprite.Scale; }
|
||||
// set { _sprite.Scale = value; }
|
||||
// }
|
||||
|
||||
public override IShapeF BoundingShape
|
||||
{
|
||||
get { return _sprite.GetBoundingRectangle(); }
|
||||
}
|
||||
// public override IShapeF BoundingShape
|
||||
// {
|
||||
// get { return _sprite.GetBoundingRectangle(); }
|
||||
// }
|
||||
|
||||
public override void Draw(GuiControl control, SpriteBatch spriteBatch)
|
||||
{
|
||||
_sprite.Position = control.Position;
|
||||
spriteBatch.Draw(_sprite);
|
||||
}
|
||||
}
|
||||
// public override void Draw(GuiControl control, SpriteBatch spriteBatch)
|
||||
// {
|
||||
// _sprite.Position = control.Position;
|
||||
// spriteBatch.Draw(_sprite);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -6,27 +6,27 @@ using MonoGame.Extended.Shapes;
|
||||
|
||||
namespace MonoGame.Extended.Gui.Styles
|
||||
{
|
||||
public class GuiTextStyle : GuiControlStyle
|
||||
{
|
||||
private readonly BitmapFont _font;
|
||||
//public class GuiTextStyle : GuiControlStyle
|
||||
//{
|
||||
// private readonly BitmapFont _font;
|
||||
|
||||
public GuiTextStyle(BitmapFont font, string text)
|
||||
{
|
||||
_font = font;
|
||||
Color = Color.White;
|
||||
}
|
||||
// public GuiTextStyle(BitmapFont font, string text)
|
||||
// {
|
||||
// _font = font;
|
||||
// Color = Color.White;
|
||||
// }
|
||||
|
||||
public Color Color { get; set; }
|
||||
public string Text { get; set; }
|
||||
// public Color Color { get; set; }
|
||||
// public string Text { get; set; }
|
||||
|
||||
public override IShapeF BoundingShape
|
||||
{
|
||||
get { return _font.GetStringRectangle(Text, Vector2.Zero).ToRectangleF(); }
|
||||
}
|
||||
// public override IShapeF BoundingShape
|
||||
// {
|
||||
// get { return _font.GetStringRectangle(Text, Vector2.Zero).ToRectangleF(); }
|
||||
// }
|
||||
|
||||
public override void Draw(GuiControl control, SpriteBatch spriteBatch)
|
||||
{
|
||||
spriteBatch.DrawString(_font, Text, control.Position, Color);
|
||||
}
|
||||
}
|
||||
// public override void Draw(GuiControl control, SpriteBatch spriteBatch)
|
||||
// {
|
||||
// spriteBatch.DrawString(_font, Text, control.Position, Color);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -88,6 +88,7 @@
|
||||
<Compile Include="Shapes\RectangleF.cs" />
|
||||
<Compile Include="Shapes\RectangleExtensions.cs" />
|
||||
<Compile Include="Shapes\ShapeF.cs" />
|
||||
<Compile Include="Size.cs" />
|
||||
<Compile Include="Sprites\SpriteExtensions.cs" />
|
||||
<Compile Include="Animations\SpriteSheetAnimation.cs" />
|
||||
<Compile Include="Animations\SpriteSheetAnimator.cs" />
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public struct Size : IEquatable<Size>
|
||||
{
|
||||
public Size(int width, int height)
|
||||
: this()
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
|
||||
public int Width { get; private set; }
|
||||
public int Height { get; private set; }
|
||||
|
||||
public static Size Empty
|
||||
{
|
||||
get { return new Size(0, 0); }
|
||||
}
|
||||
|
||||
public bool IsEmpty
|
||||
{
|
||||
get { return Width == 0 && Height == 0; }
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return Width.GetHashCode() + Height.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool operator ==(Size a, Size b)
|
||||
{
|
||||
return a.Width == b.Width && a.Height == b.Height;
|
||||
}
|
||||
|
||||
public static bool operator !=(Size a, Size b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public bool Equals(Size other)
|
||||
{
|
||||
return Width == other.Width && Height == other.Height;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
return obj is Size && Equals((Size)obj);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("Width: {0}, Height: {1}", Width, Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,3 +238,25 @@
|
||||
/processorParam:TextureFormat=Color
|
||||
/build:Gui/button-normal.png
|
||||
|
||||
#begin Gui/9patch-1.png
|
||||
/importer:TextureImporter
|
||||
/processor:TextureProcessor
|
||||
/processorParam:ColorKeyColor=255,0,255,255
|
||||
/processorParam:ColorKeyEnabled=True
|
||||
/processorParam:GenerateMipmaps=False
|
||||
/processorParam:PremultiplyAlpha=True
|
||||
/processorParam:ResizeToPowerOfTwo=False
|
||||
/processorParam:TextureFormat=Color
|
||||
/build:Gui/9patch-1.png
|
||||
|
||||
#begin Gui/9patch-2.png
|
||||
/importer:TextureImporter
|
||||
/processor:TextureProcessor
|
||||
/processorParam:ColorKeyColor=255,0,255,255
|
||||
/processorParam:ColorKeyEnabled=True
|
||||
/processorParam:GenerateMipmaps=False
|
||||
/processorParam:PremultiplyAlpha=True
|
||||
/processorParam:ResizeToPowerOfTwo=False
|
||||
/processorParam:TextureFormat=Color
|
||||
/build:Gui/9patch-2.png
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ using MonoGame.Extended.BitmapFonts;
|
||||
using MonoGame.Extended.Gui;
|
||||
using MonoGame.Extended.Gui.Controls;
|
||||
using MonoGame.Extended.Gui.Styles;
|
||||
using MonoGame.Extended.Gui.Styles.Drawables;
|
||||
using MonoGame.Extended.Screens;
|
||||
using MonoGame.Extended.TextureAtlases;
|
||||
using MonoGame.Extended.ViewportAdapters;
|
||||
@@ -61,10 +62,11 @@ namespace SpaceGame
|
||||
_viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 480);
|
||||
|
||||
_guiManager = new GuiManager(_viewportAdapter, GraphicsDevice);
|
||||
var normalStyle = new GuiSpriteStyle(new TextureRegion2D(Content.Load<Texture2D>("Gui/button-normal")));
|
||||
var pressedStyle = new GuiSpriteStyle(new TextureRegion2D(Content.Load<Texture2D>("Gui/button-clicked"))) { Scale = Vector2.One * 0.95f };
|
||||
var hoveredStyle = new GuiSpriteStyle(new TextureRegion2D(Content.Load<Texture2D>("Gui/button-hover"))) { Scale = Vector2.One * 1.05f };
|
||||
var button = new GuiButton(normalStyle, pressedStyle, hoveredStyle)
|
||||
var normal = new GuiTextureRegionDrawable(new TextureRegion2D(Content.Load<Texture2D>("Gui/button-normal")));
|
||||
var pressed = new GuiTextureRegionDrawable(new TextureRegion2D(Content.Load<Texture2D>("Gui/button-clicked")));
|
||||
var hover = new GuiTextureRegionDrawable(new TextureRegion2D(Content.Load<Texture2D>("Gui/button-hover")));
|
||||
var buttonStyle = new GuiButtonStyle(normal, pressed, hover);
|
||||
var button = new GuiButton(buttonStyle)
|
||||
{
|
||||
Position = new Vector2(400, 370)
|
||||
};
|
||||
|
||||
@@ -68,6 +68,8 @@
|
||||
<ItemGroup>
|
||||
<Content Include="Content\black.png" />
|
||||
<Content Include="Content\Fonts\courier-new-32_0.png" />
|
||||
<Content Include="Content\Gui\9patch-1.png" />
|
||||
<Content Include="Content\Gui\9patch-2.png" />
|
||||
<Content Include="Content\Gui\button-clicked.png" />
|
||||
<Content Include="Content\Gui\button-hover.png" />
|
||||
<Content Include="Content\Gui\button-locked.png" />
|
||||
|
||||
Reference in New Issue
Block a user