1 | // Avaruuslaatikko Graphical User Interface |
---|
2 | // Written by Rami "Rampastring" Pasanen, 2012 |
---|
3 | |
---|
4 | using System; |
---|
5 | using System.Collections.Generic; |
---|
6 | using System.Linq; |
---|
7 | using Microsoft.Xna.Framework; |
---|
8 | using Microsoft.Xna.Framework.Audio; |
---|
9 | using Microsoft.Xna.Framework.Content; |
---|
10 | using Microsoft.Xna.Framework.GamerServices; |
---|
11 | using Microsoft.Xna.Framework.Graphics; |
---|
12 | using Microsoft.Xna.Framework.Input; |
---|
13 | using Microsoft.Xna.Framework.Media; |
---|
14 | using Microsoft.Xna.Framework.Net; |
---|
15 | using Microsoft.Xna.Framework.Storage; |
---|
16 | |
---|
17 | namespace ALTK.GUI |
---|
18 | { |
---|
19 | public class AUIButton : DrawableGameComponent |
---|
20 | { |
---|
21 | TimeSpan elapsedTime = TimeSpan.Zero; |
---|
22 | TimeSpan time = TimeSpan.FromMilliseconds(30); |
---|
23 | |
---|
24 | public bool AdaptiveSize = true; |
---|
25 | |
---|
26 | ContentManager content; |
---|
27 | SpriteBatch spriteBatch; |
---|
28 | SpriteFont spriteFont; |
---|
29 | |
---|
30 | public int Width = 200; |
---|
31 | public int Height = 30; |
---|
32 | public int TextXAdd = 0; |
---|
33 | public int TextYAdd = 0; |
---|
34 | |
---|
35 | public string AlignX; |
---|
36 | public string AlignY; |
---|
37 | |
---|
38 | public string FontPath = "aMainFont"; |
---|
39 | |
---|
40 | public Texture2D IdleTexture; |
---|
41 | public Texture2D HoverTexture; |
---|
42 | |
---|
43 | //bool drawIdleTexture = true; |
---|
44 | //bool drawHoverTexture = false; |
---|
45 | |
---|
46 | public float AlphaRate = 0.02f; |
---|
47 | float idleTextureAlpha = 1.0f; |
---|
48 | float hoverTextureAlpha = 0.0f; |
---|
49 | |
---|
50 | public bool AnimationMode; |
---|
51 | //bool animationFinished; |
---|
52 | bool isAnimEnterDirection; |
---|
53 | |
---|
54 | //public double Width; |
---|
55 | //public double Height; |
---|
56 | |
---|
57 | public float PositionX = 0.0F; |
---|
58 | public float PositionY = 0.0F; |
---|
59 | |
---|
60 | public string ButtonText = ""; |
---|
61 | public Color TextColor = Color.Red; |
---|
62 | public Color TextColorPressed = Color.Green; |
---|
63 | public Color TextColorCurrent = Color.Red; |
---|
64 | |
---|
65 | public string Action; |
---|
66 | public string ClassName; |
---|
67 | |
---|
68 | public bool Locked = false; |
---|
69 | |
---|
70 | public int TextPosXAdd; |
---|
71 | |
---|
72 | public bool isVisible = true; |
---|
73 | public bool isAnimated = true; |
---|
74 | |
---|
75 | /// <summary> |
---|
76 | /// Checks whether the button has been pressed. Change to false after checking and performing the wanted actions. |
---|
77 | /// </summary> |
---|
78 | public bool NeedsUpdating = false; |
---|
79 | bool Pressed; |
---|
80 | |
---|
81 | public AUIButton(Game game) |
---|
82 | : base(game) |
---|
83 | { |
---|
84 | content = new ContentManager(game.Services); |
---|
85 | } |
---|
86 | |
---|
87 | public override void Initialize() |
---|
88 | { |
---|
89 | base.Initialize(); |
---|
90 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
91 | |
---|
92 | UpdateTextPosition(); |
---|
93 | //spriteFont = ALTK.Loader.loadFont(FontPath); |
---|
94 | } |
---|
95 | |
---|
96 | protected override void LoadContent() |
---|
97 | { |
---|
98 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
99 | |
---|
100 | spriteFont = ALTKHandler.Loader.loadFont(FontPath); |
---|
101 | } |
---|
102 | |
---|
103 | public void UpdateTextPosition() |
---|
104 | { |
---|
105 | if (AdaptiveSize) |
---|
106 | { |
---|
107 | Vector2 textSize = spriteFont.MeasureString(ButtonText); |
---|
108 | if (textSize.X < Width) |
---|
109 | { |
---|
110 | TextXAdd = Convert.ToInt32(((Width - textSize.X) / 2) - (textSize.X / 4)); |
---|
111 | //TextXAdd = Convert.ToInt32((textSize.X - Width) / -2); |
---|
112 | } |
---|
113 | else if (textSize.X > Width) |
---|
114 | { |
---|
115 | TextXAdd = Convert.ToInt32((textSize.X - Width) / -2); |
---|
116 | } |
---|
117 | |
---|
118 | if (textSize.Y < Height) |
---|
119 | { |
---|
120 | TextYAdd = Convert.ToInt32((Height - textSize.Y) / 2); |
---|
121 | } |
---|
122 | else if (textSize.Y > Height) |
---|
123 | { |
---|
124 | TextYAdd = Convert.ToInt32((textSize.Y - Height) / -2); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public void UpdateFont(string Modpath) |
---|
130 | { |
---|
131 | spriteFont = ALTKHandler.Loader.loadFont(FontPath); |
---|
132 | } |
---|
133 | |
---|
134 | public bool CursoronButton = false; |
---|
135 | public override void Update(GameTime gameTime) |
---|
136 | { |
---|
137 | int mpX = Cursor.PosX; |
---|
138 | int mpY = Cursor.PosY; |
---|
139 | |
---|
140 | if (Locked == false) |
---|
141 | { |
---|
142 | if (mpX > this.PositionX && |
---|
143 | mpX < this.PositionX + this.Width && |
---|
144 | mpY > this.PositionY && |
---|
145 | mpY < this.PositionY + this.Height && |
---|
146 | Locked == false) |
---|
147 | { |
---|
148 | if (CursoronButton == false && isAnimated) |
---|
149 | { |
---|
150 | // enter animation mode |
---|
151 | isAnimEnterDirection = true; |
---|
152 | AnimationMode = true; |
---|
153 | idleTextureAlpha = 0.5f; |
---|
154 | hoverTextureAlpha = 0.75f; |
---|
155 | } |
---|
156 | CursoronButton = true; |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | if (CursoronButton == true && isAnimated) |
---|
161 | { |
---|
162 | AnimationMode = true; |
---|
163 | isAnimEnterDirection = false; |
---|
164 | idleTextureAlpha = 0.75f; |
---|
165 | hoverTextureAlpha = 0.5f; |
---|
166 | } |
---|
167 | CursoronButton = false; |
---|
168 | } |
---|
169 | //} |
---|
170 | |
---|
171 | if (CursoronButton == true) |
---|
172 | { |
---|
173 | this.TextColorCurrent = TextColorPressed; |
---|
174 | if (Cursor.leftPressed) |
---|
175 | { |
---|
176 | Pressed = true; |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | if (Pressed == true) |
---|
181 | { |
---|
182 | Pressed = false; |
---|
183 | NeedsUpdating = true; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | this.TextColorCurrent = TextColor; |
---|
190 | Pressed = false; |
---|
191 | CursoronButton = false; |
---|
192 | } |
---|
193 | |
---|
194 | ProcessAnims(); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | public void Update(GameTime gameTime, int baseX, int baseY) |
---|
199 | { |
---|
200 | int mpX = Cursor.SecPosX; |
---|
201 | int mpY = Cursor.SecPosY; |
---|
202 | |
---|
203 | if (Locked == false) |
---|
204 | { |
---|
205 | if (mpX > this.PositionX + baseX && |
---|
206 | mpX < this.PositionX + baseX + this.Width && |
---|
207 | mpY > this.PositionY + baseY + this.Height / 2 && |
---|
208 | mpY < this.PositionY + baseY + this.Height * 1.5 && |
---|
209 | Locked == false) |
---|
210 | { |
---|
211 | if (CursoronButton == false && isAnimated) |
---|
212 | { |
---|
213 | // enter animation mode |
---|
214 | isAnimEnterDirection = true; |
---|
215 | AnimationMode = true; |
---|
216 | idleTextureAlpha = 0.5f; |
---|
217 | hoverTextureAlpha = 0.75f; |
---|
218 | } |
---|
219 | CursoronButton = true; |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | if (CursoronButton == true && isAnimated) |
---|
224 | { |
---|
225 | AnimationMode = true; |
---|
226 | isAnimEnterDirection = false; |
---|
227 | idleTextureAlpha = 0.75f; |
---|
228 | hoverTextureAlpha = 0.5f; |
---|
229 | } |
---|
230 | CursoronButton = false; |
---|
231 | } |
---|
232 | //} |
---|
233 | |
---|
234 | if (CursoronButton == true) |
---|
235 | { |
---|
236 | this.TextColorCurrent = TextColorPressed; |
---|
237 | if (Cursor.SecLeftPressed) |
---|
238 | { |
---|
239 | Pressed = true; |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | if (Pressed == true) |
---|
244 | { |
---|
245 | Pressed = false; |
---|
246 | NeedsUpdating = true; |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | else |
---|
251 | { |
---|
252 | this.TextColorCurrent = TextColor; |
---|
253 | Pressed = false; |
---|
254 | CursoronButton = false; |
---|
255 | } |
---|
256 | |
---|
257 | ProcessAnims(); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | private void ProcessAnims() |
---|
262 | { |
---|
263 | if (AnimationMode && isAnimated) |
---|
264 | { |
---|
265 | bool idleTextureReady = false; |
---|
266 | bool hoverTextureReady = false; |
---|
267 | |
---|
268 | if (isAnimEnterDirection) |
---|
269 | { |
---|
270 | idleTextureAlpha = idleTextureAlpha - AlphaRate; |
---|
271 | if (idleTextureAlpha < 0.0f) |
---|
272 | { |
---|
273 | idleTextureAlpha = 0.0f; |
---|
274 | idleTextureReady = true; |
---|
275 | } |
---|
276 | |
---|
277 | hoverTextureAlpha = hoverTextureAlpha + AlphaRate; |
---|
278 | if (hoverTextureAlpha >= 1.0f) |
---|
279 | { |
---|
280 | hoverTextureAlpha = 1.0f; |
---|
281 | hoverTextureReady = true; |
---|
282 | } |
---|
283 | |
---|
284 | if (idleTextureReady && hoverTextureReady) |
---|
285 | AnimationMode = false; |
---|
286 | } |
---|
287 | else |
---|
288 | { |
---|
289 | hoverTextureAlpha = hoverTextureAlpha - AlphaRate; |
---|
290 | if (hoverTextureAlpha < 0.0f) |
---|
291 | { |
---|
292 | hoverTextureAlpha = 0.0f; |
---|
293 | hoverTextureReady = true; |
---|
294 | } |
---|
295 | |
---|
296 | idleTextureAlpha = idleTextureAlpha + AlphaRate; |
---|
297 | if (idleTextureAlpha >= 1.0f) |
---|
298 | { |
---|
299 | idleTextureAlpha = 1.0f; |
---|
300 | idleTextureReady = true; |
---|
301 | } |
---|
302 | |
---|
303 | if (idleTextureReady && hoverTextureReady) |
---|
304 | AnimationMode = false; |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | public override void Draw(GameTime gameTime) |
---|
310 | { |
---|
311 | if (isVisible) |
---|
312 | { |
---|
313 | spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied); |
---|
314 | if (IdleTexture != null) |
---|
315 | { |
---|
316 | if (HoverTexture == null) |
---|
317 | { |
---|
318 | spriteBatch.Draw(IdleTexture, new Rectangle(Convert.ToInt32(PositionX), Convert.ToInt32(PositionY), Width, Height), new Color(new Vector4(128, 128, 128, idleTextureAlpha))); |
---|
319 | } |
---|
320 | else |
---|
321 | { |
---|
322 | if (CursoronButton == true) |
---|
323 | { |
---|
324 | spriteBatch.Draw(IdleTexture, new Rectangle(Convert.ToInt32(PositionX), Convert.ToInt32(PositionY), Width, Height), new Color(new Vector4(128, 128, 128, idleTextureAlpha))); |
---|
325 | spriteBatch.Draw(HoverTexture, new Rectangle(Convert.ToInt32(PositionX), Convert.ToInt32(PositionY), Width, Height), new Color(new Vector4(128, 128, 128, hoverTextureAlpha))); |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | spriteBatch.Draw(HoverTexture, new Rectangle(Convert.ToInt32(PositionX), Convert.ToInt32(PositionY), Width, Height), new Color(new Vector4(128, 128, 128, hoverTextureAlpha))); |
---|
330 | spriteBatch.Draw(IdleTexture, new Rectangle(Convert.ToInt32(PositionX), Convert.ToInt32(PositionY), Width, Height), new Color(new Vector4(128, 128, 128, idleTextureAlpha))); |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | spriteBatch.End(); |
---|
335 | |
---|
336 | spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); |
---|
337 | spriteBatch.DrawString(spriteFont, ButtonText, new Vector2(PositionX + 1 + TextXAdd, PositionY + 1 + TextYAdd), Color.Black); // varjo |
---|
338 | spriteBatch.DrawString(spriteFont, ButtonText, new Vector2(PositionX + TextXAdd, PositionY + TextYAdd), TextColorCurrent); |
---|
339 | spriteBatch.End(); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | public void Draw(GameTime gameTime, int baseX, int baseY) |
---|
344 | { |
---|
345 | if (isVisible) |
---|
346 | { |
---|
347 | spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied); |
---|
348 | if (IdleTexture != null) |
---|
349 | { |
---|
350 | int xPos = Convert.ToInt32(PositionX + baseX) - 10; |
---|
351 | int yPos = Convert.ToInt32(PositionY + baseY); |
---|
352 | |
---|
353 | Console.WriteLine("xPos: " + xPos + "; yPos: " + yPos); |
---|
354 | |
---|
355 | if (HoverTexture == null) |
---|
356 | { |
---|
357 | spriteBatch.Draw(IdleTexture, new Rectangle(Convert.ToInt32(PositionX + baseX), Convert.ToInt32(PositionY + baseY), Width, Height), new Color(new Vector4(128, 128, 128, idleTextureAlpha))); |
---|
358 | } |
---|
359 | else |
---|
360 | { |
---|
361 | if (CursoronButton == true) |
---|
362 | { |
---|
363 | spriteBatch.Draw(IdleTexture, new Rectangle(xPos, yPos, Width, Height), new Color(new Vector4(128, 128, 128, idleTextureAlpha))); |
---|
364 | spriteBatch.Draw(HoverTexture, new Rectangle(xPos, yPos, Width, Height), new Color(new Vector4(128, 128, 128, hoverTextureAlpha))); |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | spriteBatch.Draw(HoverTexture, new Rectangle(xPos, yPos, Width, Height), new Color(new Vector4(128, 128, 128, hoverTextureAlpha))); |
---|
369 | spriteBatch.Draw(IdleTexture, new Rectangle(xPos, yPos, Width, Height), new Color(new Vector4(128, 128, 128, idleTextureAlpha))); |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | spriteBatch.End(); |
---|
374 | |
---|
375 | spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); |
---|
376 | spriteBatch.DrawString(spriteFont, ButtonText, new Vector2(PositionX + 1 + TextXAdd + baseX, PositionY + 1 + TextYAdd + baseY), Color.Black); // varjo |
---|
377 | spriteBatch.DrawString(spriteFont, ButtonText, new Vector2(PositionX + TextXAdd + baseX, PositionY + TextYAdd + baseY), TextColorCurrent); |
---|
378 | spriteBatch.End(); |
---|
379 | } |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|