1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Audio; |
---|
6 | using Microsoft.Xna.Framework.Content; |
---|
7 | using Microsoft.Xna.Framework.GamerServices; |
---|
8 | using Microsoft.Xna.Framework.Graphics; |
---|
9 | using Microsoft.Xna.Framework.Input; |
---|
10 | using Microsoft.Xna.Framework.Media; |
---|
11 | using Microsoft.Xna.Framework.Net; |
---|
12 | using Microsoft.Xna.Framework.Storage; |
---|
13 | |
---|
14 | |
---|
15 | namespace ALTK.GUI |
---|
16 | { |
---|
17 | public delegate void ChangedEventHandler(object sender, EventArgs e); |
---|
18 | |
---|
19 | |
---|
20 | // A multichoice button which allows you to switch the button's item by clicking on it. |
---|
21 | public class AChooseButton : DrawableGameComponent |
---|
22 | { |
---|
23 | TimeSpan elapsedTime = TimeSpan.Zero; |
---|
24 | TimeSpan time = TimeSpan.FromMilliseconds(30); |
---|
25 | |
---|
26 | ContentManager content; |
---|
27 | SpriteBatch spriteBatch; |
---|
28 | SpriteFont spriteFont; |
---|
29 | |
---|
30 | public int Width = 200; |
---|
31 | public int Height = 30; |
---|
32 | |
---|
33 | public string AlignX; |
---|
34 | public string AlignY; |
---|
35 | |
---|
36 | public string FontPath = "aMainFont"; |
---|
37 | |
---|
38 | public Texture2D Texture; |
---|
39 | |
---|
40 | //public double Width; |
---|
41 | //public double Height; |
---|
42 | |
---|
43 | public float PositionX = 0.0F; |
---|
44 | public float PositionY = 0.0F; |
---|
45 | |
---|
46 | public string ButtonText = ""; |
---|
47 | public Color TextColor = Color.Red; |
---|
48 | public Color TextColorPressed = Color.Green; |
---|
49 | public Color TextColorCurrent = Color.Red; |
---|
50 | |
---|
51 | public string Action; |
---|
52 | |
---|
53 | public bool Locked = false; |
---|
54 | public bool Pressed = false; |
---|
55 | bool LeftPressed = false; |
---|
56 | |
---|
57 | public int NumItems; |
---|
58 | public int CurrentIndex; |
---|
59 | public string[] Items = new string[100]; |
---|
60 | public bool[] ForbiddenItems = new bool[100]; |
---|
61 | |
---|
62 | public event ChangedEventHandler Changed; |
---|
63 | |
---|
64 | public bool HasChanged; |
---|
65 | |
---|
66 | public int MaximumAllowedIndex = 33; |
---|
67 | |
---|
68 | public AChooseButton(Game game) |
---|
69 | : base(game) |
---|
70 | { |
---|
71 | content = new ContentManager(game.Services); |
---|
72 | } |
---|
73 | |
---|
74 | protected override void LoadContent() |
---|
75 | { |
---|
76 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
77 | |
---|
78 | spriteFont = ALTKHandler.Loader.loadFont(FontPath); |
---|
79 | } |
---|
80 | |
---|
81 | public void UpdateFont(string Modpath) |
---|
82 | { |
---|
83 | spriteFont = ALTKHandler.Loader.loadFont(FontPath); |
---|
84 | } |
---|
85 | |
---|
86 | protected virtual void OnChanged(EventArgs e) |
---|
87 | { |
---|
88 | if (Changed != null) |
---|
89 | Changed(this, e); |
---|
90 | } |
---|
91 | |
---|
92 | public override void Update(GameTime gameTime) |
---|
93 | { |
---|
94 | int mpX = Cursor.PosX; |
---|
95 | int mpY = Cursor.PosY; |
---|
96 | |
---|
97 | if (mpX > this.PositionX - (this.Width * .25) && |
---|
98 | mpX < this.PositionX + (this.Width / 2) && |
---|
99 | mpY > this.PositionY - (this.Height * .25) && |
---|
100 | mpY < this.PositionY + (this.Height / 2) && |
---|
101 | Locked == false) |
---|
102 | { |
---|
103 | this.TextColorCurrent = TextColorPressed; |
---|
104 | if (Cursor.leftPressed) |
---|
105 | { |
---|
106 | LeftPressed = true; |
---|
107 | Pressed = true; |
---|
108 | } |
---|
109 | else if (Cursor.rightPressed) |
---|
110 | { |
---|
111 | LeftPressed = false; |
---|
112 | Pressed = true; |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | if (Pressed == true) |
---|
117 | { |
---|
118 | if (LeftPressed == true) |
---|
119 | { |
---|
120 | int NextItem = CurrentIndex + 1; |
---|
121 | do |
---|
122 | { |
---|
123 | if (NextItem == NumItems) |
---|
124 | { |
---|
125 | if (ForbiddenItems[0] == false) |
---|
126 | { |
---|
127 | CurrentIndex = 0; |
---|
128 | OnChanged(EventArgs.Empty); |
---|
129 | HasChanged = true; |
---|
130 | } |
---|
131 | break; |
---|
132 | } |
---|
133 | else if (ForbiddenItems[NextItem] == true) |
---|
134 | { |
---|
135 | NextItem = NextItem + 1; |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | if (NextItem < MaximumAllowedIndex) |
---|
140 | { |
---|
141 | CurrentIndex = NextItem; |
---|
142 | OnChanged(EventArgs.Empty); |
---|
143 | HasChanged = true; |
---|
144 | } |
---|
145 | break; |
---|
146 | } |
---|
147 | } |
---|
148 | while (true); |
---|
149 | } |
---|
150 | else if (LeftPressed == false) |
---|
151 | { |
---|
152 | int NextItem = CurrentIndex - 1; |
---|
153 | do |
---|
154 | { |
---|
155 | if (NextItem == -1) |
---|
156 | { |
---|
157 | if (ForbiddenItems[NumItems - 1] == false && (MaximumAllowedIndex == 0 | MaximumAllowedIndex > NumItems - 1)) |
---|
158 | { |
---|
159 | CurrentIndex = NumItems - 1; |
---|
160 | OnChanged(EventArgs.Empty); |
---|
161 | HasChanged = true; |
---|
162 | } |
---|
163 | break; |
---|
164 | } |
---|
165 | else if (ForbiddenItems[NextItem] == true) |
---|
166 | { |
---|
167 | NextItem = NextItem - 1; |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | CurrentIndex = NextItem; |
---|
172 | OnChanged(EventArgs.Empty); |
---|
173 | HasChanged = true; |
---|
174 | break; |
---|
175 | } |
---|
176 | } |
---|
177 | while (true); |
---|
178 | } |
---|
179 | } |
---|
180 | LeftPressed = false; |
---|
181 | Pressed = false; |
---|
182 | } |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | this.TextColorCurrent = TextColor; |
---|
187 | Pressed = false; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | public override void Draw(GameTime gameTime) |
---|
192 | { |
---|
193 | spriteBatch.Begin(); |
---|
194 | Vector2 pos = new Vector2(PositionX, PositionY); |
---|
195 | if (Texture != null) |
---|
196 | { |
---|
197 | spriteBatch.Draw(Texture, pos, Color.White); |
---|
198 | } |
---|
199 | spriteBatch.DrawString(spriteFont, Items[CurrentIndex], new Vector2(pos.X + 1, pos.Y + 1), Color.Black); |
---|
200 | spriteBatch.DrawString(spriteFont, Items[CurrentIndex], pos, TextColorCurrent); |
---|
201 | spriteBatch.End(); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | } |
---|