1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
using Castle.Core.Internal;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using V3.Input;
namespace V3.Widgets
{
// ReSharper disable once ClassNeverInstantiated.Global
public sealed class Button : AbstractTextWidget, IClickable, IImageWidget, ISelectable
{
public bool IsClicked { get; set; }
public bool IsEnabled { get; set; } = true;
public bool IsSelected { get; set; }
public Color BackgroundColor { private get; set; } = Color.Gray;
public Texture2D Image { get; set; }
public string Tooltip { get; set; }
public string TooltipTitle { private get; set; }
public Rectangle Rectangle
{
get { return new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y); }
}
private readonly ContentManager mContentManager;
private readonly WidgetFactory mWidgetFactory;
private Texture2D mRectangle;
private AchievementBox mTooltipBox;
public Button(ContentManager contentManager, WidgetFactory widgetFactory) : base(contentManager)
{
mContentManager = contentManager;
mWidgetFactory = widgetFactory;
}
public override void Initialize()
{
mRectangle = mContentManager.Load<Texture2D>("Sprites/WhiteRectangle");
mTooltipBox = mWidgetFactory.CreateAchievementBox();
base.Initialize();
}
public void HandleMouseEvent(IMouseEvent mouseEvent)
{
if (!IsEnabled)
return;
if (mouseEvent.MouseButton == MouseButton.Left && mouseEvent.ButtonState == ButtonState.Released)
{
if (!mouseEvent.PositionReleased.HasValue)
return;
if (Rectangle.Contains(mouseEvent.PositionReleased.Value))
IsClicked = true;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
var rectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
if (IsSelected && IsEnabled)
{
var borderRectangle = new Rectangle((int)Position.X - 2, (int)Position.Y - 2, (int)Size.X + 4, (int)Size.Y + 4);
spriteBatch.Draw(mRectangle, borderRectangle, Color.Red);
}
if (Image == null)
{
spriteBatch.Draw(mRectangle, rectangle, GetBackgroundColor());
}
else
{
spriteBatch.Draw(Image, rectangle, Color.White);
if (!IsEnabled)
{
spriteBatch.Draw(mRectangle, rectangle, Color.Black * 0.7f);
}
}
if (IsSelected && !TooltipTitle.IsNullOrEmpty() && !Tooltip.IsNullOrEmpty())
{
mTooltipBox.SetText(TooltipTitle, Tooltip);
mTooltipBox.Size = mTooltipBox.GetMinimumSize();
mTooltipBox.Position = Position - new Vector2(0, mTooltipBox.Size.Y);
mTooltipBox.IsEnabled = true;
if (mTooltipBox.Position.Y < 0)
mTooltipBox.Position = Position + new Vector2(0, Size.Y);
mTooltipBox.Draw(spriteBatch);
}
base.Draw(spriteBatch);
}
protected override Color GetColor()
{
return IsEnabled ? Color : Color.Gray;
}
private Color GetBackgroundColor()
{
return IsEnabled ? BackgroundColor : Color.LightGray;
}
}
}
|