/// <summary> /// Creates a new instance /// </summary> /// <param name="fontPath">The font used by this control and its inner default ButtonSkins</param> /// <param name="text">The initial text for this button</param> public Button(string fontPath, string text) { Normal = new ButtonSkin(fontPath); Pressed = new ButtonSkin(fontPath) {TextColor = Color.Green}; Selected = new ButtonSkin(fontPath) {TextColor = Color.Red}; Text = text; State = ButtonState.Normal; FocusAcquired += OnFocusAcquired; FocusLost += OnFocusLost; Clicked += OnButtonClicked; }
/// <summary> /// Refreshes the control properties when it requires to be redrawn to the RenderTarget /// </summary> /// <param name="device"></param> public override void Refresh(GraphicsDevice device) { // what is the current state of the Control ButtonSkin state = State == ButtonState.Selected ? State == ButtonState.Pressed ? Pressed : Selected : Normal; // we retrieve the size of the text Vector2 textSize = state.Font.MeasureString(_text); Vector2 backgroundSize = state.BackgroundTexture != null ? new Vector2(state.BackgroundTexture.Width, state.BackgroundTexture.Height) : Vector2.One; // we resize the Control to the highest size between the text size and the background texture size. Width = (int)(Math.Max(textSize.X - state.TextOffset.X, backgroundSize.X) * Scale.X); Height = (int)(Math.Max(textSize.Y - state.TextOffset.Y, backgroundSize.Y) * Scale.Y); }
/// <summary> /// Creates a new instance /// </summary> /// <param name="fontPath">The font used by this control and its inner default ButtonSkins</param> /// <param name="text">The initial text for this button</param> public Button(string fontPath, string text) { Normal = new ButtonSkin(fontPath); Pressed = new ButtonSkin(fontPath) { TextColor = Color.Green }; Selected = new ButtonSkin(fontPath) { TextColor = Color.Red }; Text = text; State = ButtonState.Normal; FocusAcquired += OnFocusAcquired; FocusLost += OnFocusLost; Clicked += OnButtonClicked; }
/// <summary> /// Renders the control /// </summary> /// <param name="spriteRenderer"></param> public override void Render(SpriteBatch spriteRenderer) { base.Render(spriteRenderer); // what is the current state of the Control ButtonSkin state = State == ButtonState.Selected ? State == ButtonState.Pressed ? Pressed : Selected : Normal; // Render first the background texture if any if (state.BackgroundTexture != null) { spriteRenderer.Draw(state.BackgroundTexture, Vector2.Zero, null, new Color(1.0f, 1.0f, 1.0f, state.BackgroundTextureAlpha), 0f, Vector2.Zero, Scale, SpriteEffects.None, 0); } // Render the text spriteRenderer.DrawString(state.Font, _text, Vector2.Zero, state.TextColor, 0f, state.TextOffset, Scale, SpriteEffects.None, 1); }