1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Microsoft.Xna.Framework; |
---|
6 | using Microsoft.Xna.Framework.Graphics; |
---|
7 | |
---|
8 | |
---|
9 | namespace AdventureGame |
---|
10 | { |
---|
11 | public delegate void ButtonClickedHandler(object sender, EventArgs e); |
---|
12 | |
---|
13 | public class Button |
---|
14 | { |
---|
15 | public event ButtonClickedHandler Clicked; |
---|
16 | |
---|
17 | public Vector2 Position { get { return new Vector2(Boundaries.X, Boundaries.Y); } set { } } |
---|
18 | public Rectangle Boundaries { get; set; } |
---|
19 | public int Width { get { return Boundaries.Width; } private set { } } |
---|
20 | public int Height { get { return Boundaries.Height; } private set { } } |
---|
21 | private Texture2D Image { get; set; } |
---|
22 | private Texture2D SelectedImage { get; set; } |
---|
23 | public Color BackgroundColor { get; set; } |
---|
24 | public Color BorderColor { get; set; } |
---|
25 | public string ButtonText { get; set; } |
---|
26 | public bool Selectable { get; set; } |
---|
27 | public bool Selected { get; set; } |
---|
28 | |
---|
29 | public Button(Rectangle boundaries, Texture2D texture, Color borderColor, bool selectable, GraphicsDevice device) |
---|
30 | { |
---|
31 | this.Boundaries = boundaries; |
---|
32 | this.BorderColor = borderColor; |
---|
33 | this.Image = CreateTexture(borderColor, texture, device); |
---|
34 | this.SelectedImage = CreateTexture(Color.White, texture, device); |
---|
35 | } |
---|
36 | |
---|
37 | public Button(Rectangle boundaries, string text, Color bgColor, Color borderColor, GraphicsDevice device, ButtonClickedHandler clickAction) |
---|
38 | { |
---|
39 | this.Boundaries = boundaries; |
---|
40 | this.ButtonText = text; |
---|
41 | this.BackgroundColor = bgColor; |
---|
42 | this.BorderColor = borderColor; |
---|
43 | this.Image = CreateTexture(bgColor, borderColor, device); |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | private Texture2D CreateTexture(Color bordercolor, Texture2D baseTexture, GraphicsDevice device) |
---|
48 | { |
---|
49 | Texture2D texture = new Texture2D(device, this.Width, this.Height); |
---|
50 | Color[,] tex = new Color[this.Width, this.Height]; |
---|
51 | Color[] baseColors = new Color[this.Width*this.Height]; |
---|
52 | baseTexture.GetData<Color>(baseColors); |
---|
53 | for (int x = 0; x < this.Width; x++) |
---|
54 | { |
---|
55 | for (int y = 0; y < this.Height; y++) |
---|
56 | { |
---|
57 | if (x == 0 || y == 0 || x == this.Width-1 || y == this.Height-1) |
---|
58 | tex[x, y] = bordercolor; |
---|
59 | else |
---|
60 | tex[x, y] = baseColors[x + y * this.Width]; |
---|
61 | } |
---|
62 | } |
---|
63 | Color[] tex1D = new Color[this.Width * this.Height]; |
---|
64 | for (int x = 0; x < this.Width; x++) |
---|
65 | for (int y = 0; y < this.Height; y++) |
---|
66 | tex1D[x + y * this.Width] = tex[x, y]; |
---|
67 | texture.SetData<Color>(tex1D); |
---|
68 | return texture; |
---|
69 | } |
---|
70 | |
---|
71 | private Texture2D CreateTexture(Color bgcolor, Color bordercolor, GraphicsDevice device) |
---|
72 | { |
---|
73 | Color[,] tex = new Color[this.Width, this.Height]; |
---|
74 | for (int x = 0; x < this.Width; x++) |
---|
75 | { |
---|
76 | for (int y = 0; y < this.Height; y++) |
---|
77 | { |
---|
78 | if (x == 0 || y == 0 || x == this.Width-1 || y == this.Height-1) |
---|
79 | tex[x, y] = bordercolor; |
---|
80 | else |
---|
81 | tex[x, y] = bgcolor; |
---|
82 | } |
---|
83 | } |
---|
84 | Color[] tex1D = new Color[this.Width * this.Height]; |
---|
85 | for (int x = 0; x < this.Width; x++) |
---|
86 | for (int y = 0; y < this.Height; y++) |
---|
87 | tex1D[x + y * this.Width] = tex[x, y]; |
---|
88 | Texture2D texture = new Texture2D(device, this.Width, this.Height); |
---|
89 | texture.SetData<Color>(tex1D); |
---|
90 | return texture; |
---|
91 | } |
---|
92 | |
---|
93 | protected void OnClicked(EventArgs e) |
---|
94 | { |
---|
95 | if (Clicked != null) |
---|
96 | Clicked(this, e); |
---|
97 | } |
---|
98 | |
---|
99 | public void Pressed() |
---|
100 | { |
---|
101 | OnClicked(EventArgs.Empty); |
---|
102 | } |
---|
103 | |
---|
104 | public void Draw(SpriteBatch sb) |
---|
105 | { |
---|
106 | if(this.Selected) |
---|
107 | sb.Draw(this.SelectedImage, this.Boundaries, Color.White); |
---|
108 | else |
---|
109 | sb.Draw(this.Image, this.Boundaries, Color.White); |
---|
110 | if(this.ButtonText != null) |
---|
111 | sb.DrawString(AdventureGame.basicFont, this.ButtonText, this.Position, Color.Black); |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|