root/Code/oMaE/oMaE/Widgets/RadioButton.cs @ 60

Revision 60, 5.8 KB (checked in by acarter, 2 years ago)

Added checking in the Match minigame.

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Audio;
7using Microsoft.Xna.Framework.Content;
8using Microsoft.Xna.Framework.GamerServices;
9using Microsoft.Xna.Framework.Graphics;
10using Microsoft.Xna.Framework.Input;
11using Microsoft.Xna.Framework.Media;
12
13namespace oMaE.Widgets
14{
15    /// <summary>
16    /// A singlular Radio Button.
17    /// </summary>
18    public class RadioButton : GameObject
19    {
20        /// <summary>
21        /// The group which this radio button is a member of.
22        /// </summary>
23        private RadioGroup Parent;
24        /// <summary>
25        /// The text of the radio button.
26        /// </summary>
27        private String text;
28        public String Text { get { return text; } }
29        /// <summary>
30        /// The center of the radio button.
31        /// </summary>
32        public Vector2 Center{ get { return new Vector2(Location.Center.X, Location.Center.Y); } }
33        /// <summary>
34        /// The location of the radio button.
35        /// </summary>
36        public Rectangle Location { get { return Picture.Location; } }
37        /// <summary>
38        /// The current picture the radio button is displaying.
39        /// </summary>
40        public Image2D Picture { get { return Active ? ClickedImg : UnclickedImg; } }
41
42
43        /// <summary>
44        /// The default size of a radio button.
45        /// </summary>
46        public static readonly Vector2 DefaultSize = new Vector2(10, 10);
47        /// <summary>
48        /// The default image for an unclicked radio button.
49        /// </summary>
50        public static Image2D DefaultUnclickedImg = new Image2D("Interface/RadioButton_Unclicked", new Rectangle(0, 0, 10, 10));
51        /// <summary>
52        /// The default image for a clicked radio button.
53        /// </summary>
54        public static Image2D DefaultClickedImg = new Image2D("Interface/RadioButton_Clicked", new Rectangle(0, 0, 10, 10));
55        /// <summary>
56        /// The image for an unclicked radio button.
57        /// </summary>
58        protected Image2D UnclickedImg;
59        /// <summary>
60        /// The image for a clicked radio button.
61        /// </summary>
62        protected Image2D ClickedImg;
63        private TextLocation textLocation;
64        /// <summary>
65        /// Creates a new radio button.
66        /// </summary>
67        /// <param name="Location">The location for this radio button.</param>
68        /// <param name="text">The text of the radio button.</param>
69        /// <param name="textLocation">The location of the text relative to the radio button.</param>
70        /// <param name="Parent">The parent group of the radio button.</param>
71        /// <param name="UnclickedImg">The unclicked image for the radio button, use null for default.</param>
72        /// <param name="UnclickedDim">The unclicked dimensions for the radio button, use null for default.</param>
73        /// <param name="ClickedImg">The clicked image for the radio button, use null for default.</param>
74        /// <param name="ClickedDim">The clicked dimensions for the radio button, use null for default.</param>
75        public RadioButton(Vector2 Location, String text, TextLocation textLocation, RadioGroup Parent,
76                            Image2D UnclickedImg = null, Nullable<Vector2> UnclickedDim = null,
77                            Image2D ClickedImg = null, Nullable<Vector2> ClickedDim = null)
78        {
79            Vector2 vUnclickedDim = UnclickedDim ?? DefaultSize;
80            Vector2 vClickedDim = ClickedDim ?? DefaultSize;
81            Rectangle UnclickedLocation = new Rectangle((int)Location.X, (int)Location.Y, (int)vUnclickedDim.X, (int)vUnclickedDim.Y);
82            Rectangle ClickedLocation = new Rectangle((int)Location.X, (int)Location.Y, (int)vClickedDim.X, (int)vClickedDim.Y);
83            this.textLocation = textLocation;
84            this.text = text;
85            this.Parent = Parent;
86            children.Add(this.UnclickedImg = (UnclickedImg ?? DefaultUnclickedImg).Locate(UnclickedLocation));
87            children.Add(this.ClickedImg = (ClickedImg ?? DefaultClickedImg).Locate(ClickedLocation));
88           
89        }
90        /// <summary>
91        /// Clears the radio button.
92        /// </summary>
93        public void Clear()
94        {
95            Updated |= Active;
96            Active = false;
97        }
98        /// <summary>
99        /// Updates the radio button.
100        /// </summary>
101        /// <param name="gamestate">The current gamestate</param>
102        public override void Update(GameState gamestate)
103        {
104            if (gamestate.CurrentMouseState.LeftButton == ButtonState.Pressed
105                    && gamestate.LastMouseState.LeftButton == ButtonState.Released
106                    && Location.Contains(gamestate.CurrentMouseState.X, gamestate.CurrentMouseState.Y))
107            {
108                Active = !Active;
109                Updated = true;
110                Parent.UpdateComponent(this);
111            }
112            else
113                Updated = false;
114            this.ClickedImg.Visible = Active;
115            this.UnclickedImg.Visible = !Active;
116            base.Update(gamestate);
117        }
118        /// <summary>
119        /// Draws the radio button.
120        /// </summary>
121        /// <param name="screen">The screen to be drawn on.</param>
122        public override void Draw(Screen2D screen)
123        {
124            if (textLocation == TextLocation.LEFT)
125                screen.DrawText(text, new Vector2(Location.Left - 5, Location.Center.Y), TextHorizontal.RIGHT, TextVertical.CENTERED, Color.White);
126            else
127                screen.DrawText(text, new Vector2(Location.Right + 5, Location.Center.Y), TextHorizontal.LEFT, TextVertical.CENTERED, Color.White);
128            base.Draw(screen);
129        }
130    }
131}
Note: See TracBrowser for help on using the browser.