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 | public TileType RelatedTileType { get; set; } |
---|
29 | |
---|
30 | |
---|
31 | public Button(Rectangle boundaries, Texture2D texture, Color borderColor, bool selectable, GraphicsDevice device) |
---|
32 | { |
---|
33 | this.Boundaries = boundaries; |
---|
34 | this.BorderColor = borderColor; |
---|
35 | this.Selectable = selectable; |
---|
36 | this.Image = CreateTexture(borderColor, texture, device); |
---|
37 | this.SelectedImage = CreateTexture(Color.White, texture, device); |
---|
38 | } |
---|
39 | |
---|
40 | public Button(Rectangle boundaries, string text, Color bgColor, Color borderColor, GraphicsDevice device)//, ButtonClickedHandler clickAction) |
---|
41 | { |
---|
42 | this.Boundaries = boundaries; |
---|
43 | this.ButtonText = text; |
---|
44 | this.BackgroundColor = bgColor; |
---|
45 | this.BorderColor = borderColor; |
---|
46 | this.Image = CreateTexture(bgColor, borderColor, device); |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | private Texture2D CreateTexture(Color bordercolor, Texture2D baseTexture, GraphicsDevice device) |
---|
51 | { |
---|
52 | Texture2D texture = new Texture2D(device, this.Width, this.Height); |
---|
53 | Color[,] tex = new Color[this.Width, this.Height]; |
---|
54 | Color[] baseColors = new Color[this.Width*this.Height]; |
---|
55 | baseTexture.GetData<Color>(baseColors); |
---|
56 | for (int x = 0; x < this.Width; x++) |
---|
57 | { |
---|
58 | for (int y = 0; y < this.Height; y++) |
---|
59 | { |
---|
60 | if (x == 0 || y == 0 || x == this.Width-1 || y == this.Height-1) |
---|
61 | tex[x, y] = bordercolor; |
---|
62 | else |
---|
63 | tex[x, y] = baseColors[x + y * this.Width]; |
---|
64 | } |
---|
65 | } |
---|
66 | Color[] tex1D = new Color[this.Width * this.Height]; |
---|
67 | for (int x = 0; x < this.Width; x++) |
---|
68 | for (int y = 0; y < this.Height; y++) |
---|
69 | tex1D[x + y * this.Width] = tex[x, y]; |
---|
70 | texture.SetData<Color>(tex1D); |
---|
71 | return texture; |
---|
72 | } |
---|
73 | |
---|
74 | private Texture2D CreateTexture(Color bgcolor, Color bordercolor, GraphicsDevice device) |
---|
75 | { |
---|
76 | Color[,] tex = new Color[this.Width, this.Height]; |
---|
77 | for (int x = 0; x < this.Width; x++) |
---|
78 | { |
---|
79 | for (int y = 0; y < this.Height; y++) |
---|
80 | { |
---|
81 | if (x == 0 || y == 0 || x == this.Width-1 || y == this.Height-1) |
---|
82 | tex[x, y] = bordercolor; |
---|
83 | else |
---|
84 | tex[x, y] = bgcolor; |
---|
85 | } |
---|
86 | } |
---|
87 | Color[] tex1D = new Color[this.Width * this.Height]; |
---|
88 | for (int x = 0; x < this.Width; x++) |
---|
89 | for (int y = 0; y < this.Height; y++) |
---|
90 | tex1D[x + y * this.Width] = tex[x, y]; |
---|
91 | Texture2D texture = new Texture2D(device, this.Width, this.Height); |
---|
92 | texture.SetData<Color>(tex1D); |
---|
93 | return texture; |
---|
94 | } |
---|
95 | |
---|
96 | protected void OnClicked(EventArgs e) |
---|
97 | { |
---|
98 | if (Clicked != null) |
---|
99 | Clicked(this, e); |
---|
100 | } |
---|
101 | |
---|
102 | public void Pressed() |
---|
103 | { |
---|
104 | OnClicked(EventArgs.Empty); |
---|
105 | } |
---|
106 | |
---|
107 | public void Draw(SpriteBatch sb) |
---|
108 | { |
---|
109 | if(this.Selected) |
---|
110 | sb.Draw(this.SelectedImage, this.Boundaries, Color.White); |
---|
111 | else |
---|
112 | sb.Draw(this.Image, this.Boundaries, Color.White); |
---|
113 | if(this.ButtonText != null) |
---|
114 | sb.DrawString(AdventureGame.basicFont, this.ButtonText, this.Position, Color.Black); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|