-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cs
109 lines (88 loc) · 2.89 KB
/
Button.cs
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
using FNAEngine2D;
using FNAEngine2D.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pong
{
public class Button : GameObject, IMouseEventHandler
{
private TextureBox _upRenderer;
private TextureBox _overRenderer;
private TextureBox _downRenderer;
private Label _label;
public Action OnClick;
public string Text { get; set; }
/// <summary>
/// Empty constructor
/// </summary>
public Button()
{
}
/// <summary>
/// Constructor
/// </summary>
public Button(string text, Rectangle bounds, Action onClick)
{
Text = text;
this.Bounds = bounds;
OnClick = onClick;
}
/// <summary>
/// Load
/// </summary>
protected override void Load()
{
_upRenderer = Add(new TextureBox("button_up", this.Bounds));
_overRenderer = Add(new TextureBox("button_over", this.Bounds));
_overRenderer.Hide();
_downRenderer = Add(new TextureBox("button_down", this.Bounds));
_downRenderer.Hide();
_label = Add(new Label(Text, "fonts\\Roboto-Bold", 22, this.Bounds, Color.White, TextHorizontalAlignment.Center, TextVerticalAlignment.Middle));
_label.PixelPerfect = true;
_label.Depth = this.Depth - 100; //Par dessus le restant
}
/// <summary>
/// Handle mouse event...
/// </summary>
public void HandleMouseEvent(MouseAction action)
{
if (action == MouseAction.Enter || action == MouseAction.LeftButtonUp)
{
_upRenderer.VisibleSelf = false;
_downRenderer.VisibleSelf = false;
_overRenderer.VisibleSelf = true;
}
else if (action == MouseAction.Leave)
{
_downRenderer.VisibleSelf = false;
_overRenderer.VisibleSelf = false;
_upRenderer.VisibleSelf = true;
}
else if (action == MouseAction.LeftButtonDown)
{
_upRenderer.VisibleSelf = false;
_overRenderer.VisibleSelf = false;
_downRenderer.VisibleSelf = true;
}
//When pressed down... the text needs to move down also...
if (_downRenderer.Visible)
{
_label.Y = this.Bounds.Y + 3;
}
else
{
_label.Y = this.Bounds.Y;
}
if (action == MouseAction.LeftButtonClicked)
{
//Clicked!
OnClick?.Invoke();
}
}
}
}